001/** 002 * Copyright 2005-2015 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.impl.rule; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.krad.maintenance.MaintenanceDocument; 020import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase; 021import org.kuali.rice.krad.util.KRADConstants; 022import org.kuali.rice.krms.api.repository.context.ContextDefinition; 023import org.kuali.rice.krms.impl.repository.ContextBo; 024import org.kuali.rice.krms.impl.repository.ContextBoService; 025import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator; 026import org.kuali.rice.krms.impl.util.KRMSPropertyConstants; 027 028public class ContextBusRule extends MaintenanceDocumentRuleBase { 029 @Override 030 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) { 031 boolean isValid = true; 032 033 ContextBo newContext = (ContextBo) document.getNewMaintainableObject().getDataObject(); 034 ContextBo oldContext = (ContextBo) document.getOldMaintainableObject().getDataObject(); 035 boolean isEditAction = KRADConstants.MAINTENANCE_EDIT_ACTION.equals(document.getNewMaintainableObject().getMaintenanceAction()); 036 037 isValid &= validateId(oldContext, newContext, isEditAction); 038 isValid &= validateNameNamespace(newContext, isEditAction); 039 040 return isValid; 041 } 042 043 private boolean validateId(ContextBo oldContext, ContextBo newContext, boolean isEditAction) { 044 if (StringUtils.isBlank(newContext.getId())) { 045 this.putFieldError(KRMSPropertyConstants.Context.CONTEXT_ID, "error.context.blankId"); 046 return false; 047 } 048 if (isEditAction) { 049 if (!oldContext.getId().equals(newContext.getId())) { 050 throw new IllegalStateException("The ID of a Context being edited must not change."); 051 } 052 } else { 053 ContextDefinition contextInDatabase = getContextBoService().getContextByContextId(newContext.getId()); 054 if (contextInDatabase != null) { 055 this.putFieldError(KRMSPropertyConstants.Context.CONTEXT_ID, "error.context.duplicateId"); 056 return false; 057 } 058 } 059 060 return true; 061 } 062 063 /** 064 * Check if the name-namespace pair already exist. 065 * @param newContext 066 * @return true if the name-namespace pair is unique, false otherwise 067 */ 068 private boolean validateNameNamespace(ContextBo newContext, boolean isEditAction) { 069 if (isEditAction) { 070 ContextDefinition contextInDatabase = getContextBoService().getContextByNameAndNamespace(newContext.getName(), newContext 071 .getNamespace()); 072 if ((contextInDatabase != null) && !contextInDatabase.getId().equals(newContext.getId())) { // if ID is the same, it's not a duplicate 073 this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.duplicateNameNamespace"); 074 return false; 075 } 076 } else if (StringUtils.isNotBlank(newContext.getName()) && StringUtils.isNotBlank(newContext.getNamespace())) { 077 ContextDefinition contextInDatabase = getContextBoService().getContextByNameAndNamespace(newContext.getName(), newContext 078 .getNamespace()); 079 if(contextInDatabase != null) { 080 this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.duplicateNameNamespace"); 081 return false; 082 } 083 } else { 084 if (StringUtils.isBlank(newContext.getName())) { 085 this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.blankName"); 086 } 087 if (StringUtils.isBlank(newContext.getNamespace())) { 088 this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.blankNamespace"); 089 } 090 return false; 091 } 092 093 return true; 094 } 095 096 public ContextBoService getContextBoService() { 097 return KrmsRepositoryServiceLocator.getContextBoService(); 098 } 099}