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.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.core.cache.CacheUtils;
025    import org.kuali.hr.time.HrBusinessObject;
026    import org.kuali.hr.time.position.Position;
027    import org.kuali.hr.time.roles.TkRole;
028    import org.kuali.hr.time.service.base.TkServiceLocator;
029    import org.kuali.hr.time.task.Task;
030    import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
031    import org.kuali.hr.time.util.TKContext;
032    import org.kuali.hr.time.util.TKUtils;
033    import org.kuali.rice.kim.api.identity.Person;
034    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
035    import org.kuali.rice.kns.document.MaintenanceDocument;
036    import org.kuali.rice.kns.maintenance.Maintainable;
037    import org.kuali.rice.kns.web.ui.Section;
038    import org.kuali.rice.krad.bo.PersistableBusinessObject;
039    import org.kuali.rice.krad.service.KRADServiceLocator;
040    import org.kuali.rice.krad.util.GlobalVariables;
041    import org.kuali.rice.krad.util.KRADConstants;
042    
043    public class WorkAreaMaintainableImpl extends HrBusinessObjectMaintainableImpl {
044    
045        private static final long serialVersionUID = 6264585236631982347L;
046    
047        @Override
048        protected void setNewCollectionLineDefaultValues(String arg0,
049                                                         PersistableBusinessObject arg1) {
050            WorkArea workArea = (WorkArea) this.getBusinessObject();
051            if (arg1 instanceof TkRole) {
052                TkRole role = (TkRole) arg1;
053                role.setEffectiveDate(workArea.getEffectiveDate());
054            } else if (arg1 instanceof Task) {
055                Task task = (Task) arg1;
056                task.setEffectiveDate(workArea.getEffectiveDate());
057            }
058            super.setNewCollectionLineDefaultValues(arg0, arg1);
059        }
060    
061        @Override
062        public PersistableBusinessObject getNewCollectionLine(String collectionName) {
063            return super.getNewCollectionLine(collectionName);    //To change body of overridden methods use File | Settings | File Templates.
064        }
065    
066        @Override
067        public void addNewLineToCollection(String collectionName) {
068            if (collectionName.equals("roles")) {
069                TkRole aRole = (TkRole)newCollectionLines.get(collectionName );
070                if ( aRole != null ) {
071                    if(!StringUtils.isEmpty(aRole.getPrincipalId()) && !StringUtils.isEmpty(aRole.getPositionNumber())) {
072                        GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles",
073                                            "error.role.principalId.positonNumber", aRole.getPrincipalId());
074                        return;
075                    }
076                    if(aRole.getPrincipalId() != null && !aRole.getPrincipalId().isEmpty()) {
077                        Person aPerson = KimApiServiceLocator.getPersonService().getPerson(aRole.getPrincipalId());
078                        if(aPerson == null) {
079                            GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles",
080                                    "error.role.person.notexist", aRole.getPrincipalId());
081                            return;
082                        }
083                    }
084                    if(aRole.getPositionNumber() != null && !aRole.getPositionNumber().isEmpty()) {
085                        Position aPositon = TkServiceLocator.getPositionService().getPositionByPositionNumber(aRole.getPositionNumber());
086                        if(aPositon == null) {
087                            GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles",
088                                    "error.role.position.notexist", aRole.getPositionNumber());
089                            return;
090                        }
091                    }
092                }
093            }
094            super.addNewLineToCollection(collectionName);
095        }
096    
097        @Override
098        public void processAfterEdit(MaintenanceDocument document,
099                                     Map<String, String[]> parameters) {
100            WorkArea waOld = (WorkArea) document.getOldMaintainableObject()
101                    .getBusinessObject();
102            WorkArea waNew = (WorkArea) document.getNewMaintainableObject()
103                    .getBusinessObject();
104    
105            List<TkRole> positionRoles = TkServiceLocator.getTkRoleService().getPositionRolesForWorkArea(waOld.getWorkArea(), waOld.getEffectiveDate());
106            TkServiceLocator.getWorkAreaService().populateWorkAreaRoles(waOld);
107            waOld.getRoles().addAll(positionRoles);
108    
109            TkServiceLocator.getWorkAreaService().populateWorkAreaRoles(waNew);
110            waNew.getRoles().addAll(positionRoles);
111            super.processAfterEdit(document, parameters);
112        }
113    
114        @SuppressWarnings("rawtypes")
115        @Override
116        public List getSections(MaintenanceDocument document,
117                                Maintainable oldMaintainable) {
118            List sections = super.getSections(document, oldMaintainable);
119            for (Object obj : sections) {
120                Section sec = (Section) obj;
121                if (document.isOldBusinessObjectInDocument()
122                        && sec.getSectionId().equals("inactiveRoles")) {
123                    sec.setHidden(false);
124                } else if (!document.isOldBusinessObjectInDocument()
125                        && sec.getSectionId().equals("inactiveRoles")) {
126                    sec.setHidden(true);
127                }
128            }
129            return sections;
130        }
131    
132        @Override
133        public HrBusinessObject getObjectById(String id) {
134            return TkServiceLocator.getWorkAreaService().getWorkArea(id);
135        }
136    
137        @Override
138        public void customSaveLogic(HrBusinessObject hrObj) {
139            WorkArea workArea = (WorkArea) hrObj;
140            List<TkRole> roles = workArea.getRoles();
141            List<TkRole> rolesCopy = new ArrayList<TkRole>();
142            rolesCopy.addAll(roles);
143            if (workArea.getInactiveRoles() != null
144                    && workArea.getInactiveRoles().size() > 0) {
145                for (TkRole role : workArea.getInactiveRoles()) {
146                    roles.add(role);
147                }
148            }
149            List<Task> tasks = workArea.getTasks();
150            for (Task task : tasks) {
151                task.setTkTaskId(null);
152                task.setTimestamp(new Timestamp(System.currentTimeMillis()));
153            }
154            workArea.setTasks(tasks);
155            workArea.setRoles(roles);
156            for (TkRole role : roles) {
157                role.setWorkAreaObj(workArea);
158                //Be sure department is not set for current state of role query
159                role.setDepartment(null);
160                role.setUserPrincipalId(TKContext.getPrincipalId());
161            }
162            TkServiceLocator.getTkRoleService().saveOrUpdate(roles);
163        }
164    
165        @Override
166        public void processAfterNew(MaintenanceDocument document,
167                                    Map<String, String[]> parameters) {
168            WorkArea workArea = (WorkArea) this.getBusinessObject();
169            if (workArea.getWorkArea() == null) {
170                workArea.setWorkArea(TkServiceLocator.getWorkAreaService().getNextWorkAreaKey());
171            }
172            super.processAfterNew(document, parameters);
173        }
174    
175        @Override
176        public void saveBusinessObject() {
177            HrBusinessObject hrObj = (HrBusinessObject) this.getBusinessObject();
178            customSaveLogic(hrObj);
179            if(hrObj.getId()!=null){
180                HrBusinessObject oldHrObj = this.getObjectById(hrObj.getId());
181                if(oldHrObj!= null){
182                    //if the effective dates are the same do not create a new row just inactivate the old one
183                    if(hrObj.getEffectiveDate().equals(oldHrObj.getEffectiveDate())){
184                        oldHrObj.setActive(false);
185                        oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime())));
186                    } else{
187                        //if effective dates not the same add a new row that inactivates the old entry based on the new effective date
188                        oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime())));
189                        oldHrObj.setEffectiveDate(hrObj.getEffectiveDate());
190                        oldHrObj.setActive(false);
191                        oldHrObj.setId(null);
192                    }
193                    KRADServiceLocator.getBusinessObjectService().save(oldHrObj);
194                }
195            }
196            hrObj.setTimestamp(new Timestamp(System.currentTimeMillis()));
197            hrObj.setId(null);
198    
199            KRADServiceLocator.getBusinessObjectService().save(hrObj);
200            CacheUtils.flushCache(WorkArea.CACHE_NAME);
201        }
202    
203    }