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.task.service;
017    
018    import java.util.List;
019    import java.util.Map;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.hr.time.authorization.TkAuthorizedLookupableHelperBase;
023    import org.kuali.hr.time.service.base.TkServiceLocator;
024    import org.kuali.hr.time.task.Task;
025    import org.kuali.hr.time.util.TKUtils;
026    import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
027    import org.kuali.rice.kns.lookup.HtmlData;
028    import org.kuali.rice.kns.web.struts.form.LookupForm;
029    import org.kuali.rice.kns.web.ui.Field;
030    import org.kuali.rice.kns.web.ui.Row;
031    import org.kuali.rice.krad.bo.BusinessObject;
032    
033    public class TaskLookupableHelper extends TkAuthorizedLookupableHelperBase {
034        /**
035         *
036         */
037        private static final long serialVersionUID = 1L;
038    
039        @SuppressWarnings({"unchecked"})
040        @Override
041        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
042    
043            String task = fieldValues.get("task");
044            String description = fieldValues.get("description");
045            String workArea = fieldValues.get("workArea");
046            String workAreaDescription = fieldValues.get("workAreaDescription");
047            String fromEffdt = fieldValues.get("rangeLowerBoundKeyPrefix_effectiveDate");
048            String toEffdt = StringUtils.isNotBlank(fieldValues.get("effectiveDate")) ? fieldValues.get("effectiveDate").replace("<=", "") : "";
049    
050            List<Task> tasks = TkServiceLocator.getTaskService().getTasks(task, description, workArea, workAreaDescription,
051                    TKUtils.formatDateString(fromEffdt), TKUtils.formatDateString(toEffdt));
052    
053            return tasks;
054    //              List finalList = new ArrayList<Task>();
055    //              String wad = "";
056    //              String wa = "";
057    //              List<String> taskFound = new ArrayList<String>();
058    //              if (fieldValues.containsKey("workAreaDescription") && !fieldValues.get("workAreaDescription").isEmpty()) {
059    //                      wad = fieldValues.get("workAreaDescription");
060    //                      fieldValues.remove("workAreaDescription");
061    //              }
062    //              if(fieldValues.containsKey("workArea") && !fieldValues.get("workArea").isEmpty()) {
063    //                      wa = fieldValues.get("workArea");
064    //                      fieldValues.remove("workArea");
065    //              }
066    //              List aList = super.getSearchResults(fieldValues);
067    //
068    //              for(int i = 0; i< aList.size(); i++) {
069    //                      Task aTask = (Task) aList.get(i);
070    //                      if(aTask.getTkWorkAreaId() != null) {
071    //                              WorkArea aWorkArea = TkServiceLocator.getWorkAreaService().getWorkArea(aTask.getTkWorkAreaId());
072    //                              if(aWorkArea != null) {
073    //                                      if( (StringUtils.isBlank(wa) || aWorkArea.getWorkArea().toString().equals(wa))
074    //                                                      || (StringUtils.isBlank(wad) || aWorkArea.getDescription().equals(wad))) {
075    //                                              if(!taskFound.contains(aWorkArea.getWorkArea() + "_"+aTask.getTask())   ){
076    //                                                      finalList.add(aTask);
077    //                                                      taskFound.add(aWorkArea.getWorkArea() +"_"+aTask.getTask());
078    //                                              }
079    //                                      }
080    //                              }
081    //                      }
082    //              }
083    
084    //        return finalList;
085        }
086    
087        @SuppressWarnings("rawtypes")
088        @Override
089        public HtmlData getReturnUrl(BusinessObject businessObject,
090                                     LookupForm lookupForm, List returnKeys,
091                                     BusinessObjectRestrictions businessObjectRestrictions) {
092            if (lookupForm.getFieldConversions().containsKey("effectiveDate")) {
093                lookupForm.getFieldConversions().remove("effectiveDate");
094            }
095            if (returnKeys.contains("effectiveDate")) {
096                returnKeys.remove("effectiveDate");
097            }
098            if (lookupForm.getFieldConversions().containsKey("workArea")) {
099                lookupForm.getFieldConversions().remove("workArea");
100            }
101            if (returnKeys.contains("workArea")) {
102                returnKeys.remove("workArea");
103            }
104    
105            return super.getReturnUrl(businessObject, lookupForm, returnKeys,
106                    businessObjectRestrictions);
107        }
108    
109        @Override
110        // remove the default value for Task for lookup
111        public List<Row> getRows() {
112            List<Row> rowList = super.getRows();
113            for (Row aRow : rowList) {
114                List<Field> fields = aRow.getFields();
115                for (Field aField : fields) {
116                    if (aField.getFieldLabel() != null && aField.getFieldLabel().equals("Task")) {
117                        if (aField.getPropertyValue() != null && aField.getPropertyValue().equals("0")) {
118                            aField.setPropertyValue("");
119                        }
120                    }
121                }
122            }
123            return rowList;
124        }
125    
126        @Override
127        public boolean shouldShowBusinessObject(BusinessObject bo) {
128            return true;
129        }
130    }