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