Revision 81c01935
Added by Andreas Kohlbecker almost 6 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/taxonGraph/AbstractHibernateTaxonGraphProcessor.java | ||
---|---|---|
8 | 8 |
*/ |
9 | 9 |
package eu.etaxonomy.cdm.persistence.dao.hibernate.taxonGraph; |
10 | 10 |
|
11 |
import java.util.ArrayList; |
|
12 |
import java.util.EnumSet; |
|
13 |
import java.util.List; |
|
11 | 14 |
import java.util.UUID; |
12 | 15 |
|
13 | 16 |
import org.apache.log4j.Logger; |
14 | 17 |
import org.hibernate.Query; |
15 | 18 |
import org.hibernate.Session; |
16 | 19 |
|
20 |
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction; |
|
17 | 21 |
import eu.etaxonomy.cdm.model.metadata.CdmPreference; |
22 |
import eu.etaxonomy.cdm.model.name.Rank; |
|
18 | 23 |
import eu.etaxonomy.cdm.model.name.TaxonName; |
19 | 24 |
import eu.etaxonomy.cdm.model.reference.Reference; |
25 |
import eu.etaxonomy.cdm.model.reference.ReferenceType; |
|
20 | 26 |
import eu.etaxonomy.cdm.model.taxon.Taxon; |
27 |
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship; |
|
28 |
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType; |
|
21 | 29 |
import eu.etaxonomy.cdm.persistence.dao.taxonGraph.TaxonGraphException; |
22 | 30 |
|
23 | 31 |
/** |
... | ... | |
29 | 37 |
|
30 | 38 |
private static final Logger logger = Logger.getLogger(AbstractHibernateTaxonGraphProcessor.class); |
31 | 39 |
|
40 |
EnumSet<ReferenceType> referenceSectionTypes = EnumSet.of(ReferenceType.Section, ReferenceType.BookSection); |
|
41 |
|
|
32 | 42 |
private Reference secReference = null; |
33 | 43 |
|
44 |
private TaxonRelationshipType relType = TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(); |
|
45 |
|
|
46 |
protected TaxonRelationshipType relType() { |
|
47 |
if(relType == null){ |
|
48 |
relType = TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(); |
|
49 |
} |
|
50 |
return relType; |
|
51 |
} |
|
52 |
|
|
53 |
|
|
34 | 54 |
/** |
35 | 55 |
* MUST ONLY BE USED IN TESTS |
36 | 56 |
*/ |
... | ... | |
76 | 96 |
return secReference; |
77 | 97 |
} |
78 | 98 |
|
99 |
|
|
100 |
/** |
|
101 |
* Create all missing edges from the <code>taxon</code>. |
|
102 |
* |
|
103 |
* @param taxonName |
|
104 |
*/ |
|
105 |
protected void updateEdges(Taxon taxon) throws TaxonGraphException { |
|
106 |
|
|
107 |
List<TaxonName> relatedHigherNames = relatedHigherNames(taxon.getName()); |
|
108 |
Reference conceptReference = conceptReference(taxon.getName().getNomenclaturalReference()); |
|
109 |
if(conceptReference != null){ |
|
110 |
List<TaxonRelationship> relations = taxonGraphRelationsFrom(taxon, conceptReference); |
|
111 |
List<TaxonName> relatedHigherNamesWithoutRels = new ArrayList<>(relatedHigherNames); |
|
112 |
for(TaxonRelationship rel : relations){ |
|
113 |
boolean isRelToHigherName = relatedHigherNames.contains(rel.getToTaxon().getName()); |
|
114 |
if(isRelToHigherName){ |
|
115 |
relatedHigherNamesWithoutRels.remove(rel.getToTaxon().getName()); |
|
116 |
} else { |
|
117 |
taxon.removeTaxonRelation(rel); |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
for(TaxonName name : relatedHigherNamesWithoutRels){ |
|
122 |
Taxon toTaxon = assureSingleTaxon(name); |
|
123 |
taxon.addTaxonRelation(toTaxon, relType(), conceptReference, null); |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* Remove all edges from the <code>taxon</code> having the <code>conceptReference</code> |
|
130 |
* |
|
131 |
* @param taxon |
|
132 |
* @param oldConceptReference |
|
133 |
*/ |
|
134 |
protected void removeEdges(Taxon taxon, Reference conceptReference) { |
|
135 |
List<TaxonRelationship> relations = taxonGraphRelationsFrom(taxon, conceptReference); |
|
136 |
List<TaxonName> relatedHigherNames = relatedHigherNames(taxon.getName()); |
|
137 |
for(TaxonRelationship rel : relations){ |
|
138 |
boolean isRelToHigherName = relatedHigherNames.contains(rel.getToTaxon().getName()); |
|
139 |
if(isRelToHigherName){ |
|
140 |
taxon.removeTaxonRelation(rel); |
|
141 |
} |
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
146 |
* @param taxon |
|
147 |
*/ |
|
148 |
protected void updateConceptReferenceInEdges(Taxon taxon, Reference oldNomReference) throws TaxonGraphException { |
|
149 |
|
|
150 |
Reference conceptReference = conceptReference(taxon.getName().getNomenclaturalReference()); |
|
151 |
Reference oldConceptReference = conceptReference(oldNomReference); |
|
152 |
|
|
153 |
if(conceptReference != null && oldConceptReference != null){ |
|
154 |
// update old with new ref |
|
155 |
updateReferenceInEdges(taxon, conceptReference, oldConceptReference); |
|
156 |
} else if(conceptReference != null && oldConceptReference == null) { |
|
157 |
// create new relations for the name as there are none so far |
|
158 |
updateEdges(taxon); |
|
159 |
} else if(conceptReference == null && oldConceptReference != null){ |
|
160 |
// remove all relations |
|
161 |
removeEdges(taxon, oldConceptReference); |
|
162 |
} |
|
163 |
} |
|
164 |
|
|
165 |
/** |
|
166 |
* @param taxon |
|
167 |
* @param conceptReference |
|
168 |
* @param oldConceptReference |
|
169 |
*/ |
|
170 |
protected void updateReferenceInEdges(Taxon taxon, Reference conceptReference, Reference oldConceptReference) { |
|
171 |
List<TaxonRelationship> relations = taxonGraphRelationsFrom(taxon, oldConceptReference); |
|
172 |
List<TaxonName> relatedHigherNames = relatedHigherNames(taxon.getName()); |
|
173 |
for(TaxonRelationship rel : relations){ |
|
174 |
boolean isRelToHigherName = relatedHigherNames.contains(rel.getToTaxon().getName()); |
|
175 |
if(isRelToHigherName){ |
|
176 |
rel.setCitation(conceptReference); |
|
177 |
getSession().saveOrUpdate(rel); |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
|
|
79 | 182 |
abstract Session getSession(); |
80 | 183 |
|
81 | 184 |
public Taxon assureSingleTaxon(TaxonName taxonName) throws TaxonGraphException { |
... | ... | |
114 | 217 |
return session.load(Taxon.class, taxon.getId()); |
115 | 218 |
} |
116 | 219 |
|
220 |
protected Reference conceptReference(Reference nomenclaturalReference) { |
|
221 |
|
|
222 |
Reference conceptRef = nomenclaturalReference; |
|
223 |
if(conceptRef != null){ |
|
224 |
while(referenceSectionTypes.contains(conceptRef.getType()) && conceptRef.getInReference() != null){ |
|
225 |
conceptRef = conceptRef.getInReference(); |
|
226 |
} |
|
227 |
} |
|
228 |
return conceptRef; |
|
229 |
} |
|
230 |
|
|
231 |
/** |
|
232 |
* @param name |
|
233 |
* @return |
|
234 |
*/ |
|
235 |
protected List<TaxonName> relatedHigherNames(TaxonName name) { |
|
236 |
|
|
237 |
List<TaxonName> relatedNames = new ArrayList<>(); |
|
238 |
|
|
239 |
if(name.getRank().isSpecies() || name.getRank().isInfraSpecific()){ |
|
240 |
if(name.getGenusOrUninomial() != null){ |
|
241 |
List<TaxonName> names = listNames(Rank.GENUS(), name.getGenusOrUninomial(), null); |
|
242 |
if(names.size() == 0){ |
|
243 |
logger.warn("Genus entity with \"" + name.getGenusOrUninomial() + "\" missing"); |
|
244 |
} else { |
|
245 |
if(names.size() > 1){ |
|
246 |
logger.warn("Duplicate genus entities found for \"" + name.getGenusOrUninomial() + "\", will create taxon graph relation to all of them!"); |
|
247 |
} |
|
248 |
relatedNames.addAll(names); |
|
249 |
} |
|
250 |
} |
|
251 |
} |
|
252 |
if(name.getRank().isInfraSpecific()){ |
|
253 |
if(name.getGenusOrUninomial() != null && name.getSpecificEpithet() != null){ |
|
254 |
List<TaxonName> names = listNames(Rank.SPECIES(), name.getGenusOrUninomial(), name.getSpecificEpithet()); |
|
255 |
if(names.size() == 0){ |
|
256 |
logger.warn("Species entity with \"" + name.getGenusOrUninomial() + " " + name.getSpecificEpithet() + "\" missing"); |
|
257 |
} else { |
|
258 |
if(names.size() > 1){ |
|
259 |
logger.warn("Duplicate species entities found for \"" + name.getGenusOrUninomial() + " " + name.getSpecificEpithet() + "\", will create taxon graph relation to all of them!"); |
|
260 |
} |
|
261 |
relatedNames.addAll(names); |
|
262 |
} |
|
263 |
} |
|
264 |
} |
|
265 |
|
|
266 |
return relatedNames; |
|
267 |
} |
|
268 |
|
|
269 |
/** |
|
270 |
* @param taxon |
|
271 |
*/ |
|
272 |
protected List<TaxonRelationship> taxonGraphRelationsFrom(Taxon taxon, Reference citation) { |
|
273 |
List<TaxonRelationship> relations = getTaxonRelationships(taxon, relType(), citation, TaxonRelationship.Direction.relatedFrom); |
|
274 |
return relations; |
|
275 |
} |
|
276 |
|
|
277 |
/** |
|
278 |
* @param taxon |
|
279 |
*/ |
|
280 |
protected List<TaxonRelationship> taxonGraphRelationsTo(Taxon taxon, Reference citation) { |
|
281 |
List<TaxonRelationship> relations = getTaxonRelationships(taxon, relType(), citation, TaxonRelationship.Direction.relatedTo); |
|
282 |
return relations; |
|
283 |
} |
|
284 |
|
|
285 |
protected List<TaxonName> listNames(Rank rank, String genusOrUninomial, String specificEpithet){ |
|
286 |
String hql = "SELECT n FROM TaxonName n WHERE n.rank = :rank AND n.genusOrUninomial = :genusOrUninomial"; |
|
287 |
if(specificEpithet != null){ |
|
288 |
hql += " AND n.specificEpithet = :specificEpithet"; |
|
289 |
} |
|
290 |
Query q = getSession().createQuery(hql); |
|
291 |
|
|
292 |
q.setParameter("rank", rank); |
|
293 |
q.setParameter("genusOrUninomial", genusOrUninomial); |
|
294 |
if(specificEpithet != null){ |
|
295 |
q.setParameter("specificEpithet", specificEpithet); |
|
296 |
} |
|
297 |
|
|
298 |
List<TaxonName> result = q.list(); |
|
299 |
return result; |
|
300 |
} |
|
301 |
|
|
302 |
protected List<TaxonRelationship> getTaxonRelationships(Taxon relatedTaxon, TaxonRelationshipType type, Reference citation, Direction direction){ |
|
303 |
String hql = "SELECT rel FROM TaxonRelationship rel WHERE rel."+direction+" = :relatedTaxon AND rel.type = :type AND rel.citation = :citation"; |
|
304 |
Query q = getSession().createQuery(hql); |
|
305 |
q.setParameter("relatedTaxon", relatedTaxon); |
|
306 |
q.setParameter("type", type); |
|
307 |
q.setParameter("citation", citation); |
|
308 |
List<TaxonRelationship> rels = q.list(); |
|
309 |
return rels; |
|
310 |
} |
|
311 |
|
|
312 |
|
|
117 | 313 |
} |
Also available in: Unified diff
ref #7648 more pulling up into AbstractHibernateTaxonGraphProcessor