1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
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.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.io.berlinModel.out;
|
12
|
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.Collection;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
|
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.berlinModel.out.mapper.IndexCounter;
|
22
|
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
|
23
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
24
|
import eu.etaxonomy.cdm.io.common.Source;
|
25
|
import eu.etaxonomy.cdm.io.common.mapping.CdmAttributeMapperBase;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
|
28
|
/**
|
29
|
* @author a.mueller
|
30
|
* @created 12.05.2009
|
31
|
* @version 1.0
|
32
|
*/
|
33
|
public class CollectionExportMapping extends BerlinModelExportMapping {
|
34
|
private static final Logger logger = Logger.getLogger(CollectionExportMapping.class);
|
35
|
|
36
|
private IdMapper parentMapper;
|
37
|
private DbSequenceMapper sequenceMapper;
|
38
|
private String collectionAttributeName;
|
39
|
|
40
|
public static CollectionExportMapping NewInstance(String tableName, String collectionAttributeName, IdMapper parentMapper){
|
41
|
return new CollectionExportMapping(tableName, collectionAttributeName, parentMapper, null);
|
42
|
}
|
43
|
|
44
|
public static CollectionExportMapping NewInstance(String tableName, String collectionAttributeName, IdMapper parentMapper, String seqenceAttribute){
|
45
|
return NewInstance(tableName, collectionAttributeName, parentMapper, seqenceAttribute, 0);
|
46
|
}
|
47
|
|
48
|
public static CollectionExportMapping NewInstance(String tableName, String collectionAttributeName, IdMapper parentMapper, String seqenceAttribute, int sequenceStart){
|
49
|
DbSequenceMapper sequenceMapper = DbSequenceMapper.NewInstance(seqenceAttribute, sequenceStart);
|
50
|
CollectionExportMapping result = new CollectionExportMapping(tableName, collectionAttributeName, parentMapper, sequenceMapper);
|
51
|
return result;
|
52
|
}
|
53
|
|
54
|
|
55
|
private CollectionExportMapping(String tableName, String collectionAttributeName, IdMapper parentMapper, DbSequenceMapper sequenceMapper){
|
56
|
super(tableName);
|
57
|
this.parentMapper = parentMapper;
|
58
|
this.addMapper(parentMapper);
|
59
|
this.sequenceMapper = sequenceMapper;
|
60
|
this.addMapper(sequenceMapper);
|
61
|
this.collectionAttributeName = collectionAttributeName;
|
62
|
}
|
63
|
|
64
|
|
65
|
@Override
|
66
|
public boolean invoke(CdmBase parent) throws SQLException{
|
67
|
try {
|
68
|
boolean result = true;
|
69
|
Collection<CdmBase> collection = getCollection(parent);
|
70
|
if (this.sequenceMapper != null){
|
71
|
this.sequenceMapper.reset();
|
72
|
}
|
73
|
for(CdmBase collectionObject : collection){
|
74
|
if (collectionObject == null){
|
75
|
logger.warn("Collection object was null");
|
76
|
result = false;
|
77
|
continue;
|
78
|
}
|
79
|
for (CdmAttributeMapperBase mapper : this.mapperList){
|
80
|
if (mapper == this.parentMapper){
|
81
|
parentMapper.invoke(parent);
|
82
|
}else if (mapper == this.sequenceMapper){
|
83
|
this.sequenceMapper.invoke(null);
|
84
|
}else if (mapper instanceof IDbExportMapper){
|
85
|
IDbExportMapper<DbExportStateBase<?>> dbMapper = (IDbExportMapper)mapper;
|
86
|
try {
|
87
|
result &= dbMapper.invoke(collectionObject);
|
88
|
} catch (Exception e) {
|
89
|
result = false;
|
90
|
logger.error("Error occurred in mapping.invoke");
|
91
|
e.printStackTrace();
|
92
|
continue;
|
93
|
}
|
94
|
}else{
|
95
|
logger.warn("mapper is not of type " + IDbExportMapper.class.getSimpleName());
|
96
|
}
|
97
|
}
|
98
|
int count = getPreparedStatement().executeUpdate();
|
99
|
if (logger.isDebugEnabled())logger.debug("Number of rows affected: " + count);
|
100
|
}
|
101
|
return result;
|
102
|
} catch(SQLException e){
|
103
|
e.printStackTrace();
|
104
|
logger.error(e.getMessage() + ": " + parent.toString());
|
105
|
return false;
|
106
|
}
|
107
|
}
|
108
|
|
109
|
private Collection getCollection(CdmBase cdmBase){
|
110
|
Object result = ImportHelper.getValue(cdmBase, collectionAttributeName, false, true);
|
111
|
return (Collection)result;
|
112
|
}
|
113
|
|
114
|
|
115
|
}
|