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.assignment;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.kuali.hr.time.service.base.TkServiceLocator;
023    import org.kuali.hr.time.timesheet.TimesheetDocument;
024    import org.kuali.hr.time.util.TKContext;
025    import org.kuali.hr.time.util.TkConstants;
026    import org.kuali.rice.core.api.util.ConcreteKeyValue;
027    import org.kuali.rice.core.api.util.KeyValue;
028    import org.kuali.rice.kns.web.struts.form.KualiForm;
029    import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm;
030    import org.kuali.rice.krad.keyvalues.KeyValuesBase;
031    
032    /**
033     * AssignmentValuesFinder provides the values for the MissedPunch maintenance
034     * document's Assignment selection.
035     *
036     * NOTE: Clock only assignments.
037     */
038    public class AssignmentValuesFinder extends KeyValuesBase {
039    
040        @Override
041        /**
042         * The KeyLabelPair values returned match up with the data the current
043         * user would get if they were selecting assignments from their clock.
044         *
045         * NOTE: These are Clock-Only assignments.
046         */
047        public List getKeyValues() {
048            List<KeyValue> labels = new ArrayList<KeyValue>();
049    
050            // We need the current timesheet document id. depending on where this
051            // was set up, we may need to look in two different places. Primarily
052            // we look directly at the context's function.
053    
054            String tdocId = TKContext.getCurrentTimesheetDocumentId();
055            if (tdocId == null) {
056                tdocId = TKContext.getHttpServletRequest().getParameter(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME);
057            }
058            if(tdocId == null){
059                KualiForm kualiForm = (KualiForm)TKContext.getHttpServletRequest().getAttribute("KualiForm");
060                if(kualiForm instanceof KualiMaintenanceForm){
061                    tdocId = ((KualiMaintenanceForm)kualiForm).getDocId();
062                }
063            }
064    
065    
066            if (tdocId != null) {
067                TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
068                Map<String,String> adMap = TkServiceLocator.getAssignmentService().getAssignmentDescriptions(tdoc, true); // Grab clock only assignments
069    
070                for (Map.Entry entry : adMap.entrySet()) {
071                    labels.add(new ConcreteKeyValue((String)entry.getKey(), (String)entry.getValue()));
072                }
073            }
074    
075            return labels;
076        }
077    }