View Javadoc

1   /**
2    * Copyright 2005-2012 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.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.DataType;
22  import org.kuali.rice.core.api.uif.RemotableAttributeField;
23  import org.kuali.rice.core.api.uif.RemotableTextInput;
24  import org.kuali.rice.core.api.util.tree.Node;
25  import org.kuali.rice.core.api.util.tree.Tree;
26  import org.kuali.rice.krad.bo.PersistableBusinessObject;
27  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
28  import org.kuali.rice.krad.maintenance.Maintainable;
29  import org.kuali.rice.krad.maintenance.MaintainableImpl;
30  import org.kuali.rice.krad.service.BusinessObjectService;
31  import org.kuali.rice.krad.service.KRADServiceLocator;
32  import org.kuali.rice.krad.service.SequenceAccessorService;
33  import org.kuali.rice.krad.uif.container.CollectionGroup;
34  import org.kuali.rice.krad.uif.container.Container;
35  import org.kuali.rice.krad.uif.view.View;
36  import org.kuali.rice.krad.util.KRADConstants;
37  import org.kuali.rice.krad.web.form.MaintenanceForm;
38  import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
39  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
40  import org.kuali.rice.krms.impl.repository.ActionBo;
41  import org.kuali.rice.krms.impl.repository.AgendaBo;
42  import org.kuali.rice.krms.impl.repository.AgendaItemBo;
43  import org.kuali.rice.krms.impl.repository.ContextBoService;
44  import org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService;
45  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
46  import org.kuali.rice.krms.impl.repository.PropositionBo;
47  import org.kuali.rice.krms.impl.repository.PropositionParameterBo;
48  import org.kuali.rice.krms.impl.repository.RuleBo;
49  import org.kuali.rice.krms.impl.repository.TermBo;
50  import org.kuali.rice.krms.impl.repository.TermParameterBo;
51  import org.kuali.rice.krms.impl.util.KrmsImplConstants;
52  import org.kuali.rice.krms.impl.util.KrmsRetriever;
53  
54  import java.util.ArrayList;
55  import java.util.Collections;
56  import java.util.Date;
57  import java.util.HashMap;
58  import java.util.List;
59  import java.util.Map;
60  
61  /**
62   * {@link Maintainable} for the {@link AgendaEditor}
63   *
64   * @author Kuali Rice Team (rice.collab@kuali.org)
65   *
66   */
67  public class AgendaEditorMaintainable extends MaintainableImpl {
68  
69      private static final long serialVersionUID = 1L;
70  
71      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AgendaEditorMaintainable.class);
72  
73      public static final String NEW_AGENDA_EDITOR_DOCUMENT_TEXT = "New Agenda Editor Document";
74  
75      private transient SequenceAccessorService sequenceAccessorService;
76  
77      private transient KrmsRetriever krmsRetriever = new KrmsRetriever();
78  
79      /**
80       * @return the boService
81       */
82      public BusinessObjectService getBoService() {
83          return KRADServiceLocator.getBusinessObjectService();
84      }
85  
86      /**
87       * return the contextBoService
88       */
89      private ContextBoService getContextBoService() {
90          return KrmsRepositoryServiceLocator.getContextBoService();
91      }
92  
93      public List<RemotableAttributeField> retrieveAgendaCustomAttributes(View view, Object model, Container container) {
94          AgendaEditor agendaEditor = getAgendaEditor(model);
95          return krmsRetriever.retrieveAgendaCustomAttributes(agendaEditor);
96      }
97  
98      /**
99       * Retrieve a list of {@link RemotableAttributeField}s for the parameters (if any) required by the resolver for
100      * the selected term in the proposition that is under edit.
101      */
102     public List<RemotableAttributeField> retrieveTermParameters(View view, Object model, Container container) {
103 
104         List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
105 
106         AgendaEditor agendaEditor = getAgendaEditor(model);
107 
108         // Figure out which rule is being edited
109         RuleBo rule = agendaEditor.getAgendaItemLine().getRule();
110         // Figure out which proposition is being edited
111         Tree<RuleTreeNode, String> propositionTree = rule.getPropositionTree();
112         Node<RuleTreeNode, String> editedPropositionNode = findEditedProposition(propositionTree.getRootElement());
113 
114         if (editedPropositionNode != null) {
115             PropositionBo propositionBo = editedPropositionNode.getData().getProposition();
116             if (StringUtils.isEmpty(propositionBo.getCompoundOpCode()) && CollectionUtils.size(propositionBo.getParameters()) > 0) {
117                 // Get the term ID; if it is a new parameterized term, it will have a special prefix
118                 PropositionParameterBo param = propositionBo.getParameters().get(0);
119                 if (param.getValue().startsWith(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX)) {
120                     String termSpecId = param.getValue().substring(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX.length());
121                     TermResolverDefinition simplestResolver = getSimplestTermResolver(termSpecId, rule.getNamespace());
122 
123                     // Get the parameters and build RemotableAttributeFields
124                     if (simplestResolver != null) {
125                         List<String> parameterNames = new ArrayList<String>(simplestResolver.getParameterNames());
126                         Collections.sort(parameterNames); // make param order deterministic
127 
128                         for (String parameterName : parameterNames) {
129                             // TODO: also allow for DD parameters if there are matching type attributes
130                             RemotableTextInput.Builder controlBuilder = RemotableTextInput.Builder.create();
131                             controlBuilder.setSize(64);
132 
133                             RemotableAttributeField.Builder builder = RemotableAttributeField.Builder.create(parameterName);
134 
135                             builder.setRequired(true);
136                             builder.setDataType(DataType.STRING);
137                             builder.setControl(controlBuilder);
138                             builder.setLongLabel(parameterName);
139                             builder.setShortLabel(parameterName);
140                             builder.setMinLength(Integer.valueOf(1));
141                             builder.setMaxLength(Integer.valueOf(64));
142 
143                             results.add(builder.build());
144                         }
145                     }
146                 }
147             }
148         }
149 
150         return results;
151     }
152 
153     /**
154      * finds the term resolver with the fewest parameters that resolves the given term specification
155      * @param termSpecId the id of the term specification
156      * @param namespace the  namespace of the term specification
157      * @return the simples {@link TermResolverDefinition} found, or null if none was found
158      */
159     // package access so that AgendaEditorController can use it too
160     static TermResolverDefinition getSimplestTermResolver(String termSpecId,
161             String namespace) {// Get the term resolver for the term spec
162 
163         List<TermResolverDefinition> resolvers =
164                 KrmsRepositoryServiceLocator.getTermBoService().getTermResolversByOutputId(
165                         termSpecId, namespace);
166 
167         TermResolverDefinition simplestResolver = null;
168 
169         for (TermResolverDefinition resolver : resolvers) {
170             if (simplestResolver == null ||
171                     simplestResolver.getParameterNames().size() < resolver.getParameterNames().size()) {
172                 simplestResolver = resolver;
173             }
174         }
175 
176         return simplestResolver;
177     }
178 
179     /**
180      * Find and return the node containing the proposition that is in currently in edit mode
181      * @param node the node to start searching from (typically the root)
182      * @return the node that is currently being edited, if any.  Otherwise, null.
183      */
184     private Node<RuleTreeNode, String> findEditedProposition(Node<RuleTreeNode, String> node) {
185         Node<RuleTreeNode, String> result = null;
186         if (node.getData() != null && node.getData().getProposition() != null &&
187                 node.getData().getProposition().getEditMode()) {
188             result = node;
189         } else {
190             for (Node<RuleTreeNode, String> child : node.getChildren()) {
191                 result = findEditedProposition(child);
192                 if (result != null) break;
193             }
194         }
195         return result;
196     }
197 
198     /**
199      * Get the AgendaEditor out of the MaintenanceForm's newMaintainableObject
200      * @param model the MaintenanceForm
201      * @return the AgendaEditor
202      */
203     private AgendaEditor getAgendaEditor(Object model) {
204         MaintenanceForm maintenanceForm = (MaintenanceForm)model;
205         return (AgendaEditor)maintenanceForm.getDocument().getNewMaintainableObject().getDataObject();
206     }
207 
208     public List<RemotableAttributeField> retrieveRuleActionCustomAttributes(View view, Object model, Container container) {
209         AgendaEditor agendaEditor = getAgendaEditor((MaintenanceForm) model);
210         return krmsRetriever.retrieveRuleActionCustomAttributes(agendaEditor);
211     }
212 
213     /**
214      *  This only supports a single action within a rule.
215      */
216     public List<RemotableAttributeField> retrieveRuleCustomAttributes(View view, Object model, Container container) {
217         AgendaEditor agendaEditor = getAgendaEditor((MaintenanceForm) model);
218         return krmsRetriever.retrieveRuleCustomAttributes(agendaEditor);
219     }
220 
221     @Override
222     public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
223         Object dataObject = null;
224 
225         try {
226             // Since the dataObject is a wrapper class we need to build it and populate with the agenda bo.
227             AgendaEditor agendaEditor = new AgendaEditor();
228             AgendaBo agenda = getLookupService().findObjectBySearch(((AgendaEditor) getDataObject()).getAgenda().getClass(), dataObjectKeys);
229             if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
230                 String dateTimeStamp = (new Date()).getTime() + "";
231                 String newAgendaName = AgendaItemBo.COPY_OF_TEXT + agenda.getName() + " " + dateTimeStamp;
232 
233                 AgendaBo copiedAgenda = agenda.copyAgenda(newAgendaName, dateTimeStamp);
234 
235                 document.getDocumentHeader().setDocumentDescription(NEW_AGENDA_EDITOR_DOCUMENT_TEXT);
236                 document.setFieldsClearedOnCopy(true);
237                 agendaEditor.setAgenda(copiedAgenda);
238             } else {
239                 // set custom attributes map in AgendaEditor
240                 //                agendaEditor.setCustomAttributesMap(agenda.getAttributes());
241                 agendaEditor.setAgenda(agenda);
242             }
243             agendaEditor.setCustomAttributesMap(agenda.getAttributes());
244 
245 
246             // set extra fields on AgendaEditor
247             agendaEditor.setNamespace(agenda.getContext().getNamespace());
248             agendaEditor.setContextName(agenda.getContext().getName());
249 
250             dataObject = agendaEditor;
251         } catch (ClassNotPersistenceCapableException ex) {
252             if (!document.getOldMaintainableObject().isExternalBusinessObject()) {
253                 throw new RuntimeException("Data Object Class: " + getDataObjectClass() +
254                         " is not persistable and is not externalizable - configuration error");
255             }
256             // otherwise, let fall through
257         }
258 
259         return dataObject;
260     }
261 
262     /**
263      *  Returns the sequenceAssessorService
264      * @return {@link SequenceAccessorService}
265      */
266     private SequenceAccessorService getSequenceAccessorService() {
267         if ( sequenceAccessorService == null ) {
268             sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService();
269         }
270         return sequenceAccessorService;
271     }
272     /**
273      * {@inheritDoc}
274      */
275     @Override
276     public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
277         super.processAfterNew(document, requestParameters);
278         document.getDocumentHeader().setDocumentDescription(NEW_AGENDA_EDITOR_DOCUMENT_TEXT);
279     }
280 
281     @Override
282     public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
283         super.processAfterEdit(document, requestParameters);
284         document.getDocumentHeader().setDocumentDescription("Modify Agenda Editor Document");
285     }
286 
287     @Override
288     public void prepareForSave() {
289         // set agenda attributes
290         AgendaEditor agendaEditor = (AgendaEditor) getDataObject();
291         agendaEditor.getAgenda().setAttributes(agendaEditor.getCustomAttributesMap());
292     }
293 
294     @Override
295     public void saveDataObject() {
296         AgendaBo agendaBo = ((AgendaEditor) getDataObject()).getAgenda();
297 
298         // handle saving new parameterized terms
299         for (AgendaItemBo agendaItem : agendaBo.getItems()) {
300             PropositionBo propositionBo = agendaItem.getRule().getProposition();
301             if (propositionBo != null) {
302                 saveNewParameterizedTerms(propositionBo);
303             }
304         }
305 
306         if (agendaBo instanceof PersistableBusinessObject) {
307             Map<String,String> primaryKeys = new HashMap<String, String>();
308             primaryKeys.put("id", agendaBo.getId());
309             AgendaBo blah = getBusinessObjectService().findByPrimaryKey(AgendaBo.class, primaryKeys);
310             getBusinessObjectService().delete(blah);
311 
312             getBusinessObjectService().linkAndSave(agendaBo);
313         } else {
314             throw new RuntimeException(
315                     "Cannot save object of type: " + agendaBo + " with business object service");
316         }
317     }
318 
319     /**
320      * walk the proposition tree and save any new parameterized terms that are contained therein
321      * @param propositionBo the root proposition from which to search
322      */
323     private void saveNewParameterizedTerms(PropositionBo propositionBo) {
324         if (StringUtils.isBlank(propositionBo.getCompoundOpCode())) {
325             // it is a simple proposition
326              if (!propositionBo.getParameters().isEmpty() && propositionBo.getParameters().get(0).getValue().startsWith(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX)) {
327                 String termId = propositionBo.getParameters().get(0).getValue();
328                 String termSpecId = termId.substring(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX.length());
329                 // create new term
330                 TermBo newTerm = new TermBo();
331                 newTerm.setDescription(propositionBo.getNewTermDescription());
332                 newTerm.setSpecificationId(termSpecId);
333                 newTerm.setId(KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRMS_TERM_S").toString());
334 
335                 List<TermParameterBo> params = new ArrayList<TermParameterBo>();
336                 for (Map.Entry<String, String> entry : propositionBo.getTermParameters().entrySet()) {
337                     TermParameterBo param = new TermParameterBo();
338                     param.setTermId(newTerm.getId());
339                     param.setName(entry.getKey());
340                     param.setValue(entry.getValue());
341                     param.setId(KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRMS_TERM_PARM_S").toString());
342 
343                     params.add(param);
344                 }
345 
346                 newTerm.setParameters(params);
347 
348                 KRADServiceLocator.getBusinessObjectService().linkAndSave(newTerm);
349                 propositionBo.getParameters().get(0).setValue(newTerm.getId());
350             }
351         } else {
352             // recurse
353             for (PropositionBo childProp : propositionBo.getCompoundComponents()) {
354                 saveNewParameterizedTerms(childProp);
355             }
356         }
357     }
358 
359     /**
360      * Build a map from attribute name to attribute definition from all the defined attribute definitions for the
361      * specified agenda type
362      * @param agendaTypeId
363      * @return
364      */
365     private Map<String, KrmsAttributeDefinition> buildAttributeDefinitionMap(String agendaTypeId) {
366         KrmsAttributeDefinitionService attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
367 
368         // build a map from attribute name to definition
369         Map<String, KrmsAttributeDefinition> attributeDefinitionMap = new HashMap<String, KrmsAttributeDefinition>();
370 
371         List<KrmsAttributeDefinition> attributeDefinitions =
372                 attributeDefinitionService.findAttributeDefinitionsByType(agendaTypeId);
373 
374         for (KrmsAttributeDefinition attributeDefinition : attributeDefinitions) {
375             attributeDefinitionMap.put(attributeDefinition.getName(), attributeDefinition);
376         }
377         return attributeDefinitionMap;
378     }
379 
380     @Override
381     public boolean isOldDataObjectInDocument() {
382         boolean isOldDataObjectInExistence = true;
383 
384         if (getDataObject() == null) {
385             isOldDataObjectInExistence = false;
386         } else {
387             // dataObject contains a non persistable wrapper - use agenda from the wrapper object instead
388             Map<String, ?> keyFieldValues = getDataObjectMetaDataService().getPrimaryKeyFieldValues(((AgendaEditor) getDataObject()).getAgenda());
389             for (Object keyValue : keyFieldValues.values()) {
390                 if (keyValue == null) {
391                     isOldDataObjectInExistence = false;
392                 } else if ((keyValue instanceof String) && StringUtils.isBlank((String) keyValue)) {
393                     isOldDataObjectInExistence = false;
394                 }
395 
396                 if (!isOldDataObjectInExistence) {
397                     break;
398                 }
399             }
400         }
401 
402         return isOldDataObjectInExistence;
403     }
404 
405     // Since the dataObject is a wrapper class we need to return the agendaBo instead.
406     @Override
407     public Class getDataObjectClass() {
408         return AgendaBo.class;
409     }
410 
411     @Override
412     protected void processBeforeAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
413         AgendaEditor agendaEditor = getAgendaEditor(model);
414         if (addLine instanceof ActionBo) {
415             ((ActionBo) addLine).setNamespace(agendaEditor.getAgendaItemLine().getRule().getNamespace());
416         }
417 
418         super.processBeforeAddLine(view, collectionGroup, model, addLine);
419     }
420 }