001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package org.kuali.workflow.attribute;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.apache.commons.lang.StringUtils;
024: import org.kuali.core.KualiModule;
025: import org.kuali.core.bo.Campus;
026: import org.kuali.core.exceptions.ValidationException;
027: import org.kuali.core.lookup.LookupUtils;
028: import org.kuali.core.lookup.keyvalues.CampusValuesFinder;
029: import org.kuali.core.lookup.keyvalues.KeyValuesFinder;
030: import org.kuali.core.lookup.keyvalues.ModuleValuesFinder;
031: import org.kuali.core.service.BusinessObjectService;
032: import org.kuali.core.service.KualiModuleService;
033: import org.kuali.core.util.FieldUtils;
034: import org.kuali.core.web.ui.KeyLabelPair;
035: import org.kuali.kfs.KFSPropertyConstants;
036: import org.kuali.kfs.bo.KualiModuleBO;
037: import org.kuali.kfs.context.SpringContext;
038: import org.kuali.kfs.lookup.keyvalues.ParameterValuesFinder;
039: import org.kuali.kfs.service.ParameterService;
040: import org.kuali.kfs.service.impl.ParameterConstants;
041: import org.kuali.workflow.KualiWorkflowUtils;
042:
043: import edu.iu.uis.eden.lookupable.Field;
044: import edu.iu.uis.eden.lookupable.Row;
045: import edu.iu.uis.eden.validation.ValidationContext;
046: import edu.iu.uis.eden.validation.ValidationResults;
047:
048: /**
049: * A workgroup type extension attribute that supports the extension information associated with System workgroups
050: */
051: public class SystemWorkgroupExtensionAttribute implements
052: ExtensionAttribute {
053: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
054: .getLogger(SystemWorkgroupExtensionAttribute.class);
055:
056: private static final String EXTENSIONS_PARAM = "extensions";
057: private static final String OPERATION_PARAM = "operation";
058: private static final String SEARCH_OP = "search";
059: private static final String ENTRY_OP = "entry";
060:
061: private static final String MODULE_CODE = "moduleCode";
062: private static final String CAMPUS_CODE = "campusCode";
063: private static final String AREA_EXTENSION_ATTRIBUTE_NAME = "area";
064: private static final String AUTHORIZATION_EXTENSION_ATTRIBUTE_NAME = "usedForAuthorization";
065: private static final String AUTHORIZATION_EXTENSION_ATTRIBUTE_LABEL = "Used For Authorization?";
066: private static final String ROUTING_EXTENSION_ATTRIBUTE_NAME = "usedForRouting";
067: private static final String ROUTING_EXTENSION_ATTRIBUTE_LABEL = "Used For Routing?";
068:
069: private List<Row> rows;
070:
071: /**
072: * Constructs a SystemWorkgroupExtensionAttribute instance
073: */
074: public SystemWorkgroupExtensionAttribute() {
075: rows = new ArrayList<Row>();
076: rows.add(buildModuleDropdownRow());
077: rows.add(buildCampusDropdownRow());
078: rows.add(buildAreaDropdownRow());
079: rows.add(buildYesNoRow(AUTHORIZATION_EXTENSION_ATTRIBUTE_NAME,
080: AUTHORIZATION_EXTENSION_ATTRIBUTE_LABEL));
081: rows.add(buildYesNoRow(ROUTING_EXTENSION_ATTRIBUTE_NAME,
082: ROUTING_EXTENSION_ATTRIBUTE_LABEL));
083: }
084:
085: /**
086: * Builds the row for campus code
087: * @return a Row for campus code
088: */
089: private Row buildCampusDropdownRow() {
090: org.kuali.core.web.ui.Field kualiCampusField = FieldUtils
091: .getPropertyField(Campus.class,
092: KFSPropertyConstants.CAMPUS_CODE, false);
093: KeyValuesFinder campusFinder = new CampusValuesFinder();
094: List campusFields = new ArrayList();
095: campusFields.add(new Field("Campus", KualiWorkflowUtils
096: .getHelpUrl(kualiCampusField), Field.DROPDOWN, false,
097: CAMPUS_CODE, "", campusFinder.getKeyValues(), null,
098: null));
099: return new Row(campusFields);
100: }
101:
102: /**
103: * Builds a row for the module drop down
104: *
105: * @return a Row for the module code
106: */
107: private Row buildModuleDropdownRow() {
108: org.kuali.core.web.ui.Field kualiModuleField = FieldUtils
109: .getPropertyField(KualiModuleBO.class, MODULE_CODE,
110: false);
111: KeyValuesFinder modulesFinder = new ModuleValuesFinder();
112: List moduleFields = new ArrayList();
113: moduleFields.add(new Field("Module", KualiWorkflowUtils
114: .getHelpUrl(kualiModuleField), Field.DROPDOWN, false,
115: MODULE_CODE, "", modulesFinder.getKeyValues(), null,
116: null));
117: return new Row(moduleFields);
118: }
119:
120: /**
121: * Builds a row for the area drop down
122: *
123: * @return a row for the area drop down
124: */
125: private Row buildAreaDropdownRow() {
126: KeyValuesFinder areasFinder = new ParameterValuesFinder(
127: ParameterConstants.FINANCIAL_SYSTEM_ALL.class,
128: "SYSTEM_WORKGROUP_TYPE_AREAS");
129: List areaFields = new ArrayList();
130: areaFields.add(new Field("Area", "", Field.DROPDOWN, false,
131: AREA_EXTENSION_ATTRIBUTE_NAME, "", areasFinder
132: .getKeyValues(), null, null));
133: return new Row(areaFields);
134: }
135:
136: /**
137: * Builds a row with yes/no radio buttons
138: * @param attributeName the attribute name of this row
139: * @param attributeLabel the label to show in the HTML for this row
140: * @return a Yes/No radio button Row
141: */
142: private Row buildYesNoRow(String attributeName,
143: String attributeLabel) {
144: List yesNoField = new ArrayList();
145: List radioValues = new ArrayList();
146: radioValues.add(new KeyLabelPair(Field.CHECKBOX_VALUE_CHECKED,
147: Field.CHECKBOX_VALUE_CHECKED));
148: radioValues.add(new KeyLabelPair("",
149: Field.CHECKBOX_VALUE_UNCHECKED));
150: yesNoField.add(new Field(attributeLabel, "", Field.RADIO,
151: false, attributeName, "", radioValues, null, null));
152: return new Row(yesNoField);
153: }
154:
155: /**
156: * Returns the prebuilt List of rows to accept the editing of System workgroup type extension data for the given workgroup
157: * @see org.kuali.workflow.attribute.ExtensionAttribute#getRows()
158: */
159: public List<Row> getRows() {
160: return rows;
161: }
162:
163: /**
164: * Validates the extension data given - basically, makes sure that if a module, campus code, or area
165: * have been selected, that they exist (ie, a module exists, a campus code exists in SH_CAMPUS_T or the area
166: * given is one of the ones within the area parameter)
167: * @param validationContext the workgroup validation context to validate
168: * @return the ValidationResults returned by the check
169: * @see org.kuali.workflow.attribute.ExtensionAttribute#validate(edu.iu.uis.eden.validation.ValidationContext)
170: */
171: public ValidationResults validate(
172: ValidationContext validationContext) {
173: Map<String, String> extensions = (Map<String, String>) validationContext
174: .getParameters().get(EXTENSIONS_PARAM);
175: String operation = (String) validationContext.getParameters()
176: .get(OPERATION_PARAM);
177: if (extensions == null) {
178: // not sure if this is the correct exception or not but it feels better than throwing a runtime
179: throw new ValidationException(
180: "Could not locate workgroup extension data in order to perform validation.");
181: }
182: List errors = new ArrayList();
183:
184: String moduleCode = LookupUtils.forceUppercase(
185: KualiModuleBO.class, MODULE_CODE, extensions
186: .get(MODULE_CODE));
187: String campusCode = LookupUtils.forceUppercase(Campus.class,
188: CAMPUS_CODE, extensions.get(CAMPUS_CODE));
189: String area = extensions.get(AREA_EXTENSION_ATTRIBUTE_NAME);
190: boolean authorizationGroup = translateExtensionToBoolean(extensions
191: .get(AUTHORIZATION_EXTENSION_ATTRIBUTE_NAME));
192: boolean routingGroup = translateExtensionToBoolean(extensions
193: .get(ROUTING_EXTENSION_ATTRIBUTE_NAME));
194:
195: ValidationResults results = new ValidationResults();
196: if (!StringUtils.isBlank(moduleCode)) {
197: KualiModule module = SpringContext.getBean(
198: KualiModuleService.class).getModuleByCode(
199: moduleCode);
200: if (module == null) {
201: results.addValidationResult("Module Code is invalid.");
202: }
203: }
204: if (!StringUtils.isBlank(campusCode)) {
205: Map<String, String> campusKeys = new HashMap<String, String>();
206: campusKeys.put("campusCode", campusCode);
207: Campus campus = (Campus) SpringContext.getBean(
208: BusinessObjectService.class).findByPrimaryKey(
209: Campus.class, campusKeys);
210: if (campus == null) {
211: results.addValidationResult("Campus Code is invalid.");
212: }
213: }
214: if (!StringUtils.isBlank(area)) {
215: List<String> paramValues = SpringContext.getBean(
216: ParameterService.class).getParameterValues(
217: ParameterConstants.FINANCIAL_SYSTEM_ALL.class,
218: "SYSTEM_WORKGROUP_TYPE_AREAS");
219: if (paramValues != null) {
220: if (!paramValues.contains(area)) {
221: results.addValidationResult("Area is invalid.");
222: }
223: }
224: }
225: return results;
226: }
227:
228: private boolean translateExtensionToBoolean(String extension) {
229: return (extension != null && extension.trim().length() > 0 && extension
230: .equals(Field.CHECKBOX_VALUE_CHECKED));
231: }
232: }
|