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.department;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.kuali.hr.time.HrBusinessObject;
023    import org.kuali.hr.time.roles.TkRole;
024    import org.kuali.hr.time.service.base.TkServiceLocator;
025    import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
026    import org.kuali.hr.time.util.TKContext;
027    import org.kuali.rice.kim.api.identity.Person;
028    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
029    import org.kuali.rice.kns.document.MaintenanceDocument;
030    import org.kuali.rice.kns.maintenance.Maintainable;
031    import org.kuali.rice.kns.web.ui.Section;
032    import org.kuali.rice.krad.bo.PersistableBusinessObject;
033    import org.kuali.rice.krad.util.GlobalVariables;
034    import org.kuali.rice.krad.util.KRADConstants;
035    
036    public class DepartmentMaintainableImpl extends HrBusinessObjectMaintainableImpl {
037    
038            private static final long serialVersionUID = -330523155799598560L;
039    
040        @Override
041            protected void setNewCollectionLineDefaultValues(String arg0,
042                            PersistableBusinessObject arg1) {
043            if(arg1 instanceof TkRole){
044                    TkRole role = (TkRole)arg1;
045                    Department dept = (Department) this.getBusinessObject();
046                    role.setEffectiveDate(dept.getEffectiveDate());
047            }
048                    super.setNewCollectionLineDefaultValues(arg0, arg1);
049            }
050    
051            @Override
052        public void processAfterEdit( MaintenanceDocument document, Map<String,String[]> parameters ) {
053            Department dOld = (Department)document.getOldMaintainableObject().getBusinessObject();
054            Department dNew = (Department)document.getNewMaintainableObject().getBusinessObject();
055    
056            TkServiceLocator.getDepartmentService().populateDepartmentRoles(dOld);
057            TkServiceLocator.getDepartmentService().populateDepartmentRoles(dNew);
058        }
059            
060            @Override
061        public void addNewLineToCollection(String collectionName) {
062            if (collectionName.equals("roles")) {
063                    TkRole aRole = (TkRole)newCollectionLines.get(collectionName );
064                if ( aRole != null ) {
065                    if(!aRole.getPrincipalId().isEmpty()) {
066                            Person aPerson = KimApiServiceLocator.getPersonService().getPerson(aRole.getPrincipalId());
067                            if(aPerson == null) {
068                                    GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles", 
069                                                    "dept.role.person.notExist",aRole.getPrincipalId());
070                                    return;
071                            }
072                    }
073                }
074            }
075            super.addNewLineToCollection(collectionName);
076            }
077            
078            @SuppressWarnings("rawtypes")
079            @Override
080            public List getSections(MaintenanceDocument document,
081                            Maintainable oldMaintainable) {
082                    List sections = super.getSections(document, oldMaintainable);
083                    for (Object obj : sections) {
084                            Section sec = (Section) obj;
085                            if (document.isOldBusinessObjectInDocument()
086                                            && sec.getSectionId().equals("inactiveRoles")) {
087                                    sec.setHidden(false);
088                            } else if (!document.isOldBusinessObjectInDocument()
089                                            && sec.getSectionId().equals("inactiveRoles")) {
090                                    sec.setHidden(true);
091                            }
092                    }
093                    return sections;
094            }
095    
096            @Override
097            public HrBusinessObject getObjectById(String id) {
098                    return TkServiceLocator.getDepartmentService().getDepartment(id);
099            }
100    
101            @Override
102            public void customSaveLogic(HrBusinessObject hrObj) {
103                    Department dept = (Department)hrObj;
104                    List<TkRole> roles = dept.getRoles();
105                    List<TkRole> rolesCopy = new ArrayList<TkRole>();
106                    
107                    rolesCopy.addAll(roles);
108                    
109                    if (dept.getInactiveRoles() != null
110                                    && dept.getInactiveRoles().size() > 0) {
111                            for (TkRole role : dept.getInactiveRoles()) {
112                                    roles.add(role);
113                            }
114                    }
115    
116                    dept.setRoles(roles);
117                    for (TkRole role : roles) {
118                            role.setDepartmentObj(dept);
119                            role.setUserPrincipalId(TKContext.getPrincipalId());
120                    }
121                    TkServiceLocator.getTkRoleService().saveOrUpdate(roles);
122            }
123    }