1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.student.cm.maintenance;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.kew.api.WorkflowDocument;
21 import org.kuali.rice.kew.framework.postprocessor.ActionTakenEvent;
22 import org.kuali.rice.kew.framework.postprocessor.DocumentRouteLevelChange;
23 import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
24 import org.kuali.rice.krad.maintenance.Maintainable;
25 import org.kuali.rice.krad.maintenance.MaintenanceDocumentBase;
26 import org.kuali.rice.krad.rules.rule.event.BlanketApproveDocumentEvent;
27 import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
28 import org.kuali.rice.krad.rules.rule.event.SaveEvent;
29 import org.kuali.student.cm.proposal.service.ProposalMaintainable;
30 import org.kuali.student.r2.core.proposal.dto.ProposalInfo;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import javax.persistence.Entity;
35
36
37
38
39
40
41
42
43
44
45
46 @Entity
47 public class CMMaintenanceDocument extends MaintenanceDocumentBase {
48
49 private static final long serialVersionUID = -505085142412593315L;
50 private static final Logger LOG = LoggerFactory.getLogger(CMMaintenanceDocument.class);
51
52 public CMMaintenanceDocument() {
53 super();
54 }
55
56 public CMMaintenanceDocument(String documentTypeName) {
57 super(documentTypeName);
58 }
59
60 @Override
61 public void processAfterRetrieve() {
62
63 if (documentHeader == null || !documentHeader.hasWorkflowDocument()) {
64 throw new RuntimeException("Document Header or workflow document is null");
65 }
66
67 String documentTypeName = documentHeader.getWorkflowDocument().getDocumentTypeName();
68
69 Class clazz = getDocumentDictionaryService().getMaintainableClass(documentTypeName);
70
71 if (!CMMaintainable.class.isAssignableFrom(clazz)) {
72 throw new RuntimeException("Maintainable should be of CMMaintainable type");
73 }
74
75 try {
76
77 Class<?> dataObjectClazz = getDocumentDictionaryService().getMaintenanceDataObjectClass(documentTypeName);
78
79
80
81
82
83 if (oldMaintainableObject == null) {
84 oldMaintainableObject = (CMMaintainable) clazz.newInstance();
85 oldMaintainableObject.setDataObject(dataObjectClazz.newInstance());
86 oldMaintainableObject.setDataObjectClass(dataObjectClazz);
87 }
88
89 if (newMaintainableObject == null) {
90 newMaintainableObject = (CMMaintainable) clazz.newInstance();
91 newMaintainableObject.setDataObject(dataObjectClazz.newInstance());
92 newMaintainableObject.setDataObjectClass(dataObjectClazz);
93 }
94
95 } catch (InstantiationException e) {
96 throw new RuntimeException("Unable to initialize maintainables of type " + clazz.getName(), e);
97 } catch (IllegalAccessException e) {
98 throw new RuntimeException("Unable to initialize maintainables of type " + clazz.getName(), e);
99 }
100
101
102 super.processAfterRetrieve();
103
104 }
105
106 @Override
107 public void prepareForSave(KualiDocumentEvent event) {
108 super.prepareForSave(event);
109
110 if ( (event instanceof SaveEvent) || (event instanceof BlanketApproveDocumentEvent) ) {
111
112 if (getDocumentHeader() != null && getDocumentHeader().getWorkflowDocument() != null && getNewMaintainableObject().getDataObject() != null) {
113 if (ProposalMaintainable.class.isAssignableFrom(getNewMaintainableObject().getClass())) {
114 ProposalInfo proposalInfo = ((ProposalMaintainable)getNewMaintainableObject()).getProposalInfo();
115 if (proposalInfo != null) {
116 getDocumentHeader().getWorkflowDocument().setApplicationDocumentId(proposalInfo.getId());
117 } else {
118 LOG.warn("No ProposalInfo object found for document with id: " + getDocumentHeader().getWorkflowDocument().getDocumentId());
119 }
120 }
121 }
122 getNewMaintainableObject().saveDataObject();
123 }
124 }
125
126
127
128
129
130
131 @Override
132 public void populateXmlDocumentContentsFromMaintainables() {
133 xmlDocumentContents = StringUtils.EMPTY;
134 }
135
136
137
138
139
140
141
142 @Override
143 public void populateMaintainablesFromXmlDocumentContents() {
144
145 xmlDocumentContents = StringUtils.EMPTY;
146
147
148 newMaintainableObject.setDocumentNumber(documentNumber);
149
150 ((CMMaintainable) newMaintainableObject).retrieveDataObject();
151 }
152
153 @Override
154 public void doActionTaken(ActionTakenEvent actionTakenEvent) {
155 Maintainable maintainable = getNewMaintainableObject();
156 Class clazz = (maintainable!=null)?maintainable.getClass() : null;
157 if (clazz==null || !ProposalMaintainable.class.isAssignableFrom(clazz)) {
158 throw new RuntimeException("Maintainable should be of ProposalMaintainable type");
159 }
160 try {
161 ((ProposalMaintainable)getNewMaintainableObject()).doActionTaken(actionTakenEvent);
162 } catch (Exception e) {
163 LOG.error("Error caught operating on action taken", e);
164 throw new RuntimeException(e);
165 }
166 }
167
168 @Override
169 public void doRouteLevelChange(DocumentRouteLevelChange documentRouteLevelChange) {
170 Maintainable maintainable = getNewMaintainableObject();
171 Class clazz = (maintainable!=null)?maintainable.getClass() : null;
172 if (clazz==null || !ProposalMaintainable.class.isAssignableFrom(clazz)) {
173 throw new RuntimeException("Maintainable should be of ProposalMaintainable type");
174 }
175 try {
176 ((ProposalMaintainable)getNewMaintainableObject()).doRouteLevelChange(documentRouteLevelChange);
177 } catch (Exception e) {
178 LOG.error("Error caught performing route level change", e);
179 throw new RuntimeException(e);
180 }
181 }
182
183 @Override
184 public void doRouteStatusChange(DocumentRouteStatusChange documentRouteStatusChange) {
185 Maintainable maintainable = getNewMaintainableObject();
186 Class clazz = (maintainable!=null)?maintainable.getClass() : null;
187 if (clazz==null || !ProposalMaintainable.class.isAssignableFrom(clazz)) {
188 throw new RuntimeException("Maintainable should be of ProposalMaintainable type");
189 }
190 try {
191 ((ProposalMaintainable)getNewMaintainableObject()).doRouteStatusChange(documentRouteStatusChange);
192 } catch (Exception e) {
193 LOG.error("Error caught performing route status change", e);
194 throw new RuntimeException(e);
195 }
196
197
198 WorkflowDocument workflowDocument = getDocumentHeader().getWorkflowDocument();
199
200
201 if (workflowDocument.isProcessed()) {
202
203
204
205 getMaintenanceDocumentService().deleteLocks(workflowDocument.getDocumentId());
206
207
208 }
209
210
211 else if (workflowDocument.isCanceled() || workflowDocument.isDisapproved() || workflowDocument.isRecalled()) {
212
213
214 getMaintenanceDocumentService().deleteLocks(workflowDocument.getDocumentId());
215 }
216
217
218
219
220
221
222 }
223
224 }