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.kns.service.KNSServiceLocator;
20 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
21 import org.kuali.rice.krad.maintenance.MaintainableImpl;
22 import org.kuali.rice.krad.service.BusinessObjectService;
23 import org.kuali.rice.krad.service.KRADServiceLocator;
24 import org.kuali.rice.krad.util.KRADConstants;
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.Collection;
33 import java.util.Collections;
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
49
50
51 public BusinessObjectService getBoService() {
52 return KNSServiceLocator.getBusinessObjectService();
53 }
54
55 @Override
56 public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
57
58 TermSpecificationBo termSpecificationBo = (TermSpecificationBo) super.retrieveObjectForEditOrCopy(document,
59 dataObjectKeys);
60
61 if (!CollectionUtils.isEmpty(termSpecificationBo.getContextIds())) {
62 for (String contextId : termSpecificationBo.getContextIds()) {
63 ContextDefinition context =
64 KrmsRepositoryServiceLocator.getContextBoService().getContextByContextId(contextId);
65 if (context != null) {
66 termSpecificationBo.getContexts().add(ContextBo.from(context));
67 }
68 }
69 }
70
71 if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
72 document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
73 }
74
75 return termSpecificationBo;
76 }
77
78
79
80
81
82
83 private void findContexts(TermSpecificationBo termSpecificationBo) {
84 Collection<ContextValidTermBo> validContextMappings =
85 getBoService().findMatching(ContextValidTermBo.class,
86 Collections.singletonMap("termSpecificationId", termSpecificationBo.getId()));
87
88 if (!CollectionUtils.isEmpty(validContextMappings)) for (ContextValidTermBo validContextMapping : validContextMappings) {
89 termSpecificationBo.getContextIds().add(validContextMapping.getContextId());
90 }
91 }
92
93
94
95
96 @Override
97 public void processAfterNew(MaintenanceDocument document,
98 Map<String, String[]> requestParameters) {
99
100 super.processAfterNew(document, requestParameters);
101 document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
102
103 }
104
105
106
107
108 @Override
109 public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
110 super.processAfterEdit(document, requestParameters);
111 copyContextsOldToNewBo(document);
112 document.getDocumentHeader().setDocumentDescription("Edited Term Specification Document");
113 }
114
115 @Override
116 public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
117 super.processAfterCopy(document, requestParameters);
118 copyContextsOldToNewBo(document);
119 }
120
121
122
123
124
125
126
127
128
129
130 private void copyContextsOldToNewBo(MaintenanceDocument document) {
131 TermSpecificationBo oldTermSpec = (TermSpecificationBo) document.getOldMaintainableObject().getDataObject();
132 TermSpecificationBo newTermSpec = (TermSpecificationBo) document.getNewMaintainableObject().getDataObject();
133 newTermSpec.setContexts(new ArrayList<ContextBo>());
134 for (ContextBo contextBo : oldTermSpec.getContexts()) {
135 newTermSpec.getContexts().add(contextBo);
136 }
137 }
138
139
140
141
142
143
144 @Override
145 public void prepareForSave() {
146 super.prepareForSave();
147
148 TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
149 termSpec.setContextIds(new ArrayList<String>());
150 for (ContextBo context : termSpec.getContexts()) {
151 termSpec.getContextIds().add(context.getId());
152 }
153 }
154
155 @Override
156 public void saveDataObject() {
157 TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
158
159 super.saveDataObject();
160
161 if (termSpec.getId() != null) {
162
163 getBoService().deleteMatching(ContextValidTermBo.class,
164 Collections.singletonMap("termSpecificationId", termSpec.getId()));
165
166
167 for (String contextId : termSpec.getContextIds()) {
168 ContextValidTermBo contextValidTerm = new ContextValidTermBo();
169 contextValidTerm.setContextId(contextId);
170 contextValidTerm.setTermSpecificationId(termSpec.getId());
171 getBoService().save(contextValidTerm);
172 }
173 }
174 }
175
176 @Override
177 public Class getDataObjectClass() {
178 return TermSpecificationBo.class;
179 }
180
181
182
183
184
185
186 @Override
187 public void processAfterRetrieve() {
188 super.processAfterRetrieve();
189
190 TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
191 termSpec.setContexts(new ArrayList<ContextBo>());
192 for (String contextId : termSpec.getContextIds()) {
193 ContextDefinition context = KrmsRepositoryServiceLocator.getContextBoService().getContextByContextId(contextId);
194 termSpec.getContexts().add(ContextBo.from(context));
195 }
196 }
197 }