001    /**
002     * Copyright 2004-2012 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.regex.Matcher;
019    import java.util.regex.Pattern;
020    
021    import org.apache.log4j.Logger;
022    import org.kuali.hr.time.util.TkConstants;
023    
024    public class AssignmentDescriptionKey {
025            private static final Logger LOG = Logger.getLogger(AssignmentDescriptionKey.class);
026    
027            private Long jobNumber;
028            private Long workArea;
029            private Long task;
030    
031        public AssignmentDescriptionKey() {
032    
033        }
034    
035        // djunk - added this constructor since we are essentially working on units
036        // of assignments and their keys
037        public AssignmentDescriptionKey(Assignment a) {
038            if (a != null) {
039                this.jobNumber = a.getJobNumber();
040                this.workArea = a.getWorkArea();
041                this.task = a.getTask();
042            }
043        }
044    
045            public AssignmentDescriptionKey(String jobNumer, String workArea, String task) {
046                    this.jobNumber = Long.parseLong(jobNumer);
047                    this.workArea = Long.parseLong(workArea);
048                    this.task = Long.parseLong(task);
049            }
050            public AssignmentDescriptionKey(Long jobNumer, Long workArea, Long task) {
051                    this.jobNumber = jobNumer;
052                    this.workArea = workArea;
053                    this.task = task;
054            }
055            public AssignmentDescriptionKey(String assignmentKey) {
056    
057                    try {
058                    Pattern keyPattern = Pattern.compile("^\\d{1,}_\\d{1,}_\\d{1,}");
059                    Matcher match = keyPattern.matcher(assignmentKey);
060                    if(!match.matches()) {
061                            throw new RuntimeException("the format of the assignment key is wrong. it should be jobNumber_workArea_task");
062                    }
063    
064                    String[] key = assignmentKey.split(TkConstants.ASSIGNMENT_KEY_DELIMITER);
065    
066                    this.jobNumber = Long.parseLong(key[0]);
067                    this.workArea = Long.parseLong(key[1]);
068                    this.task = Long.parseLong(key[2]);
069                    }
070                    catch(NullPointerException npe) {
071                            LOG.error("The assignment key is null");
072                    }
073    
074            }
075            public Long getJobNumber() {
076                    return jobNumber;
077            }
078            public void setJobNumber(Long jobNumber) {
079                    this.jobNumber = jobNumber;
080            }
081            public Long getWorkArea() {
082                    return workArea;
083            }
084            public void setWorkArea(Long workArea) {
085                    this.workArea = workArea;
086            }
087            public Long getTask() {
088                    return task;
089            }
090            public void setTask(Long task) {
091                    this.task = task;
092            }
093    
094        @Override
095        public String toString() {
096            return "jobNumber: " + jobNumber + "; workArea: " + workArea + "; task: " + task;
097        }
098    
099        public String toAssignmentKeyString() {
100            return jobNumber + TkConstants.ASSIGNMENT_KEY_DELIMITER + workArea + TkConstants.ASSIGNMENT_KEY_DELIMITER + task;
101        }
102    
103    
104        public static String getAssignmentKeyString(Assignment a) {
105            return a.getJobNumber() + TkConstants.ASSIGNMENT_KEY_DELIMITER + a.getWorkArea() + TkConstants.ASSIGNMENT_KEY_DELIMITER + a.getTask();
106        }
107    }