View Javadoc
1   /**
2    * Copyright 2005-2014 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 java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
23  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
24  import org.kuali.rice.krad.uif.component.Component;
25  import org.kuali.rice.krad.uif.util.LifecycleElement;
26  import org.kuali.rice.krad.uif.util.ScriptUtils;
27  import org.kuali.rice.krad.util.KRADUtils;
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      private static final long serialVersionUID = 5940714417896326889L;
36  
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       * {@inheritDoc}
48       */
49      @Override
50      public void performFinalize(Object model, LifecycleElement parent) {
51          super.performFinalize(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       * {@inheritDoc}
64       */
65      @Override
66      public LocationSuggestPostData getPostData() {
67          return new LocationSuggestPostData(this);
68      }
69  
70      /**
71       * BaseUrl for the suggestions.  Unless the suggestion contains an href, baseUrl + additionalUrlPath value +
72       * request parameters is used to generate the url.
73       *
74       * @return the baseUrl
75       */
76      @BeanTagAttribute(name = "baseUrl")
77      public String getBaseUrl() {
78          return baseUrl;
79      }
80  
81      /**
82       * Set the baseUrl
83       *
84       * @param baseUrl
85       */
86      public void setBaseUrl(String baseUrl) {
87          this.baseUrl = baseUrl;
88      }
89  
90      /**
91       * AdditionalUrlPathProperty specifies the property on the retrieved suggestion result that contains a url
92       * appendage
93       * to be appended to the baseUrl when this selection is chosen.
94       *
95       * <p>One use case for setting this is to retrieve a controllerMapping that changes based on selection.  Note:
96       * for suggestions that all point to the same controllerMapping, simply set it as part of the baseUrl.</p>
97       *
98       * @return the additionalUrlPathPropertyName
99       */
100     @BeanTagAttribute(name = "additionalUrlPropertyName")
101     public String getAdditionalUrlPathPropertyName() {
102         return additionalUrlPathPropertyName;
103     }
104 
105     /**
106      * Set additionalUrlPathProperty
107      *
108      * @param additionalUrlPathPropertyName
109      */
110     public void setAdditionalUrlPathPropertyName(String additionalUrlPathPropertyName) {
111         this.additionalUrlPathPropertyName = additionalUrlPathPropertyName;
112     }
113 
114     /**
115      * The hrefPropertyName specifies the property on the retrieved suggestion result that contains the href
116      * value (full url).
117      *
118      * <p>This property must contain a full url if it exists on the object.  If this property name is matched on
119      * the suggestion result, it takes precedence over any other settings set on this locationSuggest
120      * and is used as the navigation url.  If the property name does not exist on the object, the suggest will fall
121      * back to building the url dynamically with baseUrl.</p>
122      *
123      * @return the hrefPropertyName
124      */
125     @BeanTagAttribute(name = "hrefPropertyName")
126     public String getHrefPropertyName() {
127         return hrefPropertyName;
128     }
129 
130     /**
131      * Set the hrefPropertyName
132      *
133      * @param hrefPropertyName
134      */
135     public void setHrefPropertyName(String hrefPropertyName) {
136         this.hrefPropertyName = hrefPropertyName;
137     }
138 
139     /**
140      * The objectIdPropertyName that represents the key for getting the object as a request parameter.  The property
141      * will be added to the request parameters by the name given with the value pulled from the result object.
142      *
143      * <p>
144      *     This convenience method is essentially equivalent to having a property by objectIdPropertyName as a
145      *     key and value in the requestParameterPropertyNames.
146      * </p>
147      *
148      * @return the objectIdPropertyName which represents which property is the "key" of the object
149      */
150     public String getObjectIdPropertyName() {
151         return objectIdPropertyName;
152     }
153 
154     /**
155      * Set the objectIdPropertyName
156      *
157      * @param objectIdPropertyName
158      */
159     public void setObjectIdPropertyName(String objectIdPropertyName) {
160         this.objectIdPropertyName = objectIdPropertyName;
161     }
162 
163     /**
164      * RequestParameterPropertyNames specify the properties that should be included in the request parameters.
165      *
166      * <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
167      * the suggestion result object.  If the property name specified exists on the result object, the request
168      * parameter in the url will appear as key=propertyValue in the request parameters.</p>
169      *
170      * @return the RequestParameterPropertyNames map with key and property names
171      */
172     @BeanTagAttribute(name = "requestParameterPropertyNames", type = BeanTagAttribute.AttributeType.MAPVALUE)
173     public Map<String, String> getRequestParameterPropertyNames() {
174         return requestParameterPropertyNames;
175     }
176 
177     /**
178      * Set the requestParameterPropertyNames
179      *
180      * @param requestParameterPropertyNames
181      */
182     public void setRequestParameterPropertyNames(Map<String, String> requestParameterPropertyNames) {
183         this.requestParameterPropertyNames = requestParameterPropertyNames;
184     }
185 
186     /**
187      * AdditionalRequestParameters specify the static(constant) request parameters that should be appended to the url.
188      *
189      * <p>The key represents the key of the request parameter and the value represents the value of the
190      * request parameter.  This will be used on each suggestion which uses a generated url (using baseUrl
191      * construction).
192      * </p>
193      *
194      * @return mapping of additional request parameters
195      */
196     @BeanTagAttribute(name = "additionalRequestParameters", type = BeanTagAttribute.AttributeType.MAPVALUE)
197     public Map<String, String> getAdditionalRequestParameters() {
198         return additionalRequestParameters;
199     }
200 
201     /**
202      * Get the additionalRequestParameters
203      *
204      * @param additionalRequestParameters
205      */
206     public void setAdditionalRequestParameters(Map<String, String> additionalRequestParameters) {
207         this.additionalRequestParameters = additionalRequestParameters;
208     }
209 
210     /**
211      * Gets an object translated to js for the requestParameterPropertyNames.  Used to construct the url on the client.
212      *
213      * @return the requestParameterPropertyNames js map object
214      */
215     public String getRequestParameterPropertyNameJsObject() {
216         if (requestParameterPropertyNames != null && !requestParameterPropertyNames.isEmpty()) {
217             return ScriptUtils.translateValue(requestParameterPropertyNames);
218         } else {
219             return "{}";
220         }
221     }
222 
223     /**
224      * Gets the constant additionalRequestParameters as a request string value to use as part of the url
225      *
226      * @return the request parameter string for additionalRequestParameters
227      */
228     public String getAdditionalRequestParameterString() {
229         if (additionalRequestParameters != null) {
230             return KRADUtils.getRequestStringFromMap(additionalRequestParameters);
231         } else {
232             return "";
233         }
234     }
235 
236     /**
237      * Holds post data for the location suggest component.
238      */
239     public static class LocationSuggestPostData extends SuggestPostData {
240         private static final long serialVersionUID = -4326794621463438438L;
241 
242         private String baseUrl;
243         private String additionalUrlPathPropertyName;
244         private String hrefPropertyName;
245         private String objectIdPropertyName;
246         private Map<String, String> requestParameterPropertyNames;
247         private Map<String, String> additionalRequestParameters;
248 
249         /**
250          * Constructor taking suggest widget to pull post data from.
251          *
252          * @param suggest component instance to pull data
253          */
254         public LocationSuggestPostData(LocationSuggest suggest) {
255             super(suggest);
256 
257             this.baseUrl = suggest.getBaseUrl();
258             this.additionalUrlPathPropertyName = suggest.getAdditionalUrlPathPropertyName();
259             this.hrefPropertyName = suggest.getHrefPropertyName();
260             this.objectIdPropertyName = suggest.getObjectIdPropertyName();
261             this.requestParameterPropertyNames = suggest.getRequestParameterPropertyNames();
262             this.additionalRequestParameters = suggest.getAdditionalRequestParameters();
263         }
264 
265         /**
266          * @see LocationSuggest#getBaseUrl()
267          */
268         public String getBaseUrl() {
269             return baseUrl;
270         }
271 
272         /**
273          * @see LocationSuggest#getAdditionalUrlPathPropertyName()
274          */
275         public String getAdditionalUrlPathPropertyName() {
276             return additionalUrlPathPropertyName;
277         }
278 
279         /**
280          * @see LocationSuggest#getHrefPropertyName()
281          */
282         public String getHrefPropertyName() {
283             return hrefPropertyName;
284         }
285 
286         /**
287          * @see LocationSuggest#getObjectIdPropertyName()
288          */
289         public String getObjectIdPropertyName() {
290             return objectIdPropertyName;
291         }
292 
293         /**
294          * @see LocationSuggest#getRequestParameterPropertyNames()
295          */
296         public Map<String, String> getRequestParameterPropertyNames() {
297             return requestParameterPropertyNames;
298         }
299 
300         /**
301          * @see LocationSuggest#getAdditionalRequestParameters()
302          */
303         public Map<String, String> getAdditionalRequestParameters() {
304             return additionalRequestParameters;
305         }
306     }
307 }