001/**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */
016package org.kuali.rice.krms.util;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.util.RiceKeyConstants;
020import org.kuali.rice.krad.bo.GlobalBusinessObject;
021import org.kuali.rice.krad.maintenance.MaintenanceDocument;
022import org.kuali.rice.krad.util.KRADConstants;
023import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
024import org.kuali.rice.krms.dto.RuleManagementWrapper;
025import org.kuali.rice.krms.framework.type.ActionTypeService;
026import org.kuali.rice.krms.impl.authorization.AgendaAuthorizationService;
027import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
028import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
029import org.kuali.student.common.uif.rule.KsMaintenanceDocumentRuleBase;
030
031/**
032 * This class contains the rules for the AgendaEditor.
033 *
034 * @author Kuali Student Team
035 */
036public class RuleEditorBusRule extends KsMaintenanceDocumentRuleBase {
037
038    @Override
039    protected boolean primaryKeyCheck(MaintenanceDocument document) {
040        // default to success if no failures
041        boolean success = true;
042        Class<?> dataObjectClass = document.getNewMaintainableObject().getDataObjectClass();
043
044        // Since the dataObject is a wrapper class we need to return the agendaBo instead.
045        Object oldBo = ((RuleManagementWrapper) document.getOldMaintainableObject().getDataObject());
046        Object newDataObject = ((RuleManagementWrapper) document.getNewMaintainableObject().getDataObject());
047
048        // We dont do primaryKeyChecks on Global Business Object maintenance documents. This is
049        // because it doesnt really make any sense to do so, given the behavior of Globals. When a
050        // Global Document completes, it will update or create a new record for each BO in the list.
051        // As a result, there's no problem with having existing BO records in the system, they will
052        // simply get updated.
053        if (newDataObject instanceof GlobalBusinessObject) {
054            return success;
055        }
056
057        // fail and complain if the person has changed the primary keys on
058        // an EDIT maintenance document.
059        if (document.isEdit()) {
060            if (!getDataObjectMetaDataService().equalsByPrimaryKeys(oldBo, newDataObject)) {
061                // add a complaint to the errors
062                putDocumentError(KRADConstants.DOCUMENT_ERRORS,
063                        RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_PRIMARY_KEYS_CHANGED_ON_EDIT,
064                        getHumanReadablePrimaryKeyFieldNames(dataObjectClass));
065                success &= false;
066            }
067        }
068
069        return success;
070    }
071
072    @Override
073    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
074        return true;
075    }
076
077    /**
078     * Check that the rule type is valid when specified.
079     * @param ruleTypeId, the type id
080     * @param contextId, the contextId the action needs to belong to.
081     * @return true if valid, false otherwise.
082     */
083    private boolean validRuleType(String ruleTypeId, String contextId) {
084        if (StringUtils.isBlank(ruleTypeId)) {
085            return true;
086        }
087
088        if (getKrmsTypeRepositoryService().getRuleTypeByRuleTypeIdAndContextId(ruleTypeId, contextId) != null) {
089            return true;
090        } else {
091            this.putFieldError(KRMSPropertyConstants.Rule.TYPE, "error.rule.invalidType");
092            return false;
093        }
094    }
095
096    /**
097     * Check that the rule action type is valid when specified.
098     * @param typeId, the action type id
099     * @parm contextId, the contextId the action needs to belong to.
100     * @return true if valid, false otherwise.
101     */
102    private boolean validRuleActionType(String typeId, String contextId) {
103        if (StringUtils.isBlank(typeId)) {
104            return true;
105        }
106
107        if (getKrmsTypeRepositoryService().getActionTypeByActionTypeIdAndContextId(typeId, contextId) != null) {
108            return true;
109        } else {
110            this.putFieldError(KRMSPropertyConstants.Action.TYPE, "error.action.invalidType");
111            return false;
112        }
113    }
114
115    /**
116     * Check that a action name is specified.
117     */
118    private boolean validRuleActionName(String name) {
119        if (StringUtils.isNotBlank(name)) {
120            return true;
121        } else {
122            this.putFieldError(KRMSPropertyConstants.Action.NAME, "error.action.missingName");
123            return false;
124        }
125    }
126
127    public KrmsTypeRepositoryService getKrmsTypeRepositoryService() {
128        return KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
129    }
130
131    public ActionTypeService getActionTypeService(String serviceName) {
132        return (ActionTypeService)KrmsRepositoryServiceLocator.getService(serviceName);
133    }
134
135    public AgendaAuthorizationService getAgendaAuthorizationService() {
136        return KrmsRepositoryServiceLocator.getAgendaAuthorizationService();
137    }
138
139}
140