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.missedpunch;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.hr.time.service.base.TkServiceLocator;
024    import org.kuali.hr.time.timesheet.TimesheetDocument;
025    import org.kuali.hr.time.util.TKContext;
026    import org.kuali.hr.time.util.TkConstants;
027    import org.kuali.rice.core.api.util.ConcreteKeyValue;
028    import org.kuali.rice.core.api.util.KeyValue;
029    import org.kuali.rice.kns.web.struts.form.KualiForm;
030    import org.kuali.rice.kns.web.struts.form.KualiTransactionalDocumentFormBase;
031    import org.kuali.rice.krad.keyvalues.KeyValuesBase;
032    
033    public class MissedPunchAssignmentFinder extends KeyValuesBase {
034    
035        @Override
036        /**
037         * The KeyLabelPair values returned match up with the data the current
038         * user would get if they were selecting assignments from their clock.
039         *
040         * NOTE: These are Clock-Only assignments.
041         */
042        public List getKeyValues() {
043            List<KeyValue> labels = new ArrayList<KeyValue>();
044            String tdocId = "";
045            String mpDocId = (String)TKContext.getHttpServletRequest().getParameter(TkConstants.DOCUMENT_ID_REQUEST_NAME);
046            if(StringUtils.isBlank(mpDocId)){
047                    KualiForm kualiForm = (KualiForm)TKContext.getHttpServletRequest().getAttribute("KualiForm");
048                    if(kualiForm instanceof KualiTransactionalDocumentFormBase){
049                            mpDocId = ((KualiTransactionalDocumentFormBase)kualiForm).getDocId();
050                    }
051            }
052            
053            if(StringUtils.isBlank(mpDocId)){
054               tdocId = TKContext.getHttpServletRequest().getParameter(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME);   
055            }
056            
057            if(StringUtils.isNotBlank(mpDocId)){
058                    MissedPunchDocument mp = TkServiceLocator.getMissedPunchService().getMissedPunchByRouteHeader(mpDocId);
059                    if(mp != null) {
060                            tdocId = mp.getTimesheetDocumentId();
061                    }
062            }
063            
064            mpDocId = (String)TKContext.getHttpServletRequest().getParameter("docId");
065            if(StringUtils.isNotBlank(mpDocId)){
066                    MissedPunchDocument mp = TkServiceLocator.getMissedPunchService().getMissedPunchByRouteHeader(mpDocId);
067                    if(mp != null) {
068                            tdocId = mp.getTimesheetDocumentId();
069                    }
070            }
071            
072            if(StringUtils.isBlank(tdocId)) {
073                    tdocId = (String) TKContext.getHttpServletRequest().getAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME);   
074            }
075            
076            if (tdocId != null) {
077                TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
078                Map<String,String> adMap = TkServiceLocator.getAssignmentService().getAssignmentDescriptions(tdoc, true); // Grab clock only assignments
079    
080                for (Map.Entry entry : adMap.entrySet()) {
081                    labels.add(new ConcreteKeyValue((String)entry.getKey(), (String)entry.getValue()));
082                }
083            } 
084    
085            return labels;
086        }
087    }
088