001/** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kew.rule.bo; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.core.api.util.RiceKeyConstants; 020import org.kuali.rice.kew.api.KewApiServiceLocator; 021import org.kuali.rice.kew.api.rule.RuleTemplate; 022import org.kuali.rice.kew.api.rule.RuleTemplateAttribute; 023import org.kuali.rice.kew.rule.WorkflowRuleAttributeRows; 024import org.kuali.rice.kew.rule.service.RuleTemplateService; 025import org.kuali.rice.kew.service.KEWServiceLocator; 026import org.kuali.rice.kim.api.group.Group; 027import org.kuali.rice.kim.api.group.GroupService; 028import org.kuali.rice.kim.api.identity.Person; 029import org.kuali.rice.kim.api.services.KimApiServiceLocator; 030import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl; 031import org.kuali.rice.kns.web.ui.Column; 032import org.kuali.rice.kns.web.ui.Field; 033import org.kuali.rice.kns.web.ui.Row; 034import org.kuali.rice.krad.exception.ValidationException; 035import org.kuali.rice.krad.util.GlobalVariables; 036 037import java.util.ArrayList; 038import java.util.Iterator; 039import java.util.List; 040import java.util.Map; 041 042/** 043 * 044 * <p> 045 * Common code used by both the RuleBaseValuesLookupableHelperServiceImpl and the 046 * RuleDelegationLookupableHelperServiceImpl 047 * </p> 048 * 049 * @author Kuali Rice Team (rice.collab@kuali.org) 050 */ 051public class AbstractRuleLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl { 052 053 private List<Row> rows = new ArrayList<Row>(); 054 private List<Row> additionalFieldRows = new ArrayList<Row>(); 055 protected static final String GROUP_REVIEWER_PROPERTY_NAME = "groupReviewer"; 056 protected static final String GROUP_REVIEWER_NAME_PROPERTY_NAME = "groupReviewerName"; 057 protected static final String GROUP_REVIEWER_NAMESPACE_PROPERTY_NAME = "groupReviewerNamespace"; 058 protected static final String PERSON_REVIEWER_PROPERTY_NAME = "personReviewer"; 059 protected static final String PERSON_REVIEWER_TYPE_PROPERTY_NAME = "personReviewerType"; 060 061 protected static final String BACK_LOCATION = "backLocation"; 062 protected static final String DOC_FORM_KEY = "docFormKey"; 063 protected static final String INVALID_WORKGROUP_ERROR = "The Group Reviewer Namespace and Name combination is not valid"; 064 protected static final String INVALID_PERSON_ERROR = "The Person Reviewer is not valid"; 065 066 protected boolean checkForAdditionalFields(Map<String, String> fieldValues, String ruleTemplateNameParam) { 067 if (StringUtils.isNotBlank(ruleTemplateNameParam)) { 068 additionalFieldRows = new ArrayList<Row>(); 069 RuleTemplate ruleTemplate = KewApiServiceLocator.getRuleService().getRuleTemplateByName(ruleTemplateNameParam); 070 for (RuleTemplateAttribute ruleTemplateAttribute : ruleTemplate.getActiveRuleTemplateAttributes()) { 071 if (!RuleAttribute.isWorkflowAttribute(ruleTemplateAttribute.getRuleAttribute().getType())) { 072 continue; 073 074 } 075 076 // run through the attributes fields once to populate field values we have to do this 077 // to allow rows dependent on another row value to populate correctly in the loop below 078 populateFieldsHelperMethod(fieldValues, ruleTemplateAttribute, false); 079 080 // now run through a second time with our shiny new field values 081 // ...by the way, just trying to preserve behavior from Rice 1.0.x here...generally speaking, this stuff is crazy!!! 082 populateFieldsHelperMethod(fieldValues, ruleTemplateAttribute, true); 083 084 } 085 086 return true; 087 088 } 089 090 additionalFieldRows.clear(); 091 092 return false; 093 } 094 095 private void populateFieldsHelperMethod(Map<String, String> fieldValues, 096 RuleTemplateAttribute ruleTemplateAttribute, boolean setAndAddValuesToRow) { 097 098 WorkflowRuleAttributeRows workflowRuleAttributeRows = 099 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}