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.CascadeType;
19 import javax.persistence.Column;
20 import javax.persistence.Entity;
21 import javax.persistence.GeneratedValue;
22 import javax.persistence.Id;
23 import javax.persistence.JoinColumn;
24 import javax.persistence.ManyToOne;
25 import javax.persistence.PrePersist;
26 import javax.persistence.PreUpdate;
27 import javax.persistence.Table;
28 import javax.persistence.Transient;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.log4j.Logger;
32 import org.kuali.rice.core.api.CoreApiServiceLocator;
33 import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
34 import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierContract;
35 import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierType;
36 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
37 import org.kuali.rice.krad.bo.DataObjectBase;
38 import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
39
40 @Entity
41 @Table(name = "KRIM_ENTITY_EXT_ID_T")
42 public class EntityExternalIdentifierBo extends DataObjectBase implements EntityExternalIdentifierContract {
43 private static final Logger LOG = org.apache.log4j.Logger.getLogger(EntityExternalIdentifierBo.class);
44 private static final long serialVersionUID = 1L;
45
46 @Id
47 @Column(name = "ENTITY_EXT_ID_ID")
48 @PortableSequenceGenerator(name = "KRIM_ENTITY_EXT_ID_ID_S")
49 @GeneratedValue(generator = "KRIM_ENTITY_EXT_ID_ID_S")
50 private String id;
51
52 @Column(name = "ENTITY_ID")
53 private String entityId;
54
55 @Column(name = "EXT_ID_TYP_CD")
56 private String externalIdentifierTypeCode;
57
58 @Column(name = "EXT_ID")
59 private String externalId;
60
61 @ManyToOne(targetEntity = EntityExternalIdentifierTypeBo.class, cascade = { CascadeType.REFRESH })
62 @JoinColumn(name = "EXT_ID_TYP_CD", referencedColumnName = "EXT_ID_TYP_CD", insertable = false, updatable = false)
63 private EntityExternalIdentifierTypeBo externalIdentifierType;
64
65 @Transient
66 private EntityExternalIdentifierType cachedExtIdType = null;
67
68 @Transient
69 private boolean encryptionRequired = false;
70
71 @Transient
72 private boolean decryptionNeeded = false;
73
74 public static EntityExternalIdentifier to(EntityExternalIdentifierBo bo) {
75 if (bo == null) {
76 return null;
77 }
78 return EntityExternalIdentifier.Builder.create(bo).build();
79 }
80
81
82
83
84
85
86
87
88 public static EntityExternalIdentifierBo from(EntityExternalIdentifier immutable) {
89 if (immutable == null) {
90 return null;
91 }
92 EntityExternalIdentifierBo bo = new EntityExternalIdentifierBo();
93 bo.id = immutable.getId();
94 bo.externalId = immutable.getExternalId();
95 bo.entityId = immutable.getEntityId();
96 bo.externalIdentifierTypeCode = immutable.getExternalIdentifierTypeCode();
97 bo.externalIdentifierType = immutable.getExternalIdentifierType() != null ? EntityExternalIdentifierTypeBo.from(immutable.getExternalIdentifierType()) : null;
98 bo.setVersionNumber(immutable.getVersionNumber());
99 bo.setObjectId(immutable.getObjectId());
100 return bo;
101 }
102
103 @Override
104 @PrePersist
105 protected void prePersist() {
106 super.prePersist();
107 encryptExternalId();
108 }
109
110 @Override
111 @PreUpdate
112 protected void preUpdate() {
113 super.preUpdate();
114 if (!this.decryptionNeeded) {
115 encryptExternalId();
116 }
117 }
118
119 protected void encryptExternalId() {
120 evaluateExternalIdentifierType();
121 if (encryptionRequired && StringUtils.isNotEmpty(this.externalId)) {
122 try {
123 if (CoreApiServiceLocator.getEncryptionService().isEnabled()) {
124 this.externalId = CoreApiServiceLocator.getEncryptionService().encrypt(this.externalId);
125 this.decryptionNeeded = true;
126 }
127 } catch (Exception e) {
128 LOG.info("Unable to encrypt value : " + e.getMessage() + " or it is already encrypted");
129 }
130 }
131 }
132
133 protected void decryptExternalId() {
134 evaluateExternalIdentifierType();
135 if (encryptionRequired && StringUtils.isNotEmpty(externalId)) {
136 try {
137 if (CoreApiServiceLocator.getEncryptionService().isEnabled()) {
138 this.externalId = CoreApiServiceLocator.getEncryptionService().decrypt(this.externalId);
139 }
140 } catch (Exception e) {
141 LOG.info("Unable to decrypt value : " + e.getMessage() + " or it is already decrypted");
142 }
143 }
144 }
145
146 protected void evaluateExternalIdentifierType() {
147 if (cachedExtIdType == null) {
148 cachedExtIdType = KimApiServiceLocator.getIdentityService().getExternalIdentifierType(externalIdentifierTypeCode);
149 encryptionRequired = cachedExtIdType != null && cachedExtIdType.isEncryptionRequired();
150 }
151 }
152
153 protected String decryptedExternalId() {
154 evaluateExternalIdentifierType();
155 if (encryptionRequired && StringUtils.isNotEmpty(externalId)) {
156 try {
157 if (CoreApiServiceLocator.getEncryptionService().isEnabled()) {
158 return CoreApiServiceLocator.getEncryptionService().decrypt(this.externalId);
159 }
160 } catch (Exception e) {
161 LOG.info("Unable to decrypt value : " + e.getMessage() + " or it is already decrypted");
162 }
163 }
164 return "";
165 }
166
167 public void setExternalId(String externalId) {
168 this.externalId = externalId;
169 this.decryptionNeeded = false;
170 }
171
172 public void setExternalIdentifierTypeCode(String externalIdentifierTypeCode) {
173 this.externalIdentifierTypeCode = externalIdentifierTypeCode;
174 cachedExtIdType = null;
175 }
176
177 public void setExternalIdentifierType(EntityExternalIdentifierTypeBo externalIdentifierType) {
178 this.externalIdentifierType = externalIdentifierType;
179 cachedExtIdType = null;
180 }
181
182 @Override
183 public EntityExternalIdentifierTypeBo getExternalIdentifierType() {
184 return this.externalIdentifierType;
185 }
186
187 @Override
188 public String getExternalId() {
189 if (this.decryptionNeeded) {
190 return decryptedExternalId();
191 }
192 return externalId;
193 }
194
195 @Override
196 public String getId() {
197 return id;
198 }
199
200 public void setId(String id) {
201 this.id = id;
202 }
203
204 @Override
205 public String getEntityId() {
206 return entityId;
207 }
208
209 public void setEntityId(String entityId) {
210 this.entityId = entityId;
211 }
212
213 @Override
214 public String getExternalIdentifierTypeCode() {
215 return externalIdentifierTypeCode;
216 }
217 }