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