001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.impl.identity;
017
018import java.sql.Timestamp;
019import java.util.Collections;
020import java.util.UUID;
021
022import javax.persistence.Column;
023import javax.persistence.Entity;
024import javax.persistence.Id;
025import javax.persistence.PrePersist;
026import javax.persistence.PreUpdate;
027import javax.persistence.Table;
028
029import org.apache.commons.lang.StringUtils;
030import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
031import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
032import org.kuali.rice.kim.api.identity.entity.EntityDefault;
033import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
034import org.kuali.rice.kim.api.identity.name.EntityName;
035import org.kuali.rice.kim.api.identity.principal.Principal;
036import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault;
037
038/**
039 * Used to store a cache of person information to be used if the user's information disappears from KIM.
040 *
041 * @author Kuali Rice Team (rice.collab@kuali.org)
042 */
043@Entity
044@Table(name = "KRIM_ENTITY_CACHE_T")
045public class EntityDefaultInfoCacheBo {
046
047    private static final String UNAVAILABLE = "Unavailable";
048
049    @Id
050    @Column(name = "PRNCPL_ID")
051    private String principalId;
052
053    @Column(name = "PRNCPL_NM")
054    private String principalName;
055
056    @Column(name = "ENTITY_ID")
057    private String entityId;
058
059    @Column(name = "ENTITY_TYP_CD")
060    private String entityTypeCode;
061
062    @Column(name = "FIRST_NM")
063    private String firstName = "";
064
065    @Column(name = "MIDDLE_NM")
066    private String middleName = "";
067
068    @Column(name = "LAST_NM")
069    private String lastName = "";
070
071    @Column(name = "PRSN_NM")
072    private String name = "";
073
074    @Column(name = "CAMPUS_CD")
075    private String campusCode = "";
076
077    @Column(name = "PRMRY_DEPT_CD")
078    private String primaryDepartmentCode = "";
079
080    @Column(name = "EMP_ID")
081    private String employeeId = "";
082
083    @Column(name = "LAST_UPDT_TS")
084    private Timestamp lastUpdateTimestamp;
085
086    @Column(name="OBJ_ID", length=36, unique=true, nullable = false)
087    protected String objectId;
088
089    public EntityDefaultInfoCacheBo() {
090    }
091
092    public EntityDefaultInfoCacheBo(EntityDefault entity) {
093        if (entity != null) {
094            entityId = entity.getEntityId();
095            if (entity.getPrincipals() != null && !entity.getPrincipals().isEmpty()) {
096                principalId = entity.getPrincipals().get(0).getPrincipalId();
097                if (entity.getPrincipals().get(0).getPrincipalName() != null) {
098                    principalName = entity.getPrincipals().get(0).getPrincipalName();
099                } else {
100                    principalName = UNAVAILABLE;
101                }
102            }
103            if (entity.getEntityTypeContactInfos() != null && !entity.getEntityTypeContactInfos().isEmpty()) {
104                entityTypeCode = entity.getEntityTypeContactInfos().get(0).getEntityTypeCode();
105            }
106            if (entity.getName() != null) {
107                firstName = entity.getName().getFirstNameUnmasked();
108                middleName = entity.getName().getMiddleNameUnmasked();
109                lastName = entity.getName().getLastNameUnmasked();
110                name = entity.getName().getCompositeNameUnmasked();
111            }
112            if (entity.getDefaultAffiliation() != null) {
113                campusCode = entity.getDefaultAffiliation().getCampusCode();
114            }
115            if (entity.getEmployment() != null) {
116                primaryDepartmentCode = entity.getEmployment().getPrimaryDepartmentCode();
117                employeeId = entity.getEmployment().getEmployeeId();
118            }
119        }
120    }
121
122    public EntityDefault convertCacheToEntityDefaultInfo() {
123        EntityDefault.Builder info = EntityDefault.Builder.create(this.entityId);
124        // identity info
125        info.setActive(this.isActive());
126        // principal info
127        Principal.Builder principalInfo = null;
128        if (this.getPrincipalName() != null) {
129            principalInfo = Principal.Builder.create(this.getPrincipalName());
130        } else {
131            principalInfo = Principal.Builder.create(UNAVAILABLE);
132        }
133        principalInfo.setEntityId(this.getEntityId());
134        principalInfo.setPrincipalId(this.getPrincipalId());
135        principalInfo.setActive(this.isActive());
136        info.setPrincipals(Collections.singletonList(principalInfo));
137        // name info
138        EntityName.Builder nameInfo = EntityName.Builder.create();
139        nameInfo.setEntityId(this.getEntityId());
140        nameInfo.setFirstName(this.getFirstName());
141        nameInfo.setLastName(this.getLastName());
142        nameInfo.setMiddleName(this.getMiddleName());
143        info.setName(nameInfo);
144        // identity type information
145        EntityTypeContactInfoDefault.Builder entityTypeInfo = EntityTypeContactInfoDefault.Builder.create();
146        entityTypeInfo.setEntityTypeCode(this.getEntityTypeCode());
147        info.setEntityTypeContactInfos(Collections.singletonList(entityTypeInfo));
148        // affiliations
149        EntityAffiliation.Builder aff = EntityAffiliation.Builder.create();
150        aff.setCampusCode(this.getCampusCode());
151        aff.setDefaultValue(true);
152        aff.setEntityId(info.getEntityId());
153        info.setDefaultAffiliation(aff);
154        info.setAffiliations(Collections.singletonList(aff));
155        // employment information
156        EntityEmployment.Builder empInfo = EntityEmployment.Builder.create();
157        empInfo.setEmployeeId(this.getEmployeeId());
158        empInfo.setPrimary(true);
159        empInfo.setPrimaryDepartmentCode(this.getPrimaryDepartmentCode());
160        info.setEmployment(empInfo);
161        // external identifiers
162        info.setExternalIdentifiers(Collections.singletonList(EntityExternalIdentifier.Builder.create()));
163        return info.build();
164    }
165
166    @PrePersist
167    protected void prePersist() {
168        if (StringUtils.isEmpty(getObjectId())) {
169            setObjectId(UUID.randomUUID().toString());
170        }
171
172        lastUpdateTimestamp = new Timestamp(System.currentTimeMillis());
173    }
174    @PreUpdate
175    protected void preUpdate() {
176        if (StringUtils.isEmpty(getObjectId())) {
177            setObjectId(UUID.randomUUID().toString());
178        }
179
180        lastUpdateTimestamp = new Timestamp(System.currentTimeMillis());
181    }
182
183    public boolean isActive() {
184        return false;
185    }
186
187    public String getPrincipalId() {
188        return principalId;
189    }
190
191    public void setPrincipalId(String principalId) {
192        this.principalId = principalId;
193    }
194
195    public String getPrincipalName() {
196        return principalName;
197    }
198
199    public void setPrincipalName(String principalName) {
200        this.principalName = principalName;
201    }
202
203    public String getEntityId() {
204        return entityId;
205    }
206
207    public void setEntityId(String entityId) {
208        this.entityId = entityId;
209    }
210
211    public String getEntityTypeCode() {
212        return entityTypeCode;
213    }
214
215    public void setEntityTypeCode(String entityTypeCode) {
216        this.entityTypeCode = entityTypeCode;
217    }
218
219    public String getFirstName() {
220        return firstName;
221    }
222
223    public void setFirstName(String firstName) {
224        this.firstName = firstName;
225    }
226
227    public String getMiddleName() {
228        return middleName;
229    }
230
231    public void setMiddleName(String middleName) {
232        this.middleName = middleName;
233    }
234
235    public String getLastName() {
236        return lastName;
237    }
238
239    public void setLastName(String lastName) {
240        this.lastName = lastName;
241    }
242
243    public String getName() {
244        return name;
245    }
246
247    public void setName(String name) {
248        this.name = name;
249    }
250
251    public String getCampusCode() {
252        return campusCode;
253    }
254
255    public void setCampusCode(String campusCode) {
256        this.campusCode = campusCode;
257    }
258
259    public String getPrimaryDepartmentCode() {
260        return primaryDepartmentCode;
261    }
262
263    public void setPrimaryDepartmentCode(String primaryDepartmentCode) {
264        this.primaryDepartmentCode = primaryDepartmentCode;
265    }
266
267    public String getEmployeeId() {
268        return employeeId;
269    }
270
271    public void setEmployeeId(String employeeId) {
272        this.employeeId = employeeId;
273    }
274
275    public Timestamp getLastUpdateTimestamp() {
276        return lastUpdateTimestamp;
277    }
278
279    public void setLastUpdateTimestamp(Timestamp lastUpdateTimestamp) {
280        this.lastUpdateTimestamp = lastUpdateTimestamp;
281    }
282
283    /**
284     * @return the objectId
285     */
286    public String getObjectId() {
287        return this.objectId;
288    }
289
290    /**
291     * @param objectId the objectId to set
292     */
293    public void setObjectId(String objectId) {
294        this.objectId = objectId;
295    }
296}