2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.identifier
;
12 import java
.io
.IOException
;
13 import java
.io
.InputStreamReader
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
21 import org
.springframework
.transaction
.TransactionStatus
;
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
;
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.
36 * The csv file has to follow the following format:
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>
41 * NOTE: This import was first written for #6542
42 * NOTE 2: TODO It was observed that the last line was not imported.
48 public class IdentifierImport
49 extends SimpleImport
<IdentifierImportConfigurator
, File
>{
51 private static final long serialVersionUID
= 5797541146159665997L;
52 private static Logger logger
= Logger
.getLogger(IdentifierImport
.class);
58 protected void doInvoke(IdentifierImportConfigurator config
) {
60 InputStreamReader inputReader
= config
.getSource();
61 CSVReader csvReader
= new CSVReader(inputReader
, ';');
62 List
<String
[]> lines
= csvReader
.readAll();
64 logger
.info("Import file is empty");
69 @SuppressWarnings("rawtypes")
70 Set
<IdentifiableEntity
> entitiesToSave
= new HashSet
<>();
72 UUID identifierTypeUuid
= config
.getIdentifierTypeUuid();
74 DefinedTermBase
<?
> identifierType
= getTermService().find(identifierTypeUuid
);
75 if (identifierType
== null || identifierType
.getTermType() != TermType
.IdentifierType
){
76 logger
.warn("IdentifierType not recognized. Skip import");
80 DefinedTerm idType
= CdmBase
.deproxy(identifierType
, DefinedTerm
.class);
83 for (String
[] strs
: lines
){
84 IdentifiableEntity
<?
> entity
= handleSingleLine(config
, strs
, idType
, i
);
86 entitiesToSave
.add(entity
);
92 //not needed as the objects update automatically during transactions in handleSingleLine()
93 // getCommonService().saveOrUpdate(entitiesToSave);
95 } catch (IOException e
) {
96 throw new RuntimeException(e
);
101 * @param config configurator
102 * @param strs text array for given line
103 * @param i line counter
106 private IdentifiableEntity
<?
> handleSingleLine(IdentifierImportConfigurator config
, String
[] strs
,
107 DefinedTerm idType
, int i
) {
110 if (strs
.length
< 1){
111 String message
= String
.format(
112 "No data available in line %d. Skipped", i
);
113 logger
.warn(message
);
118 String uuidStr
= strs
[0];
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
);
129 TransactionStatus tx
= this.startTransaction();
130 IdentifiableEntity
<?
> entity
= getEntityFromRepository(config
, uuid
);
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
);
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
);
145 //skip column 1, it is for default language label, but not used during import
147 if (isNotBlank(strs
[1])){
151 Identifier
<?
> identifier
= Identifier
.NewInstance(value
, idType
);
152 entity
.addIdentifier(identifier
);
154 this.commitTransaction(tx
);
158 private IdentifiableEntity
<?
> getEntityFromRepository(IdentifierImportConfigurator config
, UUID uuid
) {
159 IdentifiableEntity
<?
> result
= getCommonService().find(config
.getCdmClass(), uuid
);