View Javadoc

1   /**
2    * Copyright 2005-2014 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.bo.Note;
26  import org.kuali.rice.krad.maintenance.MaintainableImpl;
27  import org.kuali.rice.krad.uif.container.CollectionGroup;
28  import org.kuali.rice.krad.uif.container.Container;
29  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
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.MaintenanceDocumentForm;
33  
34  import javax.xml.namespace.QName;
35  import java.util.ArrayList;
36  import java.util.Collection;
37  import java.util.Collections;
38  import java.util.Comparator;
39  import java.util.List;
40  
41  /**
42   * Custom view helper for the people flow maintenance document to retrieve the type attribute remotable fields
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  public class PeopleFlowMaintainableImpl extends MaintainableImpl {
47  
48  
49      /**
50       * sort {@link org.kuali.rice.kew.impl.peopleflow.PeopleFlowMemberBo}s by stop number (priority)
51       *
52       * @param collection - the Collection to add the given addLine to
53       * @param addLine - the line to add to the given collection
54       * @param insertFirst - indicates if the item should be inserted as the first item
55       */
56      @Override
57      protected void addLine(Collection<Object> collection, Object addLine, boolean insertFirst) {
58          if (collection instanceof List) {
59              ((List) collection).add(0, addLine);
60              if (addLine instanceof PeopleFlowMemberBo) {
61                  Collections.sort((List) collection, new Comparator<Object>() {
62                      public int compare(Object o1, Object o2) {
63                          if ((o1 instanceof PeopleFlowMemberBo) && (o1 instanceof PeopleFlowMemberBo)) {
64                              return ((PeopleFlowMemberBo) o1).getPriority() - ((PeopleFlowMemberBo) o2)
65                                      .getPriority();
66                          }
67                          return 0; // if not both PeopleFlowMemberBo something strange is going on.  Use equals as doing nothing.
68                      }
69                  });
70              }
71          } else {
72              collection.add(addLine);
73          }
74      }
75  
76      /**
77       * Invokes the {@link org.kuali.rice.kew.api.repository.type.KewTypeRepositoryService} to retrieve the remotable
78       * field definitions for the attributes associated with the selected type
79       *
80       * @param view - view instance
81       * @param model - object containing the form data, from which the selected type will be pulled
82       * @param container - container that holds the remotable fields
83       * @return List<RemotableAttributeField> instances for the type attributes, or empty list if not attributes exist
84       */
85      public List<RemotableAttributeField> retrieveTypeAttributes(View view, Object model, Container container) {
86          List<RemotableAttributeField> remoteFields = new ArrayList<RemotableAttributeField>();
87  
88          PeopleFlowBo peopleFlow =
89                  (PeopleFlowBo) ((MaintenanceDocumentForm) model).getDocument().getNewMaintainableObject().getDataObject();
90  
91          // retrieve the type service and invoke to get the remotable field definitions
92          String typeId = peopleFlow.getTypeId();
93          if (StringUtils.isNotBlank(typeId)) {
94              KewTypeDefinition typeDefinition = KewApiServiceLocator.getKewTypeRepositoryService().getTypeById(typeId);
95              PeopleFlowTypeService peopleFlowTypeService = GlobalResourceLoader.<PeopleFlowTypeService>getService(
96                  QName.valueOf(typeDefinition.getServiceName()));
97              remoteFields = peopleFlowTypeService.getAttributeFields(typeId);
98          }
99  
100         return remoteFields;
101     }
102 
103     /**
104      * Set the attribute bo list from the map of attribute key/value pairs and then calls
105      * {@link org.kuali.rice.kew.api.peopleflow.PeopleFlowService} to save the people flow instance
106      */
107     @Override
108     public void saveDataObject() {
109         ((PeopleFlowBo) getDataObject()).updateAttributeBoValues();
110 
111         PeopleFlowDefinition peopleFlowDefinition;
112         if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
113             peopleFlowDefinition = PeopleFlowBo.maintenanceCopy(((PeopleFlowBo) getDataObject()));
114         } else {
115         // this to method ends up copying a versionNumber to null versionNumber 
116             peopleFlowDefinition = PeopleFlowBo.to(((PeopleFlowBo) getDataObject()));
117         }
118         if (StringUtils.isNotBlank(peopleFlowDefinition.getId())) {
119             KewApiServiceLocator.getPeopleFlowService().updatePeopleFlow(peopleFlowDefinition);
120         } else {
121             KewApiServiceLocator.getPeopleFlowService().createPeopleFlow(peopleFlowDefinition);
122         }
123     }
124 
125     /**
126      * In the case of edit maintenance adds a new blank line to the old side
127      * This method is intended to override the method in MaintainableImpl
128      * but has a different set of parameters, so it is not actually an override.
129      * This version was needed to fetch the old collection from a different path
130      * than MaintainableImpl uses.
131      *
132      * @see org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl#processAfterAddLine(org.kuali.rice.krad.uif.view.View,
133      *      org.kuali.rice.krad.uif.container.CollectionGroup, java.lang.Object,
134      *      java.lang.Object)
135      */
136     protected void processAfterAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine, String collectionPath) {
137         // Check for maintenance documents in edit but exclude notes
138         if (model instanceof MaintenanceDocumentForm
139                 && KRADConstants.MAINTENANCE_EDIT_ACTION.equals(((MaintenanceDocumentForm)model).getMaintenanceAction()) && !(addLine instanceof Note)) {
140 //            MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) model;
141 //            MaintenanceDocument document = maintenanceForm.getDocument();
142 
143             // get the old object's collection
144             String oldCollectionPath = collectionPath.replace("newMaintainableObject","oldMaintainableObject");
145             Collection<Object> oldCollection = ObjectPropertyUtils.getPropertyValue(model, oldCollectionPath );
146             try {
147                 Object blankLine = collectionGroup.getCollectionObjectClass().newInstance();
148                 oldCollection.add(blankLine);
149             } catch (Exception e) {
150                 throw new RuntimeException("Unable to create new line instance for old maintenance object", e);
151             }
152         }
153     }
154 
155     /**
156      * This method is an override of ViewHelperService.processCollectionDeleteLine().
157      * It is virtually identical except that a local processAfterDeleteLine() method is called
158      * with a different parameter set than is called from within this method to delete the line
159      * from the old maintainable object.
160      * @see org.kuali.rice.krad.uif.service.ViewHelperService#processCollectionDeleteLine(org.kuali.rice.krad.uif.view.View,
161      *      java.lang.Object, java.lang.String, int)
162      */
163     @Override
164     public void processCollectionDeleteLine(View view, Object model, String collectionPath, int lineIndex) {
165         // get the collection group from the view
166         CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(collectionPath);
167         if (collectionGroup == null) {
168             logAndThrowRuntime("Unable to get collection group component for path: " + collectionPath);
169         }
170 
171         // get the collection instance for adding the new line
172         Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model, collectionPath);
173         if (collection == null) {
174             logAndThrowRuntime("Unable to get collection property from model for path: " + collectionPath);
175         }
176 
177         // TODO: look into other ways of identifying a line so we can deal with
178         // unordered collections
179         if (collection instanceof List) {
180             Object deleteLine = ((List<Object>) collection).get(lineIndex);
181 
182             // validate the delete action is allowed for this line
183             boolean isValid = performDeleteLineValidation(view, collectionGroup, deleteLine);
184             if (isValid) {
185                 ((List<Object>) collection).remove(lineIndex);
186                 processAfterDeleteLine(view, collectionPath, model, lineIndex);
187             }
188         } else {
189             logAndThrowRuntime("Only List collection implementations are supported for the delete by index method");
190         }
191     }
192 
193     /**
194      * In the case of edit maintenance deleted the item on the old side.
195      * This method is intended to override the method in MaintainableImpl
196      * but has a different set of parameters, so it is not actually an override.
197      * This was needed to fetch the old collection from a different path
198      * than MaintainableImpl uses. This version has the path (to the newMaintainableObject
199      * provided as a parameter, this is used to generate a path to the oldMaintainableObject
200      *
201      *
202      * @see org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl#processAfterDeleteLine(View,
203      *      org.kuali.rice.krad.uif.container.CollectionGroup, java.lang.Object,  int)
204      */
205     protected void processAfterDeleteLine(View view, String collectionPath, Object model, int lineIndex) {
206 
207         // Check for maintenance documents in edit
208         if (model instanceof MaintenanceDocumentForm
209                 && KRADConstants.MAINTENANCE_EDIT_ACTION.equals(((MaintenanceDocumentForm)model).getMaintenanceAction())) {
210 
211             // get the old object's collection
212             String oldCollectionPath = collectionPath.replace("newMaintainableObject","oldMaintainableObject");
213             Collection<Object> oldCollection = ObjectPropertyUtils.getPropertyValue(model, oldCollectionPath);
214             try {
215                 // Remove the object at lineIndex from the collection
216                 oldCollection.remove(oldCollection.toArray()[lineIndex]);
217             } catch (Exception e) {
218                 throw new RuntimeException("Unable to delete line instance for old maintenance object", e);
219             }
220         }
221     }
222 
223 
224 }