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