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.workarea;
017    
018    import java.sql.Timestamp;
019    import java.util.ArrayList;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.hr.time.HrBusinessObject;
025    import org.kuali.hr.time.position.Position;
026    import org.kuali.hr.time.roles.TkRole;
027    import org.kuali.hr.time.service.base.TkServiceLocator;
028    import org.kuali.hr.time.task.Task;
029    import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
030    import org.kuali.hr.time.util.TKContext;
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.maintenance.Maintainable;
035    import org.kuali.rice.kns.web.ui.Section;
036    import org.kuali.rice.krad.bo.PersistableBusinessObject;
037    import org.kuali.rice.krad.util.GlobalVariables;
038    import org.kuali.rice.krad.util.KRADConstants;
039    
040    public class WorkAreaMaintainableImpl extends HrBusinessObjectMaintainableImpl {
041    
042        private static final long serialVersionUID = 6264585236631982347L;
043    
044        @Override
045        protected void setNewCollectionLineDefaultValues(String arg0,
046                                                         PersistableBusinessObject arg1) {
047            WorkArea workArea = (WorkArea) this.getBusinessObject();
048            if (arg1 instanceof TkRole) {
049                TkRole role = (TkRole) arg1;
050                role.setEffectiveDate(workArea.getEffectiveDate());
051            } else if (arg1 instanceof Task) {
052                Task task = (Task) arg1;
053                task.setEffectiveDate(workArea.getEffectiveDate());
054            }
055            super.setNewCollectionLineDefaultValues(arg0, arg1);
056        }
057    
058        @Override
059        public PersistableBusinessObject getNewCollectionLine(String collectionName) {
060            return super.getNewCollectionLine(collectionName);    //To change body of overridden methods use File | Settings | File Templates.
061        }
062    
063        @Override
064        public void addNewLineToCollection(String collectionName) {
065            if (collectionName.equals("roles")) {
066                TkRole aRole = (TkRole)newCollectionLines.get(collectionName );
067                if ( aRole != null ) {
068                    if(!StringUtils.isEmpty(aRole.getPrincipalId()) && !StringUtils.isEmpty(aRole.getPositionNumber())) {
069                        GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles",
070                                            "error.role.principalId.positonNumber", aRole.getPrincipalId());
071                        return;
072                    }
073                    if(aRole.getPrincipalId() != null && !aRole.getPrincipalId().isEmpty()) {
074                        Person aPerson = KimApiServiceLocator.getPersonService().getPerson(aRole.getPrincipalId());
075                        if(aPerson == null) {
076                            GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles",
077                                    "error.role.person.notexist", aRole.getPrincipalId());
078                            return;
079                        }
080                    }
081                    if(aRole.getPositionNumber() != null && !aRole.getPositionNumber().isEmpty()) {
082                        Position aPositon = TkServiceLocator.getPositionService().getPositionByPositionNumber(aRole.getPositionNumber());
083                        if(aPositon == null) {
084                            GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles",
085                                    "error.role.position.notexist", aRole.getPositionNumber());
086                            return;
087                        }
088                    }
089                }
090            }
091            super.addNewLineToCollection(collectionName);
092        }
093    
094        @Override
095        public void processAfterEdit(MaintenanceDocument document,
096                                     Map<String, String[]> parameters) {
097            WorkArea waOld = (WorkArea) document.getOldMaintainableObject()
098                    .getBusinessObject();
099            WorkArea waNew = (WorkArea) document.getNewMaintainableObject()
100                    .getBusinessObject();
101    
102            List<TkRole> positionRoles = TkServiceLocator.getTkRoleService().getPositionRolesForWorkArea(waOld.getWorkArea(), waOld.getEffectiveDate());
103            TkServiceLocator.getWorkAreaService().populateWorkAreaTasks(waOld);
104            TkServiceLocator.getWorkAreaService().populateWorkAreaRoles(waOld);
105            waOld.getRoles().addAll(positionRoles);
106    
107            TkServiceLocator.getWorkAreaService().populateWorkAreaTasks(waNew);
108            TkServiceLocator.getWorkAreaService().populateWorkAreaRoles(waNew);
109            waNew.getRoles().addAll(positionRoles);
110            super.processAfterEdit(document, parameters);
111        }
112    
113        @SuppressWarnings("rawtypes")
114        @Override
115        public List getSections(MaintenanceDocument document,
116                                Maintainable oldMaintainable) {
117            List sections = super.getSections(document, oldMaintainable);
118            for (Object obj : sections) {
119                Section sec = (Section) obj;
120                if (document.isOldBusinessObjectInDocument()
121                        && sec.getSectionId().equals("inactiveRoles")) {
122                    sec.setHidden(false);
123                } else if (!document.isOldBusinessObjectInDocument()
124                        && sec.getSectionId().equals("inactiveRoles")) {
125                    sec.setHidden(true);
126                }
127            }
128            return sections;
129        }
130    
131        @Override
132        public HrBusinessObject getObjectById(String id) {
133            return TkServiceLocator.getWorkAreaService().getWorkArea(id);
134        }
135    
136        @Override
137        public void customSaveLogic(HrBusinessObject hrObj) {
138            WorkArea workArea = (WorkArea) hrObj;
139            
140            List<TkRole> roles = new ArrayList<TkRole>();
141            roles.addAll(workArea.getRoles());
142            roles.addAll(workArea.getInactiveRoles());
143            roles.addAll(createInactiveRoles(workArea.getRoles()));
144            
145            for (TkRole role : roles) {
146                role.setWorkAreaObj(workArea);
147                role.setUserPrincipalId(TKContext.getPrincipalId());
148            }
149            workArea.setRoles(roles);
150            
151            TkServiceLocator.getTkRoleService().saveOrUpdate(roles);
152            
153            List<Task> tasks = workArea.getTasks();
154            for (Task task : tasks) {
155                task.setTkTaskId(null);
156                task.setTimestamp(new Timestamp(System.currentTimeMillis()));
157            }
158            workArea.setTasks(tasks);
159    
160            TkServiceLocator.getTaskService().saveTasks(tasks);
161        }
162        
163        private List<TkRole> createInactiveRoles(List<TkRole> activeRoles) {
164            List<TkRole> inactiveRoles = new ArrayList<TkRole>();
165            
166            List<TkRole> oldRoles = new ArrayList<TkRole>();
167            List<TkRole> newRoles = new ArrayList<TkRole>();
168            for (TkRole activeRole : activeRoles) {
169                            if (!StringUtils.isEmpty(activeRole.getHrRolesId())) {
170                                    oldRoles.add(activeRole);
171                            } else {
172                                    newRoles.add(activeRole);
173                            }
174            }
175            
176            for (TkRole newRole : newRoles) {
177                    for (TkRole oldRole : oldRoles) {
178                                    if (StringUtils.equals(newRole.getRoleName(), oldRole.getRoleName()) 
179                                     && StringUtils.equals(newRole.getPrincipalId(), oldRole.getPrincipalId()) 
180                                     && StringUtils.equals(newRole.getPositionNumber(), oldRole.getPositionNumber())) {
181                                    
182                                            TkRole newInactiveRole = new TkRole();
183                                            newInactiveRole.setPrincipalId(oldRole.getPrincipalId());
184                                            newInactiveRole.setRoleName(oldRole.getRoleName());
185                                            newInactiveRole.setWorkArea(oldRole.getWorkArea());
186                                            newInactiveRole.setDepartment(oldRole.getDepartment());
187                                            newInactiveRole.setChart(oldRole.getChart());
188                                            newInactiveRole.setHrDeptId(oldRole.getHrDeptId());
189                                            newInactiveRole.setPositionNumber(oldRole.getPositionNumber());
190                                            newInactiveRole.setExpirationDate(oldRole.getExpirationDate());
191                                            newInactiveRole.setEffectiveDate(newRole.getEffectiveDate());
192                                            newInactiveRole.setTimestamp(new Timestamp(System.currentTimeMillis()));
193                                            newInactiveRole.setUserPrincipalId(TKContext.getPrincipalId());
194                                            newInactiveRole.setActive(false);
195      
196                                            inactiveRoles.add(newInactiveRole);
197                                    }
198                    }
199            }
200            
201            return inactiveRoles;
202        }
203    
204        @Override
205        public void processAfterNew(MaintenanceDocument document,
206                                    Map<String, String[]> parameters) {
207            WorkArea workArea = (WorkArea) this.getBusinessObject();
208            if (workArea.getWorkArea() == null) {
209                workArea.setWorkArea(TkServiceLocator.getWorkAreaService().getNextWorkAreaKey());
210            }
211            super.processAfterNew(document, parameters);
212        }
213    }