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.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 org.kuali.rice.krms.api.repository.term.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             parameters.clear();
153         }
154 
155         for (Entry<String, String> paramEntry : parametersMap.entrySet()) {
156             TermParameterDefinition termDef = TermParameterDefinition.Builder.create(null, id, paramEntry.getKey(),
157                     paramEntry.getValue()).build();
158             parameters.add(TermParameterBo.from(termDef));
159         }
160     }
161 
162     public String getDescription() {
163         return description;
164     }
165 
166     public void setDescription(String description) {
167         this.description = description;
168     }
169 
170     public String getSpecificationId() {
171         return specificationId;
172     }
173 
174     public void setSpecificationId(String specificationId) {
175         this.specificationId = specificationId;
176     }
177 
178     public String getId() {
179         return id;
180     }
181 
182     public void setId(String id) {
183         this.id = id;
184     }
185 
186     public Long getVersionNumber() {
187         return versionNumber;
188     }
189 
190     public void setVersionNumber(Long versionNumber) {
191         this.versionNumber = versionNumber;
192     }
193 }