001    package org.kuali.rice.kim.impl.identity;
002    
003    import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
004    import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
005    import org.kuali.rice.kim.api.identity.entity.EntityDefault;
006    import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
007    import org.kuali.rice.kim.api.identity.name.EntityName;
008    import org.kuali.rice.kim.api.identity.principal.Principal;
009    import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault;
010    import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
011    
012    import javax.persistence.Column;
013    import javax.persistence.Entity;
014    import javax.persistence.Id;
015    import javax.persistence.Table;
016    import javax.persistence.Transient;
017    import java.sql.Timestamp;
018    import java.util.Collections;
019    
020    /**
021     * Used to store a cache of person information to be used if the user's information disappears from KIM.
022     *
023     * @author Kuali Rice Team (rice.collab@kuali.org)
024     */
025    @Entity
026    @Table(name = "KRIM_ENTITY_CACHE_T")
027    public class EntityDefaultInfoCacheBo extends PersistableBusinessObjectBase {
028        private static final long serialVersionUID = 1L;
029        private static final String UNAVAILABLE = "Unavailable";
030        @Transient
031        private Long versionNumber;
032        @Id
033        @Column(name = "PRNCPL_ID")
034        private String principalId;
035        @Column(name = "PRNCPL_NM")
036        private String principalName;
037        @Column(name = "ENTITY_ID")
038        private String entityId;
039        @Column(name = "ENTITY_TYP_CD")
040        private String entityTypeCode;
041        @Column(name = "FIRST_NM")
042        private String firstName = "";
043        @Column(name = "MIDDLE_NM")
044        private String middleName = "";
045        @Column(name = "LAST_NM")
046        private String lastName = "";
047        @Column(name = "PRSN_NM")
048        private String name = "";
049        @Column(name = "CAMPUS_CD")
050        private String campusCode = "";
051        @Column(name = "PRMRY_DEPT_CD")
052        private String primaryDepartmentCode = "";
053        @Column(name = "EMP_ID")
054        private String employeeId = "";
055        @Column(name = "LAST_UPDT_TS")
056        private Timestamp lastUpdateTimestamp;
057    
058        public EntityDefaultInfoCacheBo() {
059        }
060    
061        public EntityDefaultInfoCacheBo(EntityDefault entity) {
062            if (entity != null) {
063                entityId = entity.getEntityId();
064                if (entity.getPrincipals() != null && !entity.getPrincipals().isEmpty()) {
065                    principalId = entity.getPrincipals().get(0).getPrincipalId();
066                    if (entity.getPrincipals().get(0).getPrincipalName() != null) {
067                        principalName = entity.getPrincipals().get(0).getPrincipalName();
068                    } else {
069                        principalName = UNAVAILABLE;
070                    }
071    
072                }
073    
074                if (entity.getEntityTypeContactInfos() != null && !entity.getEntityTypeContactInfos().isEmpty()) {
075                    entityTypeCode = entity.getEntityTypeContactInfos().get(0).getEntityTypeCode();
076                }
077    
078                if (entity.getName() != null) {
079                    firstName = entity.getName().getFirstNameUnmasked();
080                    middleName = entity.getName().getMiddleNameUnmasked();
081                    lastName = entity.getName().getLastNameUnmasked();
082                    name = entity.getName().getCompositeNameUnmasked();
083                }
084    
085                if (entity.getDefaultAffiliation() != null) {
086                    campusCode = entity.getDefaultAffiliation().getCampusCode();
087                }
088    
089                if (entity.getEmployment() != null) {
090                    primaryDepartmentCode = entity.getEmployment().getPrimaryDepartmentCode();
091                    employeeId = entity.getEmployment().getEmployeeId();
092                }
093    
094            }
095    
096        }
097    
098        @SuppressWarnings("unchecked")
099        public EntityDefault convertCacheToEntityDefaultInfo() {
100            EntityDefault.Builder info = EntityDefault.Builder.create(
101                    this.entityId);
102    
103            // identity info
104            info.setActive(this.isActive());
105    
106            // principal info
107            Principal.Builder principalInfo = null;
108            if (this.getPrincipalName() != null) {
109                principalInfo = Principal.Builder.create(
110                        this.getPrincipalName());
111            } else {
112                principalInfo = Principal.Builder.create(UNAVAILABLE);
113            }
114    
115            principalInfo.setEntityId(this.getEntityId());
116            principalInfo.setPrincipalId(this.getPrincipalId());
117            principalInfo.setActive(this.isActive());
118            info.setPrincipals(Collections.singletonList(principalInfo));
119    
120            // name info
121            EntityName.Builder nameInfo = EntityName.Builder.create();
122            nameInfo.setEntityId(this.getEntityId());
123            nameInfo.setFirstName(this.getFirstName());
124            nameInfo.setLastName(this.getLastName());
125            nameInfo.setMiddleName(this.getMiddleName());
126            info.setName(nameInfo);
127    
128            // identity type information
129            EntityTypeContactInfoDefault.Builder entityTypeInfo =
130                    EntityTypeContactInfoDefault.Builder
131                            .create();
132            entityTypeInfo.setEntityTypeCode(this.getEntityTypeCode());
133            info.setEntityTypeContactInfos(Collections.singletonList(entityTypeInfo));
134    
135            // affiliations
136            EntityAffiliation.Builder aff =
137                    EntityAffiliation.Builder.create();
138            aff.setCampusCode(this.getCampusCode());
139            aff.setDefaultValue(true);
140            aff.setEntityId(info.getEntityId());
141            info.setDefaultAffiliation(aff);
142            info.setAffiliations(Collections.singletonList(aff));
143    
144            // employment information
145            EntityEmployment.Builder empInfo =
146                    EntityEmployment.Builder.create();
147            empInfo.setEmployeeId(this.getEmployeeId());
148            empInfo.setPrimary(true);
149            empInfo.setPrimaryDepartmentCode(this.getPrimaryDepartmentCode());
150            info.setEmployment(empInfo);
151    
152            // external identifiers
153            info.setExternalIdentifiers(Collections.singletonList(
154                    EntityExternalIdentifier.Builder
155                            .create()));
156            return info.build();
157    
158        }
159    
160        @Override
161        protected void prePersist() {
162            super.prePersist();
163            lastUpdateTimestamp = new Timestamp(System.currentTimeMillis());
164        }
165    
166        @Override
167        protected void preUpdate() {
168            super.preUpdate();
169            lastUpdateTimestamp = new Timestamp(System.currentTimeMillis());
170        }
171    
172        public boolean isActive() {
173            return false;
174        }
175    
176        @Override
177        public Long getVersionNumber() {
178            return versionNumber;
179        }
180    
181        @Override
182        public void setVersionNumber(Long versionNumber) {
183            this.versionNumber = versionNumber;
184        }
185    
186        public String getPrincipalId() {
187            return principalId;
188        }
189    
190        public void setPrincipalId(String principalId) {
191            this.principalId = principalId;
192        }
193    
194        public String getPrincipalName() {
195            return principalName;
196        }
197    
198        public void setPrincipalName(String principalName) {
199            this.principalName = principalName;
200        }
201    
202        public String getEntityId() {
203            return entityId;
204        }
205    
206        public void setEntityId(String entityId) {
207            this.entityId = entityId;
208        }
209    
210        public String getEntityTypeCode() {
211            return entityTypeCode;
212        }
213    
214        public void setEntityTypeCode(String entityTypeCode) {
215            this.entityTypeCode = entityTypeCode;
216        }
217    
218        public String getFirstName() {
219            return firstName;
220        }
221    
222        public void setFirstName(String firstName) {
223            this.firstName = firstName;
224        }
225    
226        public String getMiddleName() {
227            return middleName;
228        }
229    
230        public void setMiddleName(String middleName) {
231            this.middleName = middleName;
232        }
233    
234        public String getLastName() {
235            return lastName;
236        }
237    
238        public void setLastName(String lastName) {
239            this.lastName = lastName;
240        }
241    
242        public String getName() {
243            return name;
244        }
245    
246        public void setName(String name) {
247            this.name = name;
248        }
249    
250        public String getCampusCode() {
251            return campusCode;
252        }
253    
254        public void setCampusCode(String campusCode) {
255            this.campusCode = campusCode;
256        }
257    
258        public String getPrimaryDepartmentCode() {
259            return primaryDepartmentCode;
260        }
261    
262        public void setPrimaryDepartmentCode(String primaryDepartmentCode) {
263            this.primaryDepartmentCode = primaryDepartmentCode;
264        }
265    
266        public String getEmployeeId() {
267            return employeeId;
268        }
269    
270        public void setEmployeeId(String employeeId) {
271            this.employeeId = employeeId;
272        }
273    
274        public Timestamp getLastUpdateTimestamp() {
275            return lastUpdateTimestamp;
276        }
277    
278        public void setLastUpdateTimestamp(Timestamp lastUpdateTimestamp) {
279            this.lastUpdateTimestamp = lastUpdateTimestamp;
280        }
281    
282    }