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.model.occurrence.DerivedUnit;
|
15
|
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
|
16
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
17
|
|
18
|
/**
|
19
|
*
|
20
|
* @author a.kohlbecker
|
21
|
* @since Jun 23, 2017
|
22
|
*
|
23
|
*/
|
24
|
public class DerivedUnitConverter<TARGET extends DerivedUnit> {
|
25
|
|
26
|
private DerivedUnit du;
|
27
|
|
28
|
/**
|
29
|
* @param specimenTypeDesignation
|
30
|
*/
|
31
|
public DerivedUnitConverter(DerivedUnit derivedUnit) {
|
32
|
this.du = derivedUnit;
|
33
|
if(derivedUnit == null){
|
34
|
throw new NullPointerException();
|
35
|
}
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* @param targetType
|
40
|
* @param recordBasis
|
41
|
* @throws DerivedUnitConversionException
|
42
|
*/
|
43
|
@SuppressWarnings("unchecked")
|
44
|
public TARGET convertTo(Class<TARGET> targetType, SpecimenOrObservationType recordBasis) throws DerivedUnitConversionException {
|
45
|
|
46
|
if(du.getClass().equals(targetType)){
|
47
|
// nothing to do
|
48
|
return (TARGET) du;
|
49
|
}
|
50
|
|
51
|
if(!isSuppoprtedType(targetType)){
|
52
|
throw new DerivedUnitConversionException(
|
53
|
String.format("Unsupported convertion target type: %s",
|
54
|
targetType.getName())
|
55
|
);
|
56
|
}
|
57
|
|
58
|
if(!canConvert()){
|
59
|
throw new DerivedUnitConversionException(
|
60
|
String.format("%s can not be converted into %s as long it contains unconvertable non null properties",
|
61
|
du.toString(),
|
62
|
targetType.getName())
|
63
|
);
|
64
|
}
|
65
|
|
66
|
TARGET newInstance = null;
|
67
|
try {
|
68
|
Method newInstanceMethod = targetType.getDeclaredMethod("NewInstance", SpecimenOrObservationType.class);
|
69
|
newInstance = (TARGET) newInstanceMethod.invoke(SpecimenOrObservationType.class, recordBasis);
|
70
|
|
71
|
copyPropertiesTo(newInstance);
|
72
|
|
73
|
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
|
74
|
| InvocationTargetException e) {
|
75
|
throw new DerivedUnitConversionException("Error during intantiation of " + targetType.getName(), e);
|
76
|
}
|
77
|
|
78
|
return newInstance;
|
79
|
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* @param newInstance
|
84
|
*/
|
85
|
private void copyPropertiesTo(TARGET n) {
|
86
|
n.setAccessionNumber(du.getAccessionNumber());
|
87
|
n.setBarcode(du.getBarcode());
|
88
|
n.setCatalogNumber(du.getCatalogNumber());
|
89
|
n.setCollection(du.getCollection());
|
90
|
n.setDerivedFrom(du.getDerivedFrom());
|
91
|
n.setExsiccatum(du.getExsiccatum());
|
92
|
n.setIndividualCount(du.getIndividualCount());
|
93
|
n.setKindOfUnit(du.getKindOfUnit());
|
94
|
n.setLifeStage(du.getLifeStage());
|
95
|
n.setLsid(du.getLsid());
|
96
|
n.setOriginalLabelInfo(du.getOriginalLabelInfo());
|
97
|
n.setPreferredStableUri(du.getPreferredStableUri());
|
98
|
n.setPreservation(du.getPreservation());
|
99
|
n.setPublish(du.isPublish());
|
100
|
n.setProtectedIdentityCache(du.isProtectedIdentityCache());
|
101
|
n.setProtectedTitleCache(du.isProtectedTitleCache());
|
102
|
// n.setRecordBasis(du.getRecordBasis()); // not to copy, this it is set for the new instance explicitly
|
103
|
n.setSex(du.getSex());
|
104
|
n.setStoredUnder(du.getStoredUnder());
|
105
|
n.setTitleCache(du.getTitleCache(), n.isProtectedTitleCache());
|
106
|
du.getSources().forEach(s -> n.addSource(s));
|
107
|
du.getAnnotations().forEach(a -> n.addAnnotation(a));
|
108
|
du.getCredits().forEach(c -> n.addCredit(c));
|
109
|
du.getDerivationEvents().forEach(de -> n.addDerivationEvent(de));
|
110
|
du.getDescriptions().forEach(d -> n.addDescription(d));
|
111
|
du.getDeterminations().forEach(det -> n.addDetermination(det));
|
112
|
du.getExtensions().forEach(e -> n.addExtension(e));
|
113
|
du.getIdentifiers().forEach(i -> n.addIdentifier(i));
|
114
|
du.getMarkers().forEach(m -> n.addMarker(m));
|
115
|
du.getRights().forEach(r -> n.addRights(r));
|
116
|
n.addSources(du.getSources());
|
117
|
du.getSpecimenTypeDesignations().forEach(std -> n.addSpecimenTypeDesignation(std));
|
118
|
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* @param targetType
|
123
|
* @return
|
124
|
*/
|
125
|
public boolean isSuppoprtedType(Class<TARGET> targetType) {
|
126
|
return targetType.equals(MediaSpecimen.class) || targetType.equals(DerivedUnit.class);
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* @return
|
131
|
*/
|
132
|
private boolean canConvert() {
|
133
|
|
134
|
if(du.getClass().equals(DerivedUnit.class)) {
|
135
|
return true;
|
136
|
}
|
137
|
if(du.getClass().equals(MediaSpecimen.class)){
|
138
|
MediaSpecimen ms = (MediaSpecimen)du;
|
139
|
return ms.getMediaSpecimen() == null;
|
140
|
}
|
141
|
|
142
|
return false;
|
143
|
}
|
144
|
|
145
|
}
|