View Javadoc

1   /**
2    * Copyright 2005-2015 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.mock;
17  
18  import java.util.ArrayList;
19  import java.util.LinkedHashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.UUID;
23  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
24  import org.kuali.rice.krms.api.repository.term.TermDefinition;
25  import org.kuali.rice.krms.api.repository.term.TermRepositoryService;
26  import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
27  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
28  
29  public class TermRepositoryServiceMockImpl implements TermRepositoryService {
30      // cache variable 
31      // The LinkedHashMap is just so the values come back in a predictable order
32  
33      private Map<String, TermDefinition> termMap = new LinkedHashMap<String, TermDefinition>();
34      private Map<String, TermSpecificationDefinition> termSpecificationMap = new LinkedHashMap<String, TermSpecificationDefinition>();
35      private Map<String, TermResolverDefinition> termResolverMap = new LinkedHashMap<String, TermResolverDefinition>();
36  
37      public void clear() {
38          this.termMap.clear();
39          this.termSpecificationMap.clear();
40          this.termResolverMap.clear();
41      }
42  
43      @Override
44      public List<TermResolverDefinition> findTermResolversByNamespace(String namespace)
45              throws RiceIllegalArgumentException {
46          List<TermResolverDefinition> list = new ArrayList<TermResolverDefinition>();
47          for (TermResolverDefinition info : this.termResolverMap.values()) {
48              if (info.getNamespace().equals(namespace)) {
49                  list.add(info);
50              }
51          }
52          return list;
53      }
54  
55      @Override
56      public TermDefinition getTerm(String termId)
57              throws RiceIllegalArgumentException {
58          // GET_BY_ID
59          if (!this.termMap.containsKey(termId)) {
60              throw new RiceIllegalArgumentException(termId);
61          }
62          return this.termMap.get(termId);
63      }
64  
65      @Override
66      public TermSpecificationDefinition getTermSpecificationById(String id) throws RiceIllegalArgumentException {
67          // GET_BY_ID
68          if (!this.termSpecificationMap.containsKey(id)) {
69              throw new RiceIllegalArgumentException(id);
70          }
71          return this.termSpecificationMap.get(id);
72      }
73  
74      @Override
75      public TermSpecificationDefinition createTermSpecification(TermSpecificationDefinition termSpec)
76              throws RiceIllegalArgumentException {
77          try {
78              TermSpecificationDefinition orig = this.getTermSpecificationById(termSpec.getId());
79              if (orig != null) {
80                  throw new RiceIllegalArgumentException(termSpec.getId() + "." + termSpec.getName());
81              }
82          } catch (RiceIllegalArgumentException ex) {
83              // same as getting null
84          }
85          TermSpecificationDefinition.Builder copy = TermSpecificationDefinition.Builder.create(termSpec);
86          if (copy.getId() == null) {
87              copy.setId(UUID.randomUUID().toString());
88          }
89          TermSpecificationDefinition termSpecDefinition = copy.build();
90          termSpecificationMap.put(termSpecDefinition.getId(), termSpecDefinition);
91          return termSpecDefinition;
92      }
93  
94      @Override
95      public TermDefinition createTerm(TermDefinition termDef) throws RiceIllegalArgumentException {
96          try {
97              TermDefinition orig = this.getTerm(termDef.getId());
98              if (orig != null) {
99                  throw new RiceIllegalArgumentException(termDef.getId());
100             }
101         } catch (RiceIllegalArgumentException ex) {
102             // same as getting null
103         }
104         TermDefinition.Builder copy = TermDefinition.Builder.create(termDef);
105         if (copy.getId() == null) {
106             copy.setId(UUID.randomUUID().toString());
107         }
108         TermDefinition termDefinition = copy.build();
109         termMap.put(termDefinition.getId(), termDefinition);
110         return termDefinition;
111     }
112 
113     @Override
114     public TermResolverDefinition getTermResolverById(String id) throws RiceIllegalArgumentException {
115         // GET_BY_ID
116         if (!this.termResolverMap.containsKey(id)) {
117             throw new RiceIllegalArgumentException(id);
118         }
119         return this.termResolverMap.get(id);
120     }
121 
122     @Override
123     public List<TermResolverDefinition> findTermResolversByOutputId(String id, String namespace)
124             throws RiceIllegalArgumentException {
125         List<TermResolverDefinition> list = new ArrayList<TermResolverDefinition>();
126         for (TermResolverDefinition info : this.termResolverMap.values()) {
127             if (info.getNamespace().equals(namespace)) {
128                 if (info.getOutput().getId().equals(id)) {
129                     list.add(info);
130                 }
131             }
132         }
133         return list;
134     }
135 
136     @Override
137     public TermResolverDefinition createTermResolver(TermResolverDefinition termResolver) throws RiceIllegalArgumentException {
138         try {
139             TermResolverDefinition orig = this.getTermResolverById(termResolver.getId());
140             if (orig != null) {
141                 throw new RiceIllegalArgumentException(termResolver.getId() + "." + termResolver.getName());
142             }
143         } catch (RiceIllegalArgumentException ex) {
144             // same as getting null
145         }
146         TermResolverDefinition.Builder copy = TermResolverDefinition.Builder.create(termResolver);
147         if (copy.getId() == null) {
148             copy.setId(UUID.randomUUID().toString());
149         }
150         TermResolverDefinition termResolverDefinition = copy.build();
151         termResolverMap.put(termResolverDefinition.getId(), termResolverDefinition);
152         return termResolverDefinition;
153     }
154 
155     @Override
156     public void updateTermSpecification(TermSpecificationDefinition termSpec) throws RiceIllegalArgumentException {
157         TermSpecificationDefinition existing = this.getTermSpecificationById(termSpec.getId());
158         if (existing == null) {
159             throw new RiceIllegalArgumentException(termSpec.getId() + " does not exist");
160         }
161         this.termSpecificationMap.put(termSpec.getId(), termSpec);
162 
163     }
164 
165     @Override
166     public void deleteTermSpecification(String id) throws RiceIllegalArgumentException {
167         if (this.termSpecificationMap.remove(id) == null) {
168             throw new RiceIllegalArgumentException(id + " does not exist");
169         }
170     }
171 
172     @Override
173     public void updateTerm(TermDefinition termDef) throws RiceIllegalArgumentException {
174         TermDefinition existing = this.getTerm(termDef.getId());
175         if (existing == null) {
176             throw new RiceIllegalArgumentException(termDef.getId() + " does not exist");
177         }
178         this.termMap.put(termDef.getId(), termDef);
179     }
180 
181     @Override
182     public void deleteTerm(String id) throws RiceIllegalArgumentException {
183         if (this.termMap.remove(id) == null) {
184             throw new RiceIllegalArgumentException(id + " does not exist");
185         }
186     }
187 
188     @Override
189     public void updateTermResolver(TermResolverDefinition termResolver) throws RiceIllegalArgumentException {
190         TermResolverDefinition existing = this.getTermResolverById(termResolver.getId());
191         if (existing == null) {
192             throw new RiceIllegalArgumentException(termResolver.getId() + " does not exist");
193         }
194         this.termResolverMap.put(termResolver.getId(), termResolver);
195     }
196 
197     @Override
198     public void deleteTermResolver(String id) throws RiceIllegalArgumentException {
199         if (this.termResolverMap.remove(id) == null) {
200             throw new RiceIllegalArgumentException(id + " does not exist");
201         }
202     }
203 
204     @Override
205     public TermResolverDefinition getTermResolverByNameAndNamespace(String name, String namespace) 
206             throws RiceIllegalArgumentException {
207       if (name == null || name.trim().isEmpty()) {
208           throw new RiceIllegalArgumentException ("name is " + name);
209       }
210       if (namespace == null || namespace.trim().isEmpty()) {
211           throw new RiceIllegalArgumentException ("namespace is " + namespace);
212       }
213       for (TermResolverDefinition info : this.termResolverMap.values()) {
214           if (namespace.equals(info.getNamespace())) {
215               if (name.equals(name)) {
216                   return info;
217               }
218           }
219       }
220       return null;
221     }
222 
223     @Override
224     public TermSpecificationDefinition getTermSpecificationByNameAndNamespace(String name, String namespace) 
225             throws RiceIllegalArgumentException {
226       if (name == null || name.trim().isEmpty()) {
227           throw new RiceIllegalArgumentException ("name is " + name);
228       }
229       if (namespace == null || namespace.trim().isEmpty()) {
230           throw new RiceIllegalArgumentException ("namespace is " + namespace);
231       }
232       for (TermSpecificationDefinition info : this.termSpecificationMap.values()) {
233           if (namespace.equals(info.getNamespace())) {
234               if (name.equals(name)) {
235                   return info;
236               }
237           }
238       }
239       return null;
240     }
241 
242     @Override
243     public List<TermSpecificationDefinition> findAllTermSpecificationsByContextId(String contextId) {
244 	throw new UnsupportedOperationException(getClass().getName() + " does not support this operation");
245     }
246 }