Project

General

Profile

Revision 0e62dfd8

ID0e62dfd8bbd48842e06fba0475349701289f225a
Parent 5aece4cd
Child b4cec96a

Added by unknown over 2 years ago

fix #6542 name identifier import

View differences:

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
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/identifier/IdentifierImportConfigurator.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.InputStreamReader;
12
import java.util.UUID;
13

  
14
import eu.etaxonomy.cdm.database.ICdmDataSource;
15
import eu.etaxonomy.cdm.io.common.SimpleImportConfiguratorBase;
16
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
17
import eu.etaxonomy.cdm.model.reference.Reference;
18

  
19
/**
20
 * Configurator for importing Identifier via csv for a given class.
21
 *
22
 * @author a.mueller
23
 * @date 14.03.2017
24
 */
25
public class IdentifierImportConfigurator
26
        extends SimpleImportConfiguratorBase<InputStreamReader>{
27

  
28
    private static final long serialVersionUID = 2399625330102810465L;
29

  
30
    private Class<? extends IdentifiableEntity> cdmClass;
31
    private UUID uuidIdentifierType;
32

  
33

  
34
    /**
35
     * @param source
36
     * @param cdmDestination
37
     * @return
38
     */
39
    public static IdentifierImportConfigurator NewInstance(InputStreamReader file,
40
            ICdmDataSource cdmDestination) {
41
        return new IdentifierImportConfigurator(file, cdmDestination);
42
    }
43

  
44
// ****************** CONSTRUCTOR *****************************/
45

  
46
    private IdentifierImportConfigurator(InputStreamReader file,
47
            ICdmDataSource cdmDestination){
48
        super(file, cdmDestination, null);
49
    }
50

  
51
// *************************************
52

  
53

  
54
    @Override
55
    @SuppressWarnings("unchecked")
56
    protected void makeIoClassList(){
57
        ioClassList = new Class[]{
58
            IdentifierImport.class,
59
        };
60
    }
61

  
62
    public Class<? extends IdentifiableEntity> getCdmClass() {
63
        return this.cdmClass;
64
    }
65
    public void setCdmClass(Class<? extends IdentifiableEntity> cdmClass) {
66
        this.cdmClass = cdmClass;
67
    }
68

  
69

  
70
    public UUID getIdentifierTypeUuid() {
71
        return uuidIdentifierType;
72
    }
73

  
74
    public void setIdentifierTypeUuid(UUID uuidIdentifierType) {
75
        this.uuidIdentifierType = uuidIdentifierType;
76
    }
77

  
78

  
79
    /**
80
     * {@inheritDoc}
81
     */
82
    @Override
83
    public Reference getSourceReference() {
84
        // TODO Auto-generated method stub
85
        return null;
86
    }
87

  
88
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)