1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.io.berlinModel.in;
|
10
|
|
11
|
import java.sql.ResultSet;
|
12
|
import java.util.Collection;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Map;
|
15
|
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.springframework.stereotype.Component;
|
19
|
|
20
|
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelAuthorImportValidator;
|
21
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
22
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
23
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
24
|
import eu.etaxonomy.cdm.model.agent.Person;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.common.Extension;
|
27
|
import eu.etaxonomy.cdm.model.common.ExtensionType;
|
28
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
29
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
30
|
|
31
|
|
32
|
/**
|
33
|
* Supported attributes:
|
34
|
* - AuthorId, Abbrev, FirstName, LastName, Dates, AreaOfInterest, NomStandard, createUpdateNotes
|
35
|
*
|
36
|
* @author a.mueller
|
37
|
* @since 20.03.2008
|
38
|
*/
|
39
|
@Component
|
40
|
public class BerlinModelAuthorImport extends BerlinModelImportBase {
|
41
|
private static final long serialVersionUID = 2155984573495140615L;
|
42
|
|
43
|
private static final boolean BLANK_TO_NULL = true;
|
44
|
|
45
|
private static final Logger logger = Logger.getLogger(BerlinModelAuthorImport.class);
|
46
|
|
47
|
public static final String NAMESPACE = "Author";
|
48
|
|
49
|
// private static int recordsPerLog = 5000;
|
50
|
private static final String dbTableName = "Author";
|
51
|
private static final String pluralString = "Authors";
|
52
|
|
53
|
public BerlinModelAuthorImport(){
|
54
|
super(dbTableName, pluralString);
|
55
|
}
|
56
|
|
57
|
@Override
|
58
|
protected String getIdQuery(BerlinModelImportState state) {
|
59
|
String result = " SELECT authorId FROM " + getTableName();
|
60
|
if (StringUtils.isNotBlank(state.getConfig().getAuthorFilter())){
|
61
|
result += " WHERE " + state.getConfig().getAuthorFilter();
|
62
|
}
|
63
|
return result;
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
68
|
String strRecordQuery =
|
69
|
" SELECT * " +
|
70
|
" FROM " + dbTableName + " " +
|
71
|
" WHERE authorId IN ( " + ID_LIST_TOKEN + " )";
|
72
|
return strRecordQuery;
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
|
77
|
String dbAttrName;
|
78
|
String cdmAttrName;
|
79
|
Map<Integer, Person> personMap = new HashMap<>();
|
80
|
|
81
|
boolean success = true;
|
82
|
ResultSet rs = partitioner.getResultSet();
|
83
|
try{
|
84
|
//for each author
|
85
|
while (rs.next()){
|
86
|
|
87
|
// partitioner.doLogPerLoop(recordsPerLog, pluralString);
|
88
|
|
89
|
//create Agent element
|
90
|
int authorId = rs.getInt("AuthorId");
|
91
|
|
92
|
Person author = Person.NewInstance();
|
93
|
|
94
|
dbAttrName = "Abbrev";
|
95
|
cdmAttrName = "nomenclaturalTitle";
|
96
|
success &= ImportHelper.addStringValue(rs, author, dbAttrName, cdmAttrName, BLANK_TO_NULL);
|
97
|
|
98
|
dbAttrName = "FirstName";
|
99
|
cdmAttrName = "givenName";
|
100
|
success &= ImportHelper.addStringValue(rs, author, dbAttrName, cdmAttrName, BLANK_TO_NULL);
|
101
|
|
102
|
dbAttrName = "LastName";
|
103
|
cdmAttrName = "familyName";
|
104
|
success &= ImportHelper.addStringValue(rs, author, dbAttrName, cdmAttrName, BLANK_TO_NULL);
|
105
|
|
106
|
String dates = rs.getString("dates");
|
107
|
if (dates != null){
|
108
|
dates.trim();
|
109
|
TimePeriod lifespan = TimePeriodParser.parseString(dates);
|
110
|
author.setLifespan(lifespan);
|
111
|
}
|
112
|
|
113
|
// //AreaOfInterest
|
114
|
String areaOfInterest = rs.getString("AreaOfInterest");
|
115
|
if (StringUtils.isNotBlank(areaOfInterest)){
|
116
|
Extension.NewInstance(author, areaOfInterest, ExtensionType.AREA_OF_INTREREST());
|
117
|
}
|
118
|
|
119
|
//nomStandard
|
120
|
String nomStandard = rs.getString("NomStandard");
|
121
|
if (StringUtils.isNotBlank(nomStandard)){
|
122
|
Extension.NewInstance(author, nomStandard, ExtensionType.NOMENCLATURAL_STANDARD());
|
123
|
}
|
124
|
|
125
|
|
126
|
//initials
|
127
|
String initials = null;
|
128
|
for (int j = 1; j <= rs.getMetaData().getColumnCount(); j++){
|
129
|
String label = rs.getMetaData().getColumnLabel(j);
|
130
|
if (label.equalsIgnoreCase("Initials") || label.equalsIgnoreCase("Kürzel")){
|
131
|
initials = rs.getString(j);
|
132
|
break;
|
133
|
}
|
134
|
}
|
135
|
if (isNotBlank(initials)){
|
136
|
author.setInitials(initials);
|
137
|
}
|
138
|
|
139
|
//created, notes
|
140
|
doIdCreatedUpdatedNotes(state, author, rs, authorId, NAMESPACE);
|
141
|
|
142
|
personMap.put(authorId, author);
|
143
|
|
144
|
} //while rs.hasNext()
|
145
|
//logger.info("save " + i + " "+pluralString + " ...");
|
146
|
getAgentService().save((Collection)personMap.values());
|
147
|
|
148
|
}catch(Exception ex){
|
149
|
logger.error(ex.getMessage());
|
150
|
ex.printStackTrace();
|
151
|
success = false;
|
152
|
}
|
153
|
return success;
|
154
|
}
|
155
|
|
156
|
@Override
|
157
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
|
158
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
159
|
// no related objects exist
|
160
|
return result;
|
161
|
}
|
162
|
|
163
|
@Override
|
164
|
protected boolean doCheck(BerlinModelImportState state){
|
165
|
IOValidator<BerlinModelImportState> validator = new BerlinModelAuthorImportValidator();
|
166
|
return validator.validate(state);
|
167
|
}
|
168
|
|
169
|
@Override
|
170
|
protected boolean isIgnore(BerlinModelImportState state){
|
171
|
return ! state.getConfig().isDoAuthors();
|
172
|
}
|
173
|
|
174
|
}
|