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.repository;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
020import org.kuali.rice.krad.service.BusinessObjectService;
021import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
022import org.kuali.rice.krms.api.repository.function.FunctionRepositoryService;
023
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030/**
031 * Default implementation of the {@link FunctionService}.
032 * 
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 *
035 */
036public class FunctionBoServiceImpl implements FunctionRepositoryService, FunctionBoService {
037                
038    private BusinessObjectService businessObjectService;
039    
040        @Override
041        public FunctionDefinition getFunction(String functionId) {
042                return getFunctionById(functionId);
043        }
044        
045        @Override
046        public List<FunctionDefinition> getFunctions(List<String> functionIds) {
047
048        if (functionIds == null) throw new RiceIllegalArgumentException();
049
050                List<FunctionDefinition> functionDefinitions = new ArrayList<FunctionDefinition>();
051                for (String functionId : functionIds){
052            if (!StringUtils.isBlank(functionId)) {
053                FunctionDefinition functionDefinition = getFunctionById(functionId);
054                if (functionDefinition != null) {
055                    functionDefinitions.add(functionDefinition);
056                }
057            }
058                }
059        return Collections.unmodifiableList(functionDefinitions);
060        }
061        
062        /**
063         * This method will create a {@link FunctionDefintion} as described
064         * by the function passed in.
065         * 
066         * @see org.kuali.rice.krms.impl.repository.FunctionBoService#createFunction(org.kuali.rice.krms.api.repository.function.FunctionDefinition)
067         */
068        @Override
069        public FunctionDefinition createFunction(FunctionDefinition function) {
070                if (function == null){
071                throw new IllegalArgumentException("function is null");
072                }
073                
074                final String nameKey = function.getName();
075                final String namespaceKey = function.getNamespace();
076                final FunctionDefinition existing = getFunctionByNameAndNamespace(nameKey, namespaceKey);
077                if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){
078            throw new IllegalStateException("the function to create already exists: " + function);                      
079                }
080                
081                FunctionBo functionBo = FunctionBo.from(function);
082                businessObjectService.save(functionBo);
083                return FunctionBo.to(functionBo);
084        }
085
086        /**
087         * This overridden method updates an existing Function in the repository
088         * 
089         * @see org.kuali.rice.krms.impl.repository.FunctionBoService#updateFunction(org.kuali.rice.krms.api.repository.function.FunctionDefinition)
090         */
091        @Override
092        public void updateFunction(FunctionDefinition function) {
093                if (function == null){
094                        throw new IllegalArgumentException("function is null");
095                }
096                
097                final String functionIdKey = function.getId();
098                final FunctionDefinition existing = getFunctionById(functionIdKey);
099                if (existing == null) {
100                        throw new IllegalStateException("the function does not exist: " + function);
101                }
102                final FunctionDefinition toUpdate;
103                if (!existing.getId().equals(function.getId())){
104                        final FunctionDefinition.Builder builder = FunctionDefinition.Builder.create(function);
105                        builder.setId(existing.getId());
106                        toUpdate = builder.build();
107                } else {
108                        toUpdate = function;
109                }
110
111                businessObjectService.save(FunctionBo.from(toUpdate));
112        }
113        
114    
115        /**
116         * This overridden method retrieves a function by the given function id.
117         * 
118         * @see org.kuali.rice.krms.impl.repository.FunctionBoService#getFunctionById(java.lang.String)
119         */
120        @Override
121        public FunctionDefinition getFunctionById(String functionId) {
122                if (StringUtils.isBlank(functionId)){
123            throw new RiceIllegalArgumentException("functionId is null or blank");
124                }
125                FunctionBo functionBo = businessObjectService.findBySinglePrimaryKey(FunctionBo.class, functionId);
126                return FunctionBo.to(functionBo);
127        }
128        
129        /**
130         * 
131         * This overridden method retrieves a function by the given name and namespace.
132         * 
133         * @see org.kuali.rice.krms.impl.repository.FunctionBoService#getFunctionByNameAndNamespace(java.lang.String, java.lang.String)
134         */
135        public FunctionDefinition getFunctionByNameAndNamespace( String name, String namespace ){
136                if (StringUtils.isBlank(name)){
137                        throw new IllegalArgumentException("name is null or blank");
138                }
139                if (StringUtils.isBlank(namespace)){
140                        throw new IllegalArgumentException("namespace is null or blank");
141                }
142                                
143        final Map<String, Object> map = new HashMap<String, Object>();
144        map.put("name", name);
145        map.put("namespace", namespace);
146                FunctionBo functionBo = businessObjectService.findByPrimaryKey(FunctionBo.class, Collections.unmodifiableMap(map));
147                return FunctionBo.to(functionBo);
148        }
149        
150    public void setBusinessObjectService(BusinessObjectService businessObjectService) {
151        this.businessObjectService = businessObjectService;
152    }
153}