View Javadoc

1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.widget;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21  import org.kuali.rice.krad.uif.component.Component;
22  import org.kuali.rice.krad.uif.util.ScriptUtils;
23  import org.kuali.rice.krad.uif.view.View;
24  import org.kuali.rice.krad.util.KRADUtils;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  /**
30   * LocationSuggest widget for providing suggestions that represent locations.  When the suggestion is clicked, the
31   * navigation occurs immediately.
32   */
33  @BeanTag(name = "locationSuggest-bean", parent = "Uif-LocationSuggest")
34  public class LocationSuggest extends Suggest {
35  
36      private static final long serialVersionUID = 5940714417896326889L;
37      private String baseUrl;
38      private String additionalUrlPathPropertyName;
39      private String hrefPropertyName;
40      private String objectIdPropertyName;
41      private Map<String, String> requestParameterPropertyNames;
42      private Map<String, String> additionalRequestParameters;
43  
44      /**
45       * Process the objectIdPropertyName, if set
46       *
47       * @see Component#performFinalize(org.kuali.rice.krad.uif.view.View, Object, org.kuali.rice.krad.uif.component.Component)
48       */
49      @Override
50      public void performFinalize(View view, Object model, Component parent) {
51          super.performFinalize(view, model, parent);
52  
53          if (requestParameterPropertyNames == null) {
54              requestParameterPropertyNames = new HashMap<String, String>();
55          }
56  
57          if (StringUtils.isNotBlank(objectIdPropertyName)) {
58              requestParameterPropertyNames.put(objectIdPropertyName, objectIdPropertyName);
59          }
60      }
61  
62      /**
63       * BaseUrl for the suggestions.  Unless the suggestion contains an href, baseUrl + additionalUrlPath value +
64       * request parameters is used to generate the url.
65       *
66       * @return the baseUrl
67       */
68      @BeanTagAttribute(name = "baseUrl")
69      public String getBaseUrl() {
70          return baseUrl;
71      }
72  
73      /**
74       * Set the baseUrl
75       *
76       * @param baseUrl
77       */
78      public void setBaseUrl(String baseUrl) {
79          this.baseUrl = baseUrl;
80      }
81  
82      /**
83       * AdditionalUrlPathProperty specifies the property on the retrieved suggestion result that contains a url
84       * appendage
85       * to be appended to the baseUrl when this selection is chosen.
86       *
87       * <p>One use case for setting this is to retrieve a controllerMapping that changes based on selection.  Note:
88       * for suggestions that all point to the same controllerMapping, simply set it as part of the baseUrl.</p>
89       *
90       * @return the additionalUrlPathPropertyName
91       */
92      @BeanTagAttribute(name = "additionalUrlPropertyName")
93      public String getAdditionalUrlPathPropertyName() {
94          return additionalUrlPathPropertyName;
95      }
96  
97      /**
98       * Set additionalUrlPathProperty
99       *
100      * @param additionalUrlPathPropertyName
101      */
102     public void setAdditionalUrlPathPropertyName(String additionalUrlPathPropertyName) {
103         this.additionalUrlPathPropertyName = additionalUrlPathPropertyName;
104     }
105 
106     /**
107      * The hrefPropertyName specifies the property on the retrieved suggestion result that contains the href
108      * value (full url).
109      *
110      * <p>This property must contain a full url if it exists on the object.  If this property name is matched on
111      * the suggestion result, it takes precedence over any other settings set on this locationSuggest
112      * and is used as the navigation url.  If the property name does not exist on the object, the suggest will fall
113      * back to building the url dynamically with baseUrl.</p>
114      *
115      * @return the hrefPropertyName
116      */
117     @BeanTagAttribute(name = "hrefPropertyName")
118     public String getHrefPropertyName() {
119         return hrefPropertyName;
120     }
121 
122     /**
123      * Set the hrefPropertyName
124      *
125      * @param hrefPropertyName
126      */
127     public void setHrefPropertyName(String hrefPropertyName) {
128         this.hrefPropertyName = hrefPropertyName;
129     }
130 
131     /**
132      * The objectIdPropertyName that represents the key for getting the object as a request parameter.  The property
133      * will be added to the request parameters by the name given with the value pulled from the result object.
134      *
135      * <p>
136      *     This convenience method is essentially equivalent to having a property by objectIdPropertyName as a
137      *     key and value in the requestParameterPropertyNames.
138      * </p>
139      *
140      * @return the objectIdPropertyName which represents which property is the "key" of the object
141      */
142     public String getObjectIdPropertyName() {
143         return objectIdPropertyName;
144     }
145 
146     /**
147      * Set the objectIdPropertyName
148      *
149      * @param objectIdPropertyName
150      */
151     public void setObjectIdPropertyName(String objectIdPropertyName) {
152         this.objectIdPropertyName = objectIdPropertyName;
153     }
154 
155     /**
156      * RequestParameterPropertyNames specify the properties that should be included in the request parameters.
157      *
158      * <p>The key is used as the key of the request parameter and the value is used as the property name to look for in
159      * the suggestion result object.  If the property name specified exists on the result object, the request
160      * parameter in the url will appear as key=propertyValue in the request parameters.</p>
161      *
162      * @return the RequestParameterPropertyNames map with key and property names
163      */
164     @BeanTagAttribute(name = "requestParameterPropertyNames", type = BeanTagAttribute.AttributeType.MAPVALUE)
165     public Map<String, String> getRequestParameterPropertyNames() {
166         return requestParameterPropertyNames;
167     }
168 
169     /**
170      * Set the requestParameterPropertyNames
171      *
172      * @param requestParameterPropertyNames
173      */
174     public void setRequestParameterPropertyNames(Map<String, String> requestParameterPropertyNames) {
175         this.requestParameterPropertyNames = requestParameterPropertyNames;
176     }
177 
178     /**
179      * AdditionalRequestParameters specify the static(constant) request parameters that should be appended to the url.
180      *
181      * <p>The key represents the key of the request parameter and the value represents the value of the
182      * request parameter.  This will be used on each suggestion which uses a generated url (using baseUrl
183      * construction).
184      * </p>
185      *
186      * @return
187      */
188     @BeanTagAttribute(name = "additionalRequestParameters", type = BeanTagAttribute.AttributeType.MAPVALUE)
189     public Map<String, String> getAdditionalRequestParameters() {
190         return additionalRequestParameters;
191     }
192 
193     /**
194      * Get the additionalRequestParameters
195      *
196      * @param additionalRequestParameters
197      */
198     public void setAdditionalRequestParameters(Map<String, String> additionalRequestParameters) {
199         this.additionalRequestParameters = additionalRequestParameters;
200     }
201 
202     /**
203      * Gets an object translated to js for the requestParameterPropertyNames.  Used to construct the url on the client.
204      *
205      * @return the requestParameterPropertyNames js map object
206      */
207     public String getRequestParameterPropertyNameJsObject() {
208         if (requestParameterPropertyNames != null && !requestParameterPropertyNames.isEmpty()) {
209             return ScriptUtils.translateValue(requestParameterPropertyNames);
210         } else {
211             return "{}";
212         }
213     }
214 
215     /**
216      * Gets the constant additionalRequestParameters as a request string value to use as part of the url
217      *
218      * @return the request parameter string for additionalRequestParameters
219      */
220     public String getAdditionalRequestParameterString() {
221         if (additionalRequestParameters != null) {
222             return KRADUtils.getRequestStringFromMap(additionalRequestParameters);
223         } else {
224             return "";
225         }
226     }
227 }