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.HashSet;
019: import java.util.List;
020: import java.util.Set;
021:
022: import org.apache.commons.lang.StringUtils;
023: import org.kuali.RicePropertyConstants;
024: import org.kuali.core.KualiModule;
025: import org.kuali.core.bo.user.KualiModuleUser;
026: import org.kuali.core.bo.user.UniversalUser;
027: import org.kuali.core.datadictionary.control.ControlDefinition;
028: import org.kuali.core.document.Document;
029: import org.kuali.core.document.MaintenanceDocument;
030: import org.kuali.core.util.FieldUtils;
031: import org.kuali.core.web.ui.Field;
032: import org.kuali.rice.KNSServiceLocator;
033:
034: public class UniversalUserPreRules extends PreRulesContinuationBase {
035:
036: public boolean doRules(Document document) {
037:
038: MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
039:
040: UniversalUser newUser = (UniversalUser) maintenanceDocument
041: .getNewMaintainableObject().getBusinessObject();
042:
043: //KULCOA-1164: If FULLNAME is blank, replace it with "Last,First"
044: Field nameField = FieldUtils.getPropertyField(
045: UniversalUser.class, RicePropertyConstants.PERSON_NAME,
046: false);
047: ControlDefinition controlDef = KNSServiceLocator
048: .getDataDictionaryService()
049: .getAttributeControlDefinition(UniversalUser.class,
050: RicePropertyConstants.PERSON_NAME);
051: // KULCOA-3104 - always set the display name to match the first/last if the field is not visible or editable
052: if (controlDef.isHidden() || nameField.isReadOnly()
053: || StringUtils.isBlank(newUser.getPersonName())) {
054: if (!StringUtils.isBlank(newUser.getPersonFirstName())
055: && !StringUtils
056: .isBlank(newUser.getPersonLastName())) {
057: if (!StringUtils.isBlank(newUser.getPersonMiddleName())) {
058: newUser.setPersonName(newUser.getPersonLastName()
059: + "," + newUser.getPersonFirstName() + " "
060: + newUser.getPersonMiddleName());
061: } else {
062: newUser.setPersonName(newUser.getPersonLastName()
063: + "," + newUser.getPersonFirstName());
064: }
065: }
066: }
067: boolean success = true;
068: List<KualiModule> modules = KNSServiceLocator
069: .getKualiModuleService().getInstalledModules();
070: PreRulesContinuationBase rule = null;
071: for (KualiModule module : modules) {
072: rule = (PreRulesContinuationBase) module
073: .getModuleUserPreRules();
074: if (rule != null) {
075: success &= rule.doRules(document);
076: }
077: }
078:
079: // determine what modules user data has changed for and set that info on the new maintainable for use in workflow
080: // see TODO associated with this property in universal user
081: deriveChangedModuleCodes(maintenanceDocument);
082:
083: return success;
084: }
085:
086: private void deriveChangedModuleCodes(MaintenanceDocument document) {
087: Set<String> changedModuleCodes = new HashSet<String>();
088: UniversalUser newUser = (UniversalUser) document
089: .getNewMaintainableObject().getBusinessObject();
090: for (KualiModuleUser newModuleUser : newUser.getModuleUsers()
091: .values()) {
092: if (newModuleUser.isModified(
093: document.isEdit() ? (UniversalUser) document
094: .getOldMaintainableObject()
095: .getBusinessObject() : null, newUser)) {
096: changedModuleCodes.add(KNSServiceLocator
097: .getKualiModuleService().getModule(
098: newModuleUser.getModuleId())
099: .getModuleCode());
100: }
101: }
102: newUser.setChangedModuleCodes(changedModuleCodes);
103: }
104: }
|