Coverage Report - org.kuali.rice.krms.impl.ui.AgendaEditorMaintainable
 
Classes in this File Line Coverage Branch Coverage Complexity
AgendaEditorMaintainable
1%
2/127
0%
0/58
2.778
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.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  
  * {@link Maintainable} for the {@link AgendaEditor}
 57  
  * 
 58  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
          * @return the boService
 69  
          */
 70  
         public BusinessObjectService getBoService() {
 71  0
                 return KRADServiceLocator.getBusinessObjectService();
 72  
         }
 73  
 
 74  
     /**
 75  
      * return the contextBoService
 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  
         // if we have an agenda w/ a typeId set on it
 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  
         // Get the AgendaTypeService by hook or by crook
 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  
      *  This only supports a single action within a rule.
 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  
         // if we have an rule action w/ a typeId set on it
 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  
         // if we have an rule w/ a typeId set on it
 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  
         // Get the ActionTypeService by hook or by crook
 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  
 //        if (actionTypeService == null) { actionTypeService = AgendaTypeServiceBase.defaultAgendaTypeService; }
 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  
             // Since the dataObject is a wrapper class we need to build it and populate with the agenda bo.
 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  
                 // If we don't clear the primary key and set the fieldsClearedOnCopy flag then the
 222  
                 // MaintenanceDocumentServiceImpl.processMaintenanceObjectForCopy() will try to locate the primary keys in
 223  
                 // an attempt to clear them which again would cause an exception due to the wrapper class.
 224  0
                 agenda.setId(null);
 225  0
                 document.setFieldsClearedOnCopy(true);
 226  
             }
 227  0
             agendaEditor.setAgenda(agenda);
 228  
 
 229  
             // set custom attributes map in AgendaEditor
 230  0
             agendaEditor.setCustomAttributesMap(agenda.getAttributes());
 231  
 
 232  
             // set extra fields on AgendaEditor
 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  
             // otherwise, let fall through
 243  0
         }
 244  
 
 245  0
         return dataObject;
 246  
     }
 247  
 
 248  
     /**
 249  
          * {@inheritDoc}
 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  
 //                if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) {
 259  
 //                        String[] agendaIds = requestParameters.get("agendaId");
 260  
 //                        if (agendaIds == null || agendaIds.length != 1) { 
 261  
 //                                //throw new RiceRuntimeException("one and only one agendaId request parameter may be passed");
 262  
 //                        } else {
 263  
 //                                // TODO: change this, it makes more sense for MAINTENANCE_EDIT_ACTION
 264  
 //                                String agendaId = agendaIds[0];
 265  
 //
 266  
 //                                AgendaBo agenda = getBoService().findBySinglePrimaryKey(AgendaBo.class, agendaId);
 267  
 //                                String contextId = agenda.getContextId();
 268  
 //
 269  
 //                                ContextBo context = getBoService().findBySinglePrimaryKey(ContextBo.class, contextId);
 270  
 //
 271  
 //                                AgendaEditor editor = (AgendaEditor) document.getDocumentBusinessObject();
 272  
 //
 273  
 //                                editor.setContext(context);
 274  
 //                                editor.setAgenda(agenda);
 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) { // apply custom attributes to agendaBo
 298  
 
 299  0
             Set<AgendaAttributeBo> attributes = new HashSet<AgendaAttributeBo>();
 300  
 
 301  0
             Map<String, KrmsAttributeDefinition> attributeDefinitionMap = buildAttributeDefinitionMap(agendaBo.getTypeId());
 302  
 
 303  
             // for each entry, build an AgendaAttributeBo and add it to the set
 304  0
             for (Map.Entry<String,String> entry  : agendaEditor.getCustomAttributesMap().entrySet()){
 305  
 
 306  0
                 KrmsAttributeDefinition attrDef = attributeDefinitionMap.get(entry.getKey()); // get the definition from our map
 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  
             // remove old attributes if any -- doesn't seem like we should have to do this!
 317  0
             if (!CollectionUtils.isEmpty(agendaBo.getAttributeBos())) {
 318  0
                 getBusinessObjectService().delete(new ArrayList<AgendaAttributeBo>(agendaBo.getAttributeBos()));
 319  
             }
 320  
             // set new ones
 321  0
             agendaBo.setAttributeBos(attributes);
 322  
 
 323  
             // And finally, save it
 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  
      * Build a map from attribute name to attribute definition from all the defined attribute definitions for the
 333  
      * specified agenda type
 334  
      * @param agendaTypeId
 335  
      * @return
 336  
      */
 337  
     private Map<String, KrmsAttributeDefinition> buildAttributeDefinitionMap(String agendaTypeId) {
 338  0
         KrmsAttributeDefinitionService attributeDefinitionService =
 339  
             KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
 340  
 
 341  
         // build a map from attribute name to definition
 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  
             // dataObject contains a non persistable wrapper - use agenda from the wrapper object instead
 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  
      // Since the dataObject is a wrapper class we need to return the agendaBo instead.
 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  
 }