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 java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.persistence.CascadeType;
25  import javax.persistence.Column;
26  import javax.persistence.Convert;
27  import javax.persistence.Entity;
28  import javax.persistence.FetchType;
29  import javax.persistence.GeneratedValue;
30  import javax.persistence.Id;
31  import javax.persistence.JoinColumn;
32  import javax.persistence.OneToMany;
33  import javax.persistence.Table;
34  import javax.persistence.Version;
35  
36  import org.apache.commons.lang.StringUtils;
37  import org.kuali.rice.krad.data.CopyOption;
38  import org.kuali.rice.krad.data.KradDataServiceLocator;
39  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
40  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
41  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
42  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
43  import org.kuali.rice.krms.api.repository.context.ContextDefinitionContract;
44  
45  @Entity
46  @Table(name = "KRMS_CNTXT_T")
47  public class ContextBo implements ContextDefinitionContract, Serializable {
48  
49      private static final long serialVersionUID = 1L;
50  
51      public static final String CONTEXT_SEQ_NAME = "KRMS_CNTXT_S";
52  
53      @PortableSequenceGenerator(name = CONTEXT_SEQ_NAME)
54      @GeneratedValue(generator = CONTEXT_SEQ_NAME)
55      @Id
56      @Column(name = "CNTXT_ID")
57      private String id;
58  
59      @Column(name = "NM")
60      private String name;
61  
62      @Column(name = "NMSPC_CD")
63      private String namespace;
64  
65      @Column(name = "TYP_ID")
66      private String typeId;
67  
68      @Column(name = "DESC_TXT")
69      private String description;
70  
71      @Column(name = "ACTV")
72      @Convert(converter = BooleanYNConverter.class)
73      private boolean active = true;
74  
75      @OneToMany(mappedBy = "context")
76      @JoinColumn(name = "CNTXT_ID", referencedColumnName = "CNTXT_ID", insertable = false, updatable = false)
77      private List<AgendaBo> agendas = new ArrayList<AgendaBo>();
78  
79      @OneToMany(
80              targetEntity = ContextAttributeBo.class, orphanRemoval = true, mappedBy = "context",
81              cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST },
82              fetch = FetchType.LAZY
83      )
84      @JoinColumn(name = "CNTXT_ID", referencedColumnName = "CNTXT_ID", insertable = true, updatable = true)
85      private List<ContextAttributeBo> attributeBos = new ArrayList<ContextAttributeBo>();
86  
87      @Column(name = "VER_NBR")
88      @Version
89      private Long versionNumber;
90  
91      @Override
92      public List<AgendaBo> getAgendas() {
93          return agendas;
94      }
95  
96      @Override
97      public Map<String, String> getAttributes() {
98          Map<String, String> attributes = new HashMap<String, String>();
99  
100         if (attributeBos != null) {
101             for (ContextAttributeBo attr : attributeBos) {
102                 ((HashMap<String, String>) attributes).put(attr.getAttributeDefinition().getName(), attr.getValue());
103             }
104         }
105 
106         return attributes;
107     }
108 
109     public ContextBo copyContext(String additionalNameText) {
110         ContextBo copy = KradDataServiceLocator.getDataObjectService().copyInstance(this, CopyOption.RESET_PK_FIELDS, CopyOption.RESET_OBJECT_ID );
111         //ContextBo copy = (ContextBo) SerializationUtils.deepCopy(this);
112 
113         //
114         // set all IDs to null
115         //
116 
117         copy.setId(null);
118 
119         // copying a context does not copy the associated agendas
120         copy.setAgendas(null);
121         for (ContextAttributeBo attributeBo : copy.getAttributeBos()) {
122             attributeBo.setId(null);
123             attributeBo.setVersionNumber(null);
124         }
125 
126         if (!StringUtils.isEmpty(additionalNameText)) {
127             copy.setName(copy.getName() + additionalNameText);
128         }
129 
130         return copy;
131     }
132 
133     /**
134      * Converts a mutable bo to it's immutable counterpart
135      *
136      * @param bo the mutable business object
137      * @return the immutable object
138      */
139     public static ContextDefinition to(ContextBo bo) {
140         if (bo == null) {
141             return null;
142         }
143 
144         return ContextDefinition.Builder.create(bo).build();
145     }
146 
147     /**
148      * Converts a immutable object to it's mutable bo counterpart
149      *
150      * @param im immutable object
151      * @return the mutable bo
152      */
153     public static ContextBo from(ContextDefinition im) {
154         if (im == null) {
155             return null;
156         }
157 
158         ContextBo bo = new ContextBo();
159         bo.id = im.getId();
160         bo.namespace = im.getNamespace();
161         bo.name = im.getName();
162         bo.typeId = im.getTypeId();
163         bo.description = im.getDescription();
164         bo.active = im.isActive();
165         bo.agendas = new ArrayList<AgendaBo>();
166         for (AgendaDefinition agenda : im.getAgendas()) {
167             bo.agendas.add(KrmsRepositoryServiceLocator.getAgendaBoService().from(agenda));
168         }
169 
170         // build the list of agenda attribute BOs
171         List<ContextAttributeBo> attrs = new ArrayList<ContextAttributeBo>();
172 
173         // for each converted pair, build an AgendaAttributeBo and add it to the list
174         ContextAttributeBo attributeBo;
175         for (Map.Entry<String, String> entry : im.getAttributes().entrySet()) {
176             KrmsAttributeDefinitionBo attrDefBo =
177                     KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService().getKrmsAttributeBo(entry.getKey(), im.getNamespace());
178             attributeBo = new ContextAttributeBo();
179             attributeBo.setContext(bo);
180             attributeBo.setValue(entry.getValue());
181             attributeBo.setAttributeDefinition(attrDefBo);
182             attrs.add(attributeBo);
183         }
184 
185         bo.setAttributeBos(attrs);
186         bo.versionNumber = im.getVersionNumber();
187 
188         return bo;
189     }
190 
191     @Override
192     public String getId() {
193         return id;
194     }
195 
196     public void setId(String id) {
197         this.id = id;
198     }
199 
200     @Override
201     public String getName() {
202         return name;
203     }
204 
205     public void setName(String name) {
206         this.name = name;
207     }
208 
209     @Override
210     public String getNamespace() {
211         return namespace;
212     }
213 
214     public void setNamespace(String namespace) {
215         this.namespace = namespace;
216     }
217 
218     @Override
219     public String getTypeId() {
220         return typeId;
221     }
222 
223     public void setTypeId(String typeId) {
224         this.typeId = typeId;
225     }
226 
227     @Override
228     public String getDescription() {
229         return description;
230     }
231 
232     public void setDescription(String description) {
233         this.description = description;
234     }
235 
236     public boolean getActive() {
237         return active;
238     }
239 
240     @Override
241     public boolean isActive() {
242         return active;
243     }
244 
245     public void setActive(boolean active) {
246         this.active = active;
247     }
248 
249     public void setAgendas(List<AgendaBo> agendas) {
250         this.agendas = agendas;
251     }
252 
253     public List<ContextAttributeBo> getAttributeBos() {
254         return attributeBos;
255     }
256 
257     public void setAttributeBos(List<ContextAttributeBo> attributeBos) {
258         this.attributeBos = attributeBos;
259     }
260 
261     @Override
262     public Long getVersionNumber() {
263         return versionNumber;
264     }
265 
266     public void setVersionNumber(Long versionNumber) {
267         this.versionNumber = versionNumber;
268     }
269 }