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;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
20  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
21  import org.kuali.rice.krms.api.repository.category.CategoryDefinition;
22  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
23  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
24  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinitionContract;
25  
26  import javax.persistence.CascadeType;
27  import javax.persistence.Column;
28  import javax.persistence.Convert;
29  import javax.persistence.Entity;
30  import javax.persistence.FetchType;
31  import javax.persistence.GeneratedValue;
32  import javax.persistence.Id;
33  import javax.persistence.JoinColumn;
34  import javax.persistence.JoinTable;
35  import javax.persistence.ManyToMany;
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.ArrayList;
42  import java.util.List;
43  
44  @Entity
45  @Table(name = "KRMS_TERM_SPEC_T")
46  public class TermSpecificationBo implements TermSpecificationDefinitionContract, Serializable {
47  
48      private static final long serialVersionUID = 1l;
49  
50      @PortableSequenceGenerator(name = "KRMS_TERM_SPEC_S")
51      @GeneratedValue(generator = "KRMS_TERM_SPEC_S")
52      @Id
53      @Column(name = "TERM_SPEC_ID")
54      private String id;
55  
56      @Column(name = "NM")
57      private String name;
58  
59      @Column(name = "NMSPC_CD")
60      private String namespace;
61  
62      @Column(name = "TYP")
63      private String type;
64  
65      @Column(name = "DESC_TXT")
66      private String description;
67  
68      @Column(name = "ACTV")
69      @Convert(converter = BooleanYNConverter.class)
70      private boolean active = true;
71  
72      @Column(name = "VER_NBR")
73      @Version
74      private Long versionNumber;
75  
76      @ManyToMany(targetEntity = CategoryBo.class, cascade = { CascadeType.REFRESH })
77      @JoinTable(name = "KRMS_TERM_SPEC_CTGRY_T",
78              joinColumns = { @JoinColumn(name = "TERM_SPEC_ID", referencedColumnName = "TERM_SPEC_ID") },
79              inverseJoinColumns = { @JoinColumn(name = "CTGRY_ID", referencedColumnName = "CTGRY_ID") })
80      private List<CategoryBo> categories = new ArrayList<CategoryBo>();
81  
82      @OneToMany(orphanRemoval = true, mappedBy = "termSpecification", fetch = FetchType.LAZY,
83              cascade = { CascadeType.REFRESH, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.PERSIST })
84      @JoinColumn(name = "TERM_SPEC_ID", referencedColumnName = "TERM_SPEC_ID")
85      private List<ContextValidTermBo> contextValidTerms = new ArrayList<ContextValidTermBo>();
86  
87      @Transient
88      private List<String> contextIds = new ArrayList<String>();
89  
90      @Transient
91      private List<ContextBo> contexts = new ArrayList<ContextBo>();
92  
93      /**
94       * Converts a mutable bo to it's immutable counterpart
95       *
96       * @param bo the mutable business object
97       * @return the immutable object
98       */
99      public static TermSpecificationDefinition to(TermSpecificationBo bo) {
100         if (bo == null) {
101             return null;
102         }
103 
104         return TermSpecificationDefinition.Builder.create(bo).build();
105     }
106 
107     /**
108      * Converts a immutable object to it's mutable bo counterpart
109      *
110      * @param im immutable object
111      * @return the mutable bo
112      */
113     public static TermSpecificationBo from(TermSpecificationDefinition im) {
114         if (im == null) {
115             return null;
116         }
117 
118         TermSpecificationBo bo = new TermSpecificationBo();
119         bo.id = im.getId();
120         bo.namespace = im.getNamespace();
121         bo.name = im.getName();
122         bo.type = im.getType();
123         bo.description = im.getDescription();
124         bo.categories = new ArrayList<CategoryBo>();
125 
126         for (CategoryDefinition category : im.getCategories()) {
127             bo.categories.add(CategoryBo.from(category));
128         }
129 
130         bo.contextIds.addAll(im.getContextIds());
131         bo.active = im.isActive();
132         bo.setVersionNumber(im.getVersionNumber());
133 
134         return bo;
135     }
136 
137     public String getId() {
138         return id;
139     }
140 
141     public void setId(String id) {
142         this.id = id;
143     }
144 
145     public String getName() {
146         return name;
147     }
148 
149     public void setName(String name) {
150         this.name = name;
151     }
152 
153     public String getNamespace() {
154         return namespace;
155     }
156 
157     public void setNamespace(String namespace) {
158         this.namespace = namespace;
159     }
160 
161     public String getType() {
162         return type;
163     }
164 
165     public void setType(String type) {
166         this.type = type;
167     }
168 
169     public String getDescription() {
170         return description;
171     }
172 
173     public void setDescription(String description) {
174         this.description = description;
175     }
176 
177     public boolean getActive() {
178         return active;
179     }
180 
181     public boolean isActive() {
182         return active;
183     }
184 
185     public void setActive(boolean active) {
186         this.active = active;
187     }
188 
189     public Long getVersionNumber() {
190         return versionNumber;
191     }
192 
193     public void setVersionNumber(Long versionNumber) {
194         this.versionNumber = versionNumber;
195     }
196 
197     public List<CategoryBo> getCategories() {
198         return categories;
199     }
200 
201     public void setCategories(List<CategoryBo> categories) {
202         this.categories = categories;
203     }
204 
205     public List<String> getContextIds() {
206         if(CollectionUtils.isEmpty(contextIds)){
207             for(ContextValidTermBo contextValidTermBo : this.getContextValidTerms()) {
208                 contextIds.add(contextValidTermBo.getContextId());
209             }
210         }
211         return contextIds;
212     }
213 
214     public void setContextIds(List<String> contextIds) {
215         this.contextIds = contextIds;
216     }
217 
218     public List<ContextBo> getContexts() {
219         if(CollectionUtils.isEmpty(contexts)){
220             for(ContextValidTermBo contextValidTermBo : this.getContextValidTerms()) {
221                 ContextDefinition context =
222                         KrmsRepositoryServiceLocator.getContextBoService().getContextByContextId(contextValidTermBo.getContextId());
223 
224                 if (context != null) {
225                     contexts.add(ContextBo.from(context));
226                 }
227             }
228         }
229         return contexts;
230     }
231 
232     public void setContexts(List<ContextBo> contexts) {
233         this.contexts = contexts;
234     }
235 
236     public List<ContextValidTermBo> getContextValidTerms() {
237         return contextValidTerms;
238     }
239 
240     public void setContextValidTerms(List<ContextValidTermBo> contextValidTerms) {
241         this.contextValidTerms = contextValidTerms;
242     }
243 }