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