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