|  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 org.apache.commons.lang.StringUtils;  | 
  |  19 |     | 
   import org.hibernate.annotations.GenericGenerator;  | 
  |  20 |     | 
   import org.hibernate.annotations.Parameter;  | 
  |  21 |     | 
   import org.kuali.rice.core.api.services.CoreApiServiceLocator;  | 
  |  22 |     | 
   import org.kuali.rice.kim.bo.entity.KimEntityExternalIdentifier;  | 
  |  23 |     | 
   import org.kuali.rice.kim.bo.reference.ExternalIdentifierType;  | 
  |  24 |     | 
   import org.kuali.rice.kim.bo.reference.impl.ExternalIdentifierTypeImpl;  | 
  |  25 |     | 
   import org.kuali.rice.kim.util.KimConstants;  | 
  |  26 |     | 
   import org.kuali.rice.kns.service.KNSServiceLocator;  | 
  |  27 |     | 
     | 
  |  28 |     | 
   import javax.persistence.*;  | 
  |  29 |     | 
   import java.util.HashMap;  | 
  |  30 |     | 
   import java.util.Map;  | 
  |  31 |     | 
     | 
  |  32 |     | 
     | 
  |  33 |     | 
     | 
  |  34 |     | 
     | 
  |  35 |     | 
   @Entity  | 
  |  36 |     | 
   @Table(name = "KRIM_ENTITY_EXT_ID_T")  | 
  |  37 |    0 |    public class KimEntityExternalIdentifierImpl extends KimEntityDataBase implements KimEntityExternalIdentifier { | 
  |  38 |    0 |        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimEntityExternalIdentifierImpl.class);  | 
  |  39 |     | 
     | 
  |  40 |     | 
           private static final long serialVersionUID = 1L;  | 
  |  41 |     | 
     | 
  |  42 |     | 
           @Id  | 
  |  43 |     | 
           @GeneratedValue(generator="KRIM_ENTITY_EXT_ID_ID_S")  | 
  |  44 |     | 
           @GenericGenerator(name="KRIM_ENTITY_EXT_ID_ID_S",strategy="org.kuali.rice.core.jpa.spring.RiceNumericStringSequenceStyleGenerator",parameters={ | 
  |  45 |     | 
                           @Parameter(name="sequence_name",value="KRIM_ENTITY_EXT_ID_ID_S"),  | 
  |  46 |     | 
                           @Parameter(name="value_column",value="id")  | 
  |  47 |     | 
                   })  | 
  |  48 |     | 
           @Column(name = "ENTITY_EXT_ID_ID")  | 
  |  49 |     | 
           protected String entityExternalIdentifierId;  | 
  |  50 |     | 
     | 
  |  51 |     | 
           @Column(name = "ENTITY_ID")  | 
  |  52 |     | 
           protected String entityId;  | 
  |  53 |     | 
             | 
  |  54 |     | 
           @Column(name = "EXT_ID_TYP_CD")  | 
  |  55 |     | 
           protected String externalIdentifierTypeCode;  | 
  |  56 |     | 
     | 
  |  57 |     | 
           @Column(name = "EXT_ID")  | 
  |  58 |     | 
           protected String externalId;  | 
  |  59 |     | 
             | 
  |  60 |     | 
           @ManyToOne(targetEntity=ExternalIdentifierTypeImpl.class, fetch = FetchType.EAGER, cascade = {}) | 
  |  61 |     | 
           @JoinColumn(name = "EXT_ID_TYP_CD", insertable = false, updatable = false)  | 
  |  62 |     | 
           protected ExternalIdentifierType externalIdentifierType;  | 
  |  63 |     | 
     | 
  |  64 |    0 |            @Transient protected ExternalIdentifierType cachedExtIdType = null;          | 
  |  65 |    0 |            @Transient protected boolean encryptionRequired = false;  | 
  |  66 |    0 |            @Transient  | 
  |  67 |     | 
           private boolean decryptionNeeded = false;  | 
  |  68 |     | 
     | 
  |  69 |     | 
     | 
  |  70 |     | 
             | 
  |  71 |     | 
     | 
  |  72 |     | 
     | 
  |  73 |     | 
           public String getEntityExternalIdentifierId() { | 
  |  74 |    0 |                    return entityExternalIdentifierId;  | 
  |  75 |     | 
           }  | 
  |  76 |     | 
     | 
  |  77 |     | 
             | 
  |  78 |     | 
     | 
  |  79 |     | 
     | 
  |  80 |     | 
           public String getExternalId() { | 
  |  81 |    0 |                    if (this.decryptionNeeded) { | 
  |  82 |    0 |                            return decryptedExternalId();  | 
  |  83 |     | 
                   }  | 
  |  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 |                    this.decryptionNeeded = false;  | 
  |  100 |    0 |            }  | 
  |  101 |     | 
     | 
  |  102 |     | 
             | 
  |  103 |     | 
     | 
  |  104 |     | 
     | 
  |  105 |     | 
           public void setExternalIdentifierTypeCode(String externalIdentifierTypeCode) { | 
  |  106 |    0 |                    this.externalIdentifierTypeCode = externalIdentifierTypeCode;  | 
  |  107 |    0 |                    cachedExtIdType = null;                  | 
  |  108 |    0 |            }  | 
  |  109 |     | 
     | 
  |  110 |     | 
           public void setEntityExternalIdentifierId(String entityExternalIdentifierId) { | 
  |  111 |    0 |                    this.entityExternalIdentifierId = entityExternalIdentifierId;  | 
  |  112 |    0 |            }  | 
  |  113 |     | 
     | 
  |  114 |     | 
           public String getEntityId() { | 
  |  115 |    0 |                    return this.entityId;  | 
  |  116 |     | 
           }  | 
  |  117 |     | 
     | 
  |  118 |     | 
           public void setEntityId(String entityId) { | 
  |  119 |    0 |                    this.entityId = entityId;  | 
  |  120 |    0 |            }  | 
  |  121 |     | 
     | 
  |  122 |     | 
           public ExternalIdentifierType getExternalIdentifierType() { | 
  |  123 |    0 |                    return this.externalIdentifierType;  | 
  |  124 |     | 
           }  | 
  |  125 |     | 
     | 
  |  126 |     | 
           public void setExternalIdentifierType(ExternalIdentifierType externalIdentifierType) { | 
  |  127 |    0 |                    this.externalIdentifierType = externalIdentifierType;  | 
  |  128 |    0 |                    cachedExtIdType = null;          | 
  |  129 |    0 |            }  | 
  |  130 |     | 
             | 
  |  131 |     | 
       @Override  | 
  |  132 |     | 
           protected void prePersist() { | 
  |  133 |    0 |                    super.prePersist();  | 
  |  134 |    0 |                    encryptExternalId();  | 
  |  135 |    0 |            }  | 
  |  136 |     | 
                     | 
  |  137 |     | 
           @Override  | 
  |  138 |     | 
           protected void postLoad() { | 
  |  139 |    0 |            super.postLoad();  | 
  |  140 |    0 |            decryptExternalId();  | 
  |  141 |    0 |            }  | 
  |  142 |     | 
             | 
  |  143 |     | 
     | 
  |  144 |     | 
           @Override  | 
  |  145 |     | 
           protected void preUpdate() { | 
  |  146 |    0 |                    super.preUpdate();  | 
  |  147 |    0 |                    if (!this.decryptionNeeded) { | 
  |  148 |    0 |                            encryptExternalId();  | 
  |  149 |     | 
                   }  | 
  |  150 |    0 |            }  | 
  |  151 |     | 
                     | 
  |  152 |     | 
           protected void evaluateExternalIdentifierType() { | 
  |  153 |    0 |                    if ( cachedExtIdType == null ) { | 
  |  154 |    0 |                            Map<String, String> criteria = new HashMap<String, String>();  | 
  |  155 |    0 |                        criteria.put(KimConstants.PrimaryKeyConstants.KIM_TYPE_CODE, externalIdentifierTypeCode);  | 
  |  156 |    0 |                        cachedExtIdType = (ExternalIdentifierType) KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(ExternalIdentifierTypeImpl.class, criteria);  | 
  |  157 |    0 |                        encryptionRequired = cachedExtIdType!= null && cachedExtIdType.isEncryptionRequired();   | 
  |  158 |     | 
                   }  | 
  |  159 |    0 |            }  | 
  |  160 |     | 
             | 
  |  161 |     | 
           protected void encryptExternalId() { | 
  |  162 |    0 |                    evaluateExternalIdentifierType();  | 
  |  163 |    0 |                    if ( encryptionRequired && StringUtils.isNotEmpty(this.externalId) ) { | 
  |  164 |     | 
                           try { | 
  |  165 |    0 |                                    this.externalId = CoreApiServiceLocator.getEncryptionService().encrypt(this.externalId);  | 
  |  166 |    0 |                                    this.decryptionNeeded = true;  | 
  |  167 |     | 
                           }  | 
  |  168 |    0 |                            catch ( Exception e ) { | 
  |  169 |    0 |                                    LOG.info("Unable to encrypt value : " + e.getMessage() + " or it is already encrypted"); | 
  |  170 |    0 |                            }                                  | 
  |  171 |     | 
                   }  | 
  |  172 |    0 |            }  | 
  |  173 |     | 
             | 
  |  174 |     | 
           protected void decryptExternalId() { | 
  |  175 |    0 |                    evaluateExternalIdentifierType();  | 
  |  176 |    0 |                    if ( encryptionRequired && StringUtils.isNotEmpty(externalId) ) { | 
  |  177 |     | 
                           try { | 
  |  178 |    0 |                                    this.externalId = CoreApiServiceLocator.getEncryptionService().decrypt(this.externalId);  | 
  |  179 |     | 
                           }  | 
  |  180 |    0 |                            catch ( Exception e ) { | 
  |  181 |    0 |                                    LOG.info("Unable to decrypt value : " + e.getMessage() + " or it is already decrypted"); | 
  |  182 |    0 |                    }  | 
  |  183 |     | 
                   }  | 
  |  184 |    0 |        }          | 
  |  185 |     | 
             | 
  |  186 |     | 
           protected String decryptedExternalId() { | 
  |  187 |    0 |                    evaluateExternalIdentifierType();  | 
  |  188 |    0 |                    if ( encryptionRequired && StringUtils.isNotEmpty(externalId) ) { | 
  |  189 |     | 
                           try { | 
  |  190 |    0 |                                    return CoreApiServiceLocator.getEncryptionService().decrypt(this.externalId);  | 
  |  191 |     | 
                           }  | 
  |  192 |    0 |                            catch ( Exception e ) { | 
  |  193 |    0 |                                    LOG.info("Unable to decrypt value : " + e.getMessage() + " or it is already decrypted"); | 
  |  194 |     | 
                   }  | 
  |  195 |     | 
                   }  | 
  |  196 |    0 |                    return "";  | 
  |  197 |     | 
       }          | 
  |  198 |     | 
   }  |