View Javadoc
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  import org.kuali.rice.core.api.mo.common.Versioned;
19  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
20  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
21  import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
22  import org.kuali.rice.krms.api.repository.term.TermResolverDefinitionContract;
23  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
24  
25  import javax.persistence.CascadeType;
26  import javax.persistence.Column;
27  import javax.persistence.Convert;
28  import javax.persistence.Entity;
29  import javax.persistence.FetchType;
30  import javax.persistence.GeneratedValue;
31  import javax.persistence.Id;
32  import javax.persistence.JoinColumn;
33  import javax.persistence.JoinTable;
34  import javax.persistence.ManyToMany;
35  import javax.persistence.ManyToOne;
36  import javax.persistence.OneToMany;
37  import javax.persistence.Table;
38  import javax.persistence.Transient;
39  import javax.persistence.Version;
40  import java.io.Serializable;
41  import java.util.Collections;
42  import java.util.HashMap;
43  import java.util.HashSet;
44  import java.util.Map;
45  import java.util.Set;
46  
47  @Entity
48  @Table(name = "KRMS_TERM_RSLVR_T")
49  public class TermResolverBo implements TermResolverDefinitionContract, Versioned, Serializable {
50  
51      private static final long serialVersionUID = 1l;
52  
53      @PortableSequenceGenerator(name = "KRMS_TERM_RSLVR_S")
54      @GeneratedValue(generator = "KRMS_TERM_RSLVR_S")
55      @Id
56      @Column(name = "TERM_RSLVR_ID")
57      private String id;
58  
59      @Column(name = "NMSPC_CD")
60      private String namespace;
61  
62      @Column(name = "NM")
63      private String name;
64  
65      @Transient
66      private String contextId;
67  
68      @Column(name = "TYP_ID")
69      private String typeId;
70  
71      @Column(name = "OUTPUT_TERM_SPEC_ID")
72      private String outputId;
73  
74      @Column(name = "ACTV")
75      @Convert(converter = BooleanYNConverter.class)
76      private boolean active = true;
77  
78      @Version
79      @Column(name="VER_NBR", length=8)
80      protected Long versionNumber;
81  
82      @ManyToOne(targetEntity = TermSpecificationBo.class, cascade = { CascadeType.REFRESH })
83      @JoinColumn(name = "OUTPUT_TERM_SPEC_ID", insertable = false, updatable = false)
84      private TermSpecificationBo output;
85  
86      @ManyToMany(targetEntity = TermSpecificationBo.class, cascade = { CascadeType.REFRESH })
87      @JoinTable(name = "KRMS_TERM_RSLVR_INPUT_SPEC_T",
88              joinColumns = { @JoinColumn(name = "TERM_RSLVR_ID", referencedColumnName = "TERM_RSLVR_ID") },
89              inverseJoinColumns = { @JoinColumn(name = "TERM_SPEC_ID", referencedColumnName = "TERM_SPEC_ID") }
90              )
91      private Set<TermSpecificationBo> prerequisites;
92  
93      @OneToMany(targetEntity = TermResolverParameterSpecificationBo.class, fetch = FetchType.LAZY, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
94      @JoinColumn(name = "TERM_RSLVR_ID", referencedColumnName = "TERM_RSLVR_ID", insertable = false, updatable = false)
95      private Set<TermResolverParameterSpecificationBo> parameterSpecifications;
96  
97      @OneToMany(targetEntity = TermResolverAttributeBo.class, fetch = FetchType.LAZY, cascade = { CascadeType.REFRESH })
98      @JoinColumn(name = "TERM_RSLVR_ID", referencedColumnName = "TERM_RSLVR_ID", insertable = false, updatable = false)
99      private Set<TermResolverAttributeBo> attributeBos;
100 
101     public void setParameterNames(Set<String> pns) {
102         if (pns != null) {
103             parameterSpecifications = new HashSet<TermResolverParameterSpecificationBo>();
104 
105             for (String pn : pns) {
106                 TermResolverParameterSpecificationBo paramSpecBo = new TermResolverParameterSpecificationBo();
107                 paramSpecBo.setName(pn);
108                 paramSpecBo.setTermResolver(this);
109                 parameterSpecifications.add(paramSpecBo);
110             }
111         }
112     }
113 
114     public Set<String> getParameterNames() {
115         Set<String> results = Collections.emptySet();
116         if (parameterSpecifications != null && parameterSpecifications.size() > 0) {
117             results = new HashSet<String>();
118 
119             for (TermResolverParameterSpecificationBo parmSpec : parameterSpecifications) {
120                 results.add(parmSpec.getName());
121             }
122         }
123 
124         return results;
125     }
126 
127     public Map<String, String> getAttributes() {
128         HashMap<String, String> attributes = new HashMap<String, String>();
129         if (attributeBos != null) for (TermResolverAttributeBo attr : attributeBos) {
130             attributes.put(attr.getAttributeDefinition().getName(), attr.getValue());
131         }
132 
133         return attributes;
134     }
135 
136     /**
137      * Converts a mutable bo to it's immutable counterpart
138      *
139      * @param bo the mutable business object
140      * @return the immutable object
141      */
142     public static TermResolverDefinition to(TermResolverBo bo) {
143         if (bo == null) {
144             return null;
145         }
146 
147         return TermResolverDefinition.Builder.create(bo).build();
148     }
149 
150     /**
151      * Converts a immutable object to it's mutable bo counterpart
152      *
153      * @param im immutable object
154      * @return the mutable bo
155      */
156     public static TermResolverBo from(TermResolverDefinition im) {
157         if (im == null) {
158             return null;
159         }
160 
161         TermResolverBo bo = new TermResolverBo();
162         bo.id = im.getId();
163         bo.namespace = im.getNamespace();
164         bo.name = im.getName();
165         bo.typeId = im.getTypeId();
166         bo.output = TermSpecificationBo.from(im.getOutput());
167         bo.outputId = im.getOutput().getId();
168         bo.setParameterNames(new HashSet<String>());
169 
170         for (String paramName : im.getParameterNames()) {
171             TermResolverParameterSpecificationBo parmSpecBo = TermResolverParameterSpecificationBo.from(im, paramName);
172             bo.parameterSpecifications.add(parmSpecBo);
173             parmSpecBo.setTermResolver(bo);
174         }
175 
176         bo.prerequisites = new HashSet<TermSpecificationBo>();
177 
178         for (TermSpecificationDefinition prereq : im.getPrerequisites()) {
179             bo.prerequisites.add(TermSpecificationBo.from(prereq));
180         }
181 
182         // build the set of term resolver attribute BOs
183         Set<TermResolverAttributeBo> attrs = new HashSet<TermResolverAttributeBo>();
184         // for each converted pair, build an TermResolverAttributeBo and add it to the set 
185         TermResolverAttributeBo attributeBo;
186 
187         for (Map.Entry<String, String> entry : im.getAttributes().entrySet()) {
188             KrmsAttributeDefinitionBo attrDefBo = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService().getKrmsAttributeBo(entry.getKey(), im.getNamespace());
189             attributeBo = new TermResolverAttributeBo();
190             attributeBo.setTermResolverId(im.getId());
191             attributeBo.setValue(entry.getValue());
192             attributeBo.setAttributeDefinition(attrDefBo);
193             ((HashSet<TermResolverAttributeBo>) attrs).add(attributeBo);
194         }
195 
196         bo.setAttributeBos(attrs);
197         bo.active = im.isActive();
198         bo.setVersionNumber(im.getVersionNumber());
199 
200         return bo;
201     }
202 
203     public TermSpecificationBo getOutput() {
204         return output;
205     }
206 
207     public String getId() {
208         return id;
209     }
210 
211     public void setId(String id) {
212         this.id = id;
213     }
214 
215     public String getNamespace() {
216         return namespace;
217     }
218 
219     public void setNamespace(String namespace) {
220         this.namespace = namespace;
221     }
222 
223     public String getName() {
224         return name;
225     }
226 
227     public void setName(String name) {
228         this.name = name;
229     }
230 
231     public String getContextId() {
232         return contextId;
233     }
234 
235     public void setContextId(String contextId) {
236         this.contextId = contextId;
237     }
238 
239     public String getTypeId() {
240         return typeId;
241     }
242 
243     public void setTypeId(String typeId) {
244         this.typeId = typeId;
245     }
246 
247     public String getOutputId() {
248         return outputId;
249     }
250 
251     public void setOutputId(String outputId) {
252         this.outputId = outputId;
253     }
254 
255     public boolean getActive() {
256         return active;
257     }
258 
259     public boolean isActive() {
260         return active;
261     }
262 
263     public void setActive(boolean active) {
264         this.active = active;
265     }
266 
267     public Long getVersionNumber() {
268         return versionNumber;
269     }
270 
271     public void setVersionNumber(Long versionNumber) {
272         this.versionNumber = versionNumber;
273     }
274 
275     public void setOutput(TermSpecificationBo output) {
276         this.output = output;
277     }
278 
279     public Set<TermSpecificationBo> getPrerequisites() {
280         return prerequisites;
281     }
282 
283     public void setPrerequisites(Set<TermSpecificationBo> prerequisites) {
284         this.prerequisites = prerequisites;
285     }
286 
287     public Set<TermResolverParameterSpecificationBo> getParameterSpecifications() {
288         return parameterSpecifications;
289     }
290 
291     public void setParameterSpecifications(Set<TermResolverParameterSpecificationBo> parameterSpecifications) {
292         this.parameterSpecifications = parameterSpecifications;
293     }
294 
295     public Set<TermResolverAttributeBo> getAttributeBos() {
296         return attributeBos;
297     }
298 
299     public void setAttributeBos(Set<TermResolverAttributeBo> attributeBos) {
300         this.attributeBos = attributeBos;
301     }
302 }