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