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