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
.api
.util
;
11 import java
.lang
.reflect
.InvocationTargetException
;
12 import java
.lang
.reflect
.Method
;
14 import org
.apache
.logging
.log4j
.LogManager
;import org
.apache
.logging
.log4j
.Logger
;
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
;
27 * @author a.kohlbecker
30 public class DerivedUnitConverter
<TARGET
extends DerivedUnit
> {
32 private DerivedUnit source
;
34 private SpecimenTypeDesignation specimenTypeDesignation
;
36 private SpecimenTypeDesignation newSpecimenTypeDesignation
;
38 private static final Logger logger
= LogManager
.getLogger(DerivedUnitConverter
.class);
41 * @param specimenTypeDesignation
43 public DerivedUnitConverter(DerivedUnit source
) throws Exception
{
45 throw new NullPointerException();
47 this.source
= HibernateProxyHelper
.deproxy(source
, DerivedUnit
.class);
48 if(source
.getSpecimenTypeDesignations().size() == 1){
49 specimenTypeDesignation
= source
.getSpecimenTypeDesignations().iterator().next();
50 } else if (source
.getSpecimenDescriptions().size() > 1){
51 throw new Exception("can not convert derived unit with multipe type designations");
56 * @param specimenTypeDesignation
58 public DerivedUnitConverter(SpecimenTypeDesignation specimenTypeDesignation
) {
59 if(specimenTypeDesignation
== null){
60 throw new NullPointerException();
62 this.specimenTypeDesignation
= HibernateProxyHelper
.deproxy(specimenTypeDesignation
);
63 this.source
= HibernateProxyHelper
.deproxy(specimenTypeDesignation
.getTypeSpecimen(), DerivedUnit
.class);
67 * converts the <code>source</code> <code>DerivedUnit</code> this converter has been created
68 * for into a <code>DerivedUnit</code> of the type <code>TARGET</code>.
69 * If the <code>source</code> instance was persisted the target instance will also be written
70 * into data base and the source is deleted from there.
74 * @throws DerivedUnitConversionException
76 @SuppressWarnings("unchecked")
77 public SpecimenTypeDesignation
convertTo(Class
<TARGET
> targetType
, SpecimenOrObservationType recordBasis
) throws DerivedUnitConversionException
{
79 if(source
.getClass().equals(targetType
)){
81 return specimenTypeDesignation
;
84 if(!isSuppoprtedType(targetType
)){
85 throw new DerivedUnitConversionException(
86 String
.format("Unsupported convertion target type: %s",
92 throw new DerivedUnitConversionException(
93 String
.format("%s can not be converted into %s as long it contains unconvertable non null properties",
99 TARGET newInstance
= null;
101 Method newInstanceMethod
= targetType
.getDeclaredMethod("NewInstance", SpecimenOrObservationType
.class);
102 newInstance
= (TARGET
) newInstanceMethod
.invoke(SpecimenOrObservationType
.class, recordBasis
);
104 copyPropertiesTo(newInstance
);
106 } catch (NoSuchMethodException
| SecurityException
| IllegalAccessException
| IllegalArgumentException
107 | InvocationTargetException e
) {
108 throw new DerivedUnitConversionException("Error during intantiation of " + targetType
.getName(), e
);
111 logger
.debug("convertion of " + source
.instanceToString() + "<--" + specimenTypeDesignation
.instanceToString()
113 + newSpecimenTypeDesignation
.getTypeSpecimen().instanceToString() + "<--" + newSpecimenTypeDesignation
.instanceToString() );
114 return newSpecimenTypeDesignation
;
121 private void copyPropertiesTo(TARGET target
) {
122 target
.setAccessionNumber(source
.getAccessionNumber());
123 target
.setBarcode(source
.getBarcode());
124 target
.setCatalogNumber(source
.getCatalogNumber());
125 target
.setCollection(source
.getCollection());
126 DerivationEvent derivationEvent
= source
.getDerivedFrom();
127 if(derivationEvent
!= null){
128 derivationEvent
.getDerivatives().remove(source
);
130 target
.setDerivedFrom(source
.getDerivedFrom());
131 source
.setDerivedFrom(null);
132 target
.setExsiccatum(source
.getExsiccatum());
133 target
.setIndividualCount(source
.getIndividualCount());
134 target
.setKindOfUnit(source
.getKindOfUnit());
135 target
.setLifeStage(source
.getLifeStage());
136 target
.setLsid(source
.getLsid());
137 target
.setOriginalLabelInfo(source
.getOriginalLabelInfo());
138 target
.setPreferredStableUri(source
.getPreferredStableUri());
139 target
.setPreservation(source
.getPreservation());
140 target
.setPublish(source
.isPublish());
141 target
.setProtectedIdentityCache(source
.isProtectedIdentityCache());
142 target
.setProtectedTitleCache(source
.isProtectedTitleCache());
143 // n.setRecordBasis(source.getRecordBasis()); // not to copy, this it is set for the new instance explicitly
144 target
.setSex(source
.getSex());
145 target
.setStoredUnder(source
.getStoredUnder());
146 target
.setTitleCache(source
.getTitleCache(), target
.isProtectedTitleCache());
147 source
.getSources().forEach(s
-> target
.addSource(s
));
148 source
.getAnnotations().forEach(a
-> target
.addAnnotation(a
));
149 source
.getCredits().forEach(c
-> target
.addCredit(c
));
150 source
.getDerivationEvents().forEach(de
-> target
.addDerivationEvent(de
));
151 source
.getDerivationEvents().clear();
152 source
.getDescriptions().forEach(d
-> target
.addDescription(d
));
153 source
.getDeterminations().forEach(det
-> target
.addDetermination(det
));
154 source
.getDeterminations().clear();
155 source
.getExtensions().forEach(e
-> target
.addExtension(e
));
156 source
.getIdentifiers().forEach(i
-> target
.addIdentifier(i
));
157 source
.getMarkers().forEach(m
-> target
.addMarker(m
));
158 source
.getRights().forEach(r
-> target
.addRights(r
));
159 target
.addSources(source
.getSources());
160 // need to clone the SpecimenTypeDesignation, since the SpecimenTypeDesignations are being deleted by the hibernate delete cascade
161 newSpecimenTypeDesignation
= specimenTypeDesignation
.clone();
162 for(Registration reg
: specimenTypeDesignation
.getRegistrations()){
163 reg
.removeTypeDesignation(specimenTypeDesignation
);
165 for(TaxonName name
: specimenTypeDesignation
.getTypifiedNames()){
166 name
.removeTypeDesignation(specimenTypeDesignation
);
168 target
.addSpecimenTypeDesignation(newSpecimenTypeDesignation
);
176 public boolean isSuppoprtedType(Class
<TARGET
> targetType
) {
177 return targetType
.equals(MediaSpecimen
.class) || targetType
.equals(DerivedUnit
.class);
183 private boolean canConvert() {
185 if(source
.getClass().equals(DerivedUnit
.class)) {
188 if(source
.getClass().equals(MediaSpecimen
.class)){
189 MediaSpecimen ms
= (MediaSpecimen
)source
;
190 Media media
= ms
.getMediaSpecimen();
191 return media
== null;
198 * Returns the derived unit which was the source entity for the conversion.
199 * You may want to delete this entity after the conversion if this makes sense
200 * in the context of the actual use case. By this you can
201 * avoid orphan derived units and type designations.
203 public DerivedUnit
oldDerivedUnit(){