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
.utility
;
11 import java
.lang
.reflect
.InvocationTargetException
;
12 import java
.lang
.reflect
.Method
;
14 import org
.apache
.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
;
28 * @author a.kohlbecker
32 public class DerivedUnitConverter
<TARGET
extends DerivedUnit
> {
34 private DerivedUnit source
;
36 private SpecimenTypeDesignation specimenTypeDesignation
;
38 private SpecimenTypeDesignation newSpecimenTypeDesignation
;
40 private static final Logger logger
= Logger
.getLogger(DerivedUnitConverter
.class);
43 * @param specimenTypeDesignation
45 public DerivedUnitConverter(DerivedUnit source
) throws Exception
{
47 throw new NullPointerException();
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");
58 * @param specimenTypeDesignation
60 public DerivedUnitConverter(SpecimenTypeDesignation specimenTypeDesignation
) {
61 if(specimenTypeDesignation
== null){
62 throw new NullPointerException();
64 this.specimenTypeDesignation
= HibernateProxyHelper
.deproxy(specimenTypeDesignation
);
65 this.source
= HibernateProxyHelper
.deproxy(specimenTypeDesignation
.getTypeSpecimen(), DerivedUnit
.class);
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.
76 * @throws DerivedUnitConversionException
78 @SuppressWarnings("unchecked")
79 public SpecimenTypeDesignation
convertTo(Class
<TARGET
> targetType
, SpecimenOrObservationType recordBasis
) throws DerivedUnitConversionException
{
81 if(source
.getClass().equals(targetType
)){
83 return specimenTypeDesignation
;
86 if(!isSuppoprtedType(targetType
)){
87 throw new DerivedUnitConversionException(
88 String
.format("Unsupported convertion target type: %s",
94 throw new DerivedUnitConversionException(
95 String
.format("%s can not be converted into %s as long it contains unconvertable non null properties",
101 TARGET newInstance
= null;
103 Method newInstanceMethod
= targetType
.getDeclaredMethod("NewInstance", SpecimenOrObservationType
.class);
104 newInstance
= (TARGET
) newInstanceMethod
.invoke(SpecimenOrObservationType
.class, recordBasis
);
106 copyPropertiesTo(newInstance
);
108 } catch (NoSuchMethodException
| SecurityException
| IllegalAccessException
| IllegalArgumentException
109 | InvocationTargetException e
) {
110 throw new DerivedUnitConversionException("Error during intantiation of " + targetType
.getName(), e
);
113 logger
.debug("convertion of " + source
.instanceToString() + "<--" + specimenTypeDesignation
.instanceToString()
115 + newSpecimenTypeDesignation
.getTypeSpecimen().instanceToString() + "<--" + newSpecimenTypeDesignation
.instanceToString() );
116 return newSpecimenTypeDesignation
;
123 private void copyPropertiesTo(TARGET target
) {
124 target
.setAccessionNumber(source
.getAccessionNumber());
125 target
.setBarcode(source
.getBarcode());
126 target
.setCatalogNumber(source
.getCatalogNumber());
127 target
.setCollection(source
.getCollection());
128 DerivationEvent derivationEvent
= source
.getDerivedFrom();
129 if(derivationEvent
!= null){
130 derivationEvent
.getDerivatives().remove(source
);
132 target
.setDerivedFrom(source
.getDerivedFrom());
133 source
.setDerivedFrom(null);
134 target
.setExsiccatum(source
.getExsiccatum());
135 target
.setIndividualCount(source
.getIndividualCount());
136 target
.setKindOfUnit(source
.getKindOfUnit());
137 target
.setLifeStage(source
.getLifeStage());
138 target
.setLsid(source
.getLsid());
139 target
.setOriginalLabelInfo(source
.getOriginalLabelInfo());
140 target
.setPreferredStableUri(source
.getPreferredStableUri());
141 target
.setPreservation(source
.getPreservation());
142 target
.setPublish(source
.isPublish());
143 target
.setProtectedIdentityCache(source
.isProtectedIdentityCache());
144 target
.setProtectedTitleCache(source
.isProtectedTitleCache());
145 // n.setRecordBasis(source.getRecordBasis()); // not to copy, this it is set for the new instance explicitly
146 target
.setSex(source
.getSex());
147 target
.setStoredUnder(source
.getStoredUnder());
148 target
.setTitleCache(source
.getTitleCache(), target
.isProtectedTitleCache());
149 source
.getSources().forEach(s
-> target
.addSource(s
));
150 source
.getAnnotations().forEach(a
-> target
.addAnnotation(a
));
151 source
.getCredits().forEach(c
-> target
.addCredit(c
));
152 source
.getDerivationEvents().forEach(de
-> target
.addDerivationEvent(de
));
153 source
.getDerivationEvents().clear();
154 source
.getDescriptions().forEach(d
-> target
.addDescription(d
));
155 source
.getDeterminations().forEach(det
-> target
.addDetermination(det
));
156 source
.getDeterminations().clear();
157 source
.getExtensions().forEach(e
-> target
.addExtension(e
));
158 source
.getIdentifiers().forEach(i
-> target
.addIdentifier(i
));
159 source
.getMarkers().forEach(m
-> target
.addMarker(m
));
160 source
.getRights().forEach(r
-> target
.addRights(r
));
161 target
.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
);
167 for(TaxonName name
: specimenTypeDesignation
.getTypifiedNames()){
168 name
.removeTypeDesignation(specimenTypeDesignation
);
170 target
.addSpecimenTypeDesignation(newSpecimenTypeDesignation
);
178 public boolean isSuppoprtedType(Class
<TARGET
> targetType
) {
179 return targetType
.equals(MediaSpecimen
.class) || targetType
.equals(DerivedUnit
.class);
185 private boolean canConvert() {
187 if(source
.getClass().equals(DerivedUnit
.class)) {
190 if(source
.getClass().equals(MediaSpecimen
.class)){
191 MediaSpecimen ms
= (MediaSpecimen
)source
;
192 Media media
= ms
.getMediaSpecimen();
193 return media
== null;
200 * Returns the derived unit which was the source entity for the conversion.
201 * You may want to delete this entity after the conversion if this makes sense
202 * in the context of the actual use case. By this you can
203 * avoid orphan derived units and type designations.
205 public DerivedUnit
oldDerivedUnit(){