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