1
|
/**
|
2
|
* Copyright (C) 2017 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.dataInserter;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.joda.time.DateTime;
|
20
|
import org.joda.time.format.DateTimeFormatter;
|
21
|
import org.springframework.beans.factory.annotation.Autowired;
|
22
|
import org.springframework.beans.factory.annotation.Qualifier;
|
23
|
import org.springframework.context.ApplicationListener;
|
24
|
import org.springframework.context.event.ContextRefreshedEvent;
|
25
|
import org.springframework.transaction.TransactionStatus;
|
26
|
import org.springframework.transaction.annotation.Transactional;
|
27
|
|
28
|
import com.fasterxml.jackson.core.JsonParseException;
|
29
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
30
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
31
|
import com.vaadin.spring.annotation.SpringComponent;
|
32
|
|
33
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
34
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
35
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
36
|
import eu.etaxonomy.cdm.model.agent.Institution;
|
37
|
import eu.etaxonomy.cdm.model.common.Extension;
|
38
|
import eu.etaxonomy.cdm.model.common.ExtensionType;
|
39
|
import eu.etaxonomy.cdm.model.name.Registration;
|
40
|
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
|
41
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
42
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
43
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
44
|
|
45
|
/**
|
46
|
* @author a.kohlbecker
|
47
|
* @since May 9, 2017
|
48
|
*
|
49
|
*/
|
50
|
@SpringComponent
|
51
|
@Transactional(readOnly=true)
|
52
|
public class RegistrationRequiredDataInserter implements ApplicationListener<ContextRefreshedEvent>{
|
53
|
|
54
|
protected static final String PARAM_NAME_CREATE = "registrationCreate";
|
55
|
|
56
|
protected static final String PARAM_NAME_WIPEOUT = "registrationWipeout";
|
57
|
|
58
|
private static final Logger logger = Logger.getLogger(RegistrationRequiredDataInserter.class);
|
59
|
|
60
|
private ExtensionType extensionTypeIAPTRegData;
|
61
|
|
62
|
public static boolean commandsExecuted = false;
|
63
|
|
64
|
@Autowired
|
65
|
@Qualifier("cdmRepository")
|
66
|
private CdmRepository repo;
|
67
|
|
68
|
// ==================== Registration creation ======================= //
|
69
|
|
70
|
/**
|
71
|
* {@inheritDoc}
|
72
|
*/
|
73
|
@Override
|
74
|
public void onApplicationEvent(ContextRefreshedEvent event) {
|
75
|
executeSuppliedCommands();
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
*
|
80
|
*/
|
81
|
|
82
|
private void executeSuppliedCommands() {
|
83
|
|
84
|
if(commandsExecuted){
|
85
|
// do not run twice
|
86
|
// a second run could take place during initialization of the web context
|
87
|
return;
|
88
|
}
|
89
|
commandsExecuted = true;
|
90
|
|
91
|
String wipeoutCmd = System.getProperty(PARAM_NAME_WIPEOUT);
|
92
|
String createCmd = System.getProperty(PARAM_NAME_CREATE);
|
93
|
|
94
|
// ============ DELETE
|
95
|
if(wipeoutCmd != null && wipeoutCmd.matches("iapt|all")){
|
96
|
|
97
|
boolean onlyIapt = wipeoutCmd.equals("iapt");
|
98
|
List<UUID> deleteCandidates = new ArrayList<UUID>();
|
99
|
|
100
|
TransactionStatus tx = repo.startTransaction(true);
|
101
|
List<Registration> allRegs = repo.getRegistrationService().list(null, null, null, null, null);
|
102
|
for(Registration reg : allRegs){
|
103
|
if(onlyIapt){
|
104
|
try {
|
105
|
@SuppressWarnings("unchecked")
|
106
|
Set<Extension> extensions = reg.getName().getExtensions(getExtensionTypeIAPTRegData());
|
107
|
deleteCandidates.add(reg.getUuid());
|
108
|
} catch(NullPointerException e){
|
109
|
// IGNORE
|
110
|
}
|
111
|
} else {
|
112
|
deleteCandidates.add(reg.getUuid());
|
113
|
}
|
114
|
}
|
115
|
repo.commitTransaction(tx);
|
116
|
repo.getRegistrationService().delete(deleteCandidates);
|
117
|
}
|
118
|
|
119
|
// ============ CREATE
|
120
|
int pageIndex = 0;
|
121
|
if(createCmd != null && createCmd.equals("iapt")){
|
122
|
|
123
|
DateTimeFormatter dateFormat = org.joda.time.format.DateTimeFormat.forPattern("dd.MM.yy").withPivotYear(1950);
|
124
|
|
125
|
TransactionStatus tx = repo.startTransaction(false);
|
126
|
while(true) {
|
127
|
Pager<TaxonNameBase> pager = repo.getNameService().page(null, 1000, pageIndex, null, null);
|
128
|
if(pager.getRecords().isEmpty()){
|
129
|
break;
|
130
|
}
|
131
|
List<Registration> newRegs = new ArrayList<>(pager.getRecords().size());
|
132
|
for(TaxonNameBase name : pager.getRecords()){
|
133
|
|
134
|
Set<String> extensionValues = name.getExtensions(getExtensionTypeIAPTRegData());
|
135
|
|
136
|
// there is for sure only one
|
137
|
if(extensionValues.isEmpty()){
|
138
|
continue;
|
139
|
}
|
140
|
String iaptJson = extensionValues.iterator().next();
|
141
|
try {
|
142
|
|
143
|
IAPTRegData iaptData = new ObjectMapper().readValue(iaptJson, IAPTRegData.class);
|
144
|
|
145
|
if(iaptData.getRegId() == null){
|
146
|
continue;
|
147
|
}
|
148
|
|
149
|
DateTime regDate = null;
|
150
|
if(iaptData.getDate() != null){
|
151
|
try {
|
152
|
regDate = dateFormat.parseDateTime(iaptData.getDate());
|
153
|
regDate.getYear();
|
154
|
} catch (Exception e) {
|
155
|
logger.error("Error parsing date: " + iaptData.getDate(), e);
|
156
|
continue;
|
157
|
}
|
158
|
}
|
159
|
|
160
|
Registration reg = Registration.NewInstance();
|
161
|
reg.setStatus(RegistrationStatus.PUBLISHED);
|
162
|
reg.setIdentifier("http://phycobank/" + iaptData.getRegId());
|
163
|
reg.setSpecificIdentifier(iaptData.getRegId().toString());
|
164
|
reg.setInstitution(getInstitution(iaptData.getOffice()));
|
165
|
reg.setName(name);
|
166
|
if(name.getTypeDesignations() != null && !name.getTypeDesignations().isEmpty()){
|
167
|
// do not add the collection directly to avoid "Found shared references to a collection" problem
|
168
|
HashSet<TypeDesignationBase> typeDesignations = new HashSet<>(name.getTypeDesignations().size());
|
169
|
typeDesignations.addAll(name.getTypeDesignations());
|
170
|
reg.setTypeDesignations(typeDesignations);
|
171
|
}
|
172
|
reg.setRegistrationDate(regDate);
|
173
|
logger.debug("IAPT Registraion for " + name.getTitleCache());
|
174
|
newRegs.add(reg);
|
175
|
|
176
|
} catch (JsonParseException e) {
|
177
|
logger.error("Error parsing IAPTRegData from extension", e);
|
178
|
} catch (JsonMappingException e) {
|
179
|
logger.error("Error mapping json from extension to IAPTRegData", e);
|
180
|
} catch (IOException e) {
|
181
|
logger.error(e);
|
182
|
}
|
183
|
|
184
|
}
|
185
|
repo.getRegistrationService().save(newRegs);
|
186
|
repo.getRegistrationService().getSession().flush();
|
187
|
logger.debug("Registrations saved");
|
188
|
pageIndex++;
|
189
|
}
|
190
|
repo.commitTransaction(tx);
|
191
|
}
|
192
|
}
|
193
|
|
194
|
|
195
|
/**
|
196
|
* @param office
|
197
|
* @return
|
198
|
*/
|
199
|
private Institution getInstitution(String office) {
|
200
|
Pager<AgentBase> pager = repo.getAgentService().findByTitle(Institution.class, office, MatchMode.EXACT, null, null, null, null, null);
|
201
|
if(!pager.getRecords().isEmpty()){
|
202
|
return (Institution) pager.getRecords().get(0);
|
203
|
} else {
|
204
|
Institution institute = (Institution) repo.getAgentService().save(Institution.NewNamedInstance(office));
|
205
|
return institute;
|
206
|
}
|
207
|
}
|
208
|
|
209
|
|
210
|
private ExtensionType getExtensionTypeIAPTRegData() {
|
211
|
if(extensionTypeIAPTRegData == null){
|
212
|
extensionTypeIAPTRegData = (ExtensionType) repo.getTermService().load(UUID.fromString("9be1bfe3-6ba0-4560-af15-86971ab96e09"));
|
213
|
}
|
214
|
return extensionTypeIAPTRegData;
|
215
|
}
|
216
|
|
217
|
|
218
|
|
219
|
}
|