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.core.rules;
017:
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: import org.kuali.RiceConstants;
023: import org.kuali.RiceKeyConstants;
024: import org.kuali.core.KualiModule;
025: import org.kuali.core.bo.user.UniversalUser;
026: import org.kuali.core.document.MaintenanceDocument;
027: import org.kuali.core.maintenance.rules.MaintenanceDocumentRule;
028: import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
029: import org.kuali.core.rule.event.ApproveDocumentEvent;
030: import org.kuali.core.util.GlobalVariables;
031: import org.kuali.core.web.format.PhoneNumberFormatter;
032: import org.kuali.rice.KNSServiceLocator;
033:
034: public class UniversalUserRule extends MaintenanceDocumentRuleBase {
035:
036: private UniversalUser oldUser;
037: private UniversalUser newUser;
038:
039: private static final PhoneNumberFormatter phoneNumberFormatter = new PhoneNumberFormatter();
040:
041: private static String userEditWorkgroupName;
042: private static List<KualiModule> installedModules;
043:
044: protected boolean processCustomRouteDocumentBusinessRules(
045: MaintenanceDocument document) {
046: boolean success = true;
047: setupConvenienceObjects(document);
048: // get the group name that we need here
049: // only check if the user can modify the universal user attributes
050: if (GlobalVariables.getUserSession().getUniversalUser()
051: .isMember(userEditWorkgroupName)) {
052: success &= checkGeneralRules(document);
053: }
054: MaintenanceDocumentRule rule = null;
055: for (KualiModule module : installedModules) {
056: rule = module.getModuleUserRule();
057: if (rule != null) {
058: success &= rule.processRouteDocument(document);
059: }
060: }
061: return success;
062: }
063:
064: protected boolean processCustomSaveDocumentBusinessRules(
065: MaintenanceDocument document) {
066: boolean success = true;
067: setupConvenienceObjects(document);
068: // only check if the user can modify the universal user attributes
069: if (GlobalVariables.getUserSession().getUniversalUser()
070: .isMember(userEditWorkgroupName)) {
071: success &= checkGeneralRules(document);
072: }
073: // save always succeeds even if there are rule violations
074: MaintenanceDocumentRule rule = null;
075: for (KualiModule module : installedModules) {
076: rule = module.getModuleUserRule();
077: if (rule != null) {
078: success &= rule.processSaveDocument(document);
079: }
080: }
081: return true;
082: }
083:
084: /**
085: *
086: * This method sets the convenience objects like newAccount and oldAccount, so you have short and easy handles to the new and
087: * old objects contained in the maintenance document.
088: *
089: * It also calls the BusinessObjectBase.refresh(), which will attempt to load all sub-objects from the DB by their primary keys,
090: * if available.
091: *
092: * @param document - the maintenanceDocument being evaluated
093: *
094: */
095: private void setupConvenienceObjects(MaintenanceDocument document) {
096:
097: // setup oldAccount convenience objects, make sure all possible sub-objects are populated
098: oldUser = (UniversalUser) document.getOldMaintainableObject()
099: .getBusinessObject();
100: oldUser.refresh();
101:
102: // setup newAccount convenience objects, make sure all possible sub-objects are populated
103: newUser = (UniversalUser) document.getNewMaintainableObject()
104: .getBusinessObject();
105: newUser.refresh();
106:
107: if (userEditWorkgroupName == null) {
108: userEditWorkgroupName = configService
109: .getParameterValue(
110: RiceConstants.KNS_NAMESPACE,
111: RiceConstants.DetailTypes.UNIVERSAL_USER_DETAIL_TYPE,
112: RiceConstants.CoreApcParms.UNIVERSAL_USER_EDIT_WORKGROUP);
113: installedModules = KNSServiceLocator
114: .getKualiModuleService().getInstalledModules();
115: }
116: }
117:
118: private boolean checkGeneralRules(MaintenanceDocument document) {
119:
120: boolean success = true;
121:
122: //KULCOA-1164: Validation phone number
123:
124: String phoneNumber = newUser.getPersonLocalPhoneNumber();
125: try {
126: newUser
127: .setPersonLocalPhoneNumber((String) phoneNumberFormatter
128: .convertFromPresentationFormat(newUser
129: .getPersonLocalPhoneNumber()));
130: } catch (Exception e) {
131: putFieldError("personLocalPhoneNumber",
132: RiceKeyConstants.ERROR_INVALID_FORMAT,
133: new String[] { "Local Phone Number", phoneNumber });
134: success = false;
135: }
136:
137: if (oldUser == null) {
138: oldUser = new UniversalUser();
139: }
140:
141: // KULCOA-1164: Check whether User Id is unique or not
142:
143: String userId = newUser.getPersonUserIdentifier();
144: if (userId != null
145: && (!userId.equals(oldUser.getPersonUserIdentifier()) || "Copy"
146: .equals(document.getNewMaintainableObject()
147: .getMaintenanceAction()))) {
148: if (userExists("personUserIdentifier", userId)) {
149: putFieldError(
150: "personUserIdentifier",
151: RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_KEYS_ALREADY_EXIST_ON_CREATE_NEW,
152: userId);
153: success = false;
154: }
155: }
156:
157: String emplId = newUser.getPersonPayrollIdentifier();
158: // KULCOA-1164: Check whether Employee Id is unique or not
159: if (emplId != null
160: && (!emplId
161: .equals(oldUser.getPersonPayrollIdentifier()) || "Copy"
162: .equals(document.getNewMaintainableObject()
163: .getMaintenanceAction()))) {
164: if (userExists("personPayrollIdentifier", newUser
165: .getPersonPayrollIdentifier())) {
166: putFieldError(
167: "personPayrollIdentifier",
168: RiceKeyConstants.ERROR_DOCUMENT_KUALIUSERMAINT_UNIQUE_EMPLID);
169: success = false;
170: }
171: }
172:
173: return success;
174: }
175:
176: private boolean userExists(String field, String value) {
177:
178: Map searchMap = new HashMap();
179: searchMap.put(field, value);
180:
181: return universalUserService.findUniversalUsers(searchMap)
182: .size() > 0;
183:
184: }
185:
186: @Override
187: public boolean processApproveDocument(
188: ApproveDocumentEvent approveEvent) {
189: boolean success = super .processApproveDocument(approveEvent);
190:
191: // remove all items from the errorPath temporarily (because it may not
192: // be what we expect, or what we need)
193: clearErrorPath();
194:
195: // loop over all installed modules and run their user rules
196: MaintenanceDocumentRule rule = null;
197: for (KualiModule module : installedModules) {
198: rule = module.getModuleUserRule();
199: if (rule != null) {
200: success &= rule.processApproveDocument(approveEvent);
201: }
202: }
203:
204: // return the original set of items to the errorPath, to ensure no impact
205: // on other upstream or downstream items that rely on the errorPath
206: resumeErrorPath();
207:
208: return success;
209: }
210:
211: }
|