Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ContextBoServiceImpl |
|
| 4.0;4 |
1 | /** | |
2 | * Copyright 2005-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 | 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 | 0 | 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 | 0 | if (context == null){ |
46 | 0 | throw new IllegalArgumentException("context is null"); |
47 | } | |
48 | 0 | final String contextIdKey = context.getId(); |
49 | 0 | final ContextDefinition existing = getContextByContextId(contextIdKey); |
50 | 0 | if (existing != null){ |
51 | 0 | throw new IllegalStateException("the context to create already exists: " + context); |
52 | } | |
53 | 0 | ContextBo bo = (ContextBo)businessObjectService.save(ContextBo.from(context)); |
54 | 0 | 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 | 0 | if (context == null){ |
63 | 0 | throw new IllegalArgumentException("context is null"); |
64 | } | |
65 | ||
66 | // must already exist to be able to update | |
67 | 0 | final String contextIdKey = context.getId(); |
68 | 0 | final ContextBo existing = businessObjectService.findBySinglePrimaryKey(ContextBo.class, contextIdKey); |
69 | 0 | if (existing == null) { |
70 | 0 | throw new IllegalStateException("the context does not exist: " + context); |
71 | } | |
72 | final ContextDefinition toUpdate; | |
73 | 0 | if (!existing.getId().equals(context.getId())){ |
74 | // if passed in id does not match existing id, correct it | |
75 | 0 | final ContextDefinition.Builder builder = ContextDefinition.Builder.create(context); |
76 | 0 | builder.setId(existing.getId()); |
77 | 0 | toUpdate = builder.build(); |
78 | 0 | } else { |
79 | 0 | toUpdate = context; |
80 | } | |
81 | ||
82 | // copy all updateable fields to bo | |
83 | 0 | ContextBo boToUpdate = ContextBo.from(toUpdate); |
84 | ||
85 | // delete any old, existing attributes | |
86 | 0 | Map<String,String> fields = new HashMap<String,String>(1); |
87 | 0 | fields.put(PropertyNames.Context.CONTEXT_ID, toUpdate.getId()); |
88 | 0 | businessObjectService.deleteMatching(ContextAttributeBo.class, fields); |
89 | ||
90 | // update the rule and create new attributes | |
91 | 0 | businessObjectService.save(boToUpdate); |
92 | 0 | } |
93 | ||
94 | /** | |
95 | * This overridden method ... | |
96 | */ | |
97 | @Override | |
98 | public ContextDefinition getContextByContextId(String contextId) { | |
99 | 0 | if (StringUtils.isBlank(contextId)){ |
100 | 0 | return null; |
101 | } | |
102 | 0 | ContextBo bo = businessObjectService.findBySinglePrimaryKey(ContextBo.class, contextId); |
103 | 0 | return ContextBo.to(bo); |
104 | } | |
105 | ||
106 | /** | |
107 | * This overridden method ... | |
108 | */ | |
109 | public ContextDefinition getContextByNameAndNamespace( String name, String namespace ){ | |
110 | 0 | if (StringUtils.isBlank(name)){ |
111 | 0 | throw new IllegalArgumentException("name is null or blank"); |
112 | } | |
113 | 0 | if (StringUtils.isBlank(namespace)){ |
114 | 0 | throw new IllegalArgumentException("namespace is null or blank"); |
115 | } | |
116 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
117 | 0 | map.put("name", name); |
118 | 0 | map.put("namespace", namespace); |
119 | 0 | ContextBo bo = businessObjectService.findByPrimaryKey(ContextBo.class, map); |
120 | 0 | 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 | 0 | this.businessObjectService = businessObjectService; |
191 | 0 | } |
192 | ||
193 | ||
194 | } |