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