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.control;
17  
18  import java.io.Serializable;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.kim.api.group.Group;
24  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
26  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
27  import org.kuali.rice.krad.uif.field.InputField;
28  import org.kuali.rice.krad.uif.util.ComponentFactory;
29  import org.kuali.rice.krad.uif.util.LifecycleElement;
30  import org.kuali.rice.krad.uif.widget.QuickFinder;
31  
32  /**
33   * Represents a group control, which is a special control to handle
34   * the input of a KIM Group by group name
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  @BeanTag(name = "kimGroupControl", parent = "Uif-KimGroupControl")
39  public class GroupControl extends TextControlBase implements FilterableLookupCriteriaControl {
40      private static final long serialVersionUID = 5598459655735440981L;
41  
42      private String namespaceCodePropertyName;
43      private String groupIdPropertyName;
44  
45      public GroupControl() {
46          super();
47      }
48  
49      @Override
50      public void performApplyModel(Object model, LifecycleElement parent) {
51          super.performApplyModel(model, parent);
52  
53          if (!(parent instanceof InputField)) {
54              return;
55          }
56  
57          InputField field = (InputField) parent;
58  
59          if (StringUtils.isNotBlank(groupIdPropertyName)) {
60              field.getAdditionalHiddenPropertyNames().add(groupIdPropertyName);
61          }
62  
63          buildGroupQuickfinder(model, field);
64      }
65  
66      protected void buildGroupQuickfinder(Object model, InputField field) {
67          QuickFinder quickFinder = field.getQuickfinder();
68  
69          // don't build quickfinder if explicitly turned off
70          if (!field.isEnableAutoQuickfinder()) {
71              return;
72          }
73  
74          if (quickFinder == null) {
75              quickFinder = ComponentFactory.getQuickFinder();
76              field.setQuickfinder(quickFinder);
77          }
78  
79          if (field.getQuickfinder() != null) {
80              if (StringUtils.isBlank(field.getQuickfinder().getDataObjectClassName())) {
81                  field.getQuickfinder().setDataObjectClassName("org.kuali.rice.kim.impl.group.GroupBo");
82              }
83  
84              if (field.getQuickfinder().getFieldConversions().isEmpty()) {
85                  if (StringUtils.isNotBlank(groupIdPropertyName)) {
86                      field.getQuickfinder().getFieldConversions().put("id", groupIdPropertyName);
87                  }
88  
89                  field.getQuickfinder().getFieldConversions().put("name", field.getPropertyName());
90  
91                  if (StringUtils.isNotBlank(namespaceCodePropertyName)) {
92                      field.getQuickfinder().getFieldConversions().put("namespaceCode", namespaceCodePropertyName);
93                  }
94              }
95  
96              if (field.getQuickfinder().getLookupParameters().isEmpty()) {
97                  if (StringUtils.isNotBlank(namespaceCodePropertyName)) {
98                      field.getQuickfinder().getLookupParameters().put(namespaceCodePropertyName, "namespaceCode");
99                  }
100             }
101         }
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public Map<String, String> filterSearchCriteria(String propertyName, Map<String, String> searchCriteria, FilterableLookupCriteriaControlPostData postData) {
109         Map<String, String> filteredSearchCriteria = new HashMap<String, String>(searchCriteria);
110 
111         GroupControlPostData groupControlPostData = (GroupControlPostData) postData;
112 
113         // check valid groupId
114         // ToDo: move the groupId check and setting to the validation stage.  At that point
115         //       an error should be displayed to the user that the group name and namespace is invalid.
116         String groupName = searchCriteria.get(propertyName);
117         String groupNamespaceCd = searchCriteria.get(groupControlPostData.getNamespaceCodePropertyName());
118         if (StringUtils.isNotBlank(groupName) && StringUtils.isNotBlank(groupNamespaceCd)) {
119             Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(groupNamespaceCd,groupName);
120             if( group == null) {
121                 return null;
122             } else {
123                 filteredSearchCriteria.put(groupControlPostData.getGroupIdPropertyName(), group.getId());
124             }
125         }
126 
127         // filter
128         filteredSearchCriteria.remove(propertyName);
129         filteredSearchCriteria.remove(groupControlPostData.getNamespaceCodePropertyName());
130 
131         return filteredSearchCriteria;
132     }
133 
134     /**
135      * The name of the property on the parent object that holds the group namespace
136      *
137      * @return namespaceCodePropertyName
138      */
139     @BeanTagAttribute
140     public String getNamespaceCodePropertyName() {
141         return namespaceCodePropertyName;
142     }
143 
144     /**
145      * Setter for the name of the property on the parent object that holds the group namespace
146      *
147      * @param namespaceCodePropertyName
148      */
149     public void setNamespaceCodePropertyName(String namespaceCodePropertyName) {
150         this.namespaceCodePropertyName = namespaceCodePropertyName;
151     }
152 
153     /**
154      * The name of the property on the parent object that holds the group id
155      *
156      * @return groupIdPropertyName
157      */
158     @BeanTagAttribute
159     public String getGroupIdPropertyName() {
160         return groupIdPropertyName;
161     }
162 
163     /**
164      * Setter for the name of the property on the parent object that holds the group id
165      *
166      * @param groupIdPropertyName
167      */
168     public void setGroupIdPropertyName(String groupIdPropertyName) {
169         this.groupIdPropertyName = groupIdPropertyName;
170     }
171 
172     /**
173      * {@inheritDoc}
174      */
175     @Override
176     public GroupControlPostData getPostData(String propertyName) {
177         return new GroupControlPostData(propertyName, this);
178     }
179 
180     /**
181      * Holds post data for the {@link GroupControl}.
182      */
183     public static class GroupControlPostData implements FilterableLookupCriteriaControlPostData, Serializable {
184 
185         private static final long serialVersionUID = -1859777965985379673L;
186 
187         private String propertyName;
188 
189         private String namespaceCodePropertyName;
190         private String groupIdPropertyName;
191 
192         /**
193          * Constructs the post data from the property name and the {@link GroupControl}.
194          *
195          * @param propertyName the name of the property to filter
196          * @param groupControl the control to pull data from
197          */
198         public GroupControlPostData(String propertyName, GroupControl groupControl) {
199             this.propertyName = propertyName;
200             this.namespaceCodePropertyName = groupControl.getNamespaceCodePropertyName();
201             this.groupIdPropertyName = groupControl.getGroupIdPropertyName();
202         }
203 
204         /**
205          * {@inheritDoc}
206          */
207         @Override
208         public Class<? extends FilterableLookupCriteriaControl> getControlClass() {
209             return GroupControl.class;
210         }
211 
212         /**
213          * {@inheritDoc}
214          */
215         @Override
216         public String getPropertyName() {
217             return propertyName;
218         }
219 
220         /**
221          * @see GroupControl#getNamespaceCodePropertyName()
222          */
223         public String getNamespaceCodePropertyName() {
224             return namespaceCodePropertyName;
225         }
226 
227         /**
228          * @see GroupControl#getGroupIdPropertyName()
229          */
230         public String getGroupIdPropertyName() {
231             return groupIdPropertyName;
232         }
233 
234     }
235 
236 }