Merge branch 'feature/#7531' into develop
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / hibernate / permission / TargetEntityStates.java
1 /**
2 * Copyright (C) 2018 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.persistence.hibernate.permission;
10
11 import java.util.Objects;
12
13 import org.hibernate.PropertyNotFoundException;
14 import org.hibernate.type.Type;
15
16 import eu.etaxonomy.cdm.model.common.CdmBase;
17
18 /**
19 * @author a.kohlbecker
20 * @since Jul 6, 2018
21 *
22 */
23 public class TargetEntityStates {
24
25 CdmBase entity;
26 Object[] currentState;
27 Object[] previousState;
28 String[] propertyNames;
29 Type[] types;
30 /**
31 * @param entity
32 * @param currentState
33 * @param previousState
34 * @param propertyNames
35 * @param types
36 */
37 public TargetEntityStates(CdmBase entity, Object[] currentState, Object[] previousState, String[] propertyNames,
38 Type[] types) {
39
40 this.entity = entity;
41 this.currentState = currentState;
42 this.previousState = previousState;
43 this.propertyNames = propertyNames;
44 this.types = types;
45 }
46
47
48 public TargetEntityStates(CdmBase entity){
49 this.entity = entity;
50 }
51
52 /**
53 * @return the entity
54 */
55 public CdmBase getEntity() {
56 return entity;
57 }
58
59 public boolean hasPreviousState() {
60 return previousState != null;
61 }
62
63 public boolean propertyChanged(String propertyName){
64 if(propertyNames == null){
65 // usually during a save or delete operation
66 return false;
67 }
68 int i = 0;
69 for(String p : propertyNames){
70 if(p.equals(propertyName)){
71 return !Objects.equals(currentState[i], previousState[i]);
72 }
73 i++;
74 }
75 throw new PropertyNotFoundException("The property " + propertyName + " does not exist in " + entity.getClass());
76 }
77
78 public <T> T previousPropertyState(String propertyName, Class<T> propertyType){
79 Object value = previousPropertyState(propertyName);
80 if(value == null){
81 return null;
82 } else {
83 return propertyType.cast(value);
84 }
85 }
86
87 public Object previousPropertyState(String propertyName){
88 int i = 0;
89 for(String p : propertyNames){
90 if(p.equals(propertyName)){
91 return previousState[i];
92 }
93 i++;
94 }
95 throw new PropertyNotFoundException("The property " + propertyName + " does not exist in " + entity.getClass());
96 }
97
98 public Object currentPropertyState(String propertyName){
99 int i = 0;
100 for(String p : propertyNames){
101 if(p.equals(propertyName)){
102 return currentState[i];
103 }
104 i++;
105 }
106 throw new PropertyNotFoundException("The property " + propertyName + " does not exist in " + entity.getClass());
107 }
108
109 }