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