Project

General

Profile

Revision c5b3791f

IDc5b3791fc1677bdb4bd2d6d4a896ec095e8f612f
Parent a400ae0f
Child 22fc2a78

Added by Andreas Kohlbecker about 2 years ago

ref #6169 externalizing data inserter in separate class and package

View differences:

src/main/java/eu/etaxonomy/cdm/addon/config/CdmVaadinConfiguration.java
47 47
 */
48 48
@Configuration
49 49
@ComponentScan(basePackages={
50
        "eu.etaxonomy.cdm.vaadin",
51 50
        "eu.etaxonomy.vaadin.ui",
51
        "eu.etaxonomy.cdm.vaadin",
52
        "eu.etaxonomy.cdm.dataInserter",
53
        "eu.etaxonomy.cdm.service",
52 54
        "eu.etaxonomy.cdm.mock" // FIXME remove once mocks are no longer needed
53 55
        },
54 56
        // exclude UI classes, these are provided via the @Bean annotated methods below
src/main/java/eu/etaxonomy/cdm/dataInserter/IAPTRegData.java
1
package eu.etaxonomy.cdm.dataInserter;
2

  
3
/**
4
 * Created by andreas on 9/16/16.
5
 */
6
public class IAPTRegData {
7

  
8
    private String date = null;
9

  
10
    private Integer regId = null;
11

  
12
    private String office = null;
13

  
14
    private Integer formNumber = null;
15

  
16
    public IAPTRegData(){
17

  
18
    }
19

  
20
    public IAPTRegData(String date, String office, Integer regID, Integer formNumber) {
21
        this.date = date;
22
        this.office = office;
23
        this.regId = regID;
24
        this.formNumber = formNumber;
25
    }
26

  
27

  
28
    public String getDate() {
29
        return date;
30
    }
31

  
32
    public void setDate(String date) {
33
        this.date = date;
34
    }
35

  
36
    public Integer getRegId() {
37
        return regId;
38
    }
39

  
40
    public void setRegId(Integer regId) {
41
        this.regId = regId;
42
    }
43

  
44
    public String getOffice() {
45
        return office;
46
    }
47

  
48
    public void setOffice(String office) {
49
        this.office = office;
50
    }
51

  
52
    public Integer getFormNumber() {
53
        return formNumber;
54
    }
55

  
56
    public void setFormNumber(Integer formNumber) {
57
        this.formNumber = formNumber;
58
    }
59
}
src/main/java/eu/etaxonomy/cdm/dataInserter/RegistrationRequiredDataInserter.java
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
}
src/main/java/eu/etaxonomy/cdm/mock/IAPTRegData.java
1
package eu.etaxonomy.cdm.mock;
2

  
3
/**
4
 * Created by andreas on 9/16/16.
5
 */
6
public class IAPTRegData {
7

  
8
    private String date = null;
9

  
10
    private Integer regId = null;
11

  
12
    private String office = null;
13

  
14
    private Integer formNumber = null;
15

  
16
    public IAPTRegData(){
17

  
18
    }
19

  
20
    public IAPTRegData(String date, String office, Integer regID, Integer formNumber) {
21
        this.date = date;
22
        this.office = office;
23
        this.regId = regID;
24
        this.formNumber = formNumber;
25
    }
26

  
27

  
28
    public String getDate() {
29
        return date;
30
    }
31

  
32
    public void setDate(String date) {
33
        this.date = date;
34
    }
35

  
36
    public Integer getRegId() {
37
        return regId;
38
    }
39

  
40
    public void setRegId(Integer regId) {
41
        this.regId = regId;
42
    }
43

  
44
    public String getOffice() {
45
        return office;
46
    }
47

  
48
    public void setOffice(String office) {
49
        this.office = office;
50
    }
51

  
52
    public Integer getFormNumber() {
53
        return formNumber;
54
    }
55

  
56
    public void setFormNumber(Integer formNumber) {
57
        this.formNumber = formNumber;
58
    }
59
}
src/main/java/eu/etaxonomy/cdm/mock/IRegistrationWorkingSetService.java
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.mock;
10

  
11
import java.util.Collection;
12

  
13
import eu.etaxonomy.cdm.model.common.User;
14
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
15
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
16
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
17
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
18

  
19
/**
20
 * @author a.kohlbecker
21
 * @since May 4, 2017
22
 *
23
 */
24
public interface IRegistrationWorkingSetService {
25

  
26
    /**
27
     * @param id the CDM Entity id
28
     * @return
29
     */
30
    public RegistrationDTO loadDtoById(Integer id);
31

  
32

  
33
    public Collection<RegistrationDTO> listDTOs();
34

  
35
    public Collection<RegistrationDTO> listDTOs(User submitter, Collection<RegistrationStatus> includedStatus);
36

  
37
    /**
38
     * @param  id the CDM Entity id
39
     * @return
40
     * @throws RegistrationValidationException
41
     */
42
    public RegistrationWorkingSet loadWorkingSetByRegistrationID(Integer id) throws RegistrationValidationException;
43

  
44
    public static final String ACTIVE_IMPL = "registrationWorkingSetService";
45

  
46
}
src/main/java/eu/etaxonomy/cdm/mock/RegistrationService.java
31 31
import eu.etaxonomy.cdm.model.name.Registration;
32 32
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
33 33
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
34
import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService;
34 35
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
35 36
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
36 37
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
src/main/java/eu/etaxonomy/cdm/mock/RegistrationWorkingSetService.java
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.mock;
10

  
11
import java.io.IOException;
12
import java.util.ArrayList;
13
import java.util.Collection;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Optional;
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.stereotype.Service;
28
import org.springframework.transaction.TransactionStatus;
29
import org.springframework.transaction.annotation.Transactional;
30

  
31
import com.fasterxml.jackson.core.JsonParseException;
32
import com.fasterxml.jackson.databind.JsonMappingException;
33
import com.fasterxml.jackson.databind.ObjectMapper;
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.Extension;
40
import eu.etaxonomy.cdm.model.common.ExtensionType;
41
import eu.etaxonomy.cdm.model.common.User;
42
import eu.etaxonomy.cdm.model.name.Registration;
43
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
44
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
45
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
46
import eu.etaxonomy.cdm.model.reference.Reference;
47
import eu.etaxonomy.cdm.persistence.query.MatchMode;
48
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
49
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
50
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
51

  
52
/**
53
 * Provides RegistrationDTOs and RegistrationWorkingsets for Registrations in the database.
54
 * <p>
55
 * Can create missing registrations for names which have Extensions of the Type <code>IAPTRegdata.json</code>.
56
 * See https://dev.e-taxonomy.eu/redmine/issues/6621 for further details.
57
 * This feature can be activated by by supplying one of the following jvm command line arguments:
58
 * <ul>
59
 * <li><code>-DregistrationCreate=iapt</code>: create all iapt Registrations if missing</li>
60
 * <li><code>-DregistrationWipeout=iapt</code>: remove all iapt Registrations</li>
61
 * <li><code>-DregistrationWipeout=all</code>: remove all Registrations</li>
62
 * </ul>
63
 * The <code>-DregistrationWipeout</code> commands are executed before the <code>-DregistrationCreate</code> and will not change the name and type designations.
64
 *
65
 *
66
 * @author a.kohlbecker
67
 * @since Mar 10, 2017
68
 *
69
 */
70
@Service("registrationWorkingSetService")
71
@Transactional(readOnly=true)
72
public class RegistrationWorkingSetService implements IRegistrationWorkingSetService, ApplicationListener<ContextRefreshedEvent> {
73

  
74
    /**
75
     *
76
     */
77
    private static final int PAGE_SIZE = 50;
78

  
79
    protected static final String PARAM_NAME_CREATE = "registrationCreate";
80

  
81
    protected static final String PARAM_NAME_WIPEOUT = "registrationWipeout";
82

  
83
    private static final Logger logger = Logger.getLogger(RegistrationWorkingSetService.class);
84

  
85
    @Autowired
86
    @Qualifier("cdmRepository")
87
    private CdmRepository repo;
88

  
89
    private ExtensionType extensionTypeIAPTRegData;
90

  
91
    public static boolean commandsExecuted = false;
92

  
93
    public RegistrationWorkingSetService() {
94

  
95
    }
96

  
97

  
98
    /**
99
     * @param id the CDM Entity id
100
     * @return
101
     */
102
    @Override
103
    public RegistrationDTO loadDtoById(Integer id) {
104
        Registration reg = repo.getRegistrationService().find(id);
105
        return new RegistrationDTO(reg);
106
    }
107

  
108

  
109
    @Override
110
    public Collection<RegistrationDTO> listDTOs() {
111

  
112
        List<Registration> regs = repo.getRegistrationService().list(null, PAGE_SIZE, 0, null, null);
113

  
114
        List<RegistrationDTO> dtos = makeDTOs(regs);
115
        return dtos;
116
    }
117

  
118
    /**
119
     * {@inheritDoc}
120
     */
121
    @Override
122
    public Collection<RegistrationDTO> listDTOs(User submitter, Collection<RegistrationStatus> includedStatus) {
123

  
124
        Pager<Registration> pager = repo.getRegistrationService().page(submitter, includedStatus, PAGE_SIZE, 0, null, null);
125
        return makeDTOs(pager.getRecords());
126
    }
127

  
128
    /**
129
     * @param  id the CDM Entity id
130
     * @return
131
     * @throws RegistrationValidationException
132
     */
133
    @Override
134
    public RegistrationWorkingSet loadWorkingSetByRegistrationID(Integer id) throws RegistrationValidationException {
135

  
136
        RegistrationDTO dto = loadDtoById(id);
137

  
138
        Pager<Registration> pager = repo.getRegistrationService().page(Optional.of((Reference)dto.getCitation()), null, null, null, null);
139

  
140
        return new RegistrationWorkingSet(makeDTOs(pager.getRecords()));
141
    }
142

  
143

  
144
    /**
145
     * @param regs
146
     * @return
147
     */
148
    private List<RegistrationDTO> makeDTOs(List<Registration> regs) {
149
        List<RegistrationDTO> dtos = new ArrayList<>(regs.size());
150
        regs.forEach(reg -> {dtos.add(new RegistrationDTO(reg));});
151
        return dtos;
152
    }
153

  
154
    // ==================== Registration creation ======================= //
155

  
156
    /**
157
     * {@inheritDoc}
158
     */
159
    @Override
160
    public void onApplicationEvent(ContextRefreshedEvent event) {
161
        executeSuppliedCommands();
162
    }
163

  
164
    /**
165
     *
166
     */
167

  
168
    private void executeSuppliedCommands() {
169

  
170
        if(commandsExecuted){
171
            // do not run twice
172
            // a second run could take place during initialization of the web context
173
            return;
174
        }
175
        commandsExecuted  = true;
176

  
177
        String wipeoutCmd = System.getProperty(PARAM_NAME_WIPEOUT);
178
        String createCmd = System.getProperty(PARAM_NAME_CREATE);
179

  
180
        // ============ DELETE
181
        if(wipeoutCmd != null && wipeoutCmd.matches("iapt|all")){
182

  
183
            boolean onlyIapt = wipeoutCmd.equals("iapt");
184
            List<UUID> deleteCandidates = new ArrayList<UUID>();
185

  
186
            TransactionStatus tx = repo.startTransaction(true);
187
            List<Registration> allRegs = repo.getRegistrationService().list(null, null, null, null, null);
188
            for(Registration reg : allRegs){
189
                if(onlyIapt){
190
                    try {
191
                        @SuppressWarnings("unchecked")
192
                        Set<Extension> extensions = reg.getName().getExtensions(getExtensionTypeIAPTRegData());
193
                        deleteCandidates.add(reg.getUuid());
194
                    } catch(NullPointerException e){
195
                        // IGNORE
196
                    }
197
                } else {
198
                    deleteCandidates.add(reg.getUuid());
199
                }
200
            }
201
            repo.commitTransaction(tx);
202
            repo.getRegistrationService().delete(deleteCandidates);
203
        }
204

  
205
        // ============ CREATE
206
        int pageIndex = 0;
207
        if(createCmd != null && createCmd.equals("iapt")){
208

  
209
            DateTimeFormatter dateFormat = org.joda.time.format.DateTimeFormat.forPattern("dd.MM.yy").withPivotYear(1950);
210

  
211
            TransactionStatus tx = repo.startTransaction(false);
212
            while(true) {
213
                Pager<TaxonNameBase> pager = repo.getNameService().page(null, 1000, pageIndex, null, null);
214
                if(pager.getRecords().isEmpty()){
215
                    break;
216
                }
217
                List<Registration> newRegs = new ArrayList<>(pager.getRecords().size());
218
                for(TaxonNameBase name : pager.getRecords()){
219

  
220
                    Set<String> extensionValues = name.getExtensions(getExtensionTypeIAPTRegData());
221

  
222
                    // there is for sure only one
223
                    if(extensionValues.isEmpty()){
224
                        continue;
225
                    }
226
                    String iaptJson = extensionValues.iterator().next();
227
                    try {
228

  
229
                        IAPTRegData iaptData = new ObjectMapper().readValue(iaptJson, IAPTRegData.class);
230

  
231
                        if(iaptData.getRegId() == null){
232
                            continue;
233
                        }
234

  
235
                        DateTime regDate = null;
236
                        if(iaptData.getDate() != null){
237
                            try {
238
                                regDate = dateFormat.parseDateTime(iaptData.getDate());
239
                                regDate.getYear();
240
                            } catch (Exception e) {
241
                                logger.error("Error parsing date: " + iaptData.getDate(), e);
242
                                continue;
243
                            }
244
                        }
245

  
246
                        Registration reg = Registration.NewInstance();
247
                        reg.setStatus(RegistrationStatus.PUBLISHED);
248
                        reg.setIdentifier("http://phycobank/" + iaptData.getRegId());
249
                        reg.setSpecificIdentifier(iaptData.getRegId().toString());
250
                        reg.setInstitution(getInstitution(iaptData.getOffice()));
251
                        reg.setName(name);
252
                        if(name.getTypeDesignations() != null && !name.getTypeDesignations().isEmpty()){
253
                            // do not add the collection directly to avoid "Found shared references to a collection" problem
254
                            HashSet<TypeDesignationBase> typeDesignations = new HashSet<>(name.getTypeDesignations().size());
255
                            typeDesignations.addAll(name.getTypeDesignations());
256
                            reg.setTypeDesignations(typeDesignations);
257
                        }
258
                        reg.setRegistrationDate(regDate);
259
                        logger.debug("IAPT Registraion for " + name.getTitleCache());
260
                        newRegs.add(reg);
261

  
262
                    } catch (JsonParseException e) {
263
                        logger.error("Error parsing IAPTRegData from extension", e);
264
                    } catch (JsonMappingException e) {
265
                        logger.error("Error mapping json from extension to IAPTRegData", e);
266
                    } catch (IOException e) {
267
                        logger.error(e);
268
                    }
269

  
270
                }
271
                repo.getRegistrationService().save(newRegs);
272
                repo.getRegistrationService().getSession().flush();
273
                logger.debug("Registrations saved");
274
                pageIndex++;
275
            }
276
            repo.commitTransaction(tx);
277
        }
278
    }
279

  
280

  
281
    /**
282
     * @param office
283
     * @return
284
     */
285
    private Institution getInstitution(String office) {
286
        Pager<AgentBase> pager = repo.getAgentService().findByTitle(Institution.class, office, MatchMode.EXACT, null, null, null, null, null);
287
        if(!pager.getRecords().isEmpty()){
288
            return (Institution) pager.getRecords().get(0);
289
        } else {
290
            Institution institute = (Institution) repo.getAgentService().save(Institution.NewNamedInstance(office));
291
            return institute;
292
        }
293
    }
294

  
295

  
296
    private ExtensionType getExtensionTypeIAPTRegData() {
297
        if(extensionTypeIAPTRegData == null){
298
            extensionTypeIAPTRegData = (ExtensionType) repo.getTermService().load(UUID.fromString("9be1bfe3-6ba0-4560-af15-86971ab96e09"));
299
        }
300
        return extensionTypeIAPTRegData;
301
    }
302

  
303

  
304

  
305

  
306
}
src/main/java/eu/etaxonomy/cdm/service/IRegistrationWorkingSetService.java
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.service;
10

  
11
import java.util.Collection;
12

  
13
import eu.etaxonomy.cdm.model.common.User;
14
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
15
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
16
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
17
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
18

  
19
/**
20
 * @author a.kohlbecker
21
 * @since May 4, 2017
22
 *
23
 */
24
public interface IRegistrationWorkingSetService {
25

  
26
    /**
27
     * @param id the CDM Entity id
28
     * @return
29
     */
30
    public RegistrationDTO loadDtoById(Integer id);
31

  
32

  
33
    public Collection<RegistrationDTO> listDTOs();
34

  
35
    public Collection<RegistrationDTO> listDTOs(User submitter, Collection<RegistrationStatus> includedStatus);
36

  
37
    /**
38
     * @param  id the CDM Entity id
39
     * @return
40
     * @throws RegistrationValidationException
41
     */
42
    public RegistrationWorkingSet loadWorkingSetByRegistrationID(Integer id) throws RegistrationValidationException;
43

  
44
    public static final String ACTIVE_IMPL = "registrationWorkingSetService";
45

  
46
}
src/main/java/eu/etaxonomy/cdm/service/RegistrationWorkingSetService.java
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.service;
10

  
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.List;
14
import java.util.Optional;
15

  
16
import org.apache.log4j.Logger;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Qualifier;
19
import org.springframework.stereotype.Service;
20
import org.springframework.transaction.annotation.Transactional;
21

  
22
import eu.etaxonomy.cdm.api.application.CdmRepository;
23
import eu.etaxonomy.cdm.api.service.pager.Pager;
24
import eu.etaxonomy.cdm.model.common.User;
25
import eu.etaxonomy.cdm.model.name.Registration;
26
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
27
import eu.etaxonomy.cdm.model.reference.Reference;
28
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
29
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
30
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
31

  
32
/**
33
 * Provides RegistrationDTOs and RegistrationWorkingsets for Registrations in the database.
34
 * <p>
35
 * Can create missing registrations for names which have Extensions of the Type <code>IAPTRegdata.json</code>.
36
 * See https://dev.e-taxonomy.eu/redmine/issues/6621 for further details.
37
 * This feature can be activated by by supplying one of the following jvm command line arguments:
38
 * <ul>
39
 * <li><code>-DregistrationCreate=iapt</code>: create all iapt Registrations if missing</li>
40
 * <li><code>-DregistrationWipeout=iapt</code>: remove all iapt Registrations</li>
41
 * <li><code>-DregistrationWipeout=all</code>: remove all Registrations</li>
42
 * </ul>
43
 * The <code>-DregistrationWipeout</code> commands are executed before the <code>-DregistrationCreate</code> and will not change the name and type designations.
44
 *
45
 *
46
 * @author a.kohlbecker
47
 * @since Mar 10, 2017
48
 *
49
 */
50
@Service("registrationWorkingSetService")
51
@Transactional(readOnly=true)
52
public class RegistrationWorkingSetService implements IRegistrationWorkingSetService {
53

  
54
    /**
55
     *
56
     */
57
    private static final int PAGE_SIZE = 50;
58

  
59
    private static final Logger logger = Logger.getLogger(RegistrationWorkingSetService.class);
60

  
61
    @Autowired
62
    @Qualifier("cdmRepository")
63
    private CdmRepository repo;
64

  
65
    public RegistrationWorkingSetService() {
66

  
67
    }
68

  
69

  
70
    /**
71
     * @param id the CDM Entity id
72
     * @return
73
     */
74
    @Override
75
    public RegistrationDTO loadDtoById(Integer id) {
76
        Registration reg = repo.getRegistrationService().find(id);
77
        return new RegistrationDTO(reg);
78
    }
79

  
80

  
81
    @Override
82
    public Collection<RegistrationDTO> listDTOs() {
83

  
84
        List<Registration> regs = repo.getRegistrationService().list(null, PAGE_SIZE, 0, null, null);
85

  
86
        List<RegistrationDTO> dtos = makeDTOs(regs);
87
        return dtos;
88
    }
89

  
90
    /**
91
     * {@inheritDoc}
92
     */
93
    @Override
94
    public Collection<RegistrationDTO> listDTOs(User submitter, Collection<RegistrationStatus> includedStatus) {
95

  
96
        Pager<Registration> pager = repo.getRegistrationService().page(submitter, includedStatus, PAGE_SIZE, 0, null, null);
97
        return makeDTOs(pager.getRecords());
98
    }
99

  
100
    /**
101
     * @param  id the CDM Entity id
102
     * @return
103
     * @throws RegistrationValidationException
104
     */
105
    @Override
106
    public RegistrationWorkingSet loadWorkingSetByRegistrationID(Integer id) throws RegistrationValidationException {
107

  
108
        RegistrationDTO dto = loadDtoById(id);
109

  
110
        Pager<Registration> pager = repo.getRegistrationService().page(Optional.of((Reference)dto.getCitation()), null, null, null, null);
111

  
112
        return new RegistrationWorkingSet(makeDTOs(pager.getRecords()));
113
    }
114

  
115

  
116
    /**
117
     * @param regs
118
     * @return
119
     */
120
    private List<RegistrationDTO> makeDTOs(List<Registration> regs) {
121
        List<RegistrationDTO> dtos = new ArrayList<>(regs.size());
122
        regs.forEach(reg -> {dtos.add(new RegistrationDTO(reg));});
123
        return dtos;
124
    }
125

  
126

  
127

  
128

  
129
}
src/main/java/eu/etaxonomy/cdm/vaadin/ui/RegistrationUI.java
10 10

  
11 11
import org.springframework.beans.factory.annotation.Autowired;
12 12
import org.springframework.context.ApplicationEventPublisher;
13
import org.springframework.context.annotation.Lazy;
13 14

  
14 15
import com.vaadin.annotations.Theme;
15 16
import com.vaadin.annotations.Title;
......
29 30
import com.vaadin.ui.UI;
30 31
import com.vaadin.ui.themes.ValoTheme;
31 32

  
33
import eu.etaxonomy.cdm.dataInserter.RegistrationRequiredDataInserter;
32 34
import eu.etaxonomy.cdm.vaadin.view.LoginViewBean;
33 35
import eu.etaxonomy.cdm.vaadin.view.registration.DashBoardView;
34 36
import eu.etaxonomy.cdm.vaadin.view.registration.ListViewBean;
......
56 58
    @Autowired
57 59
    private ViewDisplay viewDisplay;
58 60

  
61
    /**
62
     * The RegistrationDefaultDataInserter is not used in the ui directly
63
     * but will as a ApplicationListener for ContextRefreshedEvents insert
64
     * data required for the registration application into the database.
65
     */
66
    @SuppressWarnings("unused")
67
    @Autowired
68
    @Lazy
69
    private RegistrationRequiredDataInserter dataInserter;
70

  
59 71
    //---- pull into abstract super class ? ---------
60 72
    @Autowired
61 73
    SpringViewProvider viewProvider;
......
111 123
        //TODO create annotation:
112 124
        // @Styles(files={""}, branding="brand")
113 125
        //
114
        // the branding can either be specified or can be read from a properties file in .cdmLibrary/{instance-name}/cdm-vaadin.properties
115
        //  See CdmUtils for appropriate methods to access this folder
126
        // the branding can either be specified or can be read from the properties file in .cdmLibrary/remote-webapp/{instance-name}-app.properties
127
        // See CdmUtils for appropriate methods to access this folder
116 128
        // the 'vaadin://' protocol refers to the VAADIN folder
117 129
        Resource registryCssFile = new ExternalResource("vaadin://branding/" + brand + "/css/branding.css");
118 130
        Page.getCurrent().getStyles().add(registryCssFile);
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/ListPresenter.java
19 19
import com.vaadin.spring.annotation.SpringComponent;
20 20
import com.vaadin.spring.annotation.ViewScope;
21 21

  
22
import eu.etaxonomy.cdm.mock.IRegistrationWorkingSetService;
23 22
import eu.etaxonomy.cdm.model.common.User;
24 23
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
25 24
import eu.etaxonomy.cdm.persistence.hibernate.permission.Role;
25
import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService;
26 26
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
27 27
import eu.etaxonomy.cdm.vaadin.security.RolesAndPermissions;
28 28
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/RegistrationWorkflowPresenter.java
21 21
import com.vaadin.spring.annotation.SpringComponent;
22 22
import com.vaadin.spring.annotation.ViewScope;
23 23

  
24
import eu.etaxonomy.cdm.mock.IRegistrationWorkingSetService;
25 24
import eu.etaxonomy.cdm.model.name.Rank;
26 25
import eu.etaxonomy.cdm.model.name.Registration;
27 26
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
28 27
import eu.etaxonomy.cdm.model.reference.Reference;
29 28
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
29
import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService;
30 30
import eu.etaxonomy.cdm.vaadin.event.ReferenceEvent;
31 31
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
32 32
import eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkflowEvent;

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)