1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.maintenance;
17
18 import org.apache.commons.lang.ObjectUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.CoreApiServiceLocator;
21 import org.kuali.rice.core.api.util.RiceKeyConstants;
22 import org.kuali.rice.kew.api.WorkflowDocument;
23 import org.kuali.rice.krad.exception.KualiExceptionIncident;
24 import org.kuali.rice.krad.exception.ValidationException;
25 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26 import org.kuali.rice.krad.util.GlobalVariables;
27 import org.kuali.rice.krad.util.KRADConstants;
28 import org.kuali.rice.krad.util.LegacyUtils;
29 import org.kuali.rice.krad.util.UrlFactory;
30
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Properties;
34 import java.util.concurrent.Executors;
35
36
37
38
39
40
41 public class MaintenanceUtils {
42 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MaintenanceUtils.class);
43
44
45
46
47
48
49
50
51
52 public static void checkForLockingDocument(MaintenanceDocument document, final boolean throwExceptionIfLocked) {
53 LOG.info("starting checkForLockingDocument (by MaintenanceDocument)");
54
55
56
57 final String blockingDocId = document.getNewMaintainableObject().getLockingDocumentId();
58
59 Maintainable maintainable = (Maintainable) ObjectUtils.defaultIfNull(document.getOldMaintainableObject(), document.getNewMaintainableObject());
60 checkDocumentBlockingDocumentId(blockingDocId, throwExceptionIfLocked);
61 }
62
63 public static void checkDocumentBlockingDocumentId(String blockingDocId, boolean throwExceptionIfLocked) {
64
65 if (StringUtils.isBlank(blockingDocId)) {
66 return;
67 }
68
69 if (MaintenanceUtils.LOG.isInfoEnabled()) {
70 MaintenanceUtils.LOG.info("Locking document found: docId = " + blockingDocId + ".");
71 }
72
73
74 WorkflowDocument lockedDocument = null;
75 try {
76
77
78
79 if (KRADServiceLocatorWeb.getWorkflowDocumentService().workflowDocumentExists(blockingDocId)) {
80 lockedDocument = KRADServiceLocatorWeb.getWorkflowDocumentService()
81 .loadWorkflowDocument(blockingDocId, GlobalVariables.getUserSession().getPerson());
82 }
83 } catch (Exception ex) {
84
85 MaintenanceUtils.LOG.error("Unable to retrieve locking document specified in the maintenance lock table: " +
86 blockingDocId, ex);
87
88 cleanOrphanLocks(blockingDocId, ex);
89 return;
90 }
91 if (lockedDocument == null) {
92 MaintenanceUtils.LOG.warn("Locking document header for " + blockingDocId + "came back null.");
93 cleanOrphanLocks(blockingDocId, null);
94 }
95
96
97 if (lockCanBeIgnored(lockedDocument)) {
98 return;
99 }
100
101
102 Properties parameters = new Properties();
103 parameters.put(KRADConstants.PARAMETER_DOC_ID, blockingDocId);
104 parameters.put(KRADConstants.PARAMETER_COMMAND, KRADConstants.METHOD_DISPLAY_DOC_SEARCH_VIEW);
105 String blockingUrl = UrlFactory.parameterizeUrl(
106 CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
107 KRADConstants.WORKFLOW_URL_KEY) +
108 "/" + KRADConstants.DOC_HANDLER_ACTION, parameters);
109 if (MaintenanceUtils.LOG.isDebugEnabled()) {
110 MaintenanceUtils.LOG.debug("blockingUrl = '" + blockingUrl + "'");
111 MaintenanceUtils.LOG.debug("Maintenance record: " + lockedDocument.getApplicationDocumentId() + "is locked.");
112 }
113 String[] errorParameters = {blockingUrl, blockingDocId};
114
115
116 if (throwExceptionIfLocked) {
117
118 GlobalVariables.getMessageMap()
119 .putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_MAINTENANCE_LOCKED, errorParameters);
120 throw new ValidationException("Maintenance Record is locked by another document.");
121 } else {
122
123 GlobalVariables.getMessageMap()
124 .putWarning(KRADConstants.GLOBAL_MESSAGES, RiceKeyConstants.WARNING_MAINTENANCE_LOCKED,
125 errorParameters);
126 }
127 }
128
129
130
131
132
133
134
135
136
137 private static boolean lockCanBeIgnored(WorkflowDocument lockedDocument) {
138
139 if (lockedDocument == null) {
140 return true;
141 }
142
143
144 String userId = GlobalVariables.getUserSession().getPrincipalId().trim();
145 if (StringUtils.isBlank(userId)) {
146 return false;
147 }
148
149
150 if (!userId.equalsIgnoreCase(lockedDocument.getInitiatorPrincipalId().trim())) {
151 return false;
152 }
153
154
155 return lockedDocument.isInitiated();
156 }
157
158 protected static void cleanOrphanLocks(String lockingDocumentNumber, Exception workflowException) {
159
160
161 try {
162
163 KRADServiceLocatorWeb.getMaintenanceDocumentService().deleteLocks(lockingDocumentNumber);
164
165 Map<String, String> parameters = new HashMap<String, String>(1);
166 parameters.put(KRADConstants.PARAMETER_DOC_ID, lockingDocumentNumber);
167 KualiExceptionIncident kei = KRADServiceLocatorWeb.getKualiExceptionIncidentService()
168 .getExceptionIncident(workflowException, parameters);
169 KRADServiceLocatorWeb.getKualiExceptionIncidentService().report(kei);
170 } catch (Exception ex) {
171 MaintenanceUtils.LOG.error("Unable to delete and notify upon locking document retrieval failure.", ex);
172 }
173 }
174
175
176
177
178
179
180
181
182
183
184
185 public static boolean isMaintenanceDocumentCreatingNewRecord(String maintenanceAction) {
186 if (KRADConstants.MAINTENANCE_EDIT_ACTION.equalsIgnoreCase(maintenanceAction)) {
187 return false;
188 } else if (KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION.equalsIgnoreCase(maintenanceAction)) {
189 return false;
190 } else if (KRADConstants.MAINTENANCE_DELETE_ACTION.equalsIgnoreCase(maintenanceAction)) {
191 return false;
192 } else if (KRADConstants.MAINTENANCE_NEW_ACTION.equalsIgnoreCase(maintenanceAction)) {
193 return true;
194 } else if (KRADConstants.MAINTENANCE_COPY_ACTION.equalsIgnoreCase(maintenanceAction)) {
195 return true;
196 } else {
197 return true;
198 }
199 }
200 }