001/**
002 * Copyright 2005-2016 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
018
019import org.apache.commons.lang.StringUtils;
020import org.kuali.rice.krad.service.BusinessObjectService;
021import org.kuali.rice.krms.api.repository.context.ContextDefinition;
022import org.kuali.rice.krms.impl.util.KrmsImplConstants.PropertyNames;
023
024import java.util.*;
025
026/**
027 * This is the interface for accessing KRMS repository Context related
028 * business objects. 
029 * 
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 *
032 */
033public final class ContextBoServiceImpl implements ContextBoService {
034
035    private BusinessObjectService businessObjectService;
036
037        /**
038         * This method will create a {@link ContextDefinition} as described
039         * by the parameter passed in.
040         * 
041         * @see org.kuali.rice.krms.impl.repository.ContextBoService#createContext(org.kuali.rice.krms.api.repository.context.ContextDefinition)
042         */
043        @Override
044        public ContextDefinition createContext(ContextDefinition context) {
045                if (context == null){
046                throw new IllegalArgumentException("context is null");
047                }
048                final String contextIdKey = context.getId();
049                final ContextDefinition existing = getContextByContextId(contextIdKey);
050                if (existing != null){
051            throw new IllegalStateException("the context to create already exists: " + context);                        
052                }       
053                ContextBo bo = (ContextBo)businessObjectService.save(ContextBo.from(context));
054                return ContextBo.to(bo);
055        }
056
057        /**
058         * This method updates an existing Context in the repository.
059         */
060        @Override
061        public void updateContext(ContextDefinition context) {
062                if (context == null){
063                throw new IllegalArgumentException("context is null");
064                }
065
066                // must already exist to be able to update
067                final String contextIdKey = context.getId();
068                final ContextBo existing = businessObjectService.findBySinglePrimaryKey(ContextBo.class, contextIdKey);
069        if (existing == null) {
070            throw new IllegalStateException("the context does not exist: " + context);
071        }
072        final ContextDefinition toUpdate;
073        if (!existing.getId().equals(context.getId())){
074                        // if passed in id does not match existing id, correct it
075                final ContextDefinition.Builder builder = ContextDefinition.Builder.create(context);
076                builder.setId(existing.getId());
077                toUpdate = builder.build();
078        } else {
079                toUpdate = context;
080        }
081        
082                // copy all updateable fields to bo
083                ContextBo boToUpdate = ContextBo.from(toUpdate);
084
085                // delete any old, existing attributes
086                Map<String,String> fields = new HashMap<String,String>(1);
087                fields.put(PropertyNames.Context.CONTEXT_ID, toUpdate.getId());
088                businessObjectService.deleteMatching(ContextAttributeBo.class, fields);
089        
090                // update the rule and create new attributes
091        businessObjectService.save(boToUpdate);
092        }
093
094        /**
095         * This overridden method ...
096         */
097        @Override
098        public ContextDefinition getContextByContextId(String contextId) {
099                if (StringUtils.isBlank(contextId)){
100            return null;                        
101                }
102                ContextBo bo = businessObjectService.findBySinglePrimaryKey(ContextBo.class, contextId);
103                return ContextBo.to(bo);
104        }
105
106        /**
107         * This overridden method ...
108         */
109        public ContextDefinition getContextByNameAndNamespace( String name, String namespace ){
110                if (StringUtils.isBlank(name)){
111                        throw new IllegalArgumentException("name is null or blank");
112                }
113                if (StringUtils.isBlank(namespace)){
114                        throw new IllegalArgumentException("namespace is null or blank");
115                }
116        final Map<String, Object> map = new HashMap<String, Object>();
117        map.put("name", name);
118        map.put("namespace", namespace);
119                ContextBo bo = businessObjectService.findByPrimaryKey(ContextBo.class, map);
120                return ContextBo.to(bo);
121        }
122        
123//      /**
124//       * This overridden method ...
125//       * 
126//       * @see org.kuali.rice.krms.impl.repository.ContextBoService#createContextAttribute(org.kuali.rice.krms.api.repository.ContextAttribute)
127//       */
128//      @Override
129//      public void createContextAttribute(ContextAttribute attribute) {
130//              if (attribute == null){
131//              throw new IllegalArgumentException("context attribute is null");
132//              }
133//              final String attrIdKey = attribute.getId();
134//              final ContextAttribute existing = getContextAttributeById(attrIdKey);
135//              if (existing != null){
136//            throw new IllegalStateException("the context attribute to create already exists: " + attribute);                  
137//              }
138//              
139//              businessObjectService.save(ContextAttributeBo.from(attribute));         
140//      }
141//
142//      /**
143//       * This overridden method ...
144//       * 
145//       * @see org.kuali.rice.krms.impl.repository.ContextBoService#updateContextAttribute(org.kuali.rice.krms.api.repository.ContextAttribute)
146//       */
147//      @Override
148//      public void updateContextAttribute(ContextAttribute attribute) {
149//              if (attribute == null){
150//              throw new IllegalArgumentException("context attribute is null");
151//              }
152//              final String attrIdKey = attribute.getId();
153//              final ContextAttribute existing = getContextAttributeById(attrIdKey);
154//        if (existing == null) {
155//            throw new IllegalStateException("the context attribute does not exist: " + attribute);
156//        }
157//        final ContextAttribute toUpdate;
158//        if (!existing.getId().equals(attribute.getContextId())){
159//              final ContextAttribute.Builder builder = ContextAttribute.Builder.create(attribute);
160//              builder.setId(existing.getId());
161//              toUpdate = builder.build();
162//        } else {
163//              toUpdate = attribute;
164//        }
165//        
166//        businessObjectService.save(ContextAttributeBo.from(toUpdate));                
167//      }
168//      
169//      
170//      /**
171//       * This method ...
172//       * 
173//       * @see org.kuali.rice.krms.impl.repository.ContextBoService#getContextsByRuleId(java.lang.String)
174//       */
175//      public ContextAttribute getContextAttributeById(String attrId) {
176//              if (StringUtils.isBlank(attrId)){
177//            return null;                      
178//              }
179//              ContextAttributeBo bo = businessObjectService.findBySinglePrimaryKey(ContextAttributeBo.class, attrId);
180//              return ContextAttributeBo.to(bo);
181//      }
182
183
184    /**
185     * Sets the businessObjectService attribute value.
186     *
187     * @param businessObjectService The businessObjectService to set.
188     */
189    public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
190        this.businessObjectService = businessObjectService;
191    }
192
193
194}