001    /**
002     * Copyright 2004-2013 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     */
016    package org.kuali.hr.time.assignment;
017    
018    import java.sql.Date;
019    import java.sql.Timestamp;
020    import java.util.LinkedList;
021    import java.util.List;
022    
023    import org.apache.commons.lang.builder.EqualsBuilder;
024    import org.apache.commons.lang.builder.HashCodeBuilder;
025    import org.kuali.hr.core.KPMEConstants;
026    import org.kuali.hr.job.Job;
027    import org.kuali.hr.time.HrBusinessObject;
028    import org.kuali.hr.time.collection.rule.TimeCollectionRule;
029    import org.kuali.hr.time.dept.lunch.DeptLunchRule;
030    import org.kuali.hr.time.service.base.TkServiceLocator;
031    import org.kuali.hr.time.task.Task;
032    import org.kuali.hr.time.util.TKUtils;
033    import org.kuali.hr.time.workarea.WorkArea;
034    import org.kuali.rice.kim.api.identity.Person;
035    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
036    
037    public class Assignment extends HrBusinessObject {
038        public static final String CACHE_NAME = KPMEConstants.APPLICATION_NAMESPACE_CODE + "/" + "Assignment";
039            /**
040         *
041         */
042            private static final long serialVersionUID = -3408305833805778653L;
043            //database id
044            private String tkAssignmentId;
045            private String principalId;
046            private Long jobNumber;
047            private String hrJobId;
048            private Job job;
049            private Long workArea;
050            //private Long tkWorkAreaId;
051            private Long task;
052            private String dept;
053            private TimeCollectionRule timeCollectionRule;
054            private DeptLunchRule deptLunchRule;
055            private WorkArea workAreaObj;
056            private Boolean history;
057        private String assignmentKey;
058    
059            private Person principal;
060    
061            private Task taskObj;
062    
063        private String calGroup;
064    
065            private List<AssignmentAccount> assignmentAccounts = new LinkedList<AssignmentAccount>();
066    
067            public List<AssignmentAccount> getAssignmentAccounts() {
068                    return assignmentAccounts;
069            }
070    
071            public void setAssignmentAccounts(List<AssignmentAccount> assignmentAccounts) {
072                    this.assignmentAccounts = assignmentAccounts;
073            }
074    
075            public Assignment(){}
076    
077            public Assignment(String principalId, Long jobNumber, Date effectiveDate,
078                            String earnCode, Long workAreaId, Long taskId) {
079                    this.principalId = principalId;
080                    this.jobNumber = jobNumber;
081                    this.effectiveDate = effectiveDate;
082                    this.workArea = workAreaId;
083                    this.task = taskId;
084            }
085    
086            public String getPrincipalId() {
087                    return principalId;
088            }
089    
090            public void setPrincipalId(String principalId) {
091                    this.principalId = principalId;
092                    this.setPrincipal(KimApiServiceLocator.getPersonService().getPerson(this.principalId));
093            }
094    
095            public String getName() {
096                    if (principal == null) {
097            principal = KimApiServiceLocator.getPersonService().getPerson(this.principalId);
098                    }
099                    return (principal != null) ? principal.getName() : "";
100            }
101    
102            public Job getJob() {
103                    if(job == null && this.getJobNumber() != null) {
104                            this.setJob(TkServiceLocator.getJobService().getJob(this.getPrincipalId(), this.getJobNumber(), this.getEffectiveDate()));
105                    }
106                    return job;
107            }
108    
109            public void setJob(Job job) {
110                    this.job = job;
111            }
112    
113            public Long getJobNumber() {
114                    return jobNumber;
115            }
116    
117            public void setJobNumber(Long jobNumber) {
118                    this.jobNumber = jobNumber;
119            }
120    
121            public String getHrJobId() {
122                    return hrJobId;
123            }
124    
125            public void setHrJobId(String hrJobId) {
126                    this.hrJobId = hrJobId;
127            }
128    
129            public Date getEffectiveDate() {
130                    return effectiveDate;
131            }
132    
133            public void setEffectiveDate(Date effectiveDate) {
134                    this.effectiveDate = effectiveDate;
135            }
136    
137            public boolean isActive() {
138                    return active;
139            }
140    
141            public void setActive(boolean active) {
142                    this.active = active;
143            }
144    
145            /**
146             * Provides us with the text to display to the user for clock actions on
147             * this assignment.
148             *
149             * @return
150             */
151            public String getClockText() {
152                    StringBuilder sb = new StringBuilder("example assignment clock text");
153    
154                    return sb.toString();
155            }
156    
157            public String getTkAssignmentId() {
158                    return tkAssignmentId;
159    
160        }
161            public void setTkAssignmentId(String tkAssignmentId) {
162                    this.tkAssignmentId = tkAssignmentId;
163            }
164    
165            public Timestamp getTimestamp() {
166                    return timestamp;
167            }
168    
169            public void setTimestamp(Timestamp timestamp) {
170                    this.timestamp = timestamp;
171            }
172    
173            public void setWorkArea(Long workArea) {
174                    this.workArea = workArea;
175            }
176    
177            public void setTask(Long task) {
178                    this.task = task;
179            }
180    
181            public String getDept() {
182                    if(this.getJobNumber()!= null) {
183                            if(this.getJob() == null || !this.getJobNumber().equals(this.getJob().getJobNumber())) {
184                                    if(this.getEffectiveDate()!=null){
185                                            this.setJob(TkServiceLocator.getJobService().getJob(this.getPrincipalId(), this.getJobNumber(), this.getEffectiveDate(), false));
186                                    }else{
187                                            this.setJob(TkServiceLocator.getJobService().getJob(this.getPrincipalId(), this.getJobNumber(), TKUtils.getCurrentDate(), false));
188                                    }
189                            }
190                            setDept((this.getJob() != null) ? this.getJob().getDept() : "");
191                    }
192                    return dept;
193            }
194    
195            public void setDept(String dept) {
196                    this.dept = dept;
197            }
198    
199            public WorkArea getWorkAreaObj() {
200                    if(workAreaObj == null && workArea != null) {
201                            this.setWorkAreaObj(TkServiceLocator.getWorkAreaService().getWorkArea(this.getWorkArea(), this.getEffectiveDate()));
202                    }
203                    return workAreaObj;
204            }
205    
206            public void setWorkAreaObj(WorkArea workAreaObj) {
207                    this.workAreaObj = workAreaObj;
208            }
209    
210            public Long getWorkArea() {
211                    return workArea;
212            }
213    
214            public Long getTask() {
215                    if(task == null) {
216                            return new Long(0);     // default task to 0 if task not provided
217                    }
218                    return task;
219            }
220    
221            public void setTimeCollectionRule(TimeCollectionRule timeCollectionRule) {
222                    this.timeCollectionRule = timeCollectionRule;
223            }
224    
225            public TimeCollectionRule getTimeCollectionRule() {
226                    return timeCollectionRule;
227            }
228    
229            public boolean isSynchronous() {
230                    return timeCollectionRule.isClockUserFl();
231            }
232    
233            public DeptLunchRule getDeptLunchRule() {
234                    return deptLunchRule;
235            }
236    
237            public void setDeptLunchRule(DeptLunchRule deptLunchRule) {
238                    this.deptLunchRule = deptLunchRule;
239            }
240    
241            public String getAssignmentDescription() {
242                    return TKUtils.getAssignmentString(this);
243            }
244    
245            public Person getPrincipal() {
246                    return principal;
247            }
248    
249            public void setPrincipal(Person principal) {
250                    this.principal = principal;
251            }
252    
253            public Task getTaskObj() {
254                    return taskObj;
255            }
256    
257            public void setTaskObj(Task taskObj) {
258                    this.taskObj = taskObj;
259            }
260    
261            /*public Long getTkWorkAreaId() {
262                    return tkWorkAreaId;
263            }
264    
265            public void setTkWorkAreaId(Long tkWorkAreaId) {
266                    this.tkWorkAreaId = tkWorkAreaId;
267            }*/
268            public Boolean getHistory() {
269                    return history;
270            }
271            public void setHistory(Boolean history) {
272                    this.history = history;
273            }
274    
275            @Override
276            public String getUniqueKey() {
277                    String jobKey = getPrincipalId()+"_"+getJobNumber()+"_"+getWorkArea()+"_"+
278                            (getTask() != null ? getTask().toString() : "");
279                    return jobKey;
280    
281            }
282    
283            @Override
284            public String getId() {
285                    return getTkAssignmentId();
286            }
287    
288            @Override
289            public void setId(String id) {
290                    setTkAssignmentId(id);
291            }
292    
293        @Override
294        public boolean equals(Object obj) {
295            if (obj == null)
296                return false;
297            if (obj == this)
298                return true;
299            if (obj.getClass() != getClass())
300                return false;
301    
302            Assignment rhs = (Assignment)obj;
303            return new EqualsBuilder().append(principalId, rhs.principalId).append(jobNumber, rhs.jobNumber)
304                    .append(workArea, rhs.workArea).append(task, rhs.task).isEquals();
305        }
306    
307        @Override
308        public int hashCode() {
309            return new HashCodeBuilder(17, 781).append(principalId).append(jobNumber).append(workArea).append(task).toHashCode();
310        }
311    
312        public String getCalGroup() {
313            return calGroup;
314        }
315    
316        public void setCalGroup(String calGroup) {
317            this.calGroup = calGroup;
318        }
319    
320        public String getAssignmentKey() {
321            return new AssignmentDescriptionKey(this).toAssignmentKeyString();
322        }
323    }