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.roles;
017    
018    import java.util.ArrayList;
019    import java.util.HashSet;
020    import java.util.Iterator;
021    import java.util.List;
022    import java.util.Map;
023    import java.util.Set;
024    
025    import org.kuali.hr.job.Job;
026    import org.kuali.hr.time.service.base.TkServiceLocator;
027    import org.kuali.hr.time.util.TKUtils;
028    import org.kuali.rice.kns.inquiry.KualiInquirableImpl;
029    import org.kuali.rice.krad.bo.BusinessObject;
030    
031    // chen, KPME-938, implement the view link on lookup page
032    public class TkRoleGroupInquirableImpl extends KualiInquirableImpl {
033    
034            @Override
035            public BusinessObject getBusinessObject(Map fieldValues) {
036                    TkRoleGroup tkRoleGroup = (TkRoleGroup) super.getBusinessObject(fieldValues);
037                    
038                    List<Job> jobs = TkServiceLocator.getJobService().getJobs(tkRoleGroup.getPrincipalId(), TKUtils.getCurrentDate());
039                    List<TkRole> positionRoles = new ArrayList<TkRole>();
040                    List<TkRole> inactivePositionRoles = new ArrayList<TkRole>();
041                    Set<String> positionNumbers = new HashSet<String>(); 
042                    for(Job job : jobs){
043                            positionNumbers.add(job.getPositionNumber());
044                    }
045                    for(String pNo : positionNumbers){
046                            TkRole positionRole = TkServiceLocator.getTkRoleService().getRolesByPosition(pNo);
047                            if(positionRole != null)
048                                    positionRoles.add(positionRole);
049                            TkRole inactivePositionRole = TkServiceLocator.getTkRoleService().getInactiveRolesByPosition(pNo);
050                            if(inactivePositionRole != null)
051                                    inactivePositionRoles.add(inactivePositionRole);
052                    }
053                    tkRoleGroup.setInactivePositionRoles(inactivePositionRoles);
054                    tkRoleGroup.setPositionRoles(positionRoles);
055                    
056                    List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(tkRoleGroup.getPrincipalId(), TKUtils.getCurrentDate());
057                    List<TkRole> tkInActiveRoles = TkServiceLocator.getTkRoleService().getInactiveRoles(tkRoleGroup.getPrincipalId(), TKUtils.getCurrentDate());
058                    Iterator<TkRole> itr = tkRoles.iterator();
059                    // remove position roles from the active/inactive roles lists
060                    while (itr.hasNext()) {
061                            TkRole tkRole = (TkRole) itr.next();
062                            if(tkRoleGroup.getPositionRoles()!=null && tkRoleGroup.getPositionRoles().contains(tkRole)){
063                                    itr.remove();
064                            }
065                    }
066                    itr = tkInActiveRoles.iterator();
067                    while (itr.hasNext()) {
068                            TkRole tkRole = (TkRole) itr.next();
069                            if(tkRoleGroup.getPositionRoles()!=null && tkRoleGroup.getPositionRoles().contains(tkRole)){
070                                    itr.remove();
071                            }
072                    }
073                    tkRoleGroup.setRoles(tkRoles);
074                    tkRoleGroup.setInactiveRoles(tkInActiveRoles);
075    
076                    return tkRoleGroup;
077            }
078    }