View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.department;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.kuali.hr.time.HrBusinessObject;
23  import org.kuali.hr.time.roles.TkRole;
24  import org.kuali.hr.time.service.base.TkServiceLocator;
25  import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
26  import org.kuali.hr.time.util.TKContext;
27  import org.kuali.rice.kim.api.identity.Person;
28  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29  import org.kuali.rice.kns.document.MaintenanceDocument;
30  import org.kuali.rice.kns.maintenance.Maintainable;
31  import org.kuali.rice.kns.web.ui.Section;
32  import org.kuali.rice.krad.bo.PersistableBusinessObject;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  import org.kuali.rice.krad.util.KRADConstants;
35  
36  public class DepartmentMaintainableImpl extends HrBusinessObjectMaintainableImpl {
37  
38  	private static final long serialVersionUID = -330523155799598560L;
39  
40      @Override
41  	protected void setNewCollectionLineDefaultValues(String arg0,
42  			PersistableBusinessObject arg1) {
43      	if(arg1 instanceof TkRole){
44      		TkRole role = (TkRole)arg1;
45      		Department dept = (Department) this.getBusinessObject();
46      		role.setEffectiveDate(dept.getEffectiveDate());
47      	}
48  		super.setNewCollectionLineDefaultValues(arg0, arg1);
49  	}
50  
51  	@Override
52      public void processAfterEdit( MaintenanceDocument document, Map<String,String[]> parameters ) {
53          Department dOld = (Department)document.getOldMaintainableObject().getBusinessObject();
54          Department dNew = (Department)document.getNewMaintainableObject().getBusinessObject();
55  
56          TkServiceLocator.getDepartmentService().populateDepartmentRoles(dOld);
57          TkServiceLocator.getDepartmentService().populateDepartmentRoles(dNew);
58      }
59  	
60  	@Override
61      public void addNewLineToCollection(String collectionName) {
62          if (collectionName.equals("roles")) {
63          	TkRole aRole = (TkRole)newCollectionLines.get(collectionName );
64              if ( aRole != null ) {
65              	if(!aRole.getPrincipalId().isEmpty()) {
66              		Person aPerson = KimApiServiceLocator.getPersonService().getPerson(aRole.getPrincipalId());
67              		if(aPerson == null) {
68              			GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles", 
69                  				"dept.role.person.notExist",aRole.getPrincipalId());
70                  		return;
71              		}
72              	}
73              }
74          }
75          super.addNewLineToCollection(collectionName);
76  	}
77  	
78  	@SuppressWarnings("rawtypes")
79  	@Override
80  	public List getSections(MaintenanceDocument document,
81  			Maintainable oldMaintainable) {
82  		List sections = super.getSections(document, oldMaintainable);
83  		for (Object obj : sections) {
84  			Section sec = (Section) obj;
85  			if (document.isOldBusinessObjectInDocument()
86  					&& sec.getSectionId().equals("inactiveRoles")) {
87  				sec.setHidden(false);
88  			} else if (!document.isOldBusinessObjectInDocument()
89  					&& sec.getSectionId().equals("inactiveRoles")) {
90  				sec.setHidden(true);
91  			}
92  		}
93  		return sections;
94  	}
95  
96  	@Override
97  	public HrBusinessObject getObjectById(String id) {
98  		return TkServiceLocator.getDepartmentService().getDepartment(id);
99  	}
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 }