View Javadoc

1   /**
2    * Copyright 2005-2013 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.control;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.kim.api.identity.Person;
20  import org.kuali.rice.kim.api.identity.PersonService;
21  import org.kuali.rice.kim.api.identity.principal.Principal;
22  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
24  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
25  import org.kuali.rice.krad.uif.field.InputField;
26  import org.kuali.rice.krad.uif.util.ScriptUtils;
27  import org.kuali.rice.krad.uif.view.View;
28  import org.kuali.rice.krad.uif.component.Component;
29  import org.kuali.rice.krad.uif.component.MethodInvokerConfig;
30  import org.kuali.rice.krad.uif.field.AttributeQuery;
31  import org.kuali.rice.krad.uif.widget.QuickFinder;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * Represents a user control, which is a special control to handle
38   * the input of a Person
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  @BeanTag(name = "kimPersonControl-bean", parent = "Uif-KimPersonControl")
43  public class UserControl extends TextControl implements FilterableLookupCriteriaControl {
44      private static final long serialVersionUID = 7468340793076585869L;
45  
46      private String principalIdPropertyName;
47      private String personNamePropertyName;
48      private String personObjectPropertyName;
49  
50      public UserControl() {
51          super();
52      }
53  
54      /**
55       * @see org.kuali.rice.krad.uif.component.ComponentBase#performApplyModel(org.kuali.rice.krad.uif.view.View,
56       *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
57       */
58      @Override
59      public void performApplyModel(View view, Object model, Component parent) {
60          super.performApplyModel(view, model, parent);
61  
62          if (!(parent instanceof InputField)) {
63              return;
64          }
65  
66          InputField field = (InputField) parent;
67          field.getAdditionalHiddenPropertyNames().add(principalIdPropertyName);
68  
69          if (!field.isReadOnly()) {
70              // add information fields
71              if (StringUtils.isNotBlank(personNamePropertyName)) {
72                  field.getPropertyNamesForAdditionalDisplay().add(personNamePropertyName);
73              } else {
74                  field.getPropertyNamesForAdditionalDisplay().add(personObjectPropertyName + ".name");
75              }
76  
77              // setup script to clear id field when name is modified
78              String idPropertyPath = field.getBindingInfo().getPropertyAdjustedBindingPath(principalIdPropertyName);
79              String onChangeScript = "setValue('" + ScriptUtils.escapeName(idPropertyPath) + "','');";
80  
81              if (StringUtils.isNotBlank(getOnChangeScript())) {
82                  onChangeScript = getOnChangeScript() + onChangeScript;
83              }
84              setOnChangeScript(onChangeScript);
85          }
86  
87          if (field.isReadOnly() && StringUtils.isBlank(field.getReadOnlyDisplaySuffixPropertyName())) {
88              if (StringUtils.isNotBlank(personNamePropertyName)) {
89                  field.setReadOnlyDisplaySuffixPropertyName(personNamePropertyName);
90              } else {
91                  field.setReadOnlyDisplaySuffixPropertyName(personObjectPropertyName + ".name");
92              }
93          }
94  
95          // setup field query for displaying name
96          AttributeQuery attributeQuery = new AttributeQuery();
97  
98          MethodInvokerConfig methodInvokerConfig = new MethodInvokerConfig();
99          PersonService personService = KimApiServiceLocator.getPersonService();
100         methodInvokerConfig.setTargetObject(personService);
101 
102         attributeQuery.setQueryMethodInvokerConfig(methodInvokerConfig);
103         attributeQuery.setQueryMethodToCall("getPersonByPrincipalName");
104         attributeQuery.getQueryMethodArgumentFieldList().add(field.getPropertyName());
105         attributeQuery.getReturnFieldMapping().put("principalId", principalIdPropertyName);
106 
107         if (StringUtils.isNotBlank(personNamePropertyName)) {
108             attributeQuery.getReturnFieldMapping().put("name", personNamePropertyName);
109         } else {
110             attributeQuery.getReturnFieldMapping().put("name", personObjectPropertyName + ".name");
111         }
112         field.setAttributeQuery(attributeQuery);
113 
114         // setup field lookup
115         QuickFinder quickFinder = field.getQuickfinder();
116         if (quickFinder.isRender()) {
117             if (StringUtils.isBlank(quickFinder.getDataObjectClassName())) {
118                 quickFinder.setDataObjectClassName(Person.class.getName());
119             }
120 
121             if (quickFinder.getFieldConversions().isEmpty()) {
122                 quickFinder.getFieldConversions().put("principalId", principalIdPropertyName);
123 
124                 if (StringUtils.isNotBlank(personNamePropertyName)) {
125                     quickFinder.getFieldConversions().put("name", personNamePropertyName);
126                 } else {
127                     quickFinder.getFieldConversions().put("name", personObjectPropertyName + ".name");
128                 }
129 
130                 quickFinder.getFieldConversions().put("principalName", field.getPropertyName());
131             }
132         }
133     }
134 
135     /**
136      * The name of the property on the parent object that holds the principal id
137      *
138      * @return principalIdPropertyName
139      */
140     @BeanTagAttribute(name="principalIdPropertyName")
141     public String getPrincipalIdPropertyName() {
142         return principalIdPropertyName;
143     }
144 
145     /**
146      * Setter for the name of the property on the parent object that holds the principal id
147      *
148      * @param principalIdPropertyName
149      */
150     public void setPrincipalIdPropertyName(String principalIdPropertyName) {
151         this.principalIdPropertyName = principalIdPropertyName;
152     }
153 
154     /**
155      * The name of the property on the parent object that holds the person name
156      *
157      * @return personNamePropertyName
158      */
159     @BeanTagAttribute(name="personNamePropertyName")
160     public String getPersonNamePropertyName() {
161         return personNamePropertyName;
162     }
163 
164     /**
165      * Setter for the name of the property on the parent object that holds the person name
166      *
167      * @param personNamePropertyName
168      */
169     public void setPersonNamePropertyName(String personNamePropertyName) {
170         this.personNamePropertyName = personNamePropertyName;
171     }
172 
173     /**
174      * The name of the property on the parent object that holds the person object
175      *
176      * @return personObjectPropertyName
177      */
178     @BeanTagAttribute(name="personObjectPropertyName")
179     public String getPersonObjectPropertyName() {
180         return personObjectPropertyName;
181     }
182 
183     /**
184      * Setter for the name of the property on the parent object that holds the person object
185      *
186      * @param personObjectPropertyName
187      */
188     public void setPersonObjectPropertyName(String personObjectPropertyName) {
189         this.personObjectPropertyName = personObjectPropertyName;
190     }
191 
192     /**
193      * @see FilterableLookupCriteriaControl#filterSearchCriteria(String, java.util.Map)
194      */
195     @Override
196     public Map<String, String>  filterSearchCriteria(String propertyName, Map<String, String> searchCriteria) {
197         Map<String, String> filteredSearchCriteria = new HashMap<String, String>(searchCriteria);
198 
199         // check valid principalName
200         // ToDo: move the principalId check and setting to the validation stage.  At that point the personName should
201         //       be set as well or an error be displayed to the user that the principalName is invalid.
202         String principalName = searchCriteria.get(propertyName);
203         if (StringUtils.isNotBlank(principalName)) {
204             Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
205             if (principal == null) {
206                 return null;
207             } else {
208                 filteredSearchCriteria.put(principalIdPropertyName, principal.getPrincipalId());
209             }
210         }
211 
212         // filter
213         filteredSearchCriteria.remove(propertyName);
214         filteredSearchCriteria.remove(personNamePropertyName);
215 
216         return filteredSearchCriteria;
217     }
218 }