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.kew.impl.peopleflow;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20  import org.kuali.rice.core.api.uif.RemotableAttributeField;
21  import org.kuali.rice.kew.api.KewApiServiceLocator;
22  import org.kuali.rice.kew.api.peopleflow.PeopleFlowDefinition;
23  import org.kuali.rice.kew.api.repository.type.KewTypeDefinition;
24  import org.kuali.rice.kew.framework.peopleflow.PeopleFlowTypeService;
25  import org.kuali.rice.krad.maintenance.MaintainableImpl;
26  import org.kuali.rice.krad.uif.container.CollectionGroup;
27  import org.kuali.rice.krad.uif.container.Container;
28  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
29  import org.kuali.rice.krad.uif.view.View;
30  import org.kuali.rice.krad.web.form.MaintenanceForm;
31  
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.Collections;
35  import java.util.Comparator;
36  import java.util.List;
37  
38  /**
39   * Custom view helper for the people flow maintenance document to retrieve the type attribute remotable fields
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class PeopleFlowMaintainableImpl extends MaintainableImpl {
44  
45      /**
46       * Invokes the {@link org.kuali.rice.kew.api.repository.type.KewTypeRepositoryService} to retrieve the remotable
47       * field definitions for the attributes associated with the selected type
48       *
49       * @param view - view instance
50       * @param model - object containing the form data, from which the selected type will be pulled
51       * @param container - container that holds the remotable fields
52       * @return List<RemotableAttributeField> instances for the type attributes, or empty list if not attributes exist
53       */
54      public List<RemotableAttributeField> retrieveTypeAttributes(View view, Object model, Container container) {
55          List<RemotableAttributeField> remoteFields = new ArrayList<RemotableAttributeField>();
56  
57          PeopleFlowBo peopleFlow =
58                  (PeopleFlowBo) ((MaintenanceForm) model).getDocument().getNewMaintainableObject().getDataObject();
59  
60          // retrieve the type service and invoke to get the remotable field definitions
61          String typeId = peopleFlow.getTypeId();
62          if (StringUtils.isNotBlank(typeId)) {
63              KewTypeDefinition typeDefinition = KewApiServiceLocator.getKewTypeRepositoryService().getTypeById(typeId);
64              PeopleFlowTypeService peopleFlowTypeService = GlobalResourceLoader.<PeopleFlowTypeService>getService(
65                      typeDefinition.getServiceName());
66              remoteFields = peopleFlowTypeService.getAttributeFields(typeId);
67          }
68  
69          return remoteFields;
70      }
71  
72      /**
73       * Set the attribute bo list from the map of attribute key/value pairs and then calls
74       * {@link org.kuali.rice.kew.api.peopleflow.PeopleFlowService} to save the people flow instance
75       */
76      @Override
77      public void saveDataObject() {
78          ((PeopleFlowBo) getDataObject()).updateAttributeBoValues();
79  
80          PeopleFlowDefinition peopleFlowDefinition = PeopleFlowBo.to(((PeopleFlowBo) getDataObject()));
81          if (StringUtils.isNotBlank(peopleFlowDefinition.getId())) {
82              KewApiServiceLocator.getPeopleFlowService().updatePeopleFlow(peopleFlowDefinition);
83          } else {
84              KewApiServiceLocator.getPeopleFlowService().createPeopleFlow(peopleFlowDefinition);
85          }
86      }
87  
88      @Override
89      public void processCollectionAddLine(View view, Object model, String collectionPath) {
90          // =======================================================================================
91          // COPIED FROM ViewHelperServiceImpl processCollectionAddLine to add sorting of collection
92          // =======================================================================================
93          // get the collection group from the view
94          CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(collectionPath);
95          if (collectionGroup == null) {
96              logAndThrowRuntime("Unable to get collection group component for path: " + collectionPath);
97          }
98  
99          // get the collection instance for adding the new line
100         Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model, collectionPath);
101         if (collection == null) {
102             logAndThrowRuntime("Unable to get collection property from model for path: " + collectionPath);
103         }
104 
105         // now get the new line we need to add
106         String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
107         Object addLine = ObjectPropertyUtils.getPropertyValue(model, addLinePath);
108         if (addLine == null) {
109             logAndThrowRuntime("Add line instance not found for path: " + addLinePath);
110         }
111 
112         processBeforeAddLine(view, collectionGroup, model, addLine);
113 
114         // validate the line to make sure it is ok to add
115         boolean isValidLine = performAddLineValidation(view, collectionGroup, model, addLine);
116         if (isValidLine) {
117             // TODO: should check to see if there is an add line method on the
118             // collection parent and if so call that instead of just adding to
119             // the collection (so that sequence can be set)
120             if (collection instanceof List) {
121                 ((List) collection).add(0, addLine);
122                 // ADDED sorting for PeopleFlowMemberBo
123                 if (addLine instanceof PeopleFlowMemberBo) {
124                     Collections.sort((List) collection, new Comparator<Object>() {
125                         public int compare(Object o1, Object o2) {
126                             if ((o1 instanceof PeopleFlowMemberBo) && (o1 instanceof PeopleFlowMemberBo)) {
127                                 return ((PeopleFlowMemberBo)o1).getPriority() - ((PeopleFlowMemberBo)o2).getPriority();
128                             }
129                             return 0; // if not both PeopleFlowMemberBo something strange is going on.  Use equals as doing nothing.
130                         }
131                     });
132                 }
133             } else {
134                 collection.add(addLine);
135             }
136 
137             // make a new instance for the add line
138             collectionGroup.initializeNewCollectionLine(view, model, collectionGroup, true);
139         }
140 
141         processAfterAddLine(view, collectionGroup, model, addLine);
142     }
143 }