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