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.exceptions.ValidationException;
025: import org.kuali.core.lookup.LookupUtils;
026: import org.kuali.kfs.KFSConstants;
027: import org.kuali.kfs.context.SpringContext;
028: import org.kuali.module.chart.bo.Chart;
029: import org.kuali.module.chart.bo.Org;
030: import org.kuali.module.chart.service.OrganizationService;
031: import org.kuali.workflow.KualiWorkflowUtils;
032:
033: import edu.iu.uis.eden.lookupable.Row;
034: import edu.iu.uis.eden.validation.ValidationContext;
035: import edu.iu.uis.eden.validation.ValidationResults;
036:
037: /**
038: * An attribute which provides Chart & Org extensions to an entity. In this case, it is used for the Chart Org Workgroup type.
039: *
040: * @author ewestfal
041: */
042: public class ChartOrgExtensionAttribute implements ExtensionAttribute {
043:
044: private static final String CHART = KFSConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME;
045: private static final String ORG = KFSConstants.ORGANIZATION_CODE_PROPERTY_NAME;
046:
047: private static final String EXTENSIONS_PARAM = "extensions";
048: private static final String OPERATION_PARAM = "operation";
049: private static final String SEARCH_OP = "search";
050: private static final String ENTRY_OP = "entry";
051:
052: private List<Row> rows;
053:
054: public ChartOrgExtensionAttribute() {
055: rows = new ArrayList<Row>();
056: rows.add(KualiWorkflowUtils.buildTextRowWithLookup(Chart.class,
057: CHART, CHART));
058: Map fieldConversionMap = new HashMap();
059: fieldConversionMap.put(CHART, CHART);
060: rows.add(KualiWorkflowUtils.buildTextRowWithLookup(Org.class,
061: ORG, ORG, fieldConversionMap));
062: }
063:
064: public List<Row> getRows() {
065: return rows;
066: }
067:
068: /**
069: * Validate the chart code and org code that are specified.
070: *
071: * @see org.kuali.workflow.attribute.ExtensionAttribute#validate(edu.iu.uis.eden.validation.ValidationContext)
072: */
073: public ValidationResults validate(
074: ValidationContext validationContext) {
075: Map<String, String> extensions = (Map<String, String>) validationContext
076: .getParameters().get(EXTENSIONS_PARAM);
077: String operation = (String) validationContext.getParameters()
078: .get(OPERATION_PARAM);
079: if (extensions == null) {
080: // not sure if this is the correct exception or not but it feels better than throwing a runtime
081: throw new ValidationException(
082: "Could not locate workgroup extension data in order to perform validation.");
083: }
084: List errors = new ArrayList();
085: String chart = LookupUtils.forceUppercase(Org.class, CHART,
086: extensions.get(CHART));
087: String org = LookupUtils.forceUppercase(Org.class, ORG,
088: extensions.get(ORG));
089:
090: ValidationResults results = new ValidationResults();
091: if (StringUtils.isBlank(chart) || StringUtils.isBlank(org)) {
092: if (ENTRY_OP.equals(operation)) {
093: results.addValidationResult("Chart/org is required.");
094: }
095: } else {
096: Org organization = SpringContext.getBean(
097: OrganizationService.class)
098: .getByPrimaryIdWithCaching(chart, org);
099: if (organization == null) {
100: results.addValidationResult("Chart/org is invalid.");
101: }
102: }
103: return results;
104: }
105:
106: }
|