001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.web.form;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.lookup.LookupUtils;
020import org.kuali.rice.krad.lookup.Lookupable;
021import org.kuali.rice.krad.uif.UifConstants.ViewType;
022import org.kuali.rice.krad.uif.view.LookupView;
023import org.kuali.rice.krad.uif.service.ViewHelperService;
024import org.kuali.rice.krad.util.KRADConstants;
025import org.kuali.rice.krad.util.KRADUtils;
026
027import javax.servlet.http.HttpServletRequest;
028import java.util.ArrayList;
029import java.util.Collection;
030import java.util.HashMap;
031import java.util.List;
032import java.util.Map;
033
034/**
035 * Form class for <code>LookupView</code> screens
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039public class LookupForm extends UifFormBase {
040    private static final long serialVersionUID = -7323484966538685327L;
041
042    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupForm.class);
043
044    private String dataObjectClassName;
045    private String docNum;
046    private String referencesToRefresh;
047
048    private boolean multipleValuesSelect;
049    private String lookupCollectionName;
050
051    private Map<String, String> criteriaFields;
052    private Map<String, String> fieldConversions;
053
054    private boolean atLeastOneRowReturnable;
055    private boolean atLeastOneRowHasActions;
056
057    private Collection<?> searchResults;
058
059    private boolean redirectedLookup;
060
061    public LookupForm() {
062        super();
063
064        setViewTypeName(ViewType.LOOKUP);
065        atLeastOneRowReturnable = false;
066        atLeastOneRowHasActions = false;
067        multipleValuesSelect = false;
068        redirectedLookup = false;
069
070        criteriaFields = new HashMap<String, String>();
071        fieldConversions = new HashMap<String, String>();
072    }
073
074    /**
075     * Picks out business object name from the request to get retrieve a
076     * lookupable and set properties
077     */
078    @Override
079    public void postBind(HttpServletRequest request) {
080        super.postBind(request);
081
082        try {
083            Lookupable lookupable = getLookupable();
084            if (lookupable == null) {
085                // assume lookupable will be set by controller or a redirect will happen
086                return;
087            }
088
089            if (StringUtils.isBlank(getDataObjectClassName())) {
090                setDataObjectClassName(((LookupView) getView()).getDataObjectClassName().getName());
091            }
092
093            // init lookupable with data object class
094            Class<?> dataObjectClass = Class.forName(getDataObjectClassName());
095            lookupable.setDataObjectClass(dataObjectClass);
096
097            // if showMaintenanceLinks is not already true, only show maintenance links
098            // if the lookup was called from the home application view
099            if (!((LookupView) getView()).isShowMaintenanceLinks()) {
100                // TODO replace with check to history
101                if (StringUtils.contains(getReturnLocation(), "/" + KRADConstants.PORTAL_ACTION) ||
102                        StringUtils.contains(getReturnLocation(), "/index.html")) {
103                    ((LookupView) getView()).setShowMaintenanceLinks(true);
104                }
105            }
106
107            // populate lookup read only fields list on lookupable
108            lookupable.setReadOnlyFieldsList(getReadOnlyFieldsList());
109
110            // populate field conversions list
111            if (request.getParameter(KRADConstants.CONVERSION_FIELDS_PARAMETER) != null) {
112                String conversionFields = request.getParameter(KRADConstants.CONVERSION_FIELDS_PARAMETER);
113                setFieldConversions(KRADUtils.convertStringParameterToMap(conversionFields));
114                lookupable.setFieldConversions(getFieldConversions());
115            }
116
117            // perform upper casing of lookup parameters
118            Map<String, String> fieldValues = new HashMap<String, String>();
119            Map<String, String> formFields = getCriteriaFields();
120
121            if (formFields != null) {
122                for (Map.Entry<String, String> entry : formFields.entrySet()) {
123                    // check here to see if this field is a criteria element on the form
124                    fieldValues.put(entry.getKey(),
125                            LookupUtils.forceUppercase(dataObjectClass, entry.getKey(), entry.getValue()));
126                }
127            }
128
129            // fieldValues.put(UifParameters.RETURN_FORM_KEY, getReturnFormKey());
130            // fieldValues.put(UifParameters.RETURN_LOCATION, getReturnLocation());
131            if (StringUtils.isNotBlank(getDocNum())) {
132                fieldValues.put(KRADConstants.DOC_NUM, getDocNum());
133            }
134
135            this.setCriteriaFields(fieldValues);
136        } catch (ClassNotFoundException e) {
137            LOG.error("Object class " + getDataObjectClassName() + " not found");
138            throw new RuntimeException("Object class " + getDataObjectClassName() + " not found", e);
139        }
140    }
141
142    public Lookupable getLookupable() {
143        if ((getView() != null) && Lookupable.class.isAssignableFrom(getView().getViewHelperService().getClass())) {
144            return (Lookupable) getView().getViewHelperService();
145        }
146
147        return null;
148    }
149
150    public String getDataObjectClassName() {
151        return this.dataObjectClassName;
152    }
153
154    public void setDataObjectClassName(String dataObjectClassName) {
155        this.dataObjectClassName = dataObjectClassName;
156    }
157
158    public String getDocNum() {
159        return this.docNum;
160    }
161
162    public void setDocNum(String docNum) {
163        this.docNum = docNum;
164    }
165
166    public String getReferencesToRefresh() {
167        return referencesToRefresh;
168    }
169
170    public void setReferencesToRefresh(String referencesToRefresh) {
171        this.referencesToRefresh = referencesToRefresh;
172    }
173
174    /**
175     * Indicates whether multiple values select should be enabled for the lookup
176     *
177     * <p>
178     * When set to true, the select field is enabled for the lookup results group that allows the user
179     * to select one or more rows for returning
180     * </p>
181     *
182     * @return boolean true if multiple values should be enabled, false otherwise
183     */
184    public boolean isMultipleValuesSelect() {
185        return multipleValuesSelect;
186    }
187
188    /**
189     * Setter for the multiple values select indicator
190     *
191     * @param multipleValuesSelect
192     */
193    public void setMultipleValuesSelect(boolean multipleValuesSelect) {
194        this.multipleValuesSelect = multipleValuesSelect;
195    }
196
197    /**
198     * For the case of multi-value lookup, indicates the collection that should be populated with
199     * the return results
200     *
201     * @return String collection name (must be full binding path)
202     */
203    public String getLookupCollectionName() {
204        return lookupCollectionName;
205    }
206
207    /**
208     * Setter for the name of the collection that should be populated with lookup results
209     *
210     * @param lookupCollectionName
211     */
212    public void setLookupCollectionName(String lookupCollectionName) {
213        this.lookupCollectionName = lookupCollectionName;
214    }
215
216    public Map<String, String> getCriteriaFields() {
217        return this.criteriaFields;
218    }
219
220    public void setCriteriaFields(Map<String, String> criteriaFields) {
221        this.criteriaFields = criteriaFields;
222    }
223
224    public Map<String, String> getFieldConversions() {
225        return this.fieldConversions;
226    }
227
228    public void setFieldConversions(Map<String, String> fieldConversions) {
229        this.fieldConversions = fieldConversions;
230    }
231
232    public Collection<?> getSearchResults() {
233        return this.searchResults;
234    }
235
236    public void setSearchResults(Collection<?> searchResults) {
237        this.searchResults = searchResults;
238    }
239
240    public boolean isAtLeastOneRowReturnable() {
241        return atLeastOneRowReturnable;
242    }
243
244    public void setAtLeastOneRowReturnable(boolean atLeastOneRowReturnable) {
245        this.atLeastOneRowReturnable = atLeastOneRowReturnable;
246    }
247
248    public boolean isAtLeastOneRowHasActions() {
249        return atLeastOneRowHasActions;
250    }
251
252    public void setAtLeastOneRowHasActions(boolean atLeastOneRowHasActions) {
253        this.atLeastOneRowHasActions = atLeastOneRowHasActions;
254    }
255
256    /**
257     * Indicates whether the requested was redirected from the lookup framework due to an external object
258     * request. This prevents the framework from performing another redirect check
259     *
260     * @return boolean true if request was a redirect, false if not
261     */
262    public boolean isRedirectedLookup() {
263        return redirectedLookup;
264    }
265
266    /**
267     * Setter for the redirected request indicator
268     *
269     * @param redirectedLookup
270     */
271    public void setRedirectedLookup(boolean redirectedLookup) {
272        this.redirectedLookup = redirectedLookup;
273    }
274}