1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.removereplace.service.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.apache.commons.lang.StringUtils; |
22 | |
import org.jdom.Element; |
23 | |
import org.kuali.rice.kew.exception.WorkflowException; |
24 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
25 | |
import org.kuali.rice.kew.export.ExportDataSet; |
26 | |
import org.kuali.rice.kew.removereplace.RemoveReplaceDocument; |
27 | |
import org.kuali.rice.kew.removereplace.RuleTarget; |
28 | |
import org.kuali.rice.kew.removereplace.WorkgroupTarget; |
29 | |
import org.kuali.rice.kew.removereplace.dao.RemoveReplaceDocumentDAO; |
30 | |
import org.kuali.rice.kew.removereplace.service.RemoveReplaceDocumentService; |
31 | |
import org.kuali.rice.kew.rule.RuleBaseValues; |
32 | |
import org.kuali.rice.kew.rule.service.RuleService; |
33 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
34 | |
import org.kuali.rice.kew.service.WorkflowDocument; |
35 | |
import org.kuali.rice.kew.user.WorkflowUserId; |
36 | |
import org.kuali.rice.kew.web.session.UserSession; |
37 | |
import org.kuali.rice.kim.bo.Group; |
38 | |
import org.kuali.rice.kim.bo.Person; |
39 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
40 | |
|
41 | |
|
42 | 0 | public class RemoveReplaceDocumentServiceImpl implements RemoveReplaceDocumentService { |
43 | |
|
44 | |
private RemoveReplaceDocumentDAO dao; |
45 | |
|
46 | |
public void save(RemoveReplaceDocument document) { |
47 | 0 | if (document.getDocumentId() == null) { |
48 | 0 | throw new WorkflowRuntimeException("The given document has a null document ID. Please assign a document ID prior to saving."); |
49 | |
} |
50 | 0 | dao.save(document); |
51 | 0 | } |
52 | |
|
53 | |
public RemoveReplaceDocument findById(Long documentId) { |
54 | 0 | return dao.findById(documentId); |
55 | |
} |
56 | |
|
57 | |
public void blanketApprove(RemoveReplaceDocument document, UserSession user, String annotation) { |
58 | 0 | save(document); |
59 | |
try { |
60 | 0 | WorkflowDocument workflowDoc = new WorkflowDocument(user.getPrincipalId(), document.getDocumentId()); |
61 | 0 | constructTitle(document, workflowDoc); |
62 | 0 | attachDocumentContent(document, workflowDoc); |
63 | 0 | workflowDoc.blanketApprove(annotation); |
64 | 0 | } catch (WorkflowException e) { |
65 | 0 | throw new WorkflowRuntimeException(e); |
66 | 0 | } |
67 | 0 | } |
68 | |
|
69 | |
public void route(RemoveReplaceDocument document, UserSession user, String annotation) { |
70 | 0 | save(document); |
71 | |
try { |
72 | 0 | WorkflowDocument workflowDoc = new WorkflowDocument(user.getPrincipalId(), document.getDocumentId()); |
73 | 0 | constructTitle(document, workflowDoc); |
74 | 0 | attachDocumentContent(document, workflowDoc); |
75 | 0 | workflowDoc.routeDocument(annotation); |
76 | 0 | } catch (WorkflowException e) { |
77 | 0 | throw new WorkflowRuntimeException(e); |
78 | 0 | } |
79 | 0 | } |
80 | |
|
81 | |
protected void constructTitle(RemoveReplaceDocument document, WorkflowDocument workflowDoc) throws WorkflowException { |
82 | 0 | Person user = KIMServiceLocator.getPersonService().getPerson(document.getUserWorkflowId()); |
83 | 0 | StringBuffer title = new StringBuffer(); |
84 | 0 | if (document.getOperation().equals(RemoveReplaceDocument.REMOVE_OPERATION)) { |
85 | 0 | title.append("Removing " + user.getPrincipalName() + " from "); |
86 | 0 | } else if (document.getOperation().equals(RemoveReplaceDocument.REPLACE_OPERATION)) { |
87 | 0 | Person replaceWithUser = KIMServiceLocator.getPersonService().getPerson(document.getReplacementUserWorkflowId()); |
88 | 0 | title.append("Replacing " + user.getPrincipalName() + " with " + replaceWithUser.getPrincipalName() + " in "); |
89 | |
} |
90 | 0 | title.append(document.getRuleTargets().size() + " rules and " + document.getWorkgroupTargets().size() + " workgroups"); |
91 | 0 | workflowDoc.setTitle(title.toString()); |
92 | 0 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
protected void attachDocumentContent(RemoveReplaceDocument document, WorkflowDocument workflowDoc) { |
98 | |
try { |
99 | 0 | Person user = KIMServiceLocator.getPersonService().getPerson(document.getUserWorkflowId()); |
100 | 0 | Element rootElement = new Element("removeReplaceUserDocument"); |
101 | 0 | Element removeReplaceElement = null; |
102 | 0 | if (document.getOperation().equals(RemoveReplaceDocument.REMOVE_OPERATION)) { |
103 | 0 | removeReplaceElement = new Element("remove"); |
104 | 0 | Element userElement = new Element("user"); |
105 | 0 | userElement.setText(user.getPrincipalName()); |
106 | 0 | removeReplaceElement.addContent(userElement); |
107 | 0 | } else if (document.getOperation().equals(RemoveReplaceDocument.REPLACE_OPERATION)) { |
108 | 0 | removeReplaceElement = new Element("replace"); |
109 | 0 | Element userElement = new Element("user"); |
110 | 0 | userElement.setText(user.getPrincipalName()); |
111 | 0 | removeReplaceElement.addContent(userElement); |
112 | 0 | Element replaceWithElement = new Element("replaceWith"); |
113 | 0 | Person replaceWithUser = KIMServiceLocator.getPersonService().getPerson(document.getReplacementUserWorkflowId()); |
114 | 0 | replaceWithElement.setText(replaceWithUser.getPrincipalName()); |
115 | 0 | removeReplaceElement.addContent(replaceWithElement); |
116 | 0 | } else { |
117 | 0 | throw new WorkflowRuntimeException("Invalid remove/replace operation specified: " + document.getOperation()); |
118 | |
} |
119 | 0 | rootElement.addContent(removeReplaceElement); |
120 | |
|
121 | |
|
122 | 0 | List<RuleBaseValues> rules = loadRules(document); |
123 | 0 | if (!rules.isEmpty()) { |
124 | 0 | ExportDataSet ruleDataSet = new ExportDataSet(); |
125 | 0 | ruleDataSet.getRules().addAll(rules); |
126 | 0 | Element rulesElement = KEWServiceLocator.getRuleService().export(ruleDataSet); |
127 | 0 | removeReplaceElement.addContent(rulesElement); |
128 | |
} |
129 | |
|
130 | |
|
131 | 0 | List<? extends Group> workgroups = loadWorkgroups(document); |
132 | 0 | if (!workgroups.isEmpty()) { |
133 | 0 | ExportDataSet workgroupDataSet = new ExportDataSet(); |
134 | 0 | workgroupDataSet.getGroups().addAll(workgroups); |
135 | |
if (true) { |
136 | 0 | throw new UnsupportedOperationException("TODO: please implement this once we have xml export of groups working in KIM!"); |
137 | |
} |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | 0 | } catch (Exception e) { |
144 | 0 | throw new WorkflowRuntimeException(e); |
145 | 0 | } |
146 | 0 | } |
147 | |
|
148 | |
protected List<? extends Group> loadWorkgroups(RemoveReplaceDocument document) { |
149 | 0 | List<Group> workgroups = new ArrayList<Group>(); |
150 | 0 | for (WorkgroupTarget workgroupTarget : document.getWorkgroupTargets()) { |
151 | 0 | Group group = KIMServiceLocator.getIdentityManagementService().getGroup(workgroupTarget.getWorkgroupId()); |
152 | 0 | if (group == null) { |
153 | 0 | throw new WorkflowRuntimeException("Failed to locate workgroup to change with id " + workgroupTarget.getWorkgroupId()); |
154 | |
} |
155 | 0 | workgroups.add(group); |
156 | 0 | } |
157 | 0 | return workgroups; |
158 | |
} |
159 | |
|
160 | |
protected List<RuleBaseValues> loadRules(RemoveReplaceDocument document) { |
161 | 0 | List<RuleBaseValues> rules = new ArrayList<RuleBaseValues>(); |
162 | 0 | for (RuleTarget ruleTarget : document.getRuleTargets()) { |
163 | 0 | RuleBaseValues rule = KEWServiceLocator.getRuleService().findRuleBaseValuesById(ruleTarget.getRuleId()); |
164 | 0 | if (rule == null) { |
165 | 0 | throw new WorkflowRuntimeException("Failed to locate rule to change with id " + ruleTarget.getRuleId()); |
166 | |
} |
167 | 0 | rules.add(rule); |
168 | 0 | } |
169 | 0 | return rules; |
170 | |
} |
171 | |
|
172 | |
|
173 | |
public void finalize(Long documentId) { |
174 | 0 | RemoveReplaceDocument document = findById(documentId); |
175 | |
|
176 | 0 | if (document == null) { |
177 | 0 | throw new WorkflowRuntimeException("Failed to locate the RemoveReplaceDocument with id " + documentId); |
178 | |
} |
179 | 0 | if (StringUtils.isEmpty(document.getUserWorkflowId())) { |
180 | 0 | throw new WorkflowRuntimeException("RemoveReplaceDocument does not have a user id."); |
181 | |
} |
182 | |
|
183 | 0 | List<Long> ruleIds = new ArrayList<Long>(); |
184 | 0 | if (document.getRuleTargets() != null) { |
185 | 0 | for (RuleTarget ruleTarget : document.getRuleTargets()) { |
186 | 0 | ruleIds.add(ruleTarget.getRuleId()); |
187 | |
} |
188 | |
} |
189 | |
|
190 | 0 | List<String> workgroupIds = new ArrayList<String>(); |
191 | 0 | if (document.getWorkgroupTargets() != null) { |
192 | 0 | for (WorkgroupTarget workgroupTarget : document.getWorkgroupTargets()) { |
193 | 0 | workgroupIds.add(workgroupTarget.getWorkgroupId()); |
194 | |
} |
195 | |
} |
196 | |
|
197 | 0 | RuleService ruleService = KEWServiceLocator.getRuleService(); |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
try { |
203 | 0 | if (RemoveReplaceDocument.REPLACE_OPERATION.equals(document.getOperation())) { |
204 | 0 | if (StringUtils.isEmpty(document.getReplacementUserWorkflowId())) { |
205 | 0 | throw new WorkflowRuntimeException("Replacement operation was indicated but RemoveReplaceDocument does not have a replacement user id."); |
206 | |
} |
207 | 0 | ruleService.replaceRuleInvolvement(new WorkflowUserId(document.getUserWorkflowId()), new WorkflowUserId(document.getReplacementUserWorkflowId()), ruleIds, documentId); |
208 | |
|
209 | 0 | } else if (RemoveReplaceDocument.REMOVE_OPERATION.equals(document.getOperation())) { |
210 | 0 | ruleService.removeRuleInvolvement(new WorkflowUserId(document.getUserWorkflowId()), ruleIds, documentId); |
211 | |
|
212 | |
} else { |
213 | 0 | throw new WorkflowRuntimeException("Invalid operation was specified on the RemoveReplaceDocument: " + document.getOperation()); |
214 | |
} |
215 | 0 | } catch (WorkflowException e) { |
216 | 0 | throw new WorkflowRuntimeException(e); |
217 | 0 | } |
218 | 0 | } |
219 | |
|
220 | |
public void setRemoveReplaceDocumentDAO(RemoveReplaceDocumentDAO dao) { |
221 | 0 | this.dao = dao; |
222 | 0 | } |
223 | |
|
224 | |
} |