Project

General

Profile

Download (8.41 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.api.utility;
10

    
11
import java.lang.reflect.InvocationTargetException;
12
import java.lang.reflect.Method;
13

    
14
import org.apache.log4j.Logger;
15

    
16
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
17
import eu.etaxonomy.cdm.model.media.Media;
18
import eu.etaxonomy.cdm.model.name.Registration;
19
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
20
import eu.etaxonomy.cdm.model.name.TaxonName;
21
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
22
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
23
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
24
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
25

    
26
/**
27
 *
28
 * @author a.kohlbecker
29
 * @since Jun 23, 2017
30
 *
31
 */
32
public class DerivedUnitConverter<TARGET extends DerivedUnit> {
33

    
34
    private DerivedUnit source;
35

    
36
    private SpecimenTypeDesignation specimenTypeDesignation;
37

    
38
    private SpecimenTypeDesignation newSpecimenTypeDesignation;
39

    
40
    private static final Logger logger = Logger.getLogger(DerivedUnitConverter.class);
41

    
42
    /**
43
     * @param specimenTypeDesignation
44
     */
45
    public DerivedUnitConverter(DerivedUnit source) throws Exception {
46
        if(source == null){
47
            throw new NullPointerException();
48
        }
49
        this.source = HibernateProxyHelper.deproxy(source, DerivedUnit.class);
50
        if(source.getSpecimenTypeDesignations().size() == 1){
51
            specimenTypeDesignation = source.getSpecimenTypeDesignations().iterator().next();
52
        } else if (source.getSpecimenDescriptions().size() > 1){
53
            throw new Exception("can not convert derived unit with multipe type designations");
54
        }
55
    }
56

    
57
    /**
58
     * @param specimenTypeDesignation
59
     */
60
    public DerivedUnitConverter(SpecimenTypeDesignation specimenTypeDesignation) {
61
        if(specimenTypeDesignation == null){
62
            throw new NullPointerException();
63
        }
64
        this.specimenTypeDesignation = HibernateProxyHelper.deproxy(specimenTypeDesignation);
65
        this.source = HibernateProxyHelper.deproxy(specimenTypeDesignation.getTypeSpecimen(), DerivedUnit.class);
66
    }
67

    
68
    /**
69
     * converts the <code>source</code> <code>DerivedUnit</code> this converter has been created
70
     * for into a <code>DerivedUnit</code> of the type <code>TARGET</code>.
71
     * If the <code>source</code> instance was persisted the target instance will also be written
72
     * into data base and the source is deleted from there.
73
     *
74
     * @param targetType
75
     * @param recordBasis
76
     * @throws DerivedUnitConversionException
77
     */
78
    @SuppressWarnings("unchecked")
79
    public SpecimenTypeDesignation convertTo(Class<TARGET> targetType, SpecimenOrObservationType recordBasis) throws DerivedUnitConversionException {
80

    
81
        if(source.getClass().equals(targetType)){
82
            // nothing to do
83
            return specimenTypeDesignation;
84
        }
85

    
86
        if(!isSuppoprtedType(targetType)){
87
            throw new DerivedUnitConversionException(
88
                    String.format("Unsupported convertion target type: %s",
89
                            targetType.getName())
90
                    );
91
        }
92

    
93
        if(!canConvert()){
94
            throw new DerivedUnitConversionException(
95
                    String.format("%s can not be converted into %s as long it contains unconvertable non null properties",
96
                            source.toString(),
97
                            targetType.getName())
98
                    );
99
        }
100

    
101
        TARGET newInstance = null;
102
        try {
103
            Method newInstanceMethod = targetType.getDeclaredMethod("NewInstance", SpecimenOrObservationType.class);
104
            newInstance = (TARGET) newInstanceMethod.invoke(SpecimenOrObservationType.class, recordBasis);
105

    
106
            copyPropertiesTo(newInstance);
107

    
108
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
109
                | InvocationTargetException e) {
110
            throw new DerivedUnitConversionException("Error during intantiation of " + targetType.getName(), e);
111
        }
112

    
113
        logger.error("convertion of " + source.instanceToString() + "<--" + specimenTypeDesignation.instanceToString()
114
                + " to "
115
                + newSpecimenTypeDesignation.getTypeSpecimen().instanceToString() +  "<--" + newSpecimenTypeDesignation.instanceToString() );
116
        return newSpecimenTypeDesignation;
117

    
118
    }
119

    
120
    /**
121
     * @param newInstance
122
     */
123
    private void copyPropertiesTo(TARGET n) {
124
        n.setAccessionNumber(source.getAccessionNumber());
125
        n.setBarcode(source.getBarcode());
126
        n.setCatalogNumber(source.getCatalogNumber());
127
        n.setCollection(source.getCollection());
128
        DerivationEvent derivationEvent = source.getDerivedFrom();
129
        if(derivationEvent != null){
130
            derivationEvent.getDerivatives().remove(source);
131
        }
132
        n.setDerivedFrom(source.getDerivedFrom());
133
        source.setDerivedFrom(null);
134
        n.setExsiccatum(source.getExsiccatum());
135
        n.setIndividualCount(source.getIndividualCount());
136
        n.setKindOfUnit(source.getKindOfUnit());
137
        n.setLifeStage(source.getLifeStage());
138
        n.setLsid(source.getLsid());
139
        n.setOriginalLabelInfo(source.getOriginalLabelInfo());
140
        n.setPreferredStableUri(source.getPreferredStableUri());
141
        n.setPreservation(source.getPreservation());
142
        n.setPublish(source.isPublish());
143
        n.setProtectedIdentityCache(source.isProtectedIdentityCache());
144
        n.setProtectedTitleCache(source.isProtectedTitleCache());
145
        // n.setRecordBasis(source.getRecordBasis()); // not to copy, this it is set for the new instance explicitly
146
        n.setSex(source.getSex());
147
        n.setStoredUnder(source.getStoredUnder());
148
        n.setTitleCache(source.getTitleCache(), n.isProtectedTitleCache());
149
        source.getSources().forEach(s -> n.addSource(s));
150
        source.getAnnotations().forEach(a -> n.addAnnotation(a));
151
        source.getCredits().forEach(c -> n.addCredit(c));
152
        source.getDerivationEvents().forEach(de -> n.addDerivationEvent(de));
153
        source.getDerivationEvents().clear();
154
        source.getDescriptions().forEach(d -> n.addDescription(d));
155
        source.getDeterminations().forEach(det -> n.addDetermination(det));
156
        source.getDeterminations().clear();
157
        source.getExtensions().forEach(e -> n.addExtension(e));
158
        source.getIdentifiers().forEach(i -> n.addIdentifier(i));
159
        source.getMarkers().forEach(m -> n.addMarker(m));
160
        source.getRights().forEach(r -> n.addRights(r));
161
        n.addSources(source.getSources());
162
        // need to clone the SpecimenTypeDesignation, since the SpecimenTypeDesignations are being deleted by the hibernate delete cascade
163
        newSpecimenTypeDesignation = (SpecimenTypeDesignation) specimenTypeDesignation.clone();
164
        for(Registration reg : specimenTypeDesignation.getRegistrations()){
165
            reg.removeTypeDesignation(specimenTypeDesignation);
166
            reg.addTypeDesignation(newSpecimenTypeDesignation);
167
        }
168
        for(TaxonName name : specimenTypeDesignation.getTypifiedNames()){
169
            name.addTypeDesignation(newSpecimenTypeDesignation, false);
170
            name.removeTypeDesignation(specimenTypeDesignation);
171
        }
172
        n.addSpecimenTypeDesignation(newSpecimenTypeDesignation);
173

    
174
    }
175

    
176
    /**
177
     * @param targetType
178
     * @return
179
     */
180
    public boolean isSuppoprtedType(Class<TARGET> targetType) {
181
        return targetType.equals(MediaSpecimen.class) || targetType.equals(DerivedUnit.class);
182
    }
183

    
184
    /**
185
     * @return
186
     */
187
    private boolean canConvert() {
188

    
189
        if(source.getClass().equals(DerivedUnit.class)) {
190
            return true;
191
        }
192
        if(source.getClass().equals(MediaSpecimen.class)){
193
            MediaSpecimen ms = (MediaSpecimen)source;
194
            Media media = ms.getMediaSpecimen();
195
            return media == null;
196
        }
197

    
198
        return false;
199
    }
200

    
201
    /**
202
     * Returns the derived unit which was the source entity for the conversion.
203
     * You may want to delete this entity after the conversion if this makes sense
204
     * in the context of the actual use case. By this you can
205
     * avoid orphan derived units and type designations.
206
     */
207
    public DerivedUnit oldDerivedUnit(){
208
        return source;
209
    }
210

    
211
}
(2-2/6)