View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.eclipse.persistence.annotations.JoinFetch;
33  import org.eclipse.persistence.annotations.JoinFetchType;
34  import org.kuali.rice.core.api.CoreApiServiceLocator;
35  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
36  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierContract;
37  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierType;
38  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
39  import org.kuali.rice.krad.bo.DataObjectBase;
40  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
41  
42  @Entity
43  @Table(name = "KRIM_ENTITY_EXT_ID_T")
44  public class EntityExternalIdentifierBo extends DataObjectBase implements EntityExternalIdentifierContract {
45      private static final Logger LOG = org.apache.log4j.Logger.getLogger(EntityExternalIdentifierBo.class);
46      private static final long serialVersionUID = 1L;
47  
48      @Id
49      @Column(name = "ENTITY_EXT_ID_ID")
50      @PortableSequenceGenerator(name = "KRIM_ENTITY_EXT_ID_ID_S")
51      @GeneratedValue(generator = "KRIM_ENTITY_EXT_ID_ID_S")
52      private String id;
53  
54      @Column(name = "ENTITY_ID")
55      private String entityId;
56  
57      @Column(name = "EXT_ID_TYP_CD")
58      private String externalIdentifierTypeCode;
59  
60      @Column(name = "EXT_ID")
61      private String externalId;
62  
63      @JoinFetch(value= JoinFetchType.OUTER)
64      @ManyToOne(targetEntity = EntityExternalIdentifierTypeBo.class, cascade = { CascadeType.REFRESH })
65      @JoinColumn(name = "EXT_ID_TYP_CD", referencedColumnName = "EXT_ID_TYP_CD", insertable = false, updatable = false)
66      private EntityExternalIdentifierTypeBo externalIdentifierType;
67  
68      @Transient
69      private EntityExternalIdentifierType cachedExtIdType = null;
70  
71      @Transient
72      private boolean encryptionRequired = false;
73  
74      @Transient
75      private boolean decryptionNeeded = false;
76  
77      public static EntityExternalIdentifier to(EntityExternalIdentifierBo bo) {
78          if (bo == null) {
79              return null;
80          }
81          return EntityExternalIdentifier.Builder.create(bo).build();
82      }
83  
84      /**
85       * Creates a EntityExternalIdentifierBo business object from an immutable representation of a
86       * EntityExternalIdentifier.
87       *
88       * @param immutable immutable EntityExternalIdentifier
89       * @return a EntityExternalIdentifierBo
90       */
91      public static EntityExternalIdentifierBo from(EntityExternalIdentifier immutable) {
92          if (immutable == null) {
93              return null;
94          }
95          EntityExternalIdentifierBo bo = new EntityExternalIdentifierBo();
96          bo.id = immutable.getId();
97          bo.externalId = immutable.getExternalId();
98          bo.entityId = immutable.getEntityId();
99          bo.externalIdentifierTypeCode = immutable.getExternalIdentifierTypeCode();
100         bo.externalIdentifierType = immutable.getExternalIdentifierType() != null ? EntityExternalIdentifierTypeBo.from(immutable.getExternalIdentifierType()) : null;
101         bo.setVersionNumber(immutable.getVersionNumber());
102         bo.setObjectId(immutable.getObjectId());
103         return bo;
104     }
105 
106     @Override
107     @PrePersist
108     protected void prePersist() {
109         super.prePersist();
110         encryptExternalId();
111     }
112 
113     @Override
114     @PreUpdate
115     protected void preUpdate() {
116         super.preUpdate();
117         if (!this.decryptionNeeded) {
118             encryptExternalId();
119         }
120     }
121 
122     protected void encryptExternalId() {
123         evaluateExternalIdentifierType();
124         if (encryptionRequired && StringUtils.isNotEmpty(this.externalId)) {
125             try {
126                 if (CoreApiServiceLocator.getEncryptionService().isEnabled()) {
127                     this.externalId = CoreApiServiceLocator.getEncryptionService().encrypt(this.externalId);
128                     this.decryptionNeeded = true;
129                 }
130             } catch (Exception e) {
131                 LOG.info("Unable to encrypt value : " + e.getMessage() + " or it is already encrypted");
132             }
133         }
134     }
135 
136     protected void decryptExternalId() {
137         evaluateExternalIdentifierType();
138         if (encryptionRequired && StringUtils.isNotEmpty(externalId)) {
139             try {
140                 if (CoreApiServiceLocator.getEncryptionService().isEnabled()) {
141                     this.externalId = CoreApiServiceLocator.getEncryptionService().decrypt(this.externalId);
142                 }
143             } catch (Exception e) {
144                 LOG.info("Unable to decrypt value : " + e.getMessage() + " or it is already decrypted");
145             }
146         }
147     }
148 
149     protected void evaluateExternalIdentifierType() {
150         if (cachedExtIdType == null) {
151             cachedExtIdType = KimApiServiceLocator.getIdentityService().getExternalIdentifierType(externalIdentifierTypeCode);
152             encryptionRequired = cachedExtIdType != null && cachedExtIdType.isEncryptionRequired();
153         }
154     }
155 
156     protected String decryptedExternalId() {
157         evaluateExternalIdentifierType();
158         if (encryptionRequired && StringUtils.isNotEmpty(externalId)) {
159             try {
160                 if (CoreApiServiceLocator.getEncryptionService().isEnabled()) {
161                     return CoreApiServiceLocator.getEncryptionService().decrypt(this.externalId);
162                 }
163             } catch (Exception e) {
164                 LOG.info("Unable to decrypt value : " + e.getMessage() + " or it is already decrypted");
165             }
166         }
167         return "";
168     }
169 
170     public void setExternalId(String externalId) {
171         this.externalId = externalId;
172         this.decryptionNeeded = false;
173     }
174 
175     public void setExternalIdentifierTypeCode(String externalIdentifierTypeCode) {
176         this.externalIdentifierTypeCode = externalIdentifierTypeCode;
177         cachedExtIdType = null;
178     }
179 
180     public void setExternalIdentifierType(EntityExternalIdentifierTypeBo externalIdentifierType) {
181         this.externalIdentifierType = externalIdentifierType;
182         cachedExtIdType = null;
183     }
184 
185     @Override
186     public EntityExternalIdentifierTypeBo getExternalIdentifierType() {
187         return this.externalIdentifierType;
188     }
189 
190     @Override
191     public String getExternalId() {
192         if (this.decryptionNeeded) {
193             return decryptedExternalId();
194         }
195         return externalId;
196     }
197 
198     @Override
199     public String getId() {
200         return id;
201     }
202 
203     public void setId(String id) {
204         this.id = id;
205     }
206 
207     @Override
208     public String getEntityId() {
209         return entityId;
210     }
211 
212     public void setEntityId(String entityId) {
213         this.entityId = entityId;
214     }
215 
216     @Override
217     public String getExternalIdentifierTypeCode() {
218         return externalIdentifierTypeCode;
219     }
220 }