3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.io
.pesi
.out
;
12 import java
.sql
.SQLException
;
13 import java
.util
.Collection
;
15 import org
.apache
.log4j
.Logger
;
17 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.CollectionExportMapping
;
18 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.DbSequenceMapper
;
19 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.IDbExportMapper
;
20 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.IdMapper
;
21 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.CdmAttributeMapperBase
;
22 import eu
.etaxonomy
.cdm
.io
.common
.DbExportStateBase
;
23 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
24 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 public class PesiCollectionExportMapping
extends PesiExportMapping
{
32 private static final Logger logger
= Logger
.getLogger(CollectionExportMapping
.class);
34 private IdMapper parentMapper
;
35 private DbSequenceMapper sequenceMapper
;
36 private String collectionAttributeName
;
38 public static PesiCollectionExportMapping
NewInstance(String tableName
, String collectionAttributeName
, IdMapper parentMapper
){
39 return new PesiCollectionExportMapping(tableName
, collectionAttributeName
, parentMapper
, null);
42 public static PesiCollectionExportMapping
NewInstance(String tableName
, String collectionAttributeName
, IdMapper parentMapper
, String seqenceAttribute
){
43 return NewInstance(tableName
, collectionAttributeName
, parentMapper
, seqenceAttribute
, 0);
46 public static PesiCollectionExportMapping
NewInstance(String tableName
, String collectionAttributeName
, IdMapper parentMapper
, String seqenceAttribute
, int sequenceStart
){
47 DbSequenceMapper sequenceMapper
= DbSequenceMapper
.NewInstance(seqenceAttribute
, sequenceStart
);
48 PesiCollectionExportMapping result
= new PesiCollectionExportMapping(tableName
, collectionAttributeName
, parentMapper
, sequenceMapper
);
53 private PesiCollectionExportMapping(String tableName
, String collectionAttributeName
, IdMapper parentMapper
, DbSequenceMapper sequenceMapper
){
55 this.parentMapper
= parentMapper
;
56 this.addMapper(parentMapper
);
57 this.sequenceMapper
= sequenceMapper
;
58 this.addMapper(sequenceMapper
);
59 this.collectionAttributeName
= collectionAttributeName
;
64 public boolean invoke(CdmBase parent
) throws SQLException
{
66 boolean result
= true;
67 Collection
<CdmBase
> collection
= getCollection(parent
);
68 if (this.sequenceMapper
!= null){
69 this.sequenceMapper
.reset();
71 for(CdmBase collectionObject
: collection
){
72 if (collectionObject
== null){
73 logger
.warn("Collection object was null");
77 for (CdmAttributeMapperBase mapper
: this.mapperList
){
78 if (mapper
== this.parentMapper
){
79 parentMapper
.invoke(parent
);
80 }else if (mapper
== this.sequenceMapper
){
81 this.sequenceMapper
.invoke(null);
82 }else if (mapper
instanceof IDbExportMapper
){
83 IDbExportMapper
<DbExportStateBase
<?
>> dbMapper
= (IDbExportMapper
)mapper
;
85 result
&= dbMapper
.invoke(collectionObject
);
86 } catch (Exception e
) {
88 logger
.error("Error occurred in mapping.invoke");
93 logger
.warn("mapper is not of type " + IDbExportMapper
.class.getSimpleName());
96 int count
= getPreparedStatement().executeUpdate();
97 if (logger
.isDebugEnabled())logger
.debug("Number of rows affected: " + count
);
100 } catch(SQLException e
){
102 logger
.error(e
.getMessage() + ": " + parent
.toString());
107 private Collection
getCollection(CdmBase cdmBase
){
108 Object result
= ImportHelper
.getValue(cdmBase
, collectionAttributeName
, false, true);
109 return (Collection
)result
;