fix #6799: add totalCount of ticks as totalwork and start the progress monitor when...
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / identifier / IdentifierImport.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.io.identifier;
10
11 import java.io.File;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import org.apache.log4j.Logger;
20 import org.springframework.stereotype.Component;
21 import org.springframework.transaction.TransactionStatus;
22
23 import au.com.bytecode.opencsv.CSVReader;
24 import eu.etaxonomy.cdm.io.common.SimpleImport;
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.common.DefinedTerm;
27 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
28 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
29 import eu.etaxonomy.cdm.model.common.Identifier;
30 import eu.etaxonomy.cdm.model.common.TermType;
31
32 /**
33 * Imports identifiers for a certain {@link IdentifiableEntity} class from a csv
34 * file. The class and the identifier type are defined in the configurator.
35 *
36 * The csv file has to follow the following format:
37 * <BR><BR>
38 * 1. field: uuid of the {@link IdentifiableEntity} of class defined in configurator<BR>
39 * 2. The value of the identifier (of type defined in configurator)<BR>
40 *
41 * NOTE: This import was first written for #6542
42 * NOTE 2: TODO It was observed that the last line was not imported.
43 *
44 * @author a.mueller
45 * @date 25.03.2017
46 */
47 @Component
48 public class IdentifierImport
49 extends SimpleImport<IdentifierImportConfigurator, File>{
50
51 private static final long serialVersionUID = 5797541146159665997L;
52 private static Logger logger = Logger.getLogger(IdentifierImport.class);
53
54 /**
55 * {@inheritDoc}
56 */
57 @Override
58 protected void doInvoke(IdentifierImportConfigurator config) {
59 try {
60 InputStreamReader inputReader = config.getSource();
61 CSVReader csvReader = new CSVReader(inputReader, ';');
62 List<String[]> lines = csvReader.readAll();
63 if (lines.isEmpty()){
64 logger.info("Import file is empty");
65 csvReader.close();
66 return;
67 }
68
69 @SuppressWarnings("rawtypes")
70 Set<IdentifiableEntity> entitiesToSave = new HashSet<>();
71
72 UUID identifierTypeUuid = config.getIdentifierTypeUuid();
73
74 DefinedTermBase<?> identifierType = getTermService().find(identifierTypeUuid);
75 if (identifierType == null || identifierType.getTermType() != TermType.IdentifierType){
76 logger.warn("IdentifierType not recognized. Skip import");
77 csvReader.close();
78 return;
79 }
80 DefinedTerm idType = CdmBase.deproxy(identifierType, DefinedTerm.class);
81
82 int i = 0;
83 for (String[] strs : lines){
84 IdentifiableEntity<?> entity = handleSingleLine(config, strs, idType, i);
85 if (entity != null){
86 entitiesToSave.add(entity);
87 }
88 i++;
89 }
90 csvReader.close();
91
92 //not needed as the objects update automatically during transactions in handleSingleLine()
93 // getCommonService().saveOrUpdate(entitiesToSave);
94
95 } catch (IOException e) {
96 throw new RuntimeException(e);
97 }
98 }
99
100 /**
101 * @param config configurator
102 * @param strs text array for given line
103 * @param i line counter
104 * @return
105 */
106 private IdentifiableEntity<?> handleSingleLine(IdentifierImportConfigurator config, String[] strs,
107 DefinedTerm idType, int i) {
108
109 //no data
110 if (strs.length < 1){
111 String message = String.format(
112 "No data available in line %d. Skipped", i);
113 logger.warn(message);
114 return null;
115 }
116
117 //entity uuid
118 String uuidStr = strs[0];
119 UUID uuid;
120 try {
121 uuid = UUID.fromString(uuidStr);
122 } catch (Exception e) {
123 String message = String.format(
124 "Entity identifier not recognized as UUID in line %d. Skipped", i);
125 logger.warn(message);
126 return null;
127 }
128
129 TransactionStatus tx = this.startTransaction();
130 IdentifiableEntity<?> entity = getEntityFromRepository(config, uuid);
131 if (entity == null){
132 String message = String.format(
133 "Entity for uuid %s could not be found in line %d. Skipped", uuid.toString(), i);
134 logger.warn(message);
135 return null;
136 }
137
138 //identifier value
139 if (strs.length < 2){
140 String message = String.format(
141 "Record in line %d has no identifier value information. Skipped", i);
142 logger.warn(message);
143 return null;
144 }
145 //skip column 1, it is for default language label, but not used during import
146 String value = null;
147 if (isNotBlank(strs[1])){
148 value = strs[1];
149 }
150
151 Identifier<?> identifier = Identifier.NewInstance(value, idType);
152 entity.addIdentifier(identifier);
153
154 this.commitTransaction(tx);
155 return entity;
156 }
157
158 private IdentifiableEntity<?> getEntityFromRepository(IdentifierImportConfigurator config, UUID uuid) {
159 IdentifiableEntity<?> result = getCommonService().find(config.getCdmClass(), uuid);
160 return result;
161 }
162 }