001/**
002 * Copyright 2005-2018 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.uif.util;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.datadictionary.parse.BeanTag;
020import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
021import org.kuali.rice.krad.datadictionary.uif.UifDictionaryBeanBase;
022import org.kuali.rice.krad.uif.UifConstants;
023import org.kuali.rice.krad.util.KRADUtils;
024
025import java.io.Serializable;
026import java.util.HashMap;
027import java.util.Map;
028
029/**
030 * This object represents a url in the Krad framework.  The url can be set explicitly to a specific href or a
031 * controller
032 * plus a viewId can be provided (at very minimum).  By default, the krad base bean config points the baseUrl property
033 * to 'krad.url' configuration property and the methodToCall to 'start', but these can be reset to any value as needed.
034 *
035 * <p>
036 * If href is not set, the generated value of href is constructed (in general) as follows:<br/>
037 * baseUrl + /controllerMapping + ? + methodToCall param + viewId param + other parameters
038 * <br/>
039 * with any necessary tokens to construct a valid url.  If baseUrl is not provided, the url is not valid and a
040 * blank string is returned.
041 * </p>
042 *
043 * @author Kuali Rice Team (rice.collab@kuali.org)
044 */
045@BeanTag(name = "url", parent = "Uif-Url")
046public class UrlInfo extends UifDictionaryBeanBase implements Serializable {
047
048    private static final long serialVersionUID = 3195177614468120958L;
049
050    private String href;
051    private String originalHref;
052    private String baseUrl;
053    private String controllerMapping;
054    private String viewType;
055    private String viewId;
056    private String pageId;
057    private String formKey;
058    private String methodToCall;
059    private Map<String, String> requestParameters;
060
061    /**
062     * Base constructor
063     */
064    public UrlInfo() {}
065
066    /**
067     * Constructor that initializes an href value
068     *
069     * @param href the href value
070     */
071    public UrlInfo(String href) {
072        this.href = href;
073        this.originalHref = href;
074    }
075
076    /**
077     * Constructor that sets the base url construction properties
078     *
079     * @param baseUrl the baseUrl
080     * @param controllerMapping the controllerMapping
081     * @param viewId the id of the view
082     * @param methodToCall the methodToCall
083     */
084    public UrlInfo(String baseUrl, String controllerMapping, String viewId, String methodToCall) {
085        this.baseUrl = baseUrl;
086        this.controllerMapping = controllerMapping;
087        this.viewId = viewId;
088        this.methodToCall = methodToCall;
089    }
090
091    public boolean isFullyConfigured() {
092        boolean fullyConfigured = false;
093
094        if (StringUtils.isNotBlank(href)) {
095            fullyConfigured = true;
096        }
097        else if (StringUtils.isNotBlank(baseUrl) && StringUtils.isNotBlank(controllerMapping)) {
098            fullyConfigured = true;
099        }
100
101        return fullyConfigured;
102    }
103
104    /**
105     * Generate the url based on properties of this object
106     *
107     * @return the generatedUrl, blank if not a valid url (no baseUrl value provided)
108     */
109    protected String generateUrl() {
110        String generatedUrl = "";
111
112        if (StringUtils.isBlank(baseUrl)) {
113            return generatedUrl;
114        }
115
116        generatedUrl = baseUrl;
117
118        if (StringUtils.isNotBlank(controllerMapping) && !controllerMapping.startsWith("/")) {
119            generatedUrl = generatedUrl + "/" + controllerMapping;
120        } else if (StringUtils.isNotBlank(controllerMapping)) {
121            generatedUrl = generatedUrl + controllerMapping;
122        }
123
124        Map<String, String> allRequestParameters = new HashMap<String, String>();
125
126        if (StringUtils.isNotBlank(methodToCall)) {
127            allRequestParameters.put(UifConstants.CONTROLLER_METHOD_DISPATCH_PARAMETER_NAME, methodToCall);
128        }
129
130        if (StringUtils.isNotBlank(viewId)) {
131            allRequestParameters.put(UifConstants.UrlParams.VIEW_ID, viewId);
132        }
133
134        if (StringUtils.isNotBlank(pageId)) {
135            allRequestParameters.put(UifConstants.UrlParams.PAGE_ID, pageId);
136        }
137
138        if (StringUtils.isNotBlank(formKey)) {
139            allRequestParameters.put(UifConstants.UrlParams.FORM_KEY, formKey);
140        }
141
142        if (requestParameters != null) {
143            allRequestParameters.putAll(requestParameters);
144        }
145
146        //add the request parameters
147        generatedUrl = generatedUrl + KRADUtils.getRequestStringFromMap(allRequestParameters);
148
149        return generatedUrl;
150    }
151
152    /**
153     * Get the href value for this url object.  This is the main call to this url object as it provides the full href
154     * value represented by this object.
155     *
156     * <p>
157     * If href has NOT been explicitly set to a value, the href is generated by
158     * constructing pieces of the url set through the properties of this url object.
159     * The generated value of href is constructed (in general) as follows:<br/>
160     * baseUrl + /controllerMapping + ? + methodToCall param + viewId param + other parameters
161     * <br/>
162     * with any necessary tokens to construct a valid url.  If baseUrl is not provided, the url is not valid and a
163     * blank string is returned.
164     * </p>
165     *
166     * @return THE href represented by this url object, or blank if not valid
167     */
168    @BeanTagAttribute
169    public String getHref() {
170        if (StringUtils.isBlank(this.href)) {
171            this.href = generateUrl();
172        }
173
174        return href;
175    }
176
177    /**
178     * Explicitly set the href value - if this is called with a value, all other properties of the url object are
179     * ignored.  This call is basically a full override.  This also sets the orginalHref value.
180     *
181     * @param href
182     */
183    public void setHref(String href) {
184        this.href = href;
185        this.originalHref = href;
186    }
187
188    /**
189     * The base url value (the value that comes before other properties).  Default base bean value is set to use
190     * 'krad.url' of the configuration properties.
191     *
192     * @return the baseUrl
193     */
194    @BeanTagAttribute
195    public String getBaseUrl() {
196        return baseUrl;
197    }
198
199    /**
200     * Set the baseUrl
201     *
202     * @param baseUrl
203     */
204    public void setBaseUrl(String baseUrl) {
205        this.baseUrl = baseUrl;
206    }
207
208    /**
209     * The controllerMapping for the url (string that represents the controllerMapping path appended to baseUrl)
210     *
211     * @return the controllerMapping string
212     */
213    @BeanTagAttribute
214    public String getControllerMapping() {
215        return controllerMapping;
216    }
217
218    /**
219     * Set the controllerMapping
220     *
221     * @param controllerMapping
222     */
223    public void setControllerMapping(String controllerMapping) {
224        this.controllerMapping = controllerMapping;
225    }
226
227    /**
228     * The viewType representing the View's base type
229     *
230     * @return the viewType
231     */
232    @BeanTagAttribute
233    public String getViewType() {
234        return viewType;
235    }
236
237    /**
238     * Set the viewType
239     *
240     * @param viewType
241     */
242    public void setViewType(String viewType) {
243        this.viewType = viewType;
244    }
245
246    /**
247     * ViewId representing the view by id to retrieve
248     *
249     * @return the viewId
250     */
251    @BeanTagAttribute
252    public String getViewId() {
253        return viewId;
254    }
255
256    /**
257     * Set viewId
258     *
259     * @param viewId
260     */
261    public void setViewId(String viewId) {
262        this.viewId = viewId;
263    }
264
265    /**
266     * PageId representing the page of the view to retrieve by id
267     *
268     * @return the pageId
269     */
270    @BeanTagAttribute
271    public String getPageId() {
272        return pageId;
273    }
274
275    /**
276     * Set pageId
277     *
278     * @param pageId
279     */
280    public void setPageId(String pageId) {
281        this.pageId = pageId;
282    }
283
284    /**
285     * FormKey representing the key of the form data to retrieve
286     *
287     * @return the formKey
288     */
289    @BeanTagAttribute
290    public String getFormKey() {
291        return formKey;
292    }
293
294    /**
295     * Set the formKey
296     *
297     * @param formKey
298     */
299    public void setFormKey(String formKey) {
300        this.formKey = formKey;
301    }
302
303    /**
304     * MethodToCall representing the methodToCall on the controller (default base bean value is 'start')
305     *
306     * @return methodToCall on controller
307     */
308    @BeanTagAttribute
309    public String getMethodToCall() {
310        return methodToCall;
311    }
312
313    /**
314     * Set the methodToCall
315     *
316     * @param methodToCall
317     */
318    public void setMethodToCall(String methodToCall) {
319        this.methodToCall = methodToCall;
320    }
321
322    /**
323     * Map of key value pairs that will be appended to the request parameters to pass in any custom data
324     *
325     * @return the requestParameters map
326     */
327    @BeanTagAttribute
328    public Map<String, String> getRequestParameters() {
329        return requestParameters;
330    }
331
332    /**
333     * Set the requestParameters
334     *
335     * @param requestParameters
336     */
337    public void setRequestParameters(Map<String, String> requestParameters) {
338        this.requestParameters = requestParameters;
339    }
340
341    /**
342     * The original(set) href value.  This is generally used to determine if the href was explicitly set and not
343     * generated by this url object.
344     *
345     * @return the original(set) href value
346     */
347    public String getOriginalHref() {
348        return originalHref;
349    }
350
351    /**
352     * toString returns the original href value of url
353     *
354     * @param originalHref original href value
355     */
356    protected void setOriginalHref(String originalHref) {
357        this.originalHref = originalHref;
358    }
359
360    /**
361     * toString override returns the href value of url
362     *
363     * @return href value
364     */
365    @Override
366    public String toString() {
367        return this.getHref();
368    }
369
370}