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