| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ContextBoServiceImpl | 
  | 
  | 4.0;4 | 
| 1 |  /* | |
| 2 |   * Copyright 2006-2011 The Kuali Foundation | |
| 3 |   * | |
| 4 |   * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
| 5 |   * you may not use this file except in compliance with the License. | |
| 6 |   * You may obtain a copy of the License at | |
| 7 |   * | |
| 8 |   * http://www.opensource.org/licenses/ecl2.php | |
| 9 |   * | |
| 10 |   * Unless required by applicable law or agreed to in writing, software | |
| 11 |   * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 |   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 |   * See the License for the specific language governing permissions and | |
| 14 |   * limitations under the License. | |
| 15 |   */ | |
| 16 | ||
| 17 |  package org.kuali.rice.krms.impl.repository; | |
| 18 | ||
| 19 | ||
| 20 |  import org.apache.commons.lang.StringUtils; | |
| 21 |  import org.kuali.rice.krad.service.BusinessObjectService; | |
| 22 |  import org.kuali.rice.krms.api.repository.context.ContextDefinition; | |
| 23 |  import org.kuali.rice.krms.impl.util.KRMSPropertyConstants; | |
| 24 | ||
| 25 |  import java.util.*; | |
| 26 | ||
| 27 |  /** | |
| 28 |   * This is the interface for accessing KRMS repository Context related | |
| 29 |   * business objects.  | |
| 30 |   *  | |
| 31 |   * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 32 |   * | |
| 33 |   */ | |
| 34 | 0 |  public final class ContextBoServiceImpl implements ContextBoService { | 
| 35 | ||
| 36 |      private BusinessObjectService businessObjectService; | |
| 37 | ||
| 38 |          /** | |
| 39 |           * This method will create a {@link ContextDefintion} as described | |
| 40 |           * by the parameter passed in. | |
| 41 |           *  | |
| 42 |           * @see org.kuali.rice.krms.impl.repository.ContextBoService#createContext(org.kuali.rice.krms.api.repository.context.ContextDefinition) | |
| 43 |           */ | |
| 44 | @Override  | |
| 45 |          public ContextDefinition createContext(ContextDefinition context) { | |
| 46 | 0 |                  if (context == null){ | 
| 47 | 0 |                  throw new IllegalArgumentException("context is null"); | 
| 48 | }  | |
| 49 | 0 |                  final String contextIdKey = context.getId(); | 
| 50 | 0 |                  final ContextDefinition existing = getContextByContextId(contextIdKey); | 
| 51 | 0 |                  if (existing != null){ | 
| 52 | 0 |              throw new IllegalStateException("the context to create already exists: " + context);                         | 
| 53 | }  | |
| 54 | 0 |                  ContextBo bo = (ContextBo)businessObjectService.save(ContextBo.from(context)); | 
| 55 | 0 |                  return ContextBo.to(bo); | 
| 56 | }  | |
| 57 | ||
| 58 |          /** | |
| 59 |           * This method updates an existing Context in the repository. | |
| 60 |           *  | |
| 61 |           * @see org.kuali.rice.krms.impl.repository.ContextBoService#updateContext(org.kuali.rice.krms.api.repository.context.ContextDefinition) | |
| 62 |           */ | |
| 63 | @Override  | |
| 64 | public void updateContext(ContextDefinition context) {  | |
| 65 | 0 |                  if (context == null){ | 
| 66 | 0 |                  throw new IllegalArgumentException("context is null"); | 
| 67 | }  | |
| 68 | ||
| 69 |                  // must already exist to be able to update | |
| 70 | 0 |                  final String contextIdKey = context.getId(); | 
| 71 | 0 |                  final ContextBo existing = businessObjectService.findBySinglePrimaryKey(ContextBo.class, contextIdKey); | 
| 72 | 0 |          if (existing == null) { | 
| 73 | 0 |              throw new IllegalStateException("the context does not exist: " + context); | 
| 74 | }  | |
| 75 |          final ContextDefinition toUpdate; | |
| 76 | 0 |          if (!existing.getId().equals(context.getId())){ | 
| 77 |                          // if passed in id does not match existing id, correct it | |
| 78 | 0 |                  final ContextDefinition.Builder builder = ContextDefinition.Builder.create(context); | 
| 79 | 0 |                  builder.setId(existing.getId()); | 
| 80 | 0 |                  toUpdate = builder.build(); | 
| 81 | 0 |          } else { | 
| 82 | 0 |                  toUpdate = context; | 
| 83 | }  | |
| 84 | ||
| 85 |                  // copy all updateable fields to bo | |
| 86 | 0 |                  ContextBo boToUpdate = ContextBo.from(toUpdate); | 
| 87 | ||
| 88 |                  // delete any old, existing attributes | |
| 89 | 0 |                  Map<String,String> fields = new HashMap<String,String>(1); | 
| 90 | 0 |                  fields.put(KRMSPropertyConstants.Context.CONTEXT_ID, toUpdate.getId()); | 
| 91 | 0 |                  businessObjectService.deleteMatching(ContextAttributeBo.class, fields); | 
| 92 | ||
| 93 |                  // update the rule and create new attributes | |
| 94 | 0 |          businessObjectService.save(boToUpdate); | 
| 95 | 0 |          } | 
| 96 | ||
| 97 |          /** | |
| 98 |           * This overridden method ... | |
| 99 |           *  | |
| 100 |           * @see org.kuali.rice.krms.impl.repository.ContextBoService#getContextsByRuleId(java.lang.String) | |
| 101 |           */ | |
| 102 | @Override  | |
| 103 |          public ContextDefinition getContextByContextId(String contextId) { | |
| 104 | 0 |                  if (StringUtils.isBlank(contextId)){ | 
| 105 | 0 |              return null;                         | 
| 106 | }  | |
| 107 | 0 |                  ContextBo bo = businessObjectService.findBySinglePrimaryKey(ContextBo.class, contextId); | 
| 108 | 0 |                  return ContextBo.to(bo); | 
| 109 | }  | |
| 110 | ||
| 111 |          /** | |
| 112 |           *  | |
| 113 |           * This overridden method ... | |
| 114 |           *  | |
| 115 |           * @see org.kuali.rice.krms.impl.repository.ContextBoService#getContextByNameAndNamespace(java.lang.String, java.lang.String) | |
| 116 |           */ | |
| 117 |          public ContextDefinition getContextByNameAndNamespace( String name, String namespace ){ | |
| 118 | 0 |                  if (StringUtils.isBlank(name)){ | 
| 119 | 0 |                          throw new IllegalArgumentException("name is null or blank"); | 
| 120 | }  | |
| 121 | 0 |                  if (StringUtils.isBlank(namespace)){ | 
| 122 | 0 |                          throw new IllegalArgumentException("namespace is null or blank"); | 
| 123 | }  | |
| 124 | 0 |          final Map<String, Object> map = new HashMap<String, Object>(); | 
| 125 | 0 |          map.put("name", name); | 
| 126 | 0 |          map.put("namespace", namespace); | 
| 127 | 0 |                  ContextBo bo = businessObjectService.findByPrimaryKey(ContextBo.class, map); | 
| 128 | 0 |                  return ContextBo.to(bo); | 
| 129 | }  | |
| 130 | ||
| 131 |  //        /** | |
| 132 |  //         * This overridden method ... | |
| 133 |  //         *  | |
| 134 |  //         * @see org.kuali.rice.krms.impl.repository.ContextBoService#createContextAttribute(org.kuali.rice.krms.api.repository.ContextAttribute) | |
| 135 |  //         */ | |
| 136 |  //        @Override | |
| 137 |  //        public void createContextAttribute(ContextAttribute attribute) { | |
| 138 |  //                if (attribute == null){ | |
| 139 |  //                throw new IllegalArgumentException("context attribute is null"); | |
| 140 |  //                } | |
| 141 |  //                final String attrIdKey = attribute.getId(); | |
| 142 |  //                final ContextAttribute existing = getContextAttributeById(attrIdKey); | |
| 143 |  //                if (existing != null){ | |
| 144 |  //            throw new IllegalStateException("the context attribute to create already exists: " + attribute);                         | |
| 145 |  //                } | |
| 146 |  //                 | |
| 147 |  //                businessObjectService.save(ContextAttributeBo.from(attribute));                 | |
| 148 |  //        } | |
| 149 |  // | |
| 150 |  //        /** | |
| 151 |  //         * This overridden method ... | |
| 152 |  //         *  | |
| 153 |  //         * @see org.kuali.rice.krms.impl.repository.ContextBoService#updateContextAttribute(org.kuali.rice.krms.api.repository.ContextAttribute) | |
| 154 |  //         */ | |
| 155 |  //        @Override | |
| 156 |  //        public void updateContextAttribute(ContextAttribute attribute) { | |
| 157 |  //                if (attribute == null){ | |
| 158 |  //                throw new IllegalArgumentException("context attribute is null"); | |
| 159 |  //                } | |
| 160 |  //                final String attrIdKey = attribute.getId(); | |
| 161 |  //                final ContextAttribute existing = getContextAttributeById(attrIdKey); | |
| 162 |  //        if (existing == null) { | |
| 163 |  //            throw new IllegalStateException("the context attribute does not exist: " + attribute); | |
| 164 |  //        } | |
| 165 |  //        final ContextAttribute toUpdate; | |
| 166 |  //        if (!existing.getId().equals(attribute.getContextId())){ | |
| 167 |  //                final ContextAttribute.Builder builder = ContextAttribute.Builder.create(attribute); | |
| 168 |  //                builder.setId(existing.getId()); | |
| 169 |  //                toUpdate = builder.build(); | |
| 170 |  //        } else { | |
| 171 |  //                toUpdate = attribute; | |
| 172 |  //        } | |
| 173 |  //         | |
| 174 |  //        businessObjectService.save(ContextAttributeBo.from(toUpdate));                 | |
| 175 |  //        } | |
| 176 |  //         | |
| 177 |  //         | |
| 178 |  //        /** | |
| 179 |  //         * This method ... | |
| 180 |  //         *  | |
| 181 |  //         * @see org.kuali.rice.krms.impl.repository.ContextBoService#getContextsByRuleId(java.lang.String) | |
| 182 |  //         */ | |
| 183 |  //        public ContextAttribute getContextAttributeById(String attrId) { | |
| 184 |  //                if (StringUtils.isBlank(attrId)){ | |
| 185 |  //            return null;                         | |
| 186 |  //                } | |
| 187 |  //                ContextAttributeBo bo = businessObjectService.findBySinglePrimaryKey(ContextAttributeBo.class, attrId); | |
| 188 |  //                return ContextAttributeBo.to(bo); | |
| 189 |  //        } | |
| 190 | ||
| 191 | ||
| 192 |      /** | |
| 193 |       * Sets the businessObjectService attribute value. | |
| 194 |       * | |
| 195 |       * @param businessObjectService The businessObjectService to set. | |
| 196 |       */ | |
| 197 | public void setBusinessObjectService(final BusinessObjectService businessObjectService) {  | |
| 198 | 0 |          this.businessObjectService = businessObjectService; | 
| 199 | 0 |      } | 
| 200 | ||
| 201 | ||
| 202 | }  |