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.service;
017    
018    import java.util.Collection;
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.hr.job.Job;
024    import org.kuali.hr.time.HrBusinessObject;
025    import org.kuali.hr.time.assignment.Assignment;
026    import org.kuali.hr.time.assignment.AssignmentAccount;
027    import org.kuali.hr.time.paytype.PayType;
028    import org.kuali.hr.time.service.base.TkServiceLocator;
029    import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
030    import org.kuali.kfs.coa.businessobject.Account;
031    import org.kuali.rice.kim.api.identity.Person;
032    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
033    import org.kuali.rice.kns.document.MaintenanceDocument;
034    import org.kuali.rice.kns.service.KNSServiceLocator;
035    import org.kuali.rice.krad.bo.PersistableBusinessObject;
036    import org.kuali.rice.krad.service.KRADServiceLocator;
037    
038    /**
039     * Override the Maintenance page behavior for Assignment object
040     * 
041     * 
042     */
043    public class AssignmentMaintainableServiceImpl extends HrBusinessObjectMaintainableImpl {
044    
045            /**
046             * 
047             */
048            private static final long serialVersionUID = 1L;
049    
050            @Override
051            public Map populateBusinessObject(Map<String, String> fieldValues,
052                            MaintenanceDocument maintenanceDocument, String methodToCall) {
053                    if (fieldValues.containsKey("principalId")
054                                    && StringUtils.isNotEmpty(fieldValues.get("principalId"))) {
055                            Person p = KimApiServiceLocator.getPersonService().getPerson(
056                                            fieldValues.get("principalId"));
057                            if (p != null) {
058                                    fieldValues.put("name", p.getName());
059                            } else {
060                                    fieldValues.put("name", "");
061                            }
062                    }
063    
064            // KPME-1139
065                    // Commented due to KPME-1226 
066          /* if (!fieldValues.containsKey("effectiveDate")) {
067                fieldValues.put("effectiveDate", new DateTime().toString(TkConstants.DT_BASIC_DATE_FORMAT));
068            }*/
069           
070                    return super.populateBusinessObject(fieldValues, maintenanceDocument,
071                                    methodToCall);
072            }
073    
074            @Override
075            public Map<String, String> populateNewCollectionLines(
076                            Map<String, String> fieldValues,
077                            MaintenanceDocument maintenanceDocument, String methodToCall) {
078                    if (fieldValues.containsKey("assignmentAccounts.accountNbr")
079                                    && StringUtils.isNotEmpty(fieldValues
080                                                    .get("assignmentAccounts.accountNbr"))) {
081                            Map<String, String> fields = new HashMap<String, String>();
082                            fields.put("accountNumber", fieldValues
083                                            .get("assignmentAccounts.accountNbr"));
084                            Collection account = KRADServiceLocator.getBusinessObjectService()
085                                            .findMatching(Account.class, fields);
086                            if (account.size() > 0) {
087                                    Account acc = (Account) account.iterator().next();
088                                    fieldValues.put("assignmentAccounts.finCoaCd", acc
089                                                    .getChartOfAccountsCode());
090                            }
091                    }
092                    if ( !fieldValues.containsKey("assignmentAccounts.earnCode")
093                                    || StringUtils.isEmpty(fieldValues.get("assignmentAccounts.earnCode"))) {
094                            Assignment assignment = (Assignment) maintenanceDocument.getDocumentBusinessObject();
095                            if(assignment != null 
096                                    && assignment.getPrincipalId() != null 
097                                    && assignment.getJobNumber() != null 
098                                    && assignment.getEffectiveDate() != null) {
099                              Job job = TkServiceLocator.getJobService().getJob(assignment.getPrincipalId(), assignment.getJobNumber(), assignment.getEffectiveDate(), false);
100                              if(job != null) {
101                                            PayType payType = TkServiceLocator.getPayTypeService().getPayType(job.getHrPayType(), assignment.getEffectiveDate());
102                                            fieldValues.put("assignmentAccounts.earnCode", (payType != null) ? payType.getRegEarnCode() : "");
103                                    }
104                            }
105                    }
106    
107                    return super.populateNewCollectionLines(fieldValues,
108                                    maintenanceDocument, methodToCall);
109            }
110    
111            @Override
112            public void processAfterEdit(MaintenanceDocument document,
113                            Map<String, String[]> parameters) {
114                    Assignment aOld = (Assignment) document.getOldMaintainableObject()
115                                    .getBusinessObject();
116                    Assignment aNew = (Assignment) document.getNewMaintainableObject()
117                                    .getBusinessObject();
118                    super.processAfterEdit(document, parameters);
119            }
120    
121    
122        @Override
123            protected void setNewCollectionLineDefaultValues(String arg0,
124                            PersistableBusinessObject arg1) {
125            if(arg1 instanceof AssignmentAccount){
126                    AssignmentAccount assignmentAccount = (AssignmentAccount)arg1;
127                    Assignment assignment = (Assignment) this.getBusinessObject();
128                    assignmentAccount.setActive(assignment.isActive());
129            }
130                    super.setNewCollectionLineDefaultValues(arg0, arg1);
131            }
132    
133            @Override
134            public HrBusinessObject getObjectById(String id) {
135                    return TkServiceLocator.getAssignmentService().getAssignment(id);
136            }
137    
138            @Override
139            public void customSaveLogic(HrBusinessObject hrObj) {
140                    Assignment assignment = (Assignment)hrObj;
141                    for (AssignmentAccount assignAcct : assignment.getAssignmentAccounts()) {
142                            if(!isOldBusinessObjectInDocument()){ //prevents duplicate object on edit
143                                    assignAcct.setTkAssignAcctId(null);
144                            }
145                            assignAcct.setTkAssignmentId(assignment.getTkAssignmentId());
146                    }
147            }
148    
149    }