1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.bo.entity.impl; |
17 | |
|
18 | |
import java.util.HashMap; |
19 | |
import java.util.LinkedHashMap; |
20 | |
import java.util.Map; |
21 | |
|
22 | |
import javax.persistence.Column; |
23 | |
import javax.persistence.Entity; |
24 | |
import javax.persistence.FetchType; |
25 | |
import javax.persistence.Id; |
26 | |
import javax.persistence.JoinColumn; |
27 | |
import javax.persistence.ManyToOne; |
28 | |
import javax.persistence.PostLoad; |
29 | |
import javax.persistence.PrePersist; |
30 | |
import javax.persistence.PreUpdate; |
31 | |
import javax.persistence.Table; |
32 | |
import javax.persistence.Transient; |
33 | |
|
34 | |
import org.apache.commons.lang.StringUtils; |
35 | |
import org.apache.ojb.broker.PersistenceBroker; |
36 | |
import org.apache.ojb.broker.PersistenceBrokerException; |
37 | |
import org.kuali.rice.kim.bo.entity.KimEntityExternalIdentifier; |
38 | |
import org.kuali.rice.kim.bo.reference.ExternalIdentifierType; |
39 | |
import org.kuali.rice.kim.bo.reference.impl.ExternalIdentifierTypeImpl; |
40 | |
import org.kuali.rice.kim.util.KimConstants; |
41 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
@Entity |
47 | |
@Table(name = "KRIM_ENTITY_EXT_ID_T") |
48 | 0 | public class KimEntityExternalIdentifierImpl extends KimEntityDataBase implements KimEntityExternalIdentifier { |
49 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimEntityExternalIdentifierImpl.class); |
50 | |
|
51 | |
private static final long serialVersionUID = 1L; |
52 | |
|
53 | |
@Id |
54 | |
@Column(name = "ENTITY_EXT_ID_ID") |
55 | |
protected String entityExternalIdentifierId; |
56 | |
|
57 | |
@Column(name = "ENTITY_ID") |
58 | |
protected String entityId; |
59 | |
|
60 | |
@Column(name = "EXT_ID_TYP_CD") |
61 | |
protected String externalIdentifierTypeCode; |
62 | |
|
63 | |
@Column(name = "EXT_ID") |
64 | |
protected String externalId; |
65 | |
|
66 | |
@ManyToOne(targetEntity=KimEntityEntityTypeImpl.class, fetch = FetchType.EAGER, cascade = {}) |
67 | |
@JoinColumn(name = "EXT_ID_TYP_CD", insertable = false, updatable = false) |
68 | |
protected ExternalIdentifierType externalIdentifierType; |
69 | |
|
70 | 0 | @Transient protected ExternalIdentifierType cachedExtIdType = null; |
71 | 0 | @Transient protected boolean encryptionRequired = false; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public String getEntityExternalIdentifierId() { |
77 | 0 | return entityExternalIdentifierId; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public String getExternalId() { |
84 | 0 | return externalId; |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public String getExternalIdentifierTypeCode() { |
91 | 0 | return externalIdentifierTypeCode; |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public void setExternalId(String externalId) { |
98 | 0 | this.externalId = externalId; |
99 | 0 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public void setExternalIdentifierTypeCode(String externalIdentifierTypeCode) { |
105 | 0 | this.externalIdentifierTypeCode = externalIdentifierTypeCode; |
106 | 0 | cachedExtIdType = null; |
107 | 0 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
@SuppressWarnings("unchecked") |
113 | |
@Override |
114 | |
protected LinkedHashMap toStringMapper() { |
115 | 0 | LinkedHashMap m = new LinkedHashMap(); |
116 | 0 | m.put( "entityExternalIdentifierId", entityExternalIdentifierId ); |
117 | 0 | m.put( "externalIdentifierTypeCode", externalIdentifierTypeCode ); |
118 | 0 | m.put( "externalId", externalId ); |
119 | 0 | return m; |
120 | |
} |
121 | |
|
122 | |
public void setEntityExternalIdentifierId(String entityExternalIdentifierId) { |
123 | 0 | this.entityExternalIdentifierId = entityExternalIdentifierId; |
124 | 0 | } |
125 | |
|
126 | |
public String getEntityId() { |
127 | 0 | return this.entityId; |
128 | |
} |
129 | |
|
130 | |
public void setEntityId(String entityId) { |
131 | 0 | this.entityId = entityId; |
132 | 0 | } |
133 | |
|
134 | |
public ExternalIdentifierType getExternalIdentifierType() { |
135 | 0 | return this.externalIdentifierType; |
136 | |
} |
137 | |
|
138 | |
public void setExternalIdentifierType(ExternalIdentifierType externalIdentifierType) { |
139 | 0 | this.externalIdentifierType = externalIdentifierType; |
140 | 0 | cachedExtIdType = null; |
141 | 0 | } |
142 | |
|
143 | |
@Override |
144 | |
public void beforeInsert(PersistenceBroker persistenceBroker) throws PersistenceBrokerException { |
145 | 0 | super.beforeInsert(persistenceBroker); |
146 | 0 | encryptExternalId(); |
147 | 0 | } |
148 | |
|
149 | |
@Override |
150 | |
public void beforeUpdate(PersistenceBroker persistenceBroker) throws PersistenceBrokerException { |
151 | 0 | beforeUpdate(); |
152 | 0 | } |
153 | |
|
154 | |
@Override |
155 | |
public void afterLookup(PersistenceBroker persistenceBroker) throws PersistenceBrokerException { |
156 | 0 | super.afterLookup(persistenceBroker); |
157 | 0 | decryptExternalId(); |
158 | 0 | } |
159 | |
|
160 | |
@Override |
161 | |
@PrePersist |
162 | |
public void beforeInsert() { |
163 | 0 | super.beforeInsert(); |
164 | 0 | encryptExternalId(); |
165 | 0 | } |
166 | |
|
167 | |
@Override |
168 | |
@PreUpdate |
169 | |
public void beforeUpdate() { |
170 | 0 | super.beforeUpdate(); |
171 | 0 | encryptExternalId(); |
172 | 0 | } |
173 | |
|
174 | |
@PostLoad |
175 | |
public void afterLookup(){ |
176 | 0 | decryptExternalId(); |
177 | 0 | } |
178 | |
|
179 | |
protected void evaluateExternalIdentifierType() { |
180 | 0 | if ( cachedExtIdType == null ) { |
181 | 0 | Map<String, String> criteria = new HashMap<String, String>(); |
182 | 0 | criteria.put(KimConstants.PrimaryKeyConstants.KIM_TYPE_CODE, externalIdentifierTypeCode); |
183 | 0 | cachedExtIdType = (ExternalIdentifierType) KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(ExternalIdentifierTypeImpl.class, criteria); |
184 | 0 | encryptionRequired = cachedExtIdType!= null && cachedExtIdType.isEncryptionRequired(); |
185 | |
} |
186 | 0 | } |
187 | |
|
188 | |
protected void encryptExternalId() { |
189 | 0 | evaluateExternalIdentifierType(); |
190 | 0 | if ( encryptionRequired && StringUtils.isNotEmpty(externalId) ) { |
191 | |
try { |
192 | 0 | externalId = KNSServiceLocator.getEncryptionService().encrypt(externalId); |
193 | |
} |
194 | 0 | catch ( Exception e ) { |
195 | 0 | LOG.info("Unable to encrypt value : " + e.getMessage() + " or it is already encrypted"); |
196 | 0 | } |
197 | |
} |
198 | 0 | } |
199 | |
|
200 | |
protected void decryptExternalId() { |
201 | 0 | evaluateExternalIdentifierType(); |
202 | 0 | if ( encryptionRequired && StringUtils.isNotEmpty(externalId) ) { |
203 | |
try { |
204 | 0 | externalId = KNSServiceLocator.getEncryptionService().decrypt(externalId); |
205 | |
} |
206 | 0 | catch ( Exception e ) { |
207 | 0 | LOG.info("Unable to decrypt value : " + e.getMessage() + " or it is already decrypted"); |
208 | 0 | } |
209 | |
} |
210 | 0 | } |
211 | |
} |