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