1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.impl.identity.external |
17 | |
|
18 | |
import javax.persistence.Column |
19 | |
import javax.persistence.FetchType |
20 | |
import javax.persistence.GeneratedValue |
21 | |
import javax.persistence.Id |
22 | |
import javax.persistence.JoinColumn |
23 | |
import javax.persistence.ManyToOne |
24 | |
import javax.persistence.Transient |
25 | |
import org.apache.commons.lang.StringUtils |
26 | |
import org.hibernate.annotations.GenericGenerator |
27 | |
import org.hibernate.annotations.Parameter |
28 | |
import org.kuali.rice.core.api.CoreApiServiceLocator |
29 | |
import org.kuali.rice.kim.api.KimConstants |
30 | |
import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier |
31 | |
import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierContract |
32 | |
import org.kuali.rice.krad.bo.PersistableBusinessObjectBase |
33 | |
import org.kuali.rice.krad.service.KRADServiceLocator |
34 | |
|
35 | |
class EntityExternalIdentifierBo extends PersistableBusinessObjectBase implements EntityExternalIdentifierContract { |
36 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EntityExternalIdentifierBo.class) |
37 | |
private static final long serialVersionUID = 1L; |
38 | |
|
39 | |
@Id |
40 | |
@GeneratedValue(generator="KRIM_ENTITY_EXT_ID_ID_S") |
41 | |
@GenericGenerator(name="KRIM_ENTITY_EXT_ID_ID_S",strategy="org.kuali.rice.core.jpa.spring.RiceNumericStringSequenceStyleGenerator",parameters=[ |
42 | |
@Parameter(name="sequence_name",value="KRIM_ENTITY_EXT_ID_ID_S"), |
43 | |
@Parameter(name="value_column",value="id") |
44 | |
]) |
45 | |
@Column(name = "ENTITY_EXT_ID_ID") |
46 | |
String id; |
47 | |
|
48 | |
@Column(name = "ENTITY_ID") |
49 | |
String entityId; |
50 | |
|
51 | |
@Column(name = "EXT_ID_TYP_CD") |
52 | |
String externalIdentifierTypeCode; |
53 | |
|
54 | |
@Column(name = "EXT_ID") |
55 | |
String externalId; |
56 | |
|
57 | |
@ManyToOne(targetEntity=EntityExternalIdentifierTypeBo.class, fetch = FetchType.EAGER, cascade = []) |
58 | |
@JoinColumn(name = "EXT_ID_TYP_CD", insertable = false, updatable = false) |
59 | |
EntityExternalIdentifierTypeBo externalIdentifierType; |
60 | |
|
61 | |
@Transient |
62 | |
private EntityExternalIdentifierTypeBo cachedExtIdType = null; |
63 | |
@Transient |
64 | |
private boolean encryptionRequired = false; |
65 | |
@Transient |
66 | |
private boolean decryptionNeeded = false; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
static EntityExternalIdentifier to(EntityExternalIdentifierBo bo) { |
74 | 0 | if (bo == null) { return null } |
75 | 0 | return EntityExternalIdentifier.Builder.create(bo).build() |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
static EntityExternalIdentifierBo from(EntityExternalIdentifier immutable) { |
84 | 0 | if (immutable == null) {return null} |
85 | |
|
86 | 0 | EntityExternalIdentifierBo bo = new EntityExternalIdentifierBo() |
87 | 0 | bo.id = immutable.id |
88 | 0 | bo.externalId = immutable.externalId |
89 | 0 | bo.entityId = immutable.entityId |
90 | 0 | bo.externalIdentifierTypeCode = immutable.externalIdentifierTypeCode |
91 | 0 | bo.externalIdentifierType = EntityExternalIdentifierTypeBo.from(immutable.externalIdentifierType) |
92 | 0 | bo.versionNumber = immutable.versionNumber |
93 | 0 | bo.objectId = immutable.objectId |
94 | |
|
95 | 0 | return bo; |
96 | |
} |
97 | |
|
98 | |
@Override |
99 | |
protected void prePersist() { |
100 | 0 | super.prePersist(); |
101 | 0 | encryptExternalId(); |
102 | |
} |
103 | |
|
104 | |
@Override |
105 | |
protected void postLoad() { |
106 | 0 | super.postLoad(); |
107 | 0 | decryptExternalId(); |
108 | |
} |
109 | |
|
110 | |
|
111 | |
@Override |
112 | |
protected void preUpdate() { |
113 | 0 | super.preUpdate(); |
114 | 0 | if (!this.decryptionNeeded) { |
115 | 0 | encryptExternalId(); |
116 | |
} |
117 | |
} |
118 | |
|
119 | |
protected void encryptExternalId() { |
120 | 0 | evaluateExternalIdentifierType(); |
121 | 0 | if ( encryptionRequired && StringUtils.isNotEmpty(this.externalId) ) { |
122 | |
try { |
123 | 0 | this.externalId = CoreApiServiceLocator.getEncryptionService().encrypt(this.externalId); |
124 | 0 | this.decryptionNeeded = true; |
125 | |
} |
126 | |
catch ( Exception e ) { |
127 | 0 | LOG.info("Unable to encrypt value : " + e.getMessage() + " or it is already encrypted"); |
128 | |
} |
129 | |
} |
130 | |
} |
131 | |
|
132 | |
protected void decryptExternalId() { |
133 | 0 | evaluateExternalIdentifierType(); |
134 | 0 | if ( encryptionRequired && StringUtils.isNotEmpty(externalId) ) { |
135 | |
try { |
136 | 0 | this.externalId = CoreApiServiceLocator.getEncryptionService().decrypt(this.externalId); |
137 | |
} |
138 | |
catch ( Exception e ) { |
139 | 0 | LOG.info("Unable to decrypt value : " + e.getMessage() + " or it is already decrypted"); |
140 | |
} |
141 | |
} |
142 | |
} |
143 | |
|
144 | |
protected void evaluateExternalIdentifierType() { |
145 | 0 | if ( cachedExtIdType == null ) { |
146 | 0 | Map<String, String> criteria = new HashMap<String, String>(); |
147 | 0 | criteria.put(KimConstants.PrimaryKeyConstants.CODE, externalIdentifierTypeCode); |
148 | 0 | cachedExtIdType = (EntityExternalIdentifierTypeBo) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(EntityExternalIdentifierTypeBo.class, criteria); |
149 | 0 | encryptionRequired = cachedExtIdType!= null && cachedExtIdType.isEncryptionRequired(); |
150 | |
} |
151 | |
} |
152 | |
|
153 | |
protected String decryptedExternalId() { |
154 | 0 | evaluateExternalIdentifierType(); |
155 | 0 | if ( encryptionRequired && StringUtils.isNotEmpty(externalId) ) { |
156 | |
try { |
157 | 0 | return CoreApiServiceLocator.getEncryptionService().decrypt(this.externalId); |
158 | |
} |
159 | |
catch ( Exception e ) { |
160 | 0 | LOG.info("Unable to decrypt value : " + e.getMessage() + " or it is already decrypted"); |
161 | |
} |
162 | |
} |
163 | 0 | return ""; |
164 | |
} |
165 | |
|
166 | |
public void setExternalId(String externalId) { |
167 | 0 | this.externalId = externalId |
168 | 0 | this.decryptionNeeded = false |
169 | |
} |
170 | |
|
171 | |
public void setExternalIdentifierTypeCode(String externalIdentifierTypeCode) { |
172 | 0 | this.externalIdentifierTypeCode = externalIdentifierTypeCode |
173 | 0 | cachedExtIdType = null |
174 | |
} |
175 | |
public void setExternalIdentifierType(EntityExternalIdentifierTypeBo externalIdentifierType) { |
176 | 0 | this.externalIdentifierType = externalIdentifierType |
177 | 0 | cachedExtIdType = null |
178 | |
} |
179 | |
|
180 | |
|
181 | |
@Override |
182 | |
public EntityExternalIdentifierTypeBo getExternalIdentifierType() { |
183 | 0 | return this.externalIdentifierType |
184 | |
} |
185 | |
|
186 | |
@Override |
187 | |
public String getExternalId() { |
188 | 0 | if (this.decryptionNeeded) { |
189 | 0 | return decryptedExternalId(); |
190 | |
} |
191 | 0 | return externalId; |
192 | |
} |
193 | |
} |