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.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
42
43
44
45 public class PeopleFlowMaintainableImpl extends MaintainableImpl {
46
47
48
49
50
51
52
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;
66 }
67 });
68 }
69 } else {
70 collection.add(addLine);
71 }
72 }
73
74
75
76
77
78
79
80
81
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
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
103
104
105 @Override
106 public void saveDataObject() {
107 ((PeopleFlowBo) getDataObject()).updateAttributeBoValues();
108
109 PeopleFlowDefinition peopleFlowDefinition;
110 if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
111 peopleFlowDefinition = PeopleFlowBo.maintenanceCopy(((PeopleFlowBo) getDataObject()));
112 } else {
113
114 peopleFlowDefinition = PeopleFlowBo.to(((PeopleFlowBo) getDataObject()));
115 }
116 if (StringUtils.isNotBlank(peopleFlowDefinition.getId())) {
117 KewApiServiceLocator.getPeopleFlowService().updatePeopleFlow(peopleFlowDefinition);
118 } else {
119 KewApiServiceLocator.getPeopleFlowService().createPeopleFlow(peopleFlowDefinition);
120 }
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134 protected void processAfterAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine, String collectionPath) {
135
136 if (model instanceof MaintenanceForm && KRADConstants.MAINTENANCE_EDIT_ACTION.equals(((MaintenanceForm)model).getMaintenanceAction()) && !(addLine instanceof Note)) {
137
138
139
140
141 String oldCollectionPath = collectionPath.replace("newMaintainableObject","oldMaintainableObject");
142 Collection<Object> oldCollection = ObjectPropertyUtils.getPropertyValue(model, oldCollectionPath );
143 try {
144 Object blankLine = collectionGroup.getCollectionObjectClass().newInstance();
145 oldCollection.add(blankLine);
146 } catch (Exception e) {
147 throw new RuntimeException("Unable to create new line instance for old maintenance object", e);
148 }
149 }
150 }
151
152
153
154
155
156
157
158
159
160 @Override
161 public void processCollectionDeleteLine(View view, Object model, String collectionPath, int lineIndex) {
162
163 CollectionGroup collectionGroup = view.getViewIndex().getCollectionGroupByPath(collectionPath);
164 if (collectionGroup == null) {
165 logAndThrowRuntime("Unable to get collection group component for path: " + collectionPath);
166 }
167
168
169 Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(model, collectionPath);
170 if (collection == null) {
171 logAndThrowRuntime("Unable to get collection property from model for path: " + collectionPath);
172 }
173
174
175
176 if (collection instanceof List) {
177 Object deleteLine = ((List<Object>) collection).get(lineIndex);
178
179
180 boolean isValid = performDeleteLineValidation(view, collectionGroup, deleteLine);
181 if (isValid) {
182 ((List<Object>) collection).remove(lineIndex);
183 processAfterDeleteLine(view, collectionPath, model, lineIndex);
184 }
185 } else {
186 logAndThrowRuntime("Only List collection implementations are supported for the delete by index method");
187 }
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202 protected void processAfterDeleteLine(View view, String collectionPath, Object model, int lineIndex) {
203
204
205 if (model instanceof MaintenanceForm && KRADConstants.MAINTENANCE_EDIT_ACTION.equals(((MaintenanceForm)model).getMaintenanceAction())) {
206
207
208 String oldCollectionPath = collectionPath.replace("newMaintainableObject","oldMaintainableObject");
209 Collection<Object> oldCollection = ObjectPropertyUtils.getPropertyValue(model, oldCollectionPath);
210 try {
211
212 oldCollection.remove(oldCollection.toArray()[lineIndex]);
213 } catch (Exception e) {
214 throw new RuntimeException("Unable to delete line instance for old maintenance object", e);
215 }
216 }
217 }
218
219
220 }