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.control;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.kim.api.group.Group;
23  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
25  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
26  import org.kuali.rice.krad.uif.field.InputField;
27  import org.kuali.rice.krad.uif.util.ComponentFactory;
28  import org.kuali.rice.krad.uif.util.LifecycleElement;
29  import org.kuali.rice.krad.uif.widget.QuickFinder;
30  
31  /**
32   * Represents a group control, which is a special control to handle
33   * the input of a KIM Group by group name
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  @BeanTag(name = "kimGroupControl-bean", parent = "Uif-KimGroupControl")
38  public class GroupControl extends TextControlBase implements FilterableLookupCriteriaControl {
39      private static final long serialVersionUID = 5598459655735440981L;
40  
41      private String namespaceCodePropertyName;
42      private String groupIdPropertyName;
43  
44      public GroupControl() {
45          super();
46      }
47  
48      @Override
49      public void performApplyModel(Object model, LifecycleElement parent) {
50          super.performApplyModel(model, parent);
51  
52          if (!(parent instanceof InputField)) {
53              return;
54          }
55  
56          InputField field = (InputField) parent;
57  
58          if (StringUtils.isNotBlank(groupIdPropertyName)) {
59              field.getAdditionalHiddenPropertyNames().add(groupIdPropertyName);
60          }
61  
62          buildGroupQuickfinder(model, field);
63      }
64  
65      protected void buildGroupQuickfinder(Object model, InputField field) {
66          QuickFinder quickFinder = field.getQuickfinder();
67  
68          // don't build quickfinder if explicitly turned off
69          if (!field.isEnableAutoQuickfinder()) {
70              return;
71          }
72  
73          if (quickFinder == null) {
74              quickFinder = ComponentFactory.getQuickFinder();
75              field.setQuickfinder(quickFinder);
76          }
77  
78          if (field.getQuickfinder() != null) {
79              if (StringUtils.isBlank(field.getQuickfinder().getDataObjectClassName())) {
80                  field.getQuickfinder().setDataObjectClassName("org.kuali.rice.kim.impl.group.GroupBo");
81              }
82  
83              if (field.getQuickfinder().getFieldConversions().isEmpty()) {
84                  if (StringUtils.isNotBlank(groupIdPropertyName)) {
85                      field.getQuickfinder().getFieldConversions().put("id", groupIdPropertyName);
86                  }
87  
88                  field.getQuickfinder().getFieldConversions().put("name", field.getPropertyName());
89  
90                  if (StringUtils.isNotBlank(namespaceCodePropertyName)) {
91                      field.getQuickfinder().getFieldConversions().put("namespaceCode", namespaceCodePropertyName);
92                  }
93              }
94  
95              if (field.getQuickfinder().getLookupParameters().isEmpty()) {
96                  if (StringUtils.isNotBlank(namespaceCodePropertyName)) {
97                      field.getQuickfinder().getLookupParameters().put(namespaceCodePropertyName, "namespaceCode");
98                  }
99              }
100         }
101     }
102 
103     /**
104      * The name of the property on the parent object that holds the group namespace
105      *
106      * @return namespaceCodePropertyName
107      */
108     @BeanTagAttribute(name="namespaceCodePropertyName")
109     public String getNamespaceCodePropertyName() {
110         return namespaceCodePropertyName;
111     }
112 
113     /**
114      * Setter for the name of the property on the parent object that holds the group namespace
115      *
116      * @param namespaceCodePropertyName
117      */
118     public void setNamespaceCodePropertyName(String namespaceCodePropertyName) {
119         this.namespaceCodePropertyName = namespaceCodePropertyName;
120     }
121 
122     /**
123      * The name of the property on the parent object that holds the group id
124      *
125      * @return groupIdPropertyName
126      */
127     @BeanTagAttribute(name="groupIdPropertyName")
128     public String getGroupIdPropertyName() {
129         return groupIdPropertyName;
130     }
131 
132     /**
133      * Setter for the name of the property on the parent object that holds the group id
134      *
135      * @param groupIdPropertyName
136      */
137     public void setGroupIdPropertyName(String groupIdPropertyName) {
138         this.groupIdPropertyName = groupIdPropertyName;
139     }
140 
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public Map<String, String> filterSearchCriteria(String propertyName, Map<String, String> searchCriteria) {
146         Map<String, String> filteredSearchCriteria = new HashMap<String, String>(searchCriteria);
147 
148         // check valid groupId
149         // ToDo: move the groupId check and setting to the validation stage.  At that point
150         //       an error should be displayed to the user that the group name and namespace is invalid.
151         String groupName = searchCriteria.get(propertyName);
152         String groupNamespaceCd = searchCriteria.get(namespaceCodePropertyName);
153         if (StringUtils.isNotBlank(groupName) && StringUtils.isNotBlank(groupNamespaceCd)) {
154             Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(groupNamespaceCd,groupName);
155             if( group == null) {
156                 return null;
157             } else {
158                 filteredSearchCriteria.put(groupIdPropertyName, group.getId());
159             }
160         }
161 
162         // filter
163         filteredSearchCriteria.remove(propertyName);
164         filteredSearchCriteria.remove(namespaceCodePropertyName);
165 
166         return filteredSearchCriteria;
167     }
168 }