View Javadoc
1   /**
2    * Copyright 2005-2014 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.krad.data.jpa.PortableSequenceGenerator;
19  import org.kuali.rice.krms.api.repository.term.TermDefinition;
20  import org.kuali.rice.krms.api.repository.term.TermDefinitionContract;
21  import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
22  
23  import javax.persistence.CascadeType;
24  import javax.persistence.Column;
25  import javax.persistence.Entity;
26  import javax.persistence.GeneratedValue;
27  import javax.persistence.Id;
28  import javax.persistence.JoinColumn;
29  import javax.persistence.ManyToOne;
30  import javax.persistence.OneToMany;
31  import javax.persistence.Table;
32  import javax.persistence.Transient;
33  import javax.persistence.Version;
34  import java.io.Serializable;
35  import java.util.ArrayList;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.Map.Entry;
40  
41  @Entity
42  @Table(name = "KRMS_TERM_T")
43  public class TermBo implements TermDefinitionContract, Serializable {
44  
45      private static final long serialVersionUID = 1l;
46  
47      public static final String TERM_SEQ_NAME = "KRMS_TERM_S";
48  
49      @PortableSequenceGenerator(name = TERM_SEQ_NAME) @GeneratedValue(generator = TERM_SEQ_NAME) @Id @Column(
50              name = "TERM_ID")
51      private String id;
52  
53      @Column(name = "TERM_SPEC_ID")
54      private String specificationId;
55  
56      @Column(name = "DESC_TXT")
57      private String description;
58  
59      @Version @Column(name = "VER_NBR", length = 8)
60      protected Long versionNumber;
61  
62      // new-ing up an empty one allows the TermBo lookup to work on fields in the term specification:
63      @ManyToOne(targetEntity = TermSpecificationBo.class, cascade = {CascadeType.REFRESH})
64      @JoinColumn(name = "TERM_SPEC_ID", referencedColumnName = "TERM_SPEC_ID", insertable = false, updatable = false)
65      private TermSpecificationBo specification = new TermSpecificationBo();
66  
67      @OneToMany(targetEntity = TermParameterBo.class, orphanRemoval = true,
68              cascade = {CascadeType.REFRESH, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.PERSIST},
69              mappedBy = "term"
70      )
71      private List<TermParameterBo> parameters;
72  
73      @Transient
74      private Map<String, String> parametersMap = new HashMap<String, String>();
75  
76      /**
77       * Converts a mutable bo to it's immutable counterpart
78       *
79       * @param bo the mutable business object
80       * @return the immutable object
81       */
82      public static TermDefinition to(TermBo bo) {
83          if (bo == null) {
84              return null;
85          }
86  
87          return TermDefinition.Builder.create(bo).build();
88      }
89  
90      /**
91       * Converts a immutable object to it's mutable bo counterpart
92       *
93       * @param im immutable object
94       * @return the mutable bo
95       */
96      public static TermBo from(TermDefinition im) {
97          if (im == null) {
98              return null;
99          }
100 
101         TermBo bo = new TermBo();
102         bo.id = im.getId();
103         bo.specificationId = im.getSpecification().getId();
104         bo.description = im.getDescription();
105         bo.specification = TermSpecificationBo.from(im.getSpecification());
106         bo.parameters = new ArrayList<TermParameterBo>();
107 
108         for (TermParameterDefinition parm : im.getParameters()) {
109             TermParameterBo termParmBo = TermParameterBo.from(parm);
110             bo.parameters.add(termParmBo);
111             termParmBo.setTerm(bo);
112         }
113 
114         bo.versionNumber = im.getVersionNumber();
115 
116         return bo;
117     }
118 
119     public TermSpecificationBo getSpecification() {
120         return specification;
121     }
122 
123     public void setSpecification(TermSpecificationBo specification) {
124         this.specification = specification;
125     }
126 
127     public List<TermParameterBo> getParameters() {
128         return parameters;
129     }
130 
131     public Map<String, String> getParametersMap() {
132         return parametersMap;
133     }
134 
135     public void setParameters(List<TermParameterBo> parameters) {
136         this.parameters = parameters;
137     }
138 
139     public void exportToParametersMap() {
140         // merge in TermParameterBo values 
141         if (parameters != null) {
142             for (TermParameterBo param : parameters) {
143                 parametersMap.put(param.getName(), param.getValue());
144             }
145         }
146     }
147 
148     public void importFromParametersMap() {
149         if (parameters == null) {
150             parameters = new ArrayList<TermParameterBo>();
151         } else {
152             if (parametersMap == null || parametersMap.isEmpty()) {
153                 for (TermParameterBo param : parameters) {
154                     parametersMap.put(param.getName(), param.getValue());
155                 }
156             }
157             parameters.clear();
158         }
159 
160         for (Entry<String, String> paramEntry : parametersMap.entrySet()) {
161             TermParameterDefinition termDef = TermParameterDefinition.Builder.create(null, id, paramEntry.getKey(),
162                     paramEntry.getValue()).build();
163             parameters.add(TermParameterBo.from(termDef));
164         }
165     }
166 
167     public String getDescription() {
168         return description;
169     }
170 
171     public void setDescription(String description) {
172         this.description = description;
173     }
174 
175     public String getSpecificationId() {
176         return specificationId;
177     }
178 
179     public void setSpecificationId(String specificationId) {
180         this.specificationId = specificationId;
181     }
182 
183     public String getId() {
184         return id;
185     }
186 
187     public void setId(String id) {
188         this.id = id;
189     }
190 
191     public Long getVersionNumber() {
192         return versionNumber;
193     }
194 
195     public void setVersionNumber(Long versionNumber) {
196         this.versionNumber = versionNumber;
197     }
198 }