View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.core.principal;
17  
18  import java.util.Date;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.joda.time.LocalDate;
22  import org.kuali.kpme.core.api.principal.PrincipalHRAttributes;
23  import org.kuali.kpme.core.api.principal.PrincipalHRAttributesContract;
24  import org.kuali.kpme.core.bo.HrBusinessObject;
25  import org.kuali.kpme.core.calendar.CalendarBo;
26  import org.kuali.kpme.core.leaveplan.LeavePlanBo;
27  import org.kuali.kpme.core.service.HrServiceLocator;
28  import org.kuali.kpme.core.util.HrConstants;
29  import org.kuali.rice.core.api.mo.ModelObjectUtils;
30  import org.kuali.rice.kim.api.identity.IdentityService;
31  import org.kuali.rice.kim.api.identity.Person;
32  import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
33  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
34  
35  import com.google.common.collect.ImmutableList;
36  import com.google.common.collect.ImmutableMap;
37  
38  public class PrincipalHRAttributesBo extends HrBusinessObject implements PrincipalHRAttributesContract {
39  
40  	private static final String PRINCIPAL_ID = "principalId";
41  	
42  	private static final long serialVersionUID = 6843318899816055301L;
43  	//KPME-2273/1965 Primary Business Keys List.	
44  	public static final ImmutableList<String> BUSINESS_KEYS = new ImmutableList.Builder<String>()
45              .add(PRINCIPAL_ID)
46              .build();
47  	public static final String CACHE_NAME = HrConstants.CacheNamespace.NAMESPACE_PREFIX + "PrincipalHRAttributes";
48  
49      /*
50  	 * convert bo to immutable
51  	 *
52       * Can be used with ModelObjectUtils:
53       *
54       * org.kuali.rice.core.api.mo.ModelObjectUtils.transform(listOfPrincipalHRAttributesBo, PrincipalHRAttributesBo.toImmutable);
55       */
56      public static final ModelObjectUtils.Transformer<PrincipalHRAttributesBo, PrincipalHRAttributes> toImmutable =
57              new ModelObjectUtils.Transformer<PrincipalHRAttributesBo, PrincipalHRAttributes>() {
58                  public PrincipalHRAttributes transform(PrincipalHRAttributesBo input) {
59                      return PrincipalHRAttributesBo.to(input);
60                  };
61              };
62  
63      /*
64       * convert immutable to bo
65       *
66       * Can be used with ModelObjectUtils:
67       *
68       * org.kuali.rice.core.api.mo.ModelObjectUtils.transform(listOfPrincipalHRAttributes, PrincipalHRAttributesBo.toBo);
69       */
70      public static final ModelObjectUtils.Transformer<PrincipalHRAttributes, PrincipalHRAttributesBo> toBo =
71              new ModelObjectUtils.Transformer<PrincipalHRAttributes, PrincipalHRAttributesBo>() {
72                  public PrincipalHRAttributesBo transform(PrincipalHRAttributes input) {
73                      return PrincipalHRAttributesBo.from(input);
74                  };
75              };
76  
77  	private String hrPrincipalAttributeId;
78  	private String principalId;
79  	private String leaveCalendar;
80  	private String payCalendar;
81  	private String leavePlan;
82  	private Date serviceDate;
83  	private boolean fmlaEligible;
84  	private boolean workersCompEligible;
85  	private String timezone;
86  	
87  	private transient CalendarBo calendar;
88  	private transient CalendarBo leaveCalObj;
89  	private transient Person person;
90  	private transient LeavePlanBo leavePlanObj;
91      private transient IdentityService identityService;
92  
93  	
94  	@Override
95  	public ImmutableMap<String, Object> getBusinessKeyValuesMap() {
96  		return new ImmutableMap.Builder<String, Object>()
97  				.put(PRINCIPAL_ID, this.getPrincipalId())
98  				.build();
99  	}
100 
101 	public String getPrincipalId() {
102 		return principalId;
103 	}
104 
105 	public void setPrincipalId(String principalId) {
106 		this.principalId = principalId;
107 	}
108 
109 	public String getName() {
110 		if (person == null) {
111             EntityNamePrincipalName name = getIdentityService().getDefaultNamesForPrincipalId(this.principalId);
112             if (name != null) {
113                 return name.getDefaultName().getCompositeName();
114             }
115             return StringUtils.EMPTY;
116         }
117 	    return (person != null) ? person.getName() : "";
118 	}
119 
120 	public String getPayCalendar() {
121 		return payCalendar;
122 	}
123 
124 	public void setPayCalendar(String payCalendar) {
125 		this.payCalendar = payCalendar;
126 	}
127 
128 	public String getLeavePlan() {
129 		return leavePlan;
130 	}
131 
132 	public void setLeavePlan(String leavePlan) {
133 		this.leavePlan = leavePlan;
134 	}
135 
136 	public Date getServiceDate() {
137 		return serviceDate;
138 	}
139 
140 	public void setServiceDate(Date serviceDate) {
141 		this.serviceDate = serviceDate;
142 	}
143 	
144 	public LocalDate getServiceLocalDate() {
145 		return serviceDate != null ? LocalDate.fromDateFields(serviceDate) : null;
146 	}
147 	
148 	public void setServiceLocalDate(LocalDate serviceLocalDate) {
149 		this.serviceDate = serviceLocalDate != null ? serviceLocalDate.toDate() : null;
150 	}
151 
152 	public boolean isFmlaEligible() {
153 		return fmlaEligible;
154 	}
155 
156 	public void setFmlaEligible(boolean fmlaEligible) {
157 		this.fmlaEligible = fmlaEligible;
158 	}
159 
160 	public boolean isWorkersCompEligible() {
161 		return workersCompEligible;
162 	}
163 
164 	public void setWorkersCompEligible(boolean workersCompEligible) {
165 		this.workersCompEligible = workersCompEligible;
166 	}
167 
168 	public String getTimezone() {
169 		return timezone;
170 	}
171 
172 	public void setTimezone(String timezone) {
173 		this.timezone = timezone;
174 	}
175 
176 	public CalendarBo getCalendar() {
177 		return calendar;
178 	}
179 
180 	public void setCalendar(CalendarBo calendar) {
181 		this.calendar = calendar;
182 	}
183 
184 	public Person getPerson() {
185 		return person;
186 	}
187 
188 	public void setPerson(Person person) {
189 		this.person = person;
190 	}
191 
192 	public LeavePlanBo getLeavePlanObj() {
193         if (leavePlanObj == null
194                 && StringUtils.isNotEmpty(leavePlan)) {
195             leavePlanObj = LeavePlanBo.from(HrServiceLocator.getLeavePlanService().getLeavePlan(leavePlan,getEffectiveLocalDate()));
196         }
197 		return leavePlanObj;
198 	}
199 
200 	public void setLeavePlanObj(LeavePlanBo leavePlanObj) {
201 		this.leavePlanObj = leavePlanObj;
202 	}
203 
204 	@Override
205 	protected String getUniqueKey() {
206 		return principalId + "_" + payCalendar == null ? "" : payCalendar + "_"
207 				+ leaveCalendar == null ? "" : leaveCalendar;
208 	}
209 
210 	public String getLeaveCalendar() {
211 		return leaveCalendar;
212 	}
213 
214 	public void setLeaveCalendar(String leaveCalendar) {
215 		this.leaveCalendar = leaveCalendar;
216 	}
217 
218 	@Override
219 	public String getId() {
220 		return this.getHrPrincipalAttributeId();
221 	}
222 	@Override
223 	public void setId(String id) {
224 		setHrPrincipalAttributeId(id);
225 	}
226 
227 	public CalendarBo getLeaveCalObj() {
228 		return leaveCalObj;
229 	}
230 
231 	public void setLeaveCalObj(CalendarBo leaveCalObj) {
232 		this.leaveCalObj = leaveCalObj;
233 	}
234 
235 	public String getHrPrincipalAttributeId() {
236 		return hrPrincipalAttributeId;
237 	}
238 
239 	public void setHrPrincipalAttributeId(String hrPrincipalAttributeId) {
240 		this.hrPrincipalAttributeId = hrPrincipalAttributeId;
241 	}
242 
243     public IdentityService getIdentityService() {
244         if (identityService == null) {
245             identityService = KimApiServiceLocator.getIdentityService();
246         }
247         return identityService;
248     }
249 
250     public void setIdentityService(IdentityService identityService) {
251         this.identityService = identityService;
252     }
253 
254     public static PrincipalHRAttributesBo from(PrincipalHRAttributes im) {
255         if (im == null) {
256             return null;
257         }
258         PrincipalHRAttributesBo phra = new PrincipalHRAttributesBo();
259 
260         phra.setHrPrincipalAttributeId(im.getHrPrincipalAttributeId());
261         phra.setPrincipalId(im.getPrincipalId());
262         phra.setLeaveCalendar(im.getLeaveCalendar());
263         phra.setPayCalendar(im.getPayCalendar());
264         phra.setLeavePlan(im.getLeavePlan());
265         phra.setServiceDate(im.getServiceLocalDate() == null ? null : im.getServiceLocalDate().toDate());
266         phra.setFmlaEligible(im.isFmlaEligible());
267         phra.setWorkersCompEligible(im.isWorkersCompEligible());
268         phra.setTimezone(im.getTimezone());
269         phra.setCalendar(CalendarBo.from(im.getCalendar()));
270         phra.setLeaveCalObj(CalendarBo.from(im.getLeaveCalObj()));
271         phra.setLeavePlanObj(LeavePlanBo.from(im.getLeavePlanObj()));
272 
273         // finally copy over the common fields into phra from im
274         copyCommonFields(phra, im);
275 
276         return phra;
277     }
278 
279     public static PrincipalHRAttributes to(PrincipalHRAttributesBo bo) {
280         if (bo == null) {
281             return null;
282         }
283 
284         return PrincipalHRAttributes.Builder.create(bo).build();
285     }
286 
287 }