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