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 java.util.ArrayList;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.lang.StringUtils;
027import org.kuali.rice.krad.service.BusinessObjectService;
028import org.kuali.rice.krms.api.repository.action.ActionDefinition;
029import org.kuali.rice.krms.impl.util.KrmsImplConstants.PropertyNames;
030
031/**
032 * Implementation of the interface for accessing KRMS repository Action related
033 * business objects.
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 *
037 */
038public final class ActionBoServiceImpl implements ActionBoService {
039
040    private BusinessObjectService businessObjectService;
041
042        /**
043         * This overridden method creates a KRMS Action in the repository.
044         */
045        @Override
046        public ActionDefinition createAction(ActionDefinition action) {
047                if (action == null){
048                throw new IllegalArgumentException("action is null");
049                }
050                final String actionNameKey = action.getName();
051                final String actionNamespaceKey = action.getNamespace();
052                final ActionDefinition existing = getActionByNameAndNamespace(actionNameKey, actionNamespaceKey);
053                if (existing != null){
054            throw new IllegalStateException("the action to create already exists: " + action);                  
055                }       
056                
057                ActionBo bo = ActionBo.from(action);
058                businessObjectService.save(bo);
059                
060                return ActionBo.to(bo);
061        }
062
063        /**
064         * This overridden method updates an existing Action in the repository.
065         */
066        @Override
067        public void updateAction(ActionDefinition action) {
068                if (action == null){
069                throw new IllegalArgumentException("action is null");
070                }
071
072                // must already exist to be able to update
073                final String actionIdKey = action.getId();
074                final ActionBo existing = businessObjectService.findBySinglePrimaryKey(ActionBo.class, actionIdKey);
075        if (existing == null) {
076            throw new IllegalStateException("the action does not exist: " + action);
077        }
078        final ActionDefinition toUpdate;
079        if (existing.getId().equals(action.getId())) {
080            toUpdate = action;
081        } else {
082            // if passed in id does not match existing id, correct it
083            final ActionDefinition.Builder builder = ActionDefinition.Builder.create(action);
084            builder.setId(existing.getId());
085            toUpdate = builder.build();
086        }
087     
088                // copy all updateable fields to bo
089        ActionBo boToUpdate = ActionBo.from(toUpdate);
090
091                // delete any old, existing attributes
092                Map<String,String> fields = new HashMap<String,String>(1);
093                fields.put(PropertyNames.Action.ACTION_ID, toUpdate.getId());
094                businessObjectService.deleteMatching(ActionAttributeBo.class, fields);
095        
096                // update the action and create new attributes
097        businessObjectService.save(boToUpdate);
098        }
099
100        /**
101         * This overridden method retrieves an Action from the repository.
102         */
103        @Override
104        public ActionDefinition getActionByActionId(String actionId) {
105                if (StringUtils.isBlank(actionId)){
106            throw new IllegalArgumentException("action ID is null or blank");                   
107                }
108                ActionBo bo = businessObjectService.findBySinglePrimaryKey(ActionBo.class, actionId);
109                return ActionBo.to(bo);
110        }
111
112        /**
113         * This overridden method retrieves an Action from the repository.
114         */
115        @Override
116        public ActionDefinition getActionByNameAndNamespace(String name, String namespace) {
117        if (StringUtils.isBlank(name)) {
118            throw new IllegalArgumentException("name is blank");
119        }
120        if (StringUtils.isBlank(namespace)) {
121            throw new IllegalArgumentException("namespace is blank");
122        }
123
124        final Map<String, Object> map = new HashMap<String, Object>();
125        map.put("name", name);
126        map.put("namespace", namespace);
127
128        ActionBo myAction = businessObjectService.findByPrimaryKey(ActionBo.class, Collections.unmodifiableMap(map));
129        return ActionBo.to(myAction);
130        }
131
132        /**
133         * This overridden method retrieves a List of Actions associated with a Rule.
134         */
135        @Override
136        public List<ActionDefinition> getActionsByRuleId(String ruleId) {
137                if (StringUtils.isBlank(ruleId)){
138            throw new IllegalArgumentException("ruleId is null or blank");                      
139                }
140        final Map<String, Object> map = new HashMap<String, Object>();
141        map.put("ruleId", ruleId);
142                List<ActionBo> bos = (List<ActionBo>) businessObjectService.findMatchingOrderBy(ActionBo.class, map, "sequenceNumber", true);
143                return convertListOfBosToImmutables(bos);
144        }
145
146        /**
147         * This overridden method retrieves a specific Action associated with a Rule.
148         */
149        @Override
150        public ActionDefinition getActionByRuleIdAndSequenceNumber(String ruleId, Integer sequenceNumber) {
151                if (StringUtils.isBlank(ruleId)) {
152            throw new IllegalArgumentException("ruleId is null or blank");
153                }
154                if (sequenceNumber == null) {
155            throw new IllegalArgumentException("sequenceNumber is null");
156                }
157        final Map<String, Object> map = new HashMap<String, Object>();
158        map.put("ruleId", ruleId);
159        map.put("sequenceNumber", sequenceNumber);
160                ActionBo bo = businessObjectService.findByPrimaryKey(ActionBo.class, map);
161                return ActionBo.to(bo);
162        }
163
164        /**
165         * This method retrieves an ActionAttributeBo by id
166         *
167         * @see org.kuali.rice.krms.impl.repository.ActionBoService#getActionsByRuleId(java.lang.String)
168         */
169        public ActionAttributeBo getActionAttributeById(String attrId) {
170                if (StringUtils.isBlank(attrId)){
171            return null;                        
172                }
173        return businessObjectService.findBySinglePrimaryKey(ActionAttributeBo.class, attrId);
174        }
175
176    /**
177     * Sets the businessObjectService attribute value.
178     *
179     * @param businessObjectService The businessObjectService to set.
180     */
181    public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
182        this.businessObjectService = businessObjectService;
183    }
184
185    /**
186     * Converts a List<ActionBo> to an Unmodifiable List<Action>
187     *
188     * @param actionBos a mutable List<ActionBo> to made completely immutable.
189     * @return An unmodifiable List<Action>
190     */
191    List<ActionDefinition> convertListOfBosToImmutables(final Collection<ActionBo> actionBos) {
192        if (actionBos == null) { return Collections.emptyList(); }
193        ArrayList<ActionDefinition> actions = new ArrayList<ActionDefinition>();
194        for (ActionBo bo : actionBos) {
195            ActionDefinition action = ActionBo.to(bo);
196            actions.add(action);
197        }
198        return Collections.unmodifiableList(actions);
199    }
200
201
202}