001/**
002 * Copyright 2005-2014 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 final 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 void 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        dataObjectService.save(boToUpdate);
100    }
101
102    @Override
103    public ContextDefinition getContextByContextId(String contextId) {
104        if (StringUtils.isBlank(contextId)){
105            return null;
106        }
107        ContextBo bo = dataObjectService.find(ContextBo.class, contextId);
108        return ContextBo.to(bo);
109    }
110
111    @Override
112    public ContextDefinition getContextByNameAndNamespace( String name, String namespace ){
113        if (StringUtils.isBlank(name)){
114            throw new IllegalArgumentException("name is null or blank");
115        }
116        if (StringUtils.isBlank(namespace)){
117            throw new IllegalArgumentException("namespace is null or blank");
118        }
119
120        final Map<String, Object> map = new HashMap<String, Object>();
121        map.put("name", name);
122        map.put("namespace", namespace);
123        ContextBo bo = findSingleMatching(dataObjectService, ContextBo.class, map);
124
125        return ContextBo.to(bo);
126    }
127
128    /**
129     * Sets the dataObjectService attribute value.
130     *
131     * @param dataObjectService The dataObjectService to set.
132     */
133    public void setDataObjectService(final DataObjectService dataObjectService) {
134        this.dataObjectService = dataObjectService;
135    }
136}