Project

General

Profile

Download (6.35 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 eu.etaxonomy.cdm.api.service.IOccurrenceService;
15
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
16
import eu.etaxonomy.cdm.model.media.Media;
17
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
18
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
19
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
20
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
21
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
22

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

    
31
    private DerivedUnit source;
32

    
33
    IOccurrenceService service;
34

    
35
    /**
36
     * @param specimenTypeDesignation
37
     */
38
    protected DerivedUnitConverter(DerivedUnit derivedUnit, IOccurrenceService service) {
39
        if(derivedUnit == null){
40
            throw new NullPointerException();
41
        }
42
        this.service = service;
43
        this.source = HibernateProxyHelper.deproxy(derivedUnit, DerivedUnit.class);
44

    
45
    }
46

    
47
    /**
48
     * converts the <code>source</code> <code>DerivedUnit</code> this converter has been created for into a <code>DerivedUnit</code> of the type <code>TARGET</code>.
49
     * If the <code>source</code> instance was persisted the target instance will also be written into data base and the source is deleted from there.
50
     *
51
     * @param targetType
52
     * @param recordBasis
53
     * @throws DerivedUnitConversionException
54
     */
55
    @SuppressWarnings("unchecked")
56
    public TARGET convertTo(Class<TARGET> targetType, SpecimenOrObservationType recordBasis) throws DerivedUnitConversionException {
57

    
58
        if(source.getClass().equals(targetType)){
59
            // nothing to do
60
            return (TARGET) source;
61
        }
62

    
63
        if(!isSuppoprtedType(targetType)){
64
            throw new DerivedUnitConversionException(
65
                    String.format("Unsupported convertion target type: %s",
66
                            targetType.getName())
67
                    );
68
        }
69

    
70
        if(!canConvert()){
71
            throw new DerivedUnitConversionException(
72
                    String.format("%s can not be converted into %s as long it contains unconvertable non null properties",
73
                            source.toString(),
74
                            targetType.getName())
75
                    );
76
        }
77

    
78
        TARGET newInstance = null;
79
        try {
80
            Method newInstanceMethod = targetType.getDeclaredMethod("NewInstance", SpecimenOrObservationType.class);
81
            newInstance = (TARGET) newInstanceMethod.invoke(SpecimenOrObservationType.class, recordBasis);
82

    
83
            copyPropertiesTo(newInstance);
84

    
85
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
86
                | InvocationTargetException e) {
87
            throw new DerivedUnitConversionException("Error during intantiation of " + targetType.getName(), e);
88
        }
89

    
90

    
91
        if(source.getId() > 0){
92
            service.merge(newInstance);
93
            service.delete(source);
94
        }
95

    
96
        return newInstance;
97

    
98
    }
99

    
100
    /**
101
     * @param newInstance
102
     */
103
    private void copyPropertiesTo(TARGET n) {
104
        n.setAccessionNumber(source.getAccessionNumber());
105
        n.setBarcode(source.getBarcode());
106
        n.setCatalogNumber(source.getCatalogNumber());
107
        n.setCollection(source.getCollection());
108
        DerivationEvent derivationEvent = source.getDerivedFrom();
109
        derivationEvent.getDerivatives().remove(source);
110
        n.setDerivedFrom(source.getDerivedFrom());
111
        source.setDerivedFrom(null);
112
        n.setExsiccatum(source.getExsiccatum());
113
        n.setIndividualCount(source.getIndividualCount());
114
        n.setKindOfUnit(source.getKindOfUnit());
115
        n.setLifeStage(source.getLifeStage());
116
        n.setLsid(source.getLsid());
117
        n.setOriginalLabelInfo(source.getOriginalLabelInfo());
118
        n.setPreferredStableUri(source.getPreferredStableUri());
119
        n.setPreservation(source.getPreservation());
120
        n.setPublish(source.isPublish());
121
        n.setProtectedIdentityCache(source.isProtectedIdentityCache());
122
        n.setProtectedTitleCache(source.isProtectedTitleCache());
123
        // n.setRecordBasis(source.getRecordBasis()); // not to copy, this it is set for the new instance explicitly
124
        n.setSex(source.getSex());
125
        n.setStoredUnder(source.getStoredUnder());
126
        n.setTitleCache(source.getTitleCache(), n.isProtectedTitleCache());
127
        source.getSources().forEach(s -> n.addSource(s));
128
        source.getAnnotations().forEach(a -> n.addAnnotation(a));
129
        source.getCredits().forEach(c -> n.addCredit(c));
130
        source.getDerivationEvents().forEach(de -> n.addDerivationEvent(de));
131
        source.getDerivationEvents().clear();
132
        source.getDescriptions().forEach(d -> n.addDescription(d));
133
        source.getDeterminations().forEach(det -> n.addDetermination(det));
134
        source.getDeterminations().clear();
135
        source.getExtensions().forEach(e -> n.addExtension(e));
136
        source.getIdentifiers().forEach(i -> n.addIdentifier(i));
137
        source.getMarkers().forEach(m -> n.addMarker(m));
138
        source.getRights().forEach(r -> n.addRights(r));
139
        n.addSources(source.getSources());
140
        for(SpecimenTypeDesignation std :  source.getSpecimenTypeDesignations()) {
141
            std.setTypeSpecimen(n);
142
            n.addSpecimenTypeDesignation(std);
143
         }
144
        source.getSpecimenTypeDesignations().clear();
145

    
146
    }
147

    
148
    /**
149
     * @param targetType
150
     * @return
151
     */
152
    public boolean isSuppoprtedType(Class<TARGET> targetType) {
153
        return targetType.equals(MediaSpecimen.class) || targetType.equals(DerivedUnit.class);
154
    }
155

    
156
    /**
157
     * @return
158
     */
159
    private boolean canConvert() {
160

    
161
        if(source.getClass().equals(DerivedUnit.class)) {
162
            return true;
163
        }
164
        if(source.getClass().equals(MediaSpecimen.class)){
165
            MediaSpecimen ms = (MediaSpecimen)source;
166
            Media media = ms.getMediaSpecimen();
167
            return media == null;
168
        }
169

    
170
        return false;
171
    }
172

    
173
}
(2-2/7)