1 /**
2 * Copyright 2005-2016 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 package org.kuali.rice.krms.impl.repository;
17
18
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.krad.service.BusinessObjectService;
21 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
22 import org.kuali.rice.krms.impl.util.KrmsImplConstants.PropertyNames;
23
24 import java.util.*;
25
26 /**
27 * This is the interface for accessing KRMS repository Context related
28 * business objects.
29 *
30 * @author Kuali Rice Team (rice.collab@kuali.org)
31 *
32 */
33 public final class ContextBoServiceImpl implements ContextBoService {
34
35 private BusinessObjectService businessObjectService;
36
37 /**
38 * This method will create a {@link ContextDefinition} as described
39 * by the parameter passed in.
40 *
41 * @see org.kuali.rice.krms.impl.repository.ContextBoService#createContext(org.kuali.rice.krms.api.repository.context.ContextDefinition)
42 */
43 @Override
44 public ContextDefinition createContext(ContextDefinition context) {
45 if (context == null){
46 throw new IllegalArgumentException("context is null");
47 }
48 final String contextIdKey = context.getId();
49 final ContextDefinition existing = getContextByContextId(contextIdKey);
50 if (existing != null){
51 throw new IllegalStateException("the context to create already exists: " + context);
52 }
53 ContextBo bo = (ContextBo)businessObjectService.save(ContextBo.from(context));
54 return ContextBo.to(bo);
55 }
56
57 /**
58 * This method updates an existing Context in the repository.
59 */
60 @Override
61 public void updateContext(ContextDefinition context) {
62 if (context == null){
63 throw new IllegalArgumentException("context is null");
64 }
65
66 // must already exist to be able to update
67 final String contextIdKey = context.getId();
68 final ContextBo existing = businessObjectService.findBySinglePrimaryKey(ContextBo.class, contextIdKey);
69 if (existing == null) {
70 throw new IllegalStateException("the context does not exist: " + context);
71 }
72 final ContextDefinition toUpdate;
73 if (!existing.getId().equals(context.getId())){
74 // if passed in id does not match existing id, correct it
75 final ContextDefinition.Builder builder = ContextDefinition.Builder.create(context);
76 builder.setId(existing.getId());
77 toUpdate = builder.build();
78 } else {
79 toUpdate = context;
80 }
81
82 // copy all updateable fields to bo
83 ContextBo boToUpdate = ContextBo.from(toUpdate);
84
85 // delete any old, existing attributes
86 Map<String,String> fields = new HashMap<String,String>(1);
87 fields.put(PropertyNames.Context.CONTEXT_ID, toUpdate.getId());
88 businessObjectService.deleteMatching(ContextAttributeBo.class, fields);
89
90 // update the rule and create new attributes
91 businessObjectService.save(boToUpdate);
92 }
93
94 /**
95 * This overridden method ...
96 */
97 @Override
98 public ContextDefinition getContextByContextId(String contextId) {
99 if (StringUtils.isBlank(contextId)){
100 return null;
101 }
102 ContextBo bo = businessObjectService.findBySinglePrimaryKey(ContextBo.class, contextId);
103 return ContextBo.to(bo);
104 }
105
106 /**
107 * This overridden method ...
108 */
109 public ContextDefinition getContextByNameAndNamespace( String name, String namespace ){
110 if (StringUtils.isBlank(name)){
111 throw new IllegalArgumentException("name is null or blank");
112 }
113 if (StringUtils.isBlank(namespace)){
114 throw new IllegalArgumentException("namespace is null or blank");
115 }
116 final Map<String, Object> map = new HashMap<String, Object>();
117 map.put("name", name);
118 map.put("namespace", namespace);
119 ContextBo bo = businessObjectService.findByPrimaryKey(ContextBo.class, map);
120 return ContextBo.to(bo);
121 }
122
123 // /**
124 // * This overridden method ...
125 // *
126 // * @see org.kuali.rice.krms.impl.repository.ContextBoService#createContextAttribute(org.kuali.rice.krms.api.repository.ContextAttribute)
127 // */
128 // @Override
129 // public void createContextAttribute(ContextAttribute attribute) {
130 // if (attribute == null){
131 // throw new IllegalArgumentException("context attribute is null");
132 // }
133 // final String attrIdKey = attribute.getId();
134 // final ContextAttribute existing = getContextAttributeById(attrIdKey);
135 // if (existing != null){
136 // throw new IllegalStateException("the context attribute to create already exists: " + attribute);
137 // }
138 //
139 // businessObjectService.save(ContextAttributeBo.from(attribute));
140 // }
141 //
142 // /**
143 // * This overridden method ...
144 // *
145 // * @see org.kuali.rice.krms.impl.repository.ContextBoService#updateContextAttribute(org.kuali.rice.krms.api.repository.ContextAttribute)
146 // */
147 // @Override
148 // public void updateContextAttribute(ContextAttribute attribute) {
149 // if (attribute == null){
150 // throw new IllegalArgumentException("context attribute is null");
151 // }
152 // final String attrIdKey = attribute.getId();
153 // final ContextAttribute existing = getContextAttributeById(attrIdKey);
154 // if (existing == null) {
155 // throw new IllegalStateException("the context attribute does not exist: " + attribute);
156 // }
157 // final ContextAttribute toUpdate;
158 // if (!existing.getId().equals(attribute.getContextId())){
159 // final ContextAttribute.Builder builder = ContextAttribute.Builder.create(attribute);
160 // builder.setId(existing.getId());
161 // toUpdate = builder.build();
162 // } else {
163 // toUpdate = attribute;
164 // }
165 //
166 // businessObjectService.save(ContextAttributeBo.from(toUpdate));
167 // }
168 //
169 //
170 // /**
171 // * This method ...
172 // *
173 // * @see org.kuali.rice.krms.impl.repository.ContextBoService#getContextsByRuleId(java.lang.String)
174 // */
175 // public ContextAttribute getContextAttributeById(String attrId) {
176 // if (StringUtils.isBlank(attrId)){
177 // return null;
178 // }
179 // ContextAttributeBo bo = businessObjectService.findBySinglePrimaryKey(ContextAttributeBo.class, attrId);
180 // return ContextAttributeBo.to(bo);
181 // }
182
183
184 /**
185 * Sets the businessObjectService attribute value.
186 *
187 * @param businessObjectService The businessObjectService to set.
188 */
189 public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
190 this.businessObjectService = businessObjectService;
191 }
192
193
194 }