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.kew.rule.bo;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.util.RiceKeyConstants;
20  import org.kuali.rice.kew.api.KewApiServiceLocator;
21  import org.kuali.rice.kew.api.rule.RuleTemplate;
22  import org.kuali.rice.kew.api.rule.RuleTemplateAttribute;
23  import org.kuali.rice.kew.rule.WorkflowRuleAttributeRows;
24  import org.kuali.rice.kew.rule.service.RuleTemplateService;
25  import org.kuali.rice.kew.service.KEWServiceLocator;
26  import org.kuali.rice.kim.api.group.Group;
27  import org.kuali.rice.kim.api.group.GroupService;
28  import org.kuali.rice.kim.api.identity.Person;
29  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
31  import org.kuali.rice.kns.web.ui.Column;
32  import org.kuali.rice.kns.web.ui.Field;
33  import org.kuali.rice.kns.web.ui.Row;
34  import org.kuali.rice.krad.exception.ValidationException;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  
37  import java.util.ArrayList;
38  import java.util.Iterator;
39  import java.util.List;
40  import java.util.Map;
41  
42  /**
43   *
44   * <p>
45   * Common code used by both the RuleBaseValuesLookupableHelperServiceImpl and the
46   * RuleDelegationLookupableHelperServiceImpl
47   * </p>
48   *
49   * @author Kuali Rice Team (rice.collab@kuali.org)
50   */
51  public class AbstractRuleLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
52  
53      private List<Row> rows = new ArrayList<Row>();
54      protected static final String GROUP_REVIEWER_PROPERTY_NAME = "groupReviewer";
55      protected static final String GROUP_REVIEWER_NAME_PROPERTY_NAME = "groupReviewerName";
56      protected static final String GROUP_REVIEWER_NAMESPACE_PROPERTY_NAME = "groupReviewerNamespace";
57      protected static final String PERSON_REVIEWER_PROPERTY_NAME = "personReviewer";
58      protected static final String PERSON_REVIEWER_TYPE_PROPERTY_NAME = "personReviewerType";
59  
60      protected static final String BACK_LOCATION = "backLocation";
61      protected static final String DOC_FORM_KEY = "docFormKey";
62      protected static final String INVALID_WORKGROUP_ERROR = "The Group Reviewer Namespace and Name combination is not valid";
63      protected static final String INVALID_PERSON_ERROR = "The Person Reviewer is not valid";
64  
65      protected boolean checkForAdditionalFields(Map<String, String> fieldValues, String ruleTemplateNameParam) {
66          if (StringUtils.isNotBlank(ruleTemplateNameParam)) {
67              rows = new ArrayList<Row>();
68              RuleTemplate ruleTemplate = KewApiServiceLocator.getRuleService().getRuleTemplateByName(ruleTemplateNameParam);
69              for (RuleTemplateAttribute ruleTemplateAttribute : ruleTemplate.getActiveRuleTemplateAttributes()) {
70                  if (!RuleAttribute.isWorkflowAttribute(ruleTemplateAttribute.getRuleAttribute().getType())) {
71                      continue;
72  
73                  }
74  
75                  // run through the attributes fields once to populate field values we have to do this
76                  // to allow rows dependent on another row value to populate correctly in the loop below
77                  populateFieldsHelperMethod(fieldValues, ruleTemplateAttribute, false);
78  
79                  // now run through a second time with our shiny new field values
80                  // ...by the way, just trying to preserve behavior from Rice 1.0.x here...generally speaking, this stuff is crazy!!!
81                  populateFieldsHelperMethod(fieldValues, ruleTemplateAttribute, true);
82  
83              }
84  
85              return true;
86  
87          }
88  
89          rows.clear();
90  
91          return false;
92      }
93  
94      private void populateFieldsHelperMethod(Map<String, String> fieldValues,
95              RuleTemplateAttribute ruleTemplateAttribute, boolean setAndAddValuesToRow) {
96  
97          WorkflowRuleAttributeRows workflowRuleAttributeRows =
98                  KEWServiceLocator.getWorkflowRuleAttributeMediator().getSearchRows(fieldValues, ruleTemplateAttribute);
99          for (Row row : workflowRuleAttributeRows.getRows()) {
100             List<Field> fields = new ArrayList<Field>();
101             for (Iterator<Field> iterator2 = row.getFields().iterator(); iterator2.hasNext(); ) {
102                 Field field = iterator2.next();
103                 if (fieldValues.get(field.getPropertyName()) != null) {
104                     field.setPropertyValue(fieldValues.get(field.getPropertyName()));
105                 }
106 
107                 fields.add(field);
108                 fieldValues.put(field.getPropertyName(), field.getPropertyValue());
109             }
110 
111             if (setAndAddValuesToRow) {
112                 row.setFields(fields);
113                 rows.add(row);
114             }
115         }
116     }
117 
118     @Override
119     public List<Row> getRows() {
120         if (rows.size()==0) {
121             rows.addAll(super.getRows());
122         }
123         List<Row> returnRows = new ArrayList<Row>();
124         returnRows.addAll(rows);
125 
126         return returnRows;
127     }
128 
129     protected void clearRows() {
130         rows.clear();
131     }
132 
133     @Override
134     public void validateSearchParameters(Map<String, String> fieldValues) {
135         super.validateSearchParameters(fieldValues);
136 
137         // make sure that if we have either groupName or Namespace, that both are filled in
138         String groupName = (String)fieldValues.get(GROUP_REVIEWER_NAME_PROPERTY_NAME);
139         String groupNamespace = (String)fieldValues.get(GROUP_REVIEWER_NAMESPACE_PROPERTY_NAME);
140         String principalName = (String)fieldValues.get(PERSON_REVIEWER_PROPERTY_NAME);
141 
142         if (StringUtils.isEmpty(groupName) && !StringUtils.isEmpty(groupNamespace)) {
143             String attributeLabel = getDataDictionaryService().getAttributeLabel(getBusinessObjectClass(), GROUP_REVIEWER_NAME_PROPERTY_NAME);
144             GlobalVariables.getMessageMap().putError(GROUP_REVIEWER_NAME_PROPERTY_NAME, RiceKeyConstants.ERROR_REQUIRED, attributeLabel);
145         }
146 
147         if  (!StringUtils.isEmpty(groupName) && StringUtils.isEmpty(groupNamespace)) {
148             String attributeLabel = getDataDictionaryService().getAttributeLabel(getBusinessObjectClass(), GROUP_REVIEWER_NAMESPACE_PROPERTY_NAME);
149             GlobalVariables.getMessageMap().putError(GROUP_REVIEWER_NAMESPACE_PROPERTY_NAME, RiceKeyConstants.ERROR_REQUIRED, attributeLabel);
150         }
151 
152         if  (!StringUtils.isEmpty(groupName) && !StringUtils.isEmpty(groupNamespace)) {
153             Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(groupNamespace,
154                     groupName);
155             if (group == null) {
156                 GlobalVariables.getMessageMap().putError(GROUP_REVIEWER_NAME_PROPERTY_NAME, RiceKeyConstants.ERROR_CUSTOM, INVALID_WORKGROUP_ERROR);
157             }
158         }
159 
160         if  (!StringUtils.isEmpty(principalName)) {
161             Person person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName);
162             if (person == null) {
163                 GlobalVariables.getMessageMap().putError(PERSON_REVIEWER_PROPERTY_NAME, RiceKeyConstants.ERROR_CUSTOM, INVALID_PERSON_ERROR);
164             }
165         }
166         if (!GlobalVariables.getMessageMap().hasNoErrors()) {
167             throw new ValidationException("errors in search criteria");
168         }
169     }
170 
171 
172     @Override
173     public List<Column> getColumns() {
174         List<Column> columns = new ArrayList<Column>();
175         for (Row row : this.getRows()) {
176             for (Field field : row.getFields()) {
177                 Column newColumn = new Column();
178                 newColumn.setColumnTitle(field.getFieldLabel());
179                 newColumn.setMaxLength(field.getMaxLength());
180                 newColumn.setPropertyName(field.getPropertyName());
181                 columns.add(newColumn);
182 
183             }
184 
185         }
186 
187         return columns;
188     }
189 
190     protected GroupService getGroupService() {
191         return KimApiServiceLocator.getGroupService();
192     }
193 
194     protected RuleTemplateService getRuleTemplateService() {
195         return (RuleTemplateService) KEWServiceLocator.getService(KEWServiceLocator.RULE_TEMPLATE_SERVICE);
196     }
197 
198 }