Project

General

Profile

Download (19.4 KB) Statistics
| Branch: | Tag: | Revision:
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.Arrays;
14
import java.util.EnumSet;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.List;
18
import java.util.Map;
19
import java.util.Set;
20
import java.util.UUID;
21

    
22
import org.apache.log4j.Logger;
23
import org.joda.time.DateTime;
24
import org.joda.time.DateTimeFieldType;
25
import org.joda.time.Partial;
26
import org.joda.time.format.DateTimeFormatter;
27
import org.springframework.context.event.ContextRefreshedEvent;
28
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
29
import org.springframework.security.core.GrantedAuthority;
30
import org.springframework.transaction.TransactionStatus;
31
import org.springframework.transaction.annotation.Transactional;
32

    
33
import com.fasterxml.jackson.core.JsonParseException;
34
import com.fasterxml.jackson.databind.JsonMappingException;
35
import com.fasterxml.jackson.databind.ObjectMapper;
36

    
37
import eu.etaxonomy.cdm.api.application.AbstractDataInserter;
38
import eu.etaxonomy.cdm.api.application.CdmRepository;
39
import eu.etaxonomy.cdm.api.service.pager.Pager;
40
import eu.etaxonomy.cdm.model.agent.AgentBase;
41
import eu.etaxonomy.cdm.model.agent.Institution;
42
import eu.etaxonomy.cdm.model.common.ExtensionType;
43
import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
44
import eu.etaxonomy.cdm.model.common.Group;
45
import eu.etaxonomy.cdm.model.common.TimePeriod;
46
import eu.etaxonomy.cdm.model.name.Registration;
47
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
48
import eu.etaxonomy.cdm.model.name.TaxonName;
49
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
50
import eu.etaxonomy.cdm.model.reference.Reference;
51
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
52
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
53
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmPermissionClass;
54
import eu.etaxonomy.cdm.persistence.hibernate.permission.Role;
55
import eu.etaxonomy.cdm.persistence.query.MatchMode;
56
import eu.etaxonomy.cdm.vaadin.model.registration.KindOfUnitTerms;
57
import eu.etaxonomy.cdm.vaadin.security.RolesAndPermissions;
58

    
59
/**
60
 *
61
 * Can create missing registrations for names which have Extensions of the Type <code>IAPTRegdata.json</code>.
62
 * See https://dev.e-taxonomy.eu/redmine/issues/6621 for further details.
63
 * This feature can be activated by by supplying one of the following jvm command line arguments:
64
 * <ul>
65
 * <li><code>-DregistrationCreate=iapt</code>: create all iapt Registrations if missing</li>
66
 * <li><code>-DregistrationWipeout=iapt</code>: remove all iapt Registrations</li>
67
 * <li><code>-DregistrationWipeout=all</code>: remove all Registrations</li>
68
 * </ul>
69
 * The <code>-DregistrationWipeout</code> commands are executed before the <code>-DregistrationCreate</code> and will not change the name and type designations.
70
 *
71
 * @author a.kohlbecker
72
 * @since May 9, 2017
73
 *
74
 */
75
public class RegistrationRequiredDataInserter extends AbstractDataInserter {
76

    
77
    protected static final String PARAM_NAME_CREATE = "registrationCreate";
78

    
79
    protected static final String PARAM_NAME_WIPEOUT = "registrationWipeout";
80

    
81
    protected static final UUID GROUP_SUBMITTER_UUID = UUID.fromString("c468c6a7-b96c-4206-849d-5a825f806d3e");
82

    
83
    protected static final UUID GROUP_CURATOR_UUID = UUID.fromString("135210d3-3db7-4a81-ab36-240444637d45");
84

    
85
    private static final EnumSet<CRUD> CREATE_READ = EnumSet.of(CRUD.CREATE, CRUD.READ);
86
    private static final EnumSet<CRUD> CREATE_READ_UPDATE_DELETE = EnumSet.of(CRUD.CREATE, CRUD.READ, CRUD.UPDATE, CRUD.DELETE);
87

    
88
    private static final Logger logger = Logger.getLogger(RegistrationRequiredDataInserter.class);
89

    
90
    private ExtensionType extensionTypeIAPTRegData;
91

    
92
    Map<String, Institution> instituteMap = new HashMap<>();
93

    
94
    public static boolean commandsExecuted = false;
95

    
96
    private CdmRepository repo;
97

    
98
    private boolean hasRun = false;
99

    
100
    public void setCdmRepository(CdmRepository repo){
101
      this.repo = repo;
102
    }
103

    
104

    
105
 // ==================== Registration creation ======================= //
106

    
107
    /**
108
     * {@inheritDoc}
109
     */
110
    @Override
111
    public void onApplicationEvent(ContextRefreshedEvent event) {
112

    
113
        if(hasRun){
114
            return;
115
        }
116

    
117
        runAsAuthentication(Role.ROLE_ADMIN);
118

    
119
        insertRequiredData();
120
        executeSuppliedCommands();
121

    
122
        restoreAuthentication();
123

    
124
        hasRun = true;
125
    }
126

    
127
    /**
128
     *
129
     */
130
    @Transactional
131
    private void insertRequiredData() {
132

    
133
        TransactionStatus txStatus = repo.startTransaction(false);
134

    
135
        Role roleCuration = RolesAndPermissions.ROLE_CURATION;
136
        if(repo.getGrantedAuthorityService().find(roleCuration.getUuid()) == null){
137
            repo.getGrantedAuthorityService().saveOrUpdate(roleCuration.asNewGrantedAuthority());
138
        }
139

    
140
        Group groupCurator = repo.getGroupService().load(GROUP_CURATOR_UUID, Arrays.asList("grantedAuthorities"));
141
        if(groupCurator == null){
142
            groupCurator = Group.NewInstance();
143
            groupCurator.setUuid(GROUP_CURATOR_UUID);
144
            groupCurator.setName("Curator");
145
        }
146
        assureGroupHas(groupCurator, new CdmAuthority(CdmPermissionClass.REGISTRATION, CREATE_READ_UPDATE_DELETE).toString());
147
        repo.getGroupService().saveOrUpdate(groupCurator);
148

    
149
        Group groupSubmitter = repo.getGroupService().load(GROUP_SUBMITTER_UUID, Arrays.asList("grantedAuthorities"));
150
        if(groupSubmitter == null){
151
            groupSubmitter = Group.NewInstance();
152
            groupSubmitter.setUuid(GROUP_SUBMITTER_UUID);
153
            groupSubmitter.setName("Submitter");
154
        }
155
        assureGroupHas(groupSubmitter, new CdmAuthority(CdmPermissionClass.TAXONNAME, CREATE_READ).toString());
156
        assureGroupHas(groupSubmitter, new CdmAuthority(CdmPermissionClass.TEAMORPERSONBASE, CREATE_READ).toString());
157
        assureGroupHas(groupSubmitter, new CdmAuthority(CdmPermissionClass.REGISTRATION, CREATE_READ).toString());
158
        assureGroupHas(groupSubmitter, new CdmAuthority(CdmPermissionClass.SPECIMENOROBSERVATIONBASE, CREATE_READ).toString());
159
        repo.getGroupService().saveOrUpdate(groupSubmitter);
160

    
161
        if(repo.getTermService().find(KindOfUnitTerms.SPECIMEN().getUuid()) == null){
162
            repo.getTermService().save(KindOfUnitTerms.SPECIMEN());
163
        }
164
        if(repo.getTermService().find(KindOfUnitTerms.PUBLISHED_IMAGE().getUuid()) == null){
165
            repo.getTermService().save(KindOfUnitTerms.PUBLISHED_IMAGE());
166
        }
167
        if(repo.getTermService().find(KindOfUnitTerms.UNPUBLISHED_IMAGE().getUuid()) == null){
168
            repo.getTermService().save(KindOfUnitTerms.UNPUBLISHED_IMAGE());
169
        }
170
        if(repo.getTermService().find(KindOfUnitTerms.CULTURE_METABOLIC_INACTIVE().getUuid()) == null){
171
            repo.getTermService().save(KindOfUnitTerms.CULTURE_METABOLIC_INACTIVE());
172
        }
173

    
174
        // --- remove after release 4.12.0 ------------------------------------------------------
175
        // delete old DerivationEventTypes terms which are no longer used, see #7059
176
        // UUID_PUBLISHED_IMAGE = UUID.fromString("b8cba359-4202-4741-8ed8-4f17ae94b3e3");
177
        // UUID UUID_UNPUBLISHED_IMAGE = UUID.fromString("6cd5681f-0918-4ed6-89a8-bda1480dc890");
178
        // UUID UUID_CULTURE_METABOLIC_INACTIVE = UUID.fromString("eaf1c853-ba8d-4c40-aa0a-56beac96b0d2");
179
        for(UUID uuid : new UUID[]{
180
                UUID.fromString("b8cba359-4202-4741-8ed8-4f17ae94b3e3"),
181
                UUID.fromString("6cd5681f-0918-4ed6-89a8-bda1480dc890"),
182
                UUID.fromString("eaf1c853-ba8d-4c40-aa0a-56beac96b0d2")}){
183
            if(repo.getTermService().find(uuid) != null){
184
                repo.getTermService().delete(uuid);
185
            }
186
        }
187
        // --------------------------------------------------------------------------------------
188
        txStatus.flush();
189
        repo.commitTransaction(txStatus);
190

    
191
    }
192

    
193
    private void assureGroupHas(Group group, String authorityString){
194
        boolean authorityExists = false;
195

    
196
        for(GrantedAuthority ga : group.getGrantedAuthorities()){
197
            if((authorityExists = ga.getAuthority().equals(authorityString)) == true){
198
                break;
199
            }
200
        }
201
        if(!authorityExists){
202
            group.addGrantedAuthority(findGrantedAuthority(authorityString));
203
        }
204
    }
205

    
206
    private GrantedAuthorityImpl findGrantedAuthority(String authorityString){
207
        GrantedAuthorityImpl ga = null;
208
        try{
209
            ga = repo.getGrantedAuthorityService().findAuthorityString(authorityString);
210
        } catch (AuthenticationCredentialsNotFoundException e){
211
            e.printStackTrace();
212
        }
213
        if(ga == null){
214
            ga = GrantedAuthorityImpl.NewInstance(authorityString);
215
            repo.getGrantedAuthorityService().save(ga);
216
        }
217
        return ga;
218
    }
219

    
220
    /**
221
     *
222
     */
223

    
224
    private void executeSuppliedCommands() {
225

    
226
        if(commandsExecuted){
227
            // do not run twice
228
            // a second run could take place during initialization of the web context
229
            return;
230
        }
231
        commandsExecuted  = true;
232

    
233
        String wipeoutCmd = System.getProperty(PARAM_NAME_WIPEOUT);
234
        String createCmd = System.getProperty(PARAM_NAME_CREATE);
235

    
236
        // ============ DELETE
237
        if(wipeoutCmd != null && wipeoutCmd.matches("iapt|all")){
238

    
239
            boolean onlyIapt = wipeoutCmd.equals("iapt");
240
            Set<UUID> deleteCandidates = new HashSet<UUID>();
241

    
242
            TransactionStatus tx = repo.startTransaction(true);
243
            List<Registration> allRegs = repo.getRegistrationService().list(null, null, null, null, null);
244
            for(Registration reg : allRegs){
245
                if(onlyIapt){
246
                    try {
247
                        @SuppressWarnings("unchecked")
248
                        Set<String> extensions = reg.getName().getExtensions(getExtensionTypeIAPTRegData());
249
                        if(reg.getUuid() != null){
250
                            deleteCandidates.add(reg.getUuid());
251
                        }
252
                    } catch(NullPointerException e){
253
                        // IGNORE
254
                    }
255
                } else {
256
                    if(reg.getUuid() != null){
257
                        deleteCandidates.add(reg.getUuid());
258
                    }
259
                }
260
            }
261
            repo.commitTransaction(tx);
262
            if(!deleteCandidates.isEmpty()){
263
                try {
264
                    repo.getRegistrationService().delete(deleteCandidates);
265
                } catch (Exception e) {
266
                    // MySQLIntegrityConstraintViolationException happens here every second run !!!
267
                    logger.error(e);
268
                }
269
            }
270
        }
271

    
272
        // ============ CREATE
273
        int pageIndex = 0;
274
        if(createCmd != null && createCmd.equals("iapt")){
275

    
276
            DateTimeFormatter dateFormat1 = org.joda.time.format.DateTimeFormat.forPattern("dd.MM.yy").withPivotYear(1950);
277
            DateTimeFormatter dateFormat2 = org.joda.time.format.DateTimeFormat.forPattern("yyyy-MM-dd").withPivotYear(1950);
278

    
279
            TransactionStatus tx = repo.startTransaction(false);
280
            while(true) {
281
                Pager<TaxonName> pager = repo.getNameService().page(null, 1000, pageIndex, null, null);
282
                if(pager.getRecords().isEmpty()){
283
                    break;
284
                }
285
                List<Registration> newRegs = new ArrayList<>(pager.getRecords().size());
286
                for(TaxonName name : pager.getRecords()){
287

    
288

    
289

    
290
                    Set<String> extensionValues = name.getExtensions(getExtensionTypeIAPTRegData());
291

    
292
                    // there is for sure only one
293
                    if(extensionValues.isEmpty()){
294
                        continue;
295
                    }
296

    
297
                    logger.debug("IAPT Registration for " + name.getTitleCache() + " ...");
298

    
299
                    String iaptJson = extensionValues.iterator().next();
300
                    try {
301

    
302
                        IAPTRegData iaptData = new ObjectMapper().readValue(iaptJson, IAPTRegData.class);
303

    
304
                        if(iaptData.getRegId() == null){
305
                            continue;
306
                        }
307

    
308
                        DateTime regDate = null;
309
                        if(iaptData.getDate() != null){
310
                            DateTimeFormatter dateFormat;
311
                            if(iaptData.getDate().matches("\\d{4}-\\d{2}-\\d{2}")){
312
                                dateFormat = dateFormat2;
313
                            } else {
314
                                dateFormat = dateFormat1;
315
                            }
316
                            try {
317
                                regDate = dateFormat.parseDateTime(iaptData.getDate());
318
                                regDate.getYear();
319
                            } catch (Exception e) {
320
                                logger.error("Error parsing date : " + iaptData.getDate(), e);
321
                                continue;
322
                            }
323
                        }
324

    
325
                        Registration reg = Registration.NewInstance();
326
                        reg.setStatus(RegistrationStatus.PUBLISHED);
327
                        reg.setIdentifier("http://phycobank.org/" + iaptData.getRegId());
328
                        reg.setSpecificIdentifier(iaptData.getRegId().toString());
329
                        reg.setInstitution(getInstitution(iaptData.getOffice()));
330

    
331
                        boolean isPhycobankID = Integer.valueOf(reg.getSpecificIdentifier()) >= 100000;
332

    
333
                        Partial youngestDate = null;
334
                        Reference youngestPub = null;
335

    
336
                        // find youngest publication
337

    
338
                        // NOTE:
339
                        // data imported from IAPT does not have typedesignation citations and sometimes no nomref
340

    
341
                        if(isPhycobankID){
342
                            youngestPub = (Reference) name.getNomenclaturalReference();
343
                            youngestDate = partial(youngestPub.getDatePublished());
344

    
345
                            if(name.getTypeDesignations() != null && !name.getTypeDesignations().isEmpty()){
346
                                for(TypeDesignationBase td : name.getTypeDesignations()){
347
                                    if(td.getCitation() == null){
348
                                        continue;
349
                                    }
350
                                    Partial pubdate = partial(td.getCitation().getDatePublished());
351
                                    if(pubdate != null){
352
                                        if(youngestDate== null || comparePartials(youngestDate, pubdate)){
353
                                            youngestDate = pubdate;
354
                                            youngestPub = td.getCitation();
355
                                        }
356
                                    }
357
                                }
358
                            }
359
                        }
360

    
361
                        if((isPhycobankID && youngestPub == name.getNomenclaturalReference()) || !isPhycobankID) {
362
                            reg.setName(name);
363
                        } else {
364
                            logger.debug("skipping name published in older referece");
365
                        }
366
                        if(name.getTypeDesignations() != null && !name.getTypeDesignations().isEmpty()){
367
                            // do not add the collection directly to avoid "Found shared references to a collection" problem
368
                            HashSet<TypeDesignationBase> typeDesignations = new HashSet<>(name.getTypeDesignations().size());
369
                            for(TypeDesignationBase td : name.getTypeDesignations()){
370
                                if(td.getCitation() == null && isPhycobankID){
371
                                    logger.error("Missing TypeDesignation Citation in Phycobank data");
372
                                    continue;
373
                                }
374
                                if((isPhycobankID && youngestPub == td.getCitation()) || !isPhycobankID){
375
                                    typeDesignations.add(td);
376
                                } else {
377
                                    logger.debug("skipping typedesignation published in older reference");
378
                                }
379
                            }
380
                            reg.setTypeDesignations(typeDesignations);
381
                        }
382
                        reg.setRegistrationDate(regDate);
383
                        newRegs.add(reg);
384

    
385
                    } catch (JsonParseException e) {
386
                        logger.error("Error parsing IAPTRegData from extension", e);
387
                    } catch (JsonMappingException e) {
388
                        logger.error("Error mapping json from extension to IAPTRegData", e);
389
                    } catch (IOException e) {
390
                        logger.error(e);
391
                    }
392

    
393
                }
394
                repo.getRegistrationService().save(newRegs);
395
                tx.flush();
396
                logger.debug("Registrations saved");
397
                pageIndex++;
398
            }
399
            repo.commitTransaction(tx);
400
        }
401
    }
402

    
403

    
404
    /**
405
     * @param youngestDate
406
     * @param pubdate
407
     * @return
408
     */
409
    protected boolean comparePartials(Partial youngestDate, Partial pubdate) {
410

    
411
        if(youngestDate.size() == pubdate.size()) {
412
            return youngestDate.compareTo(pubdate) < 0;
413
        }
414
        youngestDate = youngestDate.without(DateTimeFieldType.dayOfMonth());
415
        pubdate = pubdate.without(DateTimeFieldType.dayOfMonth());
416
        if(youngestDate.size() == pubdate.size()) {
417
            return youngestDate.compareTo(pubdate) < 0;
418
        }
419
        youngestDate = youngestDate.without(DateTimeFieldType.monthOfYear());
420
        pubdate = pubdate.without(DateTimeFieldType.monthOfYear());
421
        return youngestDate.compareTo(pubdate) < 0;
422

    
423
    }
424

    
425

    
426
    /**
427
     * @param datePublished
428
     * @return
429
     */
430
    private Partial partial(TimePeriod datePublished) {
431
        if(datePublished != null){
432
            if(datePublished.getEnd() != null){
433
                return datePublished.getEnd();
434
            } else {
435
                return datePublished.getStart();
436
            }
437
        }
438
        return null;
439
    }
440

    
441

    
442
    /**
443
     * @param office
444
     * @return
445
     */
446
    private Institution getInstitution(String office) {
447
        Institution institution;
448
        if(instituteMap.containsKey(office)){
449
            institution = instituteMap.get(office);
450
        } else {
451

    
452
            Pager<AgentBase> pager = repo.getAgentService().findByTitle(Institution.class, office, MatchMode.EXACT, null, null, null, null, null);
453
            if(!pager.getRecords().isEmpty()){
454
                institution =  (Institution) pager.getRecords().get(0);
455
            } else {
456
                Institution institute = (Institution) repo.getAgentService().save(Institution.NewNamedInstance(office));
457
                institution = institute;
458
            }
459
            instituteMap.put(office, institution);
460
        }
461
        return institution;
462
    }
463

    
464

    
465
    private ExtensionType getExtensionTypeIAPTRegData() {
466
        if(extensionTypeIAPTRegData == null){
467
            extensionTypeIAPTRegData = (ExtensionType) repo.getTermService().load(UUID.fromString("9be1bfe3-6ba0-4560-af15-86971ab96e09"));
468
        }
469
        return extensionTypeIAPTRegData;
470
    }
471

    
472

    
473

    
474
}
(2-2/2)