001/** 002 * Copyright 2005-2017 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.repository; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.krad.data.DataObjectService; 020import org.kuali.rice.krms.api.repository.context.ContextDefinition; 021 022import java.util.HashMap; 023import java.util.Map; 024 025import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.deleteMatching; 026import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.findSingleMatching; 027 028/** 029 * This is the interface for accessing KRMS repository Context related 030 * business objects. 031 * 032 * @author Kuali Rice Team (rice.collab@kuali.org) 033 * 034 */ 035public class ContextBoServiceImpl implements ContextBoService { 036 037 private DataObjectService dataObjectService; 038 039 /** 040 * This method will create a {@link ContextDefinition} as described 041 * by the parameter passed in. 042 * 043 * @see org.kuali.rice.krms.impl.repository.ContextBoService#createContext(org.kuali.rice.krms.api.repository.context.ContextDefinition) 044 */ 045 @Override 046 public ContextDefinition createContext(ContextDefinition context) { 047 if (context == null) { 048 throw new IllegalArgumentException("context is null"); 049 } 050 051 final String contextIdKey = context.getId(); 052 final ContextDefinition existing = getContextByContextId(contextIdKey); 053 054 if (existing != null) { 055 throw new IllegalStateException("the context to create already exists: " + context); 056 } 057 058 ContextBo bo = dataObjectService.save(ContextBo.from(context)); 059 return ContextBo.to(bo); 060 } 061 062 /** 063 * This method updates an existing Context in the repository. 064 */ 065 @Override 066 public ContextDefinition updateContext(ContextDefinition context) { 067 if (context == null){ 068 throw new IllegalArgumentException("context is null"); 069 } 070 071 // must already exist to be able to update 072 final String contextIdKey = context.getId(); 073 final ContextBo existing = dataObjectService.find(ContextBo.class, contextIdKey); 074 075 if (existing == null) { 076 throw new IllegalStateException("the context does not exist: " + context); 077 } 078 079 final ContextDefinition toUpdate; 080 081 if (!existing.getId().equals(context.getId())){ 082 // if passed in id does not match existing id, correct it 083 final ContextDefinition.Builder builder = ContextDefinition.Builder.create(context); 084 builder.setId(existing.getId()); 085 toUpdate = builder.build(); 086 } else { 087 toUpdate = context; 088 } 089 090 // copy all updateable fields to bo 091 ContextBo boToUpdate = ContextBo.from(toUpdate); 092 093 // delete any old, existing attributes 094 Map<String,String> fields = new HashMap<String,String>(1); 095 fields.put("context.id", toUpdate.getId()); 096 deleteMatching(dataObjectService, ContextAttributeBo.class, fields); 097 098 // update the rule and create new attributes 099 final ContextBo updatedData = dataObjectService.save(boToUpdate); 100 101 return ContextBo.to(updatedData); 102 } 103 104 @Override 105 public ContextDefinition getContextByContextId(String contextId) { 106 if (StringUtils.isBlank(contextId)){ 107 return null; 108 } 109 ContextBo bo = dataObjectService.find(ContextBo.class, contextId); 110 return ContextBo.to(bo); 111 } 112 113 @Override 114 public ContextDefinition getContextByNameAndNamespace( String name, String namespace ){ 115 if (StringUtils.isBlank(name)){ 116 throw new IllegalArgumentException("name is null or blank"); 117 } 118 if (StringUtils.isBlank(namespace)){ 119 throw new IllegalArgumentException("namespace is null or blank"); 120 } 121 122 final Map<String, Object> map = new HashMap<String, Object>(); 123 map.put("name", name); 124 map.put("namespace", namespace); 125 ContextBo bo = findSingleMatching(dataObjectService, ContextBo.class, map); 126 127 return ContextBo.to(bo); 128 } 129 130 /** 131 * Sets the dataObjectService attribute value. 132 * 133 * @param dataObjectService The dataObjectService to set. 134 */ 135 public void setDataObjectService(final DataObjectService dataObjectService) { 136 this.dataObjectService = dataObjectService; 137 } 138}