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 (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
55 document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
56 }
57
58 return termSpecificationBo;
59 }
60
61
62
63
64 @Override
65 public void processAfterNew(MaintenanceDocument document,
66 Map<String, String[]> requestParameters) {
67
68 super.processAfterNew(document, requestParameters);
69 document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
70
71 }
72
73
74
75
76 @Override
77 public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
78 super.processAfterEdit(document, requestParameters);
79 document.getDocumentHeader().setDocumentDescription("Edited Term Specification Document");
80 }
81
82 @Override
83 public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
84 super.processAfterCopy(document, requestParameters);
85 copyContextsOldToNewBo(document);
86 setNewContextValidTermsNewBO(document);
87 }
88
89
90
91
92
93
94
95
96
97
98 private void copyContextsOldToNewBo(MaintenanceDocument document) {
99 TermSpecificationBo newTermSpec = (TermSpecificationBo) document.getNewMaintainableObject().getDataObject();
100
101
102 newTermSpec.getContexts().clear();
103 newTermSpec.getContexts();
104 }
105
106 private void setNewContextValidTermsNewBO(MaintenanceDocument document) {
107 TermSpecificationBo newTermSpec = (TermSpecificationBo) document.getNewMaintainableObject().getDataObject();
108 for (ContextValidTermBo contextValidTermBo : newTermSpec.getContextValidTerms()) {
109 contextValidTermBo.setId(null);
110 contextValidTermBo.setTermSpecification(newTermSpec);
111 }
112 }
113
114
115
116
117
118
119
120 @Override
121 public void prepareForSave() {
122 super.prepareForSave();
123
124 TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
125 termSpec.getContexts().clear();
126 }
127
128
129
130
131
132
133
134 @Override
135 public void processAfterAddLine(ViewModel viewModel, Object addLine, String collectionId, String collectionPath,
136 boolean isValidLine) {
137
138 if (addLine == null || !(addLine instanceof ContextBo)) {
139 super.processAfterAddLine(viewModel, addLine, collectionId, collectionPath, isValidLine);
140 return;
141 }
142
143 ContextBo addedContext = (ContextBo) addLine;
144 TermSpecificationBo termSpec = getDataObjectFromForm(viewModel);
145
146 boolean alreadyHasContextValidTerm = false;
147
148 for (ContextValidTermBo contextValidTerm : termSpec.getContextValidTerms()) {
149 if (contextValidTerm.getContextId().equals(addedContext.getId())) {
150 alreadyHasContextValidTerm = true;
151 break;
152 }
153 }
154
155 if (!alreadyHasContextValidTerm) {
156 ContextValidTermBo contextValidTerm = new ContextValidTermBo();
157 contextValidTerm.setContextId(addedContext.getId());
158 contextValidTerm.setTermSpecification(termSpec);
159 termSpec.getContextValidTerms().add(contextValidTerm);
160 }
161 }
162
163
164
165
166
167
168
169 @Override
170 public void processCollectionDeleteLine(ViewModel viewModel, String collectionId, String collectionPath,
171 int lineIndex) {
172 List collection = ObjectPropertyUtils.getPropertyValue(viewModel, collectionPath);
173 Object deletedItem = collection.get(lineIndex);
174
175
176 if (deletedItem == null || !(deletedItem instanceof ContextBo)) {
177 super.processCollectionDeleteLine(viewModel, collectionId, collectionPath, lineIndex);
178 return;
179 }
180
181 ContextBo context = (ContextBo) deletedItem;
182 TermSpecificationBo termSpec = getDataObjectFromForm(viewModel);
183
184
185 ListIterator<ContextValidTermBo> contextValidTermListIter = termSpec.getContextValidTerms().listIterator();
186 while (contextValidTermListIter.hasNext()) {
187 ContextValidTermBo contextValidTerm = contextValidTermListIter.next();
188
189 if (contextValidTerm.getContextId().equals(context.getId())) {
190 contextValidTerm.setTermSpecification(null);
191 contextValidTermListIter.remove();
192 termSpec.getContexts().remove(context);
193 }
194 }
195 }
196
197
198
199
200
201
202
203
204
205
206
207 private static <T> T getDataObjectFromForm(ViewModel form) {
208 if (form == null) { return null; }
209
210 return (T) ((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject();
211 }
212
213 @Override
214 public Class getDataObjectClass() {
215 return TermSpecificationBo.class;
216 }
217
218
219
220
221
222
223 @Override
224 public void processAfterRetrieve() {
225 super.processAfterRetrieve();
226 }
227 }