001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.krms.impl.repository;
017    
018    
019    import org.apache.commons.lang.StringUtils;
020    import org.kuali.rice.krad.service.BusinessObjectService;
021    import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
022    import org.kuali.rice.krms.impl.util.KrmsImplConstants.PropertyNames;
023    
024    import java.util.*;
025    
026    public final class RuleBoServiceImpl implements RuleBoService {
027    
028            private BusinessObjectService businessObjectService;
029    
030            /**
031             * This overridden creates a KRMS Rule in the repository
032             * 
033             * @see org.kuali.rice.krms.impl.repository.RuleBoService#createRule(org.kuali.rice.krms.api.repository.rule.RuleDefinition)
034             */
035            @Override
036            public RuleDefinition createRule(RuleDefinition rule) {
037                    if (rule == null){
038                            throw new IllegalArgumentException("rule is null");
039                    }
040                    final String nameKey = rule.getName();
041                    final String namespaceKey = rule.getNamespace();
042                    final RuleDefinition existing = getRuleByNameAndNamespace(nameKey, namespaceKey);
043                    if (existing != null){
044                            throw new IllegalStateException("the rule to create already exists: " + rule);                  
045                    }       
046                    RuleBo ruleBo = RuleBo.from(rule);
047                    businessObjectService.save(ruleBo);
048                    return RuleBo.to(ruleBo);
049            }
050    
051            /**
052             * This overridden updates an existing Rule in the Repository
053             * 
054             * @see org.kuali.rice.krms.impl.repository.RuleBoService#updateRule(org.kuali.rice.krms.api.repository.rule.RuleDefinition)
055             */
056            @Override
057            public void updateRule(RuleDefinition rule) {
058                    if (rule == null){
059                            throw new IllegalArgumentException("rule is null");
060                    }
061    
062                    // must already exist to be able to update
063                    final String ruleIdKey = rule.getId();
064                    final RuleBo existing = businessObjectService.findBySinglePrimaryKey(RuleBo.class, ruleIdKey);
065                    if (existing == null) {
066                            throw new IllegalStateException("the rule does not exist: " + rule);
067                    }
068                    final RuleDefinition toUpdate;
069                    if (!existing.getId().equals(rule.getId())){
070                            // if passed in id does not match existing id, correct it
071                            final RuleDefinition.Builder builder = RuleDefinition.Builder.create(rule);
072                            builder.setId(existing.getId());
073                            toUpdate = builder.build();
074                    } else {
075                            toUpdate = rule;
076                    }
077                 
078                    // copy all updateable fields to bo
079                    RuleBo boToUpdate = RuleBo.from(toUpdate);
080    
081                    // delete any old, existing attributes
082                    Map<String,String> fields = new HashMap<String,String>(1);
083                    fields.put(PropertyNames.Rule.RULE_ID, toUpdate.getId());
084                    businessObjectService.deleteMatching(RuleAttributeBo.class, fields);
085            
086                    // update the rule and create new attributes
087                    businessObjectService.save(boToUpdate);
088            }
089    
090            /**
091             * This method retrieves a rule from the repository given the rule id.
092             * 
093             * @see org.kuali.rice.krms.impl.repository.RuleBoService#getRuleByRuleId(java.lang.String)
094             */
095            @Override
096            public RuleDefinition getRuleByRuleId(String ruleId) {
097                    if (StringUtils.isBlank(ruleId)){
098                            throw new IllegalArgumentException("rule id is null");
099                    }
100                    RuleBo bo = businessObjectService.findBySinglePrimaryKey(RuleBo.class, ruleId);
101                    return RuleBo.to(bo);
102            }
103    
104            /**
105             * This method retrieves a rule from the repository given the name of the rule
106             * and namespace.
107             * 
108             * @see org.kuali.rice.krms.impl.repository.RuleBoService#getRuleByRuleId(java.lang.String)
109             */
110            @Override
111            public RuleDefinition getRuleByNameAndNamespace(String name, String namespace) {
112            if (StringUtils.isBlank(name)) {
113                throw new IllegalArgumentException("name is blank");
114            }
115            if (StringUtils.isBlank(namespace)) {
116                throw new IllegalArgumentException("namespace is blank");
117            }
118    
119            final Map<String, Object> map = new HashMap<String, Object>();
120            map.put("name", name);
121            map.put("namespace", namespace);
122    
123            RuleBo myRule = businessObjectService.findByPrimaryKey(RuleBo.class, Collections.unmodifiableMap(map));
124                    return RuleBo.to(myRule);
125            }
126    
127    //      /**
128    //       * This overridden method ...
129    //       * 
130    //       * @see org.kuali.rice.krms.impl.repository.RuleBoService#createRuleAttribute(org.kuali.rice.krms.api.repository.rule.RuleAttribute)
131    //       */
132    //      @Override
133    //      public void createRuleAttribute(RuleAttribute attribute) {
134    //              if (attribute == null){
135    //                      throw new IllegalArgumentException("rule attribute is null");
136    //              }
137    //              final String attrIdKey = attribute.getId();
138    //              final RuleAttribute existing = getRuleAttributeById(attrIdKey);
139    //              if (existing != null){
140    //                      throw new IllegalStateException("the rule attribute to create already exists: " + attribute);                   
141    //              }
142    //
143    //              businessObjectService.save(RuleAttributeBo.from(attribute));            
144    //      }
145    //
146    //      /**
147    //       * This overridden method ...
148    //       * 
149    //       * @see org.kuali.rice.krms.impl.repository.RuleBoService#updateRuleAttribute(org.kuali.rice.krms.api.repository.rule.RuleAttribute)
150    //       */
151    //      @Override
152    //      public void updateRuleAttribute(RuleAttribute attribute) {
153    //              if (attribute == null){
154    //                      throw new IllegalArgumentException("rule attribute is null");
155    //              }
156    //              final String attrIdKey = attribute.getId();
157    //              final RuleAttribute existing = getRuleAttributeById(attrIdKey);
158    //              if (existing == null) {
159    //                      throw new IllegalStateException("the rule attribute does not exist: " + attribute);
160    //              }
161    //              final RuleAttribute toUpdate;
162    //              if (!existing.getId().equals(attribute.getRuleId())){
163    //                      final RuleAttribute.Builder builder = RuleAttribute.Builder.create(attribute);
164    //                      builder.setId(existing.getId());
165    //                      toUpdate = builder.build();
166    //              } else {
167    //                      toUpdate = attribute;
168    //              }
169    //
170    //              businessObjectService.save(RuleAttributeBo.from(toUpdate));
171    //      }
172    //
173            /**
174             * This method ...
175             * 
176             * @see org.kuali.rice.krms.impl.repository.RuleBoService#getRuleAttributeById(java.lang.String)
177             */
178            public RuleAttributeBo getRuleAttributeById(String attrId) {
179                    if (StringUtils.isBlank(attrId)){
180                            return null;                    
181                    }
182                    RuleAttributeBo bo = businessObjectService.findBySinglePrimaryKey(RuleAttributeBo.class, attrId);
183                    return bo;
184            }
185    
186            /**
187             * Sets the businessObjectService attribute value.
188             *
189             * @param businessObjectService The businessObjectService to set.
190             */
191            public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
192                    this.businessObjectService = businessObjectService;
193            }
194    
195            /**
196             * Converts a List<RuleBo> to an Unmodifiable List<Rule>
197             *
198             * @param RuleBos a mutable List<RuleBo> to made completely immutable.
199             * @return An unmodifiable List<Rule>
200             */
201            public List<RuleDefinition> convertListOfBosToImmutables(final Collection<RuleBo> ruleBos) {
202                    ArrayList<RuleDefinition> rules = new ArrayList<RuleDefinition>();
203                    for (RuleBo bo : ruleBos) {
204                            RuleDefinition rule = RuleBo.to(bo);
205                            rules.add(rule);
206                    }
207                    return Collections.unmodifiableList(rules);
208            }
209    
210    }