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
|
|
10
|
package eu.etaxonomy.cdm.io.globis;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
import org.springframework.transaction.TransactionStatus;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
|
23
|
import eu.etaxonomy.cdm.api.service.IAgentService;
|
24
|
import eu.etaxonomy.cdm.io.common.mapping.IMappingImport;
|
25
|
import eu.etaxonomy.cdm.model.agent.Person;
|
26
|
import eu.etaxonomy.cdm.model.agent.Team;
|
27
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
28
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
29
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
30
|
|
31
|
|
32
|
/**
|
33
|
* @author a.mueller
|
34
|
* @created 25.02.2014
|
35
|
*/
|
36
|
@Component
|
37
|
public class GlobisAuthorImport extends GlobisImportBase<TeamOrPersonBase<?>> implements IMappingImport<TeamOrPersonBase<?>, GlobisImportState>{
|
38
|
private static final Logger logger = Logger.getLogger(GlobisAuthorImport.class);
|
39
|
|
40
|
private static final String pluralString = "references";
|
41
|
private static final String dbTableName = "Literatur";
|
42
|
private static final Class<?> cdmTargetClass = Reference.class;
|
43
|
|
44
|
public GlobisAuthorImport(){
|
45
|
super(pluralString, dbTableName, cdmTargetClass);
|
46
|
}
|
47
|
|
48
|
|
49
|
|
50
|
|
51
|
@Override
|
52
|
protected void doInvoke(GlobisImportState state) {
|
53
|
|
54
|
//create reference authors
|
55
|
GlobisReferenceImport globisReferenceImport = (GlobisReferenceImport)getBean("globisReferenceImport");
|
56
|
if (globisReferenceImport == null){
|
57
|
logger.error("globisReferenceImport could not be found");
|
58
|
}else{
|
59
|
String query = globisReferenceImport.getIdQuery();
|
60
|
query = query.replace("SELECT refID", "SELECT refID, RefAuthor");
|
61
|
ResultSet rs = state.getConfig().getSource().getResultSet(query);
|
62
|
try {
|
63
|
TransactionStatus tx = startTransaction();
|
64
|
while (rs.next()){
|
65
|
String refAuthor = rs.getString("RefAuthor");
|
66
|
TeamOrPersonBase<?> agent = makeAuthor(refAuthor, state, getAgentService());
|
67
|
// getAgentService().saveOrUpdate(agent);
|
68
|
}
|
69
|
commitTransaction(tx);
|
70
|
} catch (SQLException e) {
|
71
|
e.printStackTrace();
|
72
|
}
|
73
|
}
|
74
|
|
75
|
}
|
76
|
|
77
|
|
78
|
|
79
|
@Override
|
80
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition( ResultSet rs, GlobisImportState state) {
|
81
|
//not required here
|
82
|
return null;
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
public TeamOrPersonBase<?> createObject(ResultSet rs, GlobisImportState state) throws SQLException {
|
87
|
//not required here
|
88
|
return null;
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
protected String getRecordQuery(GlobisImportConfigurator config) {
|
93
|
//not required here
|
94
|
return null;
|
95
|
}
|
96
|
|
97
|
|
98
|
|
99
|
protected static TeamOrPersonBase<?> makeAuthor(String refAuthor, GlobisImportState state, IAgentService service) {
|
100
|
|
101
|
//create string list for single authors
|
102
|
List<String> singleAuthorStrings = makeAuthorStringList(refAuthor);
|
103
|
|
104
|
TeamOrPersonBase<?> result;
|
105
|
if (singleAuthorStrings.size() > 1){
|
106
|
Team newTeam = Team.NewInstance();
|
107
|
for (String str : singleAuthorStrings){
|
108
|
Person person = makePerson(str, state, service);
|
109
|
newTeam.addTeamMember(person);
|
110
|
}
|
111
|
String teamKey = makeTeamKey(newTeam, state, service);
|
112
|
Team team = state.getTeam(teamKey);
|
113
|
if (team == null){
|
114
|
team = newTeam;
|
115
|
service.save(team);
|
116
|
state.putTeam(teamKey, newTeam);
|
117
|
}
|
118
|
result = team;
|
119
|
}else{
|
120
|
result = makePerson(singleAuthorStrings.get(0), state, service);
|
121
|
}
|
122
|
|
123
|
return result;
|
124
|
}
|
125
|
|
126
|
private static List<String> makeAuthorStringList(String refAuthor) {
|
127
|
List<String> singleAuthorStrings = new ArrayList<String>();
|
128
|
String[] split = refAuthor.split(";");
|
129
|
for (String single : split){
|
130
|
single = single.trim();
|
131
|
if (single.startsWith("&")){
|
132
|
single = single.substring(1).trim();
|
133
|
}
|
134
|
String[] split2 = single.split("&");
|
135
|
for (String single2 : split2){
|
136
|
singleAuthorStrings.add(single2.trim());
|
137
|
}
|
138
|
}
|
139
|
return singleAuthorStrings;
|
140
|
}
|
141
|
|
142
|
|
143
|
|
144
|
|
145
|
protected static String makeTeamKey(Team team, GlobisImportState state, IAgentService service){
|
146
|
String uuidList = "";
|
147
|
for (Person person : team.getTeamMembers()){
|
148
|
String str = person.getTitleCache();
|
149
|
Person existingPerson = makePerson(str, state, service);
|
150
|
uuidList += "@" + existingPerson.getUuid();
|
151
|
}
|
152
|
return uuidList;
|
153
|
}
|
154
|
|
155
|
private static Person makePerson(String string, GlobisImportState state, IAgentService service) {
|
156
|
if (string == null){
|
157
|
return null;
|
158
|
}
|
159
|
string = string.trim();
|
160
|
Person person = state.getPerson(string);
|
161
|
if (person == null){
|
162
|
person = Person.NewTitledInstance(string);
|
163
|
service.save(person);
|
164
|
state.putPerson(string, person);
|
165
|
}
|
166
|
return person;
|
167
|
}
|
168
|
|
169
|
|
170
|
@Override
|
171
|
protected boolean doCheck(GlobisImportState state){
|
172
|
logger.info("Do check not implemented for Author import");
|
173
|
return true;
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
protected boolean isIgnore(GlobisImportState state){
|
178
|
return !state.getConfig().isDoAuthors();
|
179
|
}
|
180
|
|
181
|
|
182
|
}
|