001: /*
002: * Copyright 2006-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.module.financial.rules;
017:
018: import org.apache.commons.lang.StringUtils;
019: import org.kuali.core.document.MaintenanceDocument;
020: import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
021: import org.kuali.core.workflow.service.KualiWorkflowInfo;
022: import org.kuali.kfs.KFSKeyConstants;
023: import org.kuali.kfs.KFSPropertyConstants;
024: import org.kuali.kfs.context.SpringContext;
025: import org.kuali.module.financial.bo.ServiceBillingControl;
026:
027: import edu.iu.uis.eden.clientapp.vo.WorkgroupNameIdVO;
028: import edu.iu.uis.eden.clientapp.vo.WorkgroupVO;
029: import edu.iu.uis.eden.exception.WorkflowException;
030:
031: /**
032: * This class validates the ServiceBillingControl maintenance document.
033: */
034: public class ServiceBillingControlRule extends
035: MaintenanceDocumentRuleBase {
036:
037: /**
038: * This method creates a new ServiceBillingControl instance and returns it.
039: *
040: * @return The new business object that this maintenance doc is creating.
041: */
042: private ServiceBillingControl getNewServiceBillingControl() {
043: return (ServiceBillingControl) getNewBo();
044: }
045:
046: /**
047: * This method performs custom business rule checks on the document being saved. The rules include confirming the
048: * validity of the work group. This method always returns true, because saves should always succeed, regardless
049: * of business rule failures.
050: *
051: * @param document The document being saved.
052: * @return This method always returns true. Saves should always succeed, regardless of business rule errors encountered
053: * or reported in the data.
054: *
055: * @see MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
056: */
057: @Override
058: protected boolean processCustomSaveDocumentBusinessRules(
059: MaintenanceDocument document) {
060: isValidWorkgroup();
061: // Save always succeeds, even if there are business rule failures
062: return true;
063: }
064:
065: /**
066: * This method performs custom route business rule checks on the document being routed. The rules include confirming the
067: * validity of the work group.
068: *
069: * @param document The document being routed.
070: * @return True if all the business rules pass, false otherwise.
071: *
072: * @see MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
073: */
074: @Override
075: protected boolean processCustomRouteDocumentBusinessRules(
076: MaintenanceDocument document) {
077: boolean valid = true;
078: valid &= isValidWorkgroup();
079: return valid;
080: }
081:
082: /**
083: * Adds a global error for the workgroupName field if it doesn't exist or is inactive.
084: *
085: * @return Whether it exists and is active
086: */
087: private boolean isValidWorkgroup() {
088: String name = getNewServiceBillingControl().getWorkgroupName();
089: if (StringUtils.isNotBlank(name)) {
090: if (!workgroupExistsAndIsActive(name)) {
091: putFieldErrorWithShortLabel(
092: KFSPropertyConstants.WORKGROUP_NAME,
093: KFSKeyConstants.ERROR_EXISTENCE);
094: return false;
095: }
096: }
097: return true;
098: }
099:
100: /**
101: * Checks whether the given workgroup exists and is active.
102: *
103: * @param name The name of the workgroup to check.
104: * @return Whether the given workgroup exists and is active.
105: */
106: private static boolean workgroupExistsAndIsActive(String name) {
107: try {
108: WorkgroupVO workgroupVo = SpringContext.getBean(
109: KualiWorkflowInfo.class).getWorkgroup(
110: new WorkgroupNameIdVO(name));
111: return workgroupVo != null && workgroupVo.isActiveInd();
112: } catch (WorkflowException e) {
113: return false;
114: }
115: }
116: }
|