2 * Copyright (C) 2018 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
.service
.taxonGraph
;
11 import org
.apache
.commons
.lang
.ArrayUtils
;
12 import org
.apache
.log4j
.Logger
;
13 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
14 import org
.springframework
.stereotype
.Component
;
16 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
17 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
18 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeEvent
;
19 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeEvent
.EventType
;
20 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
21 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.ICdmPostDataChangeObserver
;
24 * @author a.kohlbecker
29 public class TaxonGraphObserver
implements ICdmPostDataChangeObserver
{
32 private ITaxonGraphService taxonGraphService
;
34 private String
[] NAMEPARTS_OR_RANK_PROPS
= new String
[]{"genusOrUninomial", "specificEpithet", "rank"};
35 private String
[] NOMREF_PROP
= new String
[]{"nomenclaturalReference"};
41 public void update(CdmDataChangeMap changeEvents
) {
44 for(CdmDataChangeEvent event
: changeEvents
.getEvents(EventType
.UPDATE
)){
45 if(event
.getEntity() instanceof TaxonName
){
46 if(checkStateChange(event
, NAMEPARTS_OR_RANK_PROPS
) > -1){
47 taxonGraphService
.onNameOrRankChange((TaxonName
) event
.getEntity());
49 int changedNomRefIndex
= checkStateChange(event
, NOMREF_PROP
);
50 if(changedNomRefIndex
> -1){
51 taxonGraphService
.onNomReferenceChange((TaxonName
) event
.getEntity(), (Reference
)event
.getOldState()[changedNomRefIndex
]);
55 for(CdmDataChangeEvent event
: changeEvents
.getEvents(EventType
.INSERT
)){
56 if(event
.getEntity() instanceof TaxonName
){
57 taxonGraphService
.onNewTaxonName((TaxonName
) event
.getEntity());
60 } catch (TaxonGraphException e
) {
61 Logger
.getLogger(this.getClass()).error(e
);
67 private int checkStateChange(CdmDataChangeEvent event
, String
[] propertyNamesToCheck
){
69 String
[] propertyNames
= event
.getPersister().getPropertyNames();
70 Object
[] oldState
= event
.getOldState();
71 Object
[] state
= event
.getState();
73 int propsCheckedCnt
= 0;
74 for(int i
= 0; i
< propertyNames
.length
; i
++){
75 if(ArrayUtils
.contains(propertyNamesToCheck
, propertyNames
[i
])){
77 if(!oldState
[i
].equals(state
[i
])){
80 if(propsCheckedCnt
== propertyNamesToCheck
.length
){
85 // this execption should be raised during the unit tests already and thus will never occur in production
86 throw new RuntimeException("TaxonName class misses at least one property of: " + ArrayUtils
.toString(propertyNamesToCheck
));