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.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.util.io.SerializationUtils;
20  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
21  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
22  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
23  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinitionContract;
24  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
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.GeneratedValue;
31  import javax.persistence.Id;
32  import javax.persistence.JoinColumn;
33  import javax.persistence.ManyToOne;
34  import javax.persistence.OneToMany;
35  import javax.persistence.Table;
36  import javax.persistence.Version;
37  import java.io.Serializable;
38  import java.util.ArrayList;
39  import java.util.HashMap;
40  import java.util.HashSet;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Set;
44  
45  @Entity
46  @Table(name = "KRMS_AGENDA_T")
47  public class AgendaBo implements AgendaDefinitionContract, Serializable {
48  
49      private static final long serialVersionUID = 1L;
50  
51      public static final String AGENDA_SEQ_NAME = "KRMS_AGENDA_S";
52      static final RepositoryBoIncrementer agendaIdIncrementer = new RepositoryBoIncrementer(AGENDA_SEQ_NAME);
53  
54      @PortableSequenceGenerator(name = AGENDA_SEQ_NAME)
55      @GeneratedValue(generator = AGENDA_SEQ_NAME)
56      @Id
57      @Column(name = "AGENDA_ID")
58      private String id;
59  
60      @Column(name = "NM")
61      private String name;
62  
63      @Column(name = "TYP_ID")
64      private String typeId;
65  
66      @Column(name = "CNTXT_ID")
67      private String contextId;
68  
69      @Column(name = "ACTV")
70      @Convert(converter = BooleanYNConverter.class)
71      private boolean active = true;
72  
73      @Column(name = "INIT_AGENDA_ITM_ID")
74      private String firstItemId;
75  
76      @Column(name = "VER_NBR")
77      @Version
78      private Long versionNumber;
79  
80      @OneToMany(orphanRemoval = true, mappedBy = "agenda", cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
81      @JoinColumn(name = "AGENDA_ID", referencedColumnName = "AGENDA_ID", insertable = true, updatable = true)
82      private Set<AgendaAttributeBo> attributeBos;
83  
84      @OneToMany(orphanRemoval = true, targetEntity = AgendaItemBo.class,
85              cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
86      @JoinColumn(name = "AGENDA_ID", referencedColumnName = "AGENDA_ID", insertable = false, updatable = false)
87      private List<AgendaItemBo> items;
88  
89      @ManyToOne(targetEntity = ContextBo.class, cascade = { CascadeType.REFRESH })
90      @JoinColumn(name = "CNTXT_ID", referencedColumnName = "CNTXT_ID", insertable = false, updatable = false)
91      private ContextBo context;
92  
93      public AgendaBo() {
94          active = true;
95          items = new ArrayList<AgendaItemBo>();
96      }
97  
98      public AgendaBo getAgenda() {
99          return this;
100     }
101 
102     public Map<String, String> getAttributes() {
103         HashMap<String, String> attributes = new HashMap<String, String>();
104 
105         if (attributeBos != null) for (AgendaAttributeBo attr : attributeBos) {
106             attributes.put(attr.getAttributeDefinition().getName(), attr.getValue());
107         }
108 
109         return attributes;
110     }
111 
112     public void setAttributes(Map<String, String> attributes) {
113         this.attributeBos = new HashSet<AgendaAttributeBo>();
114 
115         if (!StringUtils.isBlank(this.typeId)) {
116             List<KrmsAttributeDefinition> attributeDefinitions = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService().findAttributeDefinitionsByType(this.getTypeId());
117             Map<String, KrmsAttributeDefinition> attributeDefinitionsByName = new HashMap<String, KrmsAttributeDefinition>();
118 
119             if (attributeDefinitions != null) {
120                 for (KrmsAttributeDefinition attributeDefinition : attributeDefinitions) {
121                     attributeDefinitionsByName.put(attributeDefinition.getName(), attributeDefinition);
122                 }
123             }
124 
125             for (Map.Entry<String, String> attr : attributes.entrySet()) {
126                 KrmsAttributeDefinition attributeDefinition = attributeDefinitionsByName.get(attr.getKey());
127                 AgendaAttributeBo attributeBo = new AgendaAttributeBo();
128                 attributeBo.setAgenda(this);
129                 attributeBo.setValue(attr.getValue());
130                 attributeBo.setAttributeDefinition(KrmsAttributeDefinitionBo.from(attributeDefinition));
131                 attributeBos.add(attributeBo);
132             }
133         }
134     }
135 
136     /**
137      * Returns of copy of this agenda, with the given newAgendaName and new ids.
138      *
139      * @param newAgendaName name of the newly copied AgendaBo
140      * @param dateTimeStamp to append to the names of objects
141      * @return AgendaBo copy of this Agenda with new ids and name
142      */
143     public AgendaBo copyAgenda(String newAgendaName, String dateTimeStamp) {
144         List<AgendaItemBo> agendaItems = this.getItems();
145         AgendaBo copiedAgenda = (AgendaBo) SerializationUtils.deepCopy(this);
146         copiedAgenda.setName(newAgendaName);
147 
148         // Using a copiedAgenda we don't mess with the existing agenda at all.
149         copiedAgenda.setId(agendaIdIncrementer.getNewId());
150 
151         String initAgendaItemId = this.getFirstItemId();
152         List<AgendaItemBo> copiedAgendaItems = new ArrayList<AgendaItemBo>();
153         Map<String, RuleBo> oldRuleIdToNew = new HashMap<String, RuleBo>();
154         Map<String, AgendaItemBo> oldAgendaItemIdToNew = new HashMap<String, AgendaItemBo>();
155 
156         for (AgendaItemBo agendaItem : agendaItems) {
157             if (!oldAgendaItemIdToNew.containsKey(agendaItem.getId())) {
158                 AgendaItemBo copiedAgendaItem =
159                         agendaItem.copyAgendaItem(copiedAgenda, oldRuleIdToNew, oldAgendaItemIdToNew, copiedAgendaItems, dateTimeStamp);
160 
161                 if (initAgendaItemId != null && initAgendaItemId.equals(agendaItem.getId())) {
162                     copiedAgenda.setFirstItemId(copiedAgendaItem.getId());
163                 }
164 
165                 copiedAgendaItems.add(copiedAgendaItem);
166                 oldAgendaItemIdToNew.put(agendaItem.getId(), copiedAgendaItem);
167             }
168         }
169 
170         copiedAgenda.setItems(copiedAgendaItems);
171 
172         return copiedAgenda;
173     }
174 
175     /**
176      * Converts a mutable bo to it's immutable counterpart
177      *
178      * @param bo the mutable business object
179      * @return the immutable object AgendaDefinition
180      */
181     public static AgendaDefinition to(AgendaBo bo) {
182         if (bo == null) {
183             return null;
184         }
185 
186         return AgendaDefinition.Builder.create(bo).build();
187     }
188 
189     public String getId() {
190         return id;
191     }
192 
193     public void setId(String id) {
194         this.id = id;
195     }
196 
197     public String getName() {
198         return name;
199     }
200 
201     public void setName(String name) {
202         this.name = name;
203     }
204 
205     public String getTypeId() {
206         return typeId;
207     }
208 
209     public void setTypeId(String typeId) {
210         this.typeId = typeId;
211     }
212 
213     public String getContextId() {
214         return contextId;
215     }
216 
217     public void setContextId(String contextId) {
218         this.contextId = contextId;
219     }
220 
221     public boolean getActive() {
222         return active;
223     }
224 
225     public boolean isActive() {
226         return active;
227     }
228 
229     public void setActive(boolean active) {
230         this.active = active;
231     }
232 
233     public String getFirstItemId() {
234         return firstItemId;
235     }
236 
237     public void setFirstItemId(String firstItemId) {
238         this.firstItemId = firstItemId;
239     }
240 
241     public Set<AgendaAttributeBo> getAttributeBos() {
242         return attributeBos;
243     }
244 
245     public void setAttributeBos(Set<AgendaAttributeBo> attributeBos) {
246         this.attributeBos = attributeBos;
247     }
248 
249     public List<AgendaItemBo> getItems() {
250         return items;
251     }
252 
253     public void setItems(List<AgendaItemBo> items) {
254         this.items = items;
255     }
256 
257     public ContextBo getContext() {
258         return context;
259     }
260 
261     public void setContext(ContextBo context) {
262         this.context = context;
263     }
264 
265     public Long getVersionNumber() {
266         return versionNumber;
267     }
268 
269     public void setVersionNumber(Long versionNumber) {
270         this.versionNumber = versionNumber;
271     }
272 }