1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.ui;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.kuali.rice.krad.maintenance.MaintainableImpl;
20 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
21 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
22 import org.kuali.rice.krad.uif.view.ViewModel;
23 import org.kuali.rice.krad.util.KRADConstants;
24 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
25 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
26 import org.kuali.rice.krms.impl.repository.ContextBo;
27 import org.kuali.rice.krms.impl.repository.ContextValidTermBo;
28 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
29 import org.kuali.rice.krms.impl.repository.TermSpecificationBo;
30
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.ListIterator;
34 import java.util.Map;
35
36
37
38
39
40
41
42 public class TermSpecificationMaintainable extends MaintainableImpl {
43
44 private static final long serialVersionUID = 1L;
45
46 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TermSpecificationMaintainable.class);
47
48 @Override
49 public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
50
51 TermSpecificationBo termSpecificationBo = (TermSpecificationBo) super.retrieveObjectForEditOrCopy(document,
52 dataObjectKeys);
53
54 if (!CollectionUtils.isEmpty(termSpecificationBo.getContextValidTerms())) {
55 for (ContextValidTermBo contextValidTerm : termSpecificationBo.getContextValidTerms()) {
56 ContextDefinition context =
57 KrmsRepositoryServiceLocator.getContextBoService().getContextByContextId(contextValidTerm.getContextId());
58
59 if (context != null) {
60 termSpecificationBo.getContexts().add(ContextBo.from(context));
61 termSpecificationBo.getContextIds().add(contextValidTerm.getContextId());
62 }
63 }
64 }
65
66 if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
67 document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
68 }
69
70 return termSpecificationBo;
71 }
72
73
74
75
76 @Override
77 public void processAfterNew(MaintenanceDocument document,
78 Map<String, String[]> requestParameters) {
79
80 super.processAfterNew(document, requestParameters);
81 document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
82
83 }
84
85
86
87
88 @Override
89 public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
90 super.processAfterEdit(document, requestParameters);
91 copyContextsOldToNewBo(document);
92 document.getDocumentHeader().setDocumentDescription("Edited Term Specification Document");
93 }
94
95 @Override
96 public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
97 super.processAfterCopy(document, requestParameters);
98 copyContextsOldToNewBo(document);
99 }
100
101
102
103
104
105
106
107
108
109
110 private void copyContextsOldToNewBo(MaintenanceDocument document) {
111 TermSpecificationBo oldTermSpec = (TermSpecificationBo) document.getOldMaintainableObject().getDataObject();
112 TermSpecificationBo newTermSpec = (TermSpecificationBo) document.getNewMaintainableObject().getDataObject();
113 newTermSpec.setContexts(new ArrayList<ContextBo>());
114 for (ContextBo contextBo : oldTermSpec.getContexts()) {
115 newTermSpec.getContexts().add(contextBo);
116 }
117 }
118
119
120
121
122
123
124
125 @Override
126 public void prepareForSave() {
127 super.prepareForSave();
128
129 TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
130 termSpec.getContexts().clear();
131 }
132
133
134
135
136
137
138
139 @Override
140 public void processAfterAddLine(ViewModel viewModel, Object addLine, String collectionId, String collectionPath,
141 boolean isValidLine) {
142 super.processAfterAddLine(viewModel, addLine, collectionId, collectionPath, isValidLine);
143
144
145 if (addLine == null || !(addLine instanceof ContextBo)) { return; }
146
147 ContextBo addedContext = (ContextBo) addLine;
148 TermSpecificationBo termSpec = getDataObjectFromForm(viewModel);
149
150 boolean alreadyHasContextValidTerm = false;
151
152 for (ContextValidTermBo contextValidTerm : termSpec.getContextValidTerms()) {
153 if (contextValidTerm.getContextId().equals(addedContext.getId())) {
154 alreadyHasContextValidTerm = true;
155 break;
156 }
157 }
158
159 if (!alreadyHasContextValidTerm) {
160 ContextValidTermBo contextValidTerm = new ContextValidTermBo();
161 contextValidTerm.setContextId(addedContext.getId());
162 contextValidTerm.setTermSpecification(termSpec);
163 termSpec.getContextValidTerms().add(contextValidTerm);
164 }
165 }
166
167
168
169
170
171
172
173 @Override
174 public void processCollectionDeleteLine(ViewModel viewModel, String collectionId, String collectionPath,
175 int lineIndex) {
176 super.processCollectionDeleteLine(viewModel, collectionId, collectionPath, lineIndex);
177
178 List collection = ObjectPropertyUtils.getPropertyValue(viewModel, collectionPath);
179 Object deletedItem = collection.get(lineIndex);
180
181
182 if (deletedItem == null || !(deletedItem instanceof ContextBo)) { return; }
183
184 ContextBo context = (ContextBo) deletedItem;
185 TermSpecificationBo termSpec = getDataObjectFromForm(viewModel);
186
187
188 ListIterator<ContextValidTermBo> contextValidTermListIter = termSpec.getContextValidTerms().listIterator();
189 while (contextValidTermListIter.hasNext()) {
190 ContextValidTermBo contextValidTerm = contextValidTermListIter.next();
191
192 if (contextValidTerm.getContextId().equals(context.getId())) {
193 contextValidTerm.setTermSpecification(null);
194 contextValidTermListIter.remove();
195 }
196 }
197 }
198
199
200
201
202
203
204
205
206
207
208
209 private static <T> T getDataObjectFromForm(ViewModel form) {
210 if (form == null) { return null; }
211
212 return (T) ((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject();
213 }
214
215 @Override
216 public Class getDataObjectClass() {
217 return TermSpecificationBo.class;
218 }
219
220
221
222
223
224
225 @Override
226 public void processAfterRetrieve() {
227 super.processAfterRetrieve();
228
229 TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
230 termSpec.setContexts(new ArrayList<ContextBo>());
231 for (ContextValidTermBo contextValidTerm : termSpec.getContextValidTerms()) {
232 ContextDefinition context = KrmsRepositoryServiceLocator.getContextBoService().getContextByContextId(contextValidTerm.getContextId());
233 termSpec.getContexts().add(ContextBo.from(context));
234 termSpec.getContextIds().add(context.getId());
235 }
236 }
237 }