1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krms.impl.ui; |
17 | |
|
18 | |
import org.apache.commons.collections.CollectionUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException; |
21 | |
import org.kuali.rice.core.api.uif.RemotableAttributeField; |
22 | |
import org.kuali.rice.krad.bo.PersistableBusinessObject; |
23 | |
import org.kuali.rice.krad.document.MaintenanceDocument; |
24 | |
import org.kuali.rice.krad.maintenance.Maintainable; |
25 | |
import org.kuali.rice.krad.maintenance.MaintainableImpl; |
26 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
27 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
28 | |
import org.kuali.rice.krad.uif.container.CollectionGroup; |
29 | |
import org.kuali.rice.krad.uif.container.Container; |
30 | |
import org.kuali.rice.krad.uif.view.View; |
31 | |
import org.kuali.rice.krad.util.KRADConstants; |
32 | |
import org.kuali.rice.krad.web.form.MaintenanceForm; |
33 | |
import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition; |
34 | |
import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition; |
35 | |
import org.kuali.rice.krms.framework.type.ActionTypeService; |
36 | |
import org.kuali.rice.krms.framework.type.AgendaTypeService; |
37 | |
import org.kuali.rice.krms.framework.type.RuleTypeService; |
38 | |
import org.kuali.rice.krms.impl.repository.ActionBo; |
39 | |
import org.kuali.rice.krms.impl.repository.AgendaAttributeBo; |
40 | |
import org.kuali.rice.krms.impl.repository.AgendaBo; |
41 | |
import org.kuali.rice.krms.impl.repository.ContextBoService; |
42 | |
import org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService; |
43 | |
import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator; |
44 | |
import org.kuali.rice.krms.impl.type.ActionTypeServiceBase; |
45 | |
import org.kuali.rice.krms.impl.type.AgendaTypeServiceBase; |
46 | |
import org.kuali.rice.krms.impl.type.RuleTypeServiceBase; |
47 | |
|
48 | |
import java.util.ArrayList; |
49 | |
import java.util.HashMap; |
50 | |
import java.util.HashSet; |
51 | |
import java.util.List; |
52 | |
import java.util.Map; |
53 | |
import java.util.Set; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 14 | public class AgendaEditorMaintainable extends MaintainableImpl { |
62 | |
|
63 | |
private static final long serialVersionUID = 1L; |
64 | |
|
65 | 1 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AgendaEditorMaintainable.class); |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public BusinessObjectService getBoService() { |
71 | 0 | return KRADServiceLocator.getBusinessObjectService(); |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private ContextBoService getContextBoService() { |
78 | 0 | return KrmsRepositoryServiceLocator.getContextBoService(); |
79 | |
} |
80 | |
|
81 | |
public List<RemotableAttributeField> retrieveCustomAttributes(View view, Object model, Container container) { |
82 | 0 | List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>(); |
83 | |
|
84 | 0 | MaintenanceForm maintenanceForm = (MaintenanceForm)model; |
85 | 0 | AgendaEditor agendaEditor = (AgendaEditor)maintenanceForm.getDocument().getNewMaintainableObject().getDataObject(); |
86 | |
|
87 | |
|
88 | 0 | if (agendaEditor.getAgenda() != null && !StringUtils.isBlank(agendaEditor.getAgenda().getTypeId())) { |
89 | |
|
90 | 0 | String krmsTypeId = agendaEditor.getAgenda().getTypeId(); |
91 | |
|
92 | 0 | AgendaTypeService agendaTypeService = getAgendaTypeService(krmsTypeId); |
93 | 0 | results.addAll(agendaTypeService.getAttributeFields(krmsTypeId)); |
94 | |
} |
95 | |
|
96 | 0 | return results; |
97 | |
} |
98 | |
|
99 | |
private AgendaTypeService getAgendaTypeService(String krmsTypeId) { |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | 0 | KrmsTypeDefinition krmsType = |
105 | |
KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService(). |
106 | |
getTypeById(krmsTypeId); |
107 | |
|
108 | 0 | AgendaTypeService agendaTypeService = null; |
109 | |
|
110 | 0 | if (!StringUtils.isBlank(krmsTypeId)) { |
111 | 0 | String serviceName = krmsType.getServiceName(); |
112 | |
|
113 | 0 | if (!StringUtils.isBlank(serviceName)) { |
114 | 0 | agendaTypeService = KrmsRepositoryServiceLocator.getService(serviceName); |
115 | |
} |
116 | |
} |
117 | |
|
118 | 0 | if (agendaTypeService == null) { agendaTypeService = AgendaTypeServiceBase.defaultAgendaTypeService; } |
119 | |
|
120 | 0 | return agendaTypeService; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public List<RemotableAttributeField> retrieveRuleActionCustomAttributes(View view, Object model, Container container) { |
127 | 0 | List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>(); |
128 | |
|
129 | 0 | MaintenanceForm maintenanceForm = (MaintenanceForm)model; |
130 | 0 | AgendaEditor agendaEditor = (AgendaEditor)maintenanceForm.getDocument().getNewMaintainableObject().getDataObject(); |
131 | |
|
132 | |
|
133 | 0 | if (!StringUtils.isBlank(agendaEditor.getAgendaItemLineRuleAction().getTypeId())) { |
134 | 0 | ActionTypeService actionTypeService = getActionTypeService(agendaEditor.getAgendaItemLineRuleAction().getTypeId()); |
135 | 0 | results.addAll(actionTypeService.getAttributeFields(agendaEditor.getAgendaItemLineRuleAction().getTypeId())); |
136 | |
} |
137 | |
|
138 | 0 | return results; |
139 | |
} |
140 | |
|
141 | |
public List<RemotableAttributeField> retrieveRuleCustomAttributes(View view, Object model, Container container) { |
142 | 0 | List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>(); |
143 | |
|
144 | 0 | MaintenanceForm maintenanceForm = (MaintenanceForm)model; |
145 | 0 | AgendaEditor agendaEditor = (AgendaEditor)maintenanceForm.getDocument().getNewMaintainableObject().getDataObject(); |
146 | |
|
147 | |
|
148 | 0 | if (agendaEditor.getAgendaItemLine() != null && agendaEditor.getAgendaItemLine().getRule() != null |
149 | |
&& !StringUtils.isBlank(agendaEditor.getAgendaItemLine().getRule().getTypeId())) { |
150 | |
|
151 | 0 | String krmsTypeId = agendaEditor.getAgendaItemLine().getRule().getTypeId(); |
152 | |
|
153 | 0 | RuleTypeService ruleTypeService = getRuleTypeService(krmsTypeId); |
154 | 0 | results.addAll(ruleTypeService.getAttributeFields(krmsTypeId)); |
155 | |
} |
156 | |
|
157 | 0 | return results; |
158 | |
} |
159 | |
|
160 | |
private ActionTypeService getActionTypeService(String krmsTypeId) { |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | 0 | KrmsTypeDefinition krmsType = |
166 | |
KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService(). |
167 | |
getTypeById(krmsTypeId); |
168 | |
|
169 | 0 | ActionTypeService actionTypeService = null; |
170 | |
|
171 | 0 | if (!StringUtils.isBlank(krmsTypeId)) { |
172 | 0 | String serviceName = krmsType.getServiceName(); |
173 | |
|
174 | 0 | if (!StringUtils.isBlank(serviceName)) { |
175 | 0 | actionTypeService = KrmsRepositoryServiceLocator.getService(serviceName); |
176 | |
} |
177 | |
} |
178 | 0 | if (actionTypeService == null) { |
179 | 0 | actionTypeService = ActionTypeServiceBase.defaultActionTypeService; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | 0 | return actionTypeService; |
185 | |
} |
186 | |
|
187 | |
private RuleTypeService getRuleTypeService(String krmsTypeId) { |
188 | 0 | RuleTypeService ruleTypeService = null; |
189 | 0 | String serviceName = getRuleTypeServiceName(krmsTypeId); |
190 | |
|
191 | 0 | if (!StringUtils.isBlank(serviceName)) { |
192 | 0 | ruleTypeService = KrmsRepositoryServiceLocator.getService(serviceName); |
193 | |
} |
194 | 0 | if (ruleTypeService == null) { |
195 | 0 | ruleTypeService = RuleTypeServiceBase.defaultRuleTypeService; |
196 | |
} |
197 | 0 | return ruleTypeService; |
198 | |
} |
199 | |
|
200 | |
private String getRuleTypeServiceName(String krmsTypeId) { |
201 | 0 | String serviceName = null; |
202 | 0 | KrmsTypeDefinition krmsType = |
203 | |
KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService(). |
204 | |
getTypeById(krmsTypeId); |
205 | |
|
206 | 0 | if (!StringUtils.isBlank(krmsTypeId)) { |
207 | 0 | serviceName = krmsType.getServiceName(); |
208 | |
} |
209 | 0 | return serviceName; |
210 | |
} |
211 | |
|
212 | |
@Override |
213 | |
public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) { |
214 | 0 | Object dataObject = null; |
215 | |
|
216 | |
try { |
217 | |
|
218 | 0 | AgendaEditor agendaEditor = new AgendaEditor(); |
219 | 0 | AgendaBo agenda = getLookupService().findObjectBySearch(((AgendaEditor) getDataObject()).getAgenda().getClass(), dataObjectKeys); |
220 | 0 | if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) { |
221 | |
|
222 | |
|
223 | |
|
224 | 0 | agenda.setId(null); |
225 | 0 | document.setFieldsClearedOnCopy(true); |
226 | |
} |
227 | 0 | agendaEditor.setAgenda(agenda); |
228 | |
|
229 | |
|
230 | 0 | agendaEditor.setCustomAttributesMap(agenda.getAttributes()); |
231 | |
|
232 | |
|
233 | 0 | agendaEditor.setNamespace(agenda.getContext().getNamespace()); |
234 | 0 | agendaEditor.setContextName(agenda.getContext().getName()); |
235 | |
|
236 | 0 | dataObject = agendaEditor; |
237 | 0 | } catch (ClassNotPersistenceCapableException ex) { |
238 | 0 | if (!document.getOldMaintainableObject().isExternalBusinessObject()) { |
239 | 0 | throw new RuntimeException("Data Object Class: " + getDataObjectClass() + |
240 | |
" is not persistable and is not externalizable - configuration error"); |
241 | |
} |
242 | |
|
243 | 0 | } |
244 | |
|
245 | 0 | return dataObject; |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
@Override |
252 | |
public void processAfterNew(MaintenanceDocument document, |
253 | |
Map<String, String[]> requestParameters) { |
254 | |
|
255 | 0 | super.processAfterNew(document, requestParameters); |
256 | 0 | document.getDocumentHeader().setDocumentDescription("New Agenda Editor Document"); |
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | 0 | } |
280 | |
|
281 | |
@Override |
282 | |
public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) { |
283 | 0 | super.processAfterCopy(document, requestParameters); |
284 | 0 | document.getDocumentHeader().setDocumentDescription("New Agenda Editor Document"); |
285 | 0 | } |
286 | |
|
287 | |
@Override |
288 | |
public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) { |
289 | 0 | super.processAfterEdit(document, requestParameters); |
290 | 0 | document.getDocumentHeader().setDocumentDescription("Modify Agenda Editor Document"); |
291 | 0 | } |
292 | |
|
293 | |
@Override |
294 | |
public void saveDataObject() { |
295 | 0 | AgendaEditor agendaEditor = (AgendaEditor) getDataObject(); |
296 | 0 | AgendaBo agendaBo = ((AgendaEditor) getDataObject()).getAgenda(); |
297 | 0 | if (agendaBo != null) { |
298 | |
|
299 | 0 | Set<AgendaAttributeBo> attributes = new HashSet<AgendaAttributeBo>(); |
300 | |
|
301 | 0 | Map<String, KrmsAttributeDefinition> attributeDefinitionMap = buildAttributeDefinitionMap(agendaBo.getTypeId()); |
302 | |
|
303 | |
|
304 | 0 | for (Map.Entry<String,String> entry : agendaEditor.getCustomAttributesMap().entrySet()){ |
305 | |
|
306 | 0 | KrmsAttributeDefinition attrDef = attributeDefinitionMap.get(entry.getKey()); |
307 | |
|
308 | 0 | if (attrDef != null) { |
309 | 0 | AgendaAttributeBo attributeBo = new AgendaAttributeBo(); |
310 | 0 | attributeBo.setAgendaId(agendaBo.getId()); |
311 | 0 | attributeBo.setAttributeDefinitionId(attrDef.getId()); |
312 | 0 | attributeBo.setValue(entry.getValue()); |
313 | 0 | attributes.add( attributeBo ); |
314 | |
} |
315 | 0 | } |
316 | |
|
317 | 0 | if (!CollectionUtils.isEmpty(agendaBo.getAttributeBos())) { |
318 | 0 | getBusinessObjectService().delete(new ArrayList<AgendaAttributeBo>(agendaBo.getAttributeBos())); |
319 | |
} |
320 | |
|
321 | 0 | agendaBo.setAttributeBos(attributes); |
322 | |
|
323 | |
|
324 | 0 | getBusinessObjectService().linkAndSave((PersistableBusinessObject) agendaBo); |
325 | 0 | } else { |
326 | 0 | throw new RuntimeException( |
327 | |
"Cannot save object of type: " + agendaBo + " with business object service"); |
328 | |
} |
329 | 0 | } |
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
private Map<String, KrmsAttributeDefinition> buildAttributeDefinitionMap(String agendaTypeId) { |
338 | 0 | KrmsAttributeDefinitionService attributeDefinitionService = |
339 | |
KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService(); |
340 | |
|
341 | |
|
342 | 0 | Map<String, KrmsAttributeDefinition> attributeDefinitionMap = new HashMap<String, KrmsAttributeDefinition>(); |
343 | |
|
344 | 0 | List<KrmsAttributeDefinition> attributeDefinitions = |
345 | |
attributeDefinitionService.findAttributeDefinitionsByType(agendaTypeId); |
346 | |
|
347 | 0 | for (KrmsAttributeDefinition attributeDefinition : attributeDefinitions) { |
348 | 0 | attributeDefinitionMap.put(attributeDefinition.getName(), attributeDefinition); |
349 | |
} |
350 | 0 | return attributeDefinitionMap; |
351 | |
} |
352 | |
|
353 | |
@Override |
354 | |
public boolean isOldDataObjectInDocument() { |
355 | 0 | boolean isOldDataObjectInExistence = true; |
356 | |
|
357 | 0 | if (getDataObject() == null) { |
358 | 0 | isOldDataObjectInExistence = false; |
359 | |
} else { |
360 | |
|
361 | 0 | Map<String, ?> keyFieldValues = getDataObjectMetaDataService().getPrimaryKeyFieldValues(((AgendaEditor) getDataObject()).getAgenda()); |
362 | 0 | for (Object keyValue : keyFieldValues.values()) { |
363 | 0 | if (keyValue == null) { |
364 | 0 | isOldDataObjectInExistence = false; |
365 | 0 | } else if ((keyValue instanceof String) && StringUtils.isBlank((String) keyValue)) { |
366 | 0 | isOldDataObjectInExistence = false; |
367 | |
} |
368 | |
|
369 | 0 | if (!isOldDataObjectInExistence) { |
370 | 0 | break; |
371 | |
} |
372 | |
} |
373 | |
} |
374 | |
|
375 | 0 | return isOldDataObjectInExistence; |
376 | |
} |
377 | |
|
378 | |
|
379 | |
@Override |
380 | |
public Class getDataObjectClass() { |
381 | 0 | return AgendaBo.class; |
382 | |
} |
383 | |
|
384 | |
@Override |
385 | |
protected void processBeforeAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) { |
386 | 0 | MaintenanceForm form = (MaintenanceForm) model; |
387 | 0 | AgendaEditor agendaEditor = (AgendaEditor) form.getDocument().getNewMaintainableObject().getDataObject(); |
388 | 0 | if (addLine instanceof ActionBo) { |
389 | 0 | ((ActionBo) addLine).setNamespace(agendaEditor.getAgendaItemLine().getRule().getNamespace()); |
390 | |
} |
391 | |
|
392 | 0 | super.processBeforeAddLine(view, collectionGroup, model, addLine); |
393 | 0 | } |
394 | |
} |