Coverage Report - org.kuali.rice.krms.impl.rule.ContextBusRule
 
Classes in this File Line Coverage Branch Coverage Complexity
ContextBusRule
0%
0/19
0%
0/14
3.25
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.krms.impl.rule;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.krad.document.MaintenanceDocument;
 20  
 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
 21  
 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
 22  
 import org.kuali.rice.krms.impl.repository.ContextBo;
 23  
 import org.kuali.rice.krms.impl.repository.ContextBoService;
 24  
 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
 25  
 import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
 26  
 
 27  0
 public class ContextBusRule extends MaintenanceDocumentRuleBase {
 28  
     @Override
 29  
     protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
 30  0
         boolean isValid = true;
 31  
 
 32  0
         ContextBo context = (ContextBo) document.getNewMaintainableObject().getDataObject();
 33  0
         isValid &= validateId(context);
 34  0
         isValid &= validateNameNamespace(context);
 35  
 
 36  0
         return isValid;
 37  
     }
 38  
 
 39  
     private boolean validateId(ContextBo context) {
 40  0
         if (StringUtils.isNotBlank(context.getId())) {
 41  0
             ContextDefinition contextInDatabase = getContextBoService().getContextByContextId(context.getId());
 42  0
             if ((contextInDatabase  != null) && (!StringUtils.equals(contextInDatabase.getId(), context.getId()))) {
 43  0
                 this.putFieldError(KRMSPropertyConstants.Context.CONTEXT_ID, "error.context.duplicateId");
 44  0
                 return false;
 45  
             }
 46  
         }
 47  
 
 48  0
         return true;
 49  
     }
 50  
 
 51  
     /**
 52  
      * Check if the name-namespace pair already exist.
 53  
      * @param context
 54  
      * @return true if the name-namespace pair is unique, false otherwise
 55  
      */
 56  
     private boolean validateNameNamespace(ContextBo context) {
 57  0
         if (StringUtils.isNotBlank(context.getName()) && StringUtils.isNotBlank(context.getNamespace())) {
 58  0
             ContextDefinition contextInDatabase = getContextBoService().getContextByNameAndNamespace(context.getName(), context.getNamespace());
 59  0
             if((contextInDatabase != null) && (!StringUtils.equals(contextInDatabase.getId(), context.getId()))) {
 60  0
                 this.putFieldError(KRMSPropertyConstants.Context.NAME, "error.context.duplicateNameNamespace");
 61  0
                 return false;
 62  
             }
 63  
         }
 64  
 
 65  0
         return true;
 66  
     }
 67  
 
 68  
     public ContextBoService getContextBoService() {
 69  0
         return KrmsRepositoryServiceLocator.getContextBoService();
 70  
     }
 71  
 }