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.chart.rules;
017:
018: import org.apache.commons.lang.StringUtils;
019: import org.kuali.core.bo.user.UniversalUser;
020: import org.kuali.core.document.MaintenanceDocument;
021: import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
022: import org.kuali.core.util.GlobalVariables;
023: import org.kuali.core.util.ObjectUtils;
024: import org.kuali.kfs.KFSConstants;
025: import org.kuali.kfs.KFSKeyConstants;
026: import org.kuali.kfs.context.SpringContext;
027: import org.kuali.kfs.service.ParameterService;
028: import org.kuali.module.chart.bo.ChartUser;
029:
030: /**
031: * Business rule(s) applicable to {@link ChartUserMaintenance} documents.
032: */
033: public class ChartUserRule extends MaintenanceDocumentRuleBase {
034:
035: private ChartUser oldUser;
036: private ChartUser newUser;
037:
038: /**
039: * This provides the basic rule checking on document routing Specifically it calls the following: checkGeneralRules
040: * checkUserGroups
041: *
042: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
043: */
044: protected boolean processCustomRouteDocumentBusinessRules(
045: MaintenanceDocument document) {
046: boolean success = true;
047: setupConvenienceObjects(document);
048: GlobalVariables.getErrorMap().addToErrorPath(
049: "document.newMaintainableObject");
050: GlobalVariables.getErrorMap().addToErrorPath(
051: "moduleUsers(chart)");
052: success &= checkGeneralRules(document);
053: if (document.isEdit() && !newUser.isActive()) {
054: success &= checkUserGroups(document);
055: }
056: GlobalVariables.getErrorMap().removeFromErrorPath(
057: "moduleUsers(chart)");
058: GlobalVariables.getErrorMap().removeFromErrorPath(
059: "document.newMaintainableObject");
060: return success;
061: }
062:
063: /**
064: * This provides the basic rule checking on document routing Specifically it calls the following: checkGeneralRules
065: * checkUserGroups
066: *
067: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
068: */
069: protected boolean processCustomSaveDocumentBusinessRules(
070: MaintenanceDocument document) {
071: boolean success = true;
072: setupConvenienceObjects(document);
073: GlobalVariables.getErrorMap().addToErrorPath(
074: "document.newMaintainableObject");
075: GlobalVariables.getErrorMap().addToErrorPath(
076: "moduleUsers(chart)");
077: success &= checkGeneralRules(document);
078: if (document.isEdit() && !newUser.isActive()) {
079: success &= checkUserGroups(document);
080: }
081: GlobalVariables.getErrorMap().removeFromErrorPath(
082: "moduleUsers(chart)");
083: GlobalVariables.getErrorMap().removeFromErrorPath(
084: "document.newMaintainableObject");
085: // save always succeeds even if there are rule violations
086: return true;
087: }
088:
089: /**
090: * This method sets the convenience objects like newAccount and oldAccount, so you have short and easy handles to the new and
091: * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
092: * all sub-objects from the DB by their primary keys, if available.
093: *
094: * @param document - the maintenanceDocument being evaluated
095: */
096: private void setupConvenienceObjects(MaintenanceDocument document) {
097:
098: // setup oldAccount convenience objects, make sure all possible sub-objects are populated
099: oldUser = (ChartUser) ((UniversalUser) document
100: .getOldMaintainableObject().getBusinessObject())
101: .getModuleUser(ChartUser.MODULE_ID);
102: // ensure that the chartUser object is pointing to the main UU BO
103: oldUser.setUniversalUser((UniversalUser) document
104: .getOldMaintainableObject().getBusinessObject());
105: oldUser.refresh();
106:
107: // setup newAccount convenience objects, make sure all possible sub-objects are populated
108: newUser = (ChartUser) ((UniversalUser) document
109: .getNewMaintainableObject().getBusinessObject())
110: .getModuleUser(ChartUser.MODULE_ID);
111: // ensure that the chartUser object is pointing to the main UU BO
112: newUser.setUniversalUser((UniversalUser) document
113: .getNewMaintainableObject().getBusinessObject());
114: newUser.refresh();
115: }
116:
117: /**
118: * This checks the following conditions: If the user is going to be active then - they must be a valid employee - they must have
119: * a valid chart of accounts - they must have a valid organization
120: *
121: * @param document
122: * @return false if any combination of the above conditions fails
123: */
124: private boolean checkGeneralRules(MaintenanceDocument document) {
125: boolean success = true;
126:
127: // only run these rules if the new user will be active
128: if (newUser.isActive()) {
129: if (newUser.getUniversalUser() != null) {
130: // user must not have an employee status in the invalid list in the business rules (KULRNE-10)
131: if (!SpringContext
132: .getBean(ParameterService.class)
133: .getParameterEvaluator(
134: ChartUser.class,
135: KFSConstants.CoreApcParms.USER_INVALID_EMPLOYEE_STATUSES,
136: newUser.getUniversalUser()
137: .getEmployeeStatusCode())
138: .evaluationSucceeds()) {
139: success = false;
140: GlobalVariables
141: .getErrorMap()
142: .putError(
143: "active",
144: KFSKeyConstants.ERROR_DOCUMENT_KUALIUSERMAINT_INVALID_EMP_STATUS);
145: }
146: }
147:
148: // chart code (fin_coa_cd) must be entered and valid (nf_duser-350)
149: if (StringUtils.isNotEmpty(newUser
150: .getUserChartOfAccountsCode())) {
151: if (ObjectUtils
152: .isNull(newUser.getUserChartOfAccounts())) {
153: success = false;
154: GlobalVariables
155: .getErrorMap()
156: .putError(
157: "userChartOfAccountsCode",
158: KFSKeyConstants.ERROR_DOCUMENT_KUALIUSERMAINT_INVALID_CODE,
159: new String[] { ddService
160: .getAttributeErrorLabel(
161: newUser.getClass(),
162: "userChartOfAccountsCode") });
163: }
164: }
165:
166: // (90-113) organization must be entered and valid (nf_duser-376)
167: if (StringUtils.isNotEmpty(newUser
168: .getUserOrganizationCode())) {
169: if (ObjectUtils.isNull(newUser.getUserOrganization())) {
170: success = false;
171: GlobalVariables
172: .getErrorMap()
173: .putError(
174: "userOrganizationCode",
175: KFSKeyConstants.ERROR_DOCUMENT_KUALIUSERMAINT_INVALID_CODE,
176: new String[] { ddService
177: .getAttributeErrorLabel(
178: newUser.getClass(),
179: "userOrganizationCode") });
180: }
181: }
182: }
183:
184: return success;
185: }
186:
187: /**
188: * This checks to make sure if a user is getting marked as inactive then they're responsibilities and groups need to be removed
189: * first
190: *
191: * @param document
192: * @return false if user is still part of groups and has responsibilities and is being marked inactive
193: */
194: private boolean checkUserGroups(MaintenanceDocument document) {
195: boolean success = true;
196: // cannot mark a user inactive if they're a member of a workgroup, a review hierarchy, or account delegate. (nf_duser-254)
197: // message to the user to tell them to remove the user from these groups before marking inactive
198: // test is against 1 since all users belong to kualiUniversalGroup
199: if (newUser.getUniversalUser().getGroups().size() > 1) {
200: success = false;
201: }
202: // this retrieves fiscal officer and account delegate responsibilities through
203: // AccountDaoOjb.getAccountsThatUserIsResponsibleFor(KualiUser kualiUser)
204: if (newUser.getAccountResponsibilities().size() > 0) {
205: success = false;
206: }
207: if (!success) {
208: GlobalVariables
209: .getErrorMap()
210: .putError(
211: "active",
212: KFSKeyConstants.ERROR_DOCUMENT_KUALIUSERMAINT_CANNOT_MARK_INACTIVE);
213: }
214:
215: return success;
216: }
217:
218: }
|