1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 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
42
43
44
45 public class PeopleFlowMaintainableImpl extends MaintainableImpl {
46
47
48
49
50
51
52
53
54
55 @Override
56 protected void addLine(Collection<Object> collection, Object addLine, boolean insertFirst) {
57 if (collection instanceof List) {
58 ((List) collection).add(0, addLine);
59 if (addLine instanceof PeopleFlowMemberBo) {
60 Collections.sort((List) collection, new Comparator<Object>() {
61 public int compare(Object o1, Object o2) {
62 if ((o1 instanceof PeopleFlowMemberBo) && (o1 instanceof PeopleFlowMemberBo)) {
63 return ((PeopleFlowMemberBo) o1).getPriority() - ((PeopleFlowMemberBo) o2)
64 .getPriority();
65 }
66 return 0;
67 }
68 });
69 }
70 } else {
71 collection.add(addLine);
72 }
73 }
74
75
76
77
78
79
80
81
82
83
84 public List<RemotableAttributeField> retrieveTypeAttributes(View view, Object model, Container container) {
85 List<RemotableAttributeField> remoteFields = new ArrayList<RemotableAttributeField>();
86
87 PeopleFlowBo peopleFlow =
88 (PeopleFlowBo) ((MaintenanceDocumentForm) model).getDocument().getNewMaintainableObject().getDataObject();
89
90
91 String typeId = peopleFlow.getTypeId();
92 if (StringUtils.isNotBlank(typeId)) {
93 KewTypeDefinition typeDefinition = KewApiServiceLocator.getKewTypeRepositoryService().getTypeById(typeId);
94 PeopleFlowTypeService peopleFlowTypeService = GlobalResourceLoader.<PeopleFlowTypeService>getService(
95 typeDefinition.getServiceName());
96 remoteFields = peopleFlowTypeService.getAttributeFields(typeId);
97 }
98
99 return remoteFields;
100 }
101
102
103
104
105
106 @Override
107 public void saveDataObject() {
108 ((PeopleFlowBo) getDataObject()).updateAttributeBoValues();
109
110 PeopleFlowDefinition peopleFlowDefinition;
111 if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
112 peopleFlowDefinition = PeopleFlowBo.maintenanceCopy(((PeopleFlowBo) getDataObject()));
113 } else {
114
115 peopleFlowDefinition = PeopleFlowBo.to(((PeopleFlowBo) getDataObject()));
116 }
117 if (StringUtils.isNotBlank(peopleFlowDefinition.getId())) {
118 KewApiServiceLocator.getPeopleFlowService().updatePeopleFlow(peopleFlowDefinition);
119 } else {
120 KewApiServiceLocator.getPeopleFlowService().createPeopleFlow(peopleFlowDefinition);
121 }
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135 protected void processAfterAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine, String collectionPath) {
136
137 if (model instanceof MaintenanceDocumentForm
138 && KRADConstants.MAINTENANCE_EDIT_ACTION.equals(((MaintenanceDocumentForm)model).getMaintenanceAction()) && !(addLine instanceof Note)) {
139
140
141
142
143 String oldCollectionPath = collectionPath.replace("newMaintainableObject","oldMaintainableObject");
144 Collection<Object> oldCollection = ObjectPropertyUtils.getPropertyValue(model, oldCollectionPath );
145 try {
146 Object blankLine = collectionGroup.getCollectionObjectClass().newInstance();
147 oldCollection.add(blankLine);
148 } catch (Exception e) {
149 throw new RuntimeException("Unable to create new line instance for old maintenance object", e);
150 }
151 }
152 }
153
154
155
156
157
158
159
160
161
162 @Override
163 public void processCollectionDeleteLine(View view, Object model, String collectionPath, int lineIndex) {
164
165 CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(collectionPath);
166 if (collectionGroup == null) {
167 logAndThrowRuntime("Unable to get collection group component for path: " + collectionPath);
168 }
169
170
171 Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model, collectionPath);
172 if (collection == null) {
173 logAndThrowRuntime("Unable to get collection property from model for path: " + collectionPath);
174 }
175
176
177
178 if (collection instanceof List) {
179 Object deleteLine = ((List<Object>) collection).get(lineIndex);
180
181
182 boolean isValid = performDeleteLineValidation(view, collectionGroup, deleteLine);
183 if (isValid) {
184 ((List<Object>) collection).remove(lineIndex);
185 processAfterDeleteLine(view, collectionPath, model, lineIndex);
186 }
187 } else {
188 logAndThrowRuntime("Only List collection implementations are supported for the delete by index method");
189 }
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204 protected void processAfterDeleteLine(View view, String collectionPath, Object model, int lineIndex) {
205
206
207 if (model instanceof MaintenanceDocumentForm
208 && KRADConstants.MAINTENANCE_EDIT_ACTION.equals(((MaintenanceDocumentForm)model).getMaintenanceAction())) {
209
210
211 String oldCollectionPath = collectionPath.replace("newMaintainableObject","oldMaintainableObject");
212 Collection<Object> oldCollection = ObjectPropertyUtils.getPropertyValue(model, oldCollectionPath);
213 try {
214
215 oldCollection.remove(oldCollection.toArray()[lineIndex]);
216 } catch (Exception e) {
217 throw new RuntimeException("Unable to delete line instance for old maintenance object", e);
218 }
219 }
220 }
221
222
223 }