1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.doctype.service.impl; |
18 | |
|
19 | |
import java.io.InputStream; |
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Collection; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import org.apache.commons.collections.CollectionUtils; |
26 | |
import org.jdom.Element; |
27 | |
import org.kuali.rice.core.api.impex.ExportDataSet; |
28 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
29 | |
import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO; |
30 | |
import org.kuali.rice.kew.doctype.service.DocumentTypePermissionService; |
31 | |
import org.kuali.rice.kew.doctype.service.DocumentTypeService; |
32 | |
import org.kuali.rice.kew.exception.WorkflowServiceErrorException; |
33 | |
import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl; |
34 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
35 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
36 | |
import org.kuali.rice.kew.xml.DocumentTypeXmlParser; |
37 | |
import org.kuali.rice.kew.xml.export.DocumentTypeXmlExporter; |
38 | |
import org.kuali.rice.krad.util.ObjectUtils; |
39 | |
import org.kuali.rice.ksb.api.KsbApiServiceLocator; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public class DocumentTypeServiceImpl implements DocumentTypeService { |
48 | |
|
49 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentTypeServiceImpl.class); |
50 | |
protected static final String XML_FILE_PARSE_ERROR = "general.error.parsexml"; |
51 | |
|
52 | |
public static final String DOCUMENT_TYPE_ID_CACHE_GROUP = "DocumentTypeId"; |
53 | |
public static final String DOCUMENT_TYPE_NAME_CACHE_GROUP = "DocumentTypeName"; |
54 | |
|
55 | |
public static final String DOCUMENT_TYPE_ID_CACHE_PREFIX = DOCUMENT_TYPE_ID_CACHE_GROUP + ":"; |
56 | |
public static final String DOCUMENT_TYPE_NAME_CACHE_PREFIX = DOCUMENT_TYPE_NAME_CACHE_GROUP + ":"; |
57 | |
public static final String CURRENT_ROOTS_IN_CACHE_KEY = "DocumentType:CurrentRootsInCache"; |
58 | |
|
59 | |
|
60 | |
private DocumentTypeDAO documentTypeDAO; |
61 | |
|
62 | |
public Collection<DocumentType> find(DocumentType documentType, String docTypeParentName, boolean climbHierarchy) { |
63 | 0 | DocumentType docTypeParent = this.findByName(docTypeParentName); |
64 | 0 | Collection<DocumentType> documentTypes = getDocumentTypeDAO().find(documentType, docTypeParent, climbHierarchy); |
65 | |
|
66 | 0 | for (Object documentType1 : documentTypes) |
67 | |
{ |
68 | 0 | insertIntoCache((DocumentType) documentType1); |
69 | |
} |
70 | 0 | return documentTypes; |
71 | |
} |
72 | |
|
73 | |
public DocumentType findById(String documentTypeId) { |
74 | 0 | if (documentTypeId == null) { |
75 | 0 | return null; |
76 | |
} |
77 | 0 | DocumentType documentType = fetchFromCacheById(documentTypeId); |
78 | 0 | if (documentType == null) { |
79 | 0 | documentType = getDocumentTypeDAO().findById(documentTypeId); |
80 | 0 | insertIntoCache(documentType); |
81 | |
} |
82 | 0 | return documentType; |
83 | |
} |
84 | |
|
85 | |
public DocumentType findByDocumentId(String documentId) { |
86 | 0 | if (documentId == null) { |
87 | 0 | return null; |
88 | |
} |
89 | 0 | String documentTypeId = getDocumentTypeDAO().findDocumentTypeIdByDocumentId(documentId); |
90 | 0 | return findById(documentTypeId); |
91 | |
} |
92 | |
|
93 | |
public DocumentType findByName(String name) { |
94 | 0 | return this.findByName(name, true, true); |
95 | |
} |
96 | |
|
97 | |
public DocumentType findByNameCaseInsensitive(String name) { |
98 | 0 | return this.findByName(name, false,true); |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
protected DocumentType findByName(String name, boolean caseSensitive) { |
111 | 0 | if (name == null) { |
112 | 0 | return null; |
113 | |
} |
114 | 0 | DocumentType documentType = fetchFromCacheByName(name); |
115 | 0 | if (documentType == null) { |
116 | 0 | documentType = getDocumentTypeDAO().findByName(name, caseSensitive); |
117 | 0 | insertIntoCache(documentType); |
118 | |
} |
119 | 0 | return documentType; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
protected DocumentType findByName(String name, boolean caseSensitive, boolean checkCache) { |
132 | 0 | if (name == null) { |
133 | 0 | return null; |
134 | |
} |
135 | |
|
136 | 0 | DocumentType documentType = null; |
137 | 0 | if(checkCache){ |
138 | 0 | documentType = fetchFromCacheByName(name); |
139 | |
} |
140 | 0 | if (documentType == null) { |
141 | 0 | documentType = getDocumentTypeDAO().findByName(name, caseSensitive); |
142 | 0 | insertIntoCache(documentType); |
143 | |
} |
144 | 0 | return documentType; |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
protected DocumentType fetchFromCacheByName(String documentTypeName) { |
152 | 0 | return (DocumentType) KsbApiServiceLocator.getCacheAdministrator().getFromCache(getNameCacheKey(documentTypeName)); |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
protected DocumentType fetchFromCacheById(String documentTypeId) { |
160 | 0 | return (DocumentType) KsbApiServiceLocator.getCacheAdministrator().getFromCache(getIdCacheKey(documentTypeId)); |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
protected String getIdCacheKey(String documentTypeId) { |
167 | 0 | return DOCUMENT_TYPE_ID_CACHE_PREFIX + documentTypeId; |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
protected String getNameCacheKey(String documentTypeName) { |
174 | 0 | return DOCUMENT_TYPE_NAME_CACHE_PREFIX + documentTypeName; |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
protected void insertIntoCache(DocumentType documentType) { |
187 | 0 | if (documentType == null) { |
188 | 0 | return; |
189 | |
} |
190 | |
|
191 | 0 | if (documentType.getCurrentInd().booleanValue()) { |
192 | 0 | KsbApiServiceLocator.getCacheAdministrator().putInCache(getNameCacheKey(documentType.getName()), documentType, DOCUMENT_TYPE_NAME_CACHE_GROUP); |
193 | |
} |
194 | |
|
195 | 0 | KsbApiServiceLocator.getCacheAdministrator().putInCache(getIdCacheKey(documentType.getDocumentTypeId()), documentType, DOCUMENT_TYPE_ID_CACHE_GROUP); |
196 | 0 | } |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
public void flushCache() { |
202 | |
|
203 | |
|
204 | |
|
205 | 0 | LOG.info("clearing DocumentType cache because of local update"); |
206 | 0 | KsbApiServiceLocator.getCacheAdministrator().flushGroup(DOCUMENT_TYPE_ID_CACHE_GROUP); |
207 | 0 | KsbApiServiceLocator.getCacheAdministrator().flushGroup(DOCUMENT_TYPE_NAME_CACHE_GROUP); |
208 | 0 | KsbApiServiceLocator.getCacheAdministrator().flushGroup(DocumentTypePermissionService.DOC_TYPE_PERM_CACHE_GROUP); |
209 | 0 | KsbApiServiceLocator.getCacheAdministrator().flushEntry(CURRENT_ROOTS_IN_CACHE_KEY); |
210 | 0 | } |
211 | |
|
212 | |
public void clearCacheForAttributeUpdate(RuleAttribute ruleAttribute) { |
213 | 0 | if (ruleAttribute.getRuleAttributeId() != null) { |
214 | 0 | List documentTypeAttributes = this.documentTypeDAO.findDocumentTypeAttributes(ruleAttribute); |
215 | 0 | if (documentTypeAttributes.size() != 0) { |
216 | 0 | flushCache(); |
217 | |
} |
218 | |
} |
219 | 0 | } |
220 | |
|
221 | |
public void versionAndSave(DocumentType documentType) { |
222 | |
try { |
223 | |
|
224 | |
|
225 | 0 | if (documentType.getDocumentTypeId() != null && documentType.getVersionNumber() != null) { |
226 | 0 | throw new RuntimeException("DocumentType configured for update and not versioning which we support"); |
227 | |
} |
228 | |
|
229 | |
|
230 | 0 | DocumentType oldDocumentType = findByName(documentType.getName(), true, false); |
231 | |
|
232 | |
|
233 | 0 | String existingDocTypeId = null; |
234 | 0 | if (oldDocumentType != null) { |
235 | 0 | existingDocTypeId = oldDocumentType.getDocumentTypeId(); |
236 | |
|
237 | 0 | Integer maxVersionNumber = documentTypeDAO.getMaxVersionNumber(documentType.getName()); |
238 | 0 | documentType.setVersion((maxVersionNumber != null) ? new Integer(maxVersionNumber.intValue() + 1) : new Integer(0)); |
239 | 0 | oldDocumentType.setCurrentInd(Boolean.FALSE); |
240 | 0 | if ( LOG.isInfoEnabled() ) { |
241 | 0 | LOG.info("Saving old document type Id " + oldDocumentType.getDocumentTypeId() + " name '" + oldDocumentType.getName() + "' (current = " + oldDocumentType.getCurrentInd() + ")"); |
242 | |
} |
243 | 0 | save(oldDocumentType, false); |
244 | |
} |
245 | |
|
246 | 0 | if (!CollectionUtils.isEmpty(documentTypeDAO.findAllCurrentByName(documentType.getName()))) { |
247 | 0 | String errorMsg = "Found invalid 'current' document with name '" + documentType.getName() + "'. None should exist."; |
248 | 0 | LOG.error(errorMsg); |
249 | 0 | throw new RuntimeException(errorMsg); |
250 | |
} |
251 | |
|
252 | 0 | documentType.setPreviousVersionId(existingDocTypeId); |
253 | 0 | documentType.setCurrentInd(Boolean.TRUE); |
254 | 0 | save(documentType, false); |
255 | 0 | if ( LOG.isInfoEnabled() ) { |
256 | 0 | LOG.info("Saved current document type Id " + documentType.getDocumentTypeId() + " name '" + documentType.getName() + "' (current = " + documentType.getCurrentInd() + ")"); |
257 | |
} |
258 | |
|
259 | 0 | if (ObjectUtils.isNotNull(existingDocTypeId)) { |
260 | |
|
261 | 0 | for (Iterator iterator = getChildDocumentTypes(existingDocTypeId).iterator(); iterator.hasNext();) { |
262 | |
|
263 | 0 | DocumentType child = (DocumentType) iterator.next(); |
264 | 0 | child.setDocTypeParentId(documentType.getDocumentTypeId()); |
265 | 0 | save(child, false); |
266 | 0 | if ( LOG.isInfoEnabled() ) { |
267 | 0 | LOG.info("Saved child document type Id " + child.getDocumentTypeId() + " name '" + child.getName() + "' (parent = " + child.getDocTypeParentId() + ", current = " + child.getCurrentInd() + ")"); |
268 | |
} |
269 | 0 | } |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | 0 | if (documentType.getDocTypeParentId() != null) { |
277 | 0 | DocumentType parent = getDocumentTypeDAO().findById(documentType.getDocTypeParentId()); |
278 | 0 | save(parent, false); |
279 | 0 | if ( LOG.isInfoEnabled() ) { |
280 | 0 | LOG.info("Saved parent document type Id " + parent.getDocumentTypeId() + " name '" + parent.getName() + "' (current = " + parent.getCurrentInd() + ")"); |
281 | |
} |
282 | |
} |
283 | |
|
284 | |
|
285 | 0 | flushCache(); |
286 | 0 | KEWServiceLocator.getRuleService().notifyCacheOfDocumentTypeChange(documentType); |
287 | |
} finally { |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | 0 | flushCache(); |
298 | 0 | } |
299 | 0 | } |
300 | |
|
301 | |
public void save(DocumentType documentType, boolean flushCache) { |
302 | 0 | getDocumentTypeDAO().save(documentType); |
303 | 0 | if (flushCache) { |
304 | |
|
305 | 0 | flushCache(); |
306 | 0 | KEWServiceLocator.getRuleService().notifyCacheOfDocumentTypeChange(documentType); |
307 | 0 | flushCache(); |
308 | |
} |
309 | 0 | } |
310 | |
|
311 | |
public void save(DocumentType documentType) { |
312 | 0 | save(documentType, true); |
313 | 0 | } |
314 | |
|
315 | |
public DocumentTypeDAO getDocumentTypeDAO() { |
316 | 0 | return documentTypeDAO; |
317 | |
} |
318 | |
|
319 | |
public void setDocumentTypeDAO(DocumentTypeDAO documentTypeDAO) { |
320 | 0 | this.documentTypeDAO = documentTypeDAO; |
321 | 0 | } |
322 | |
|
323 | |
public synchronized List findAllCurrentRootDocuments() { |
324 | 0 | List currentRootsInCache = (List) KsbApiServiceLocator.getCacheAdministrator().getFromCache(CURRENT_ROOTS_IN_CACHE_KEY); |
325 | |
|
326 | 0 | if (currentRootsInCache == null) { |
327 | 0 | currentRootsInCache = getDocumentTypeDAO().findAllCurrentRootDocuments(); |
328 | 0 | KsbApiServiceLocator.getCacheAdministrator().putInCache(CURRENT_ROOTS_IN_CACHE_KEY, currentRootsInCache); |
329 | |
} |
330 | 0 | return currentRootsInCache; |
331 | |
} |
332 | |
|
333 | |
public List findAllCurrent() { |
334 | 0 | return getDocumentTypeDAO().findAllCurrent(); |
335 | |
} |
336 | |
|
337 | |
public List<DocumentType> findPreviousInstances(String documentTypeName) { |
338 | 0 | return getDocumentTypeDAO().findPreviousInstances(documentTypeName); |
339 | |
} |
340 | |
|
341 | |
public DocumentType findRootDocumentType(DocumentType docType) { |
342 | 0 | if (docType.getParentDocType() != null) { |
343 | 0 | return findRootDocumentType(docType.getParentDocType()); |
344 | |
} else { |
345 | 0 | return docType; |
346 | |
} |
347 | |
} |
348 | |
|
349 | |
public void loadXml(InputStream inputStream, String principalId) { |
350 | 0 | DocumentTypeXmlParser parser = new DocumentTypeXmlParser(); |
351 | |
try { |
352 | 0 | parser.parseDocumentTypes(inputStream); |
353 | 0 | } catch (Exception e) { |
354 | 0 | WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error parsing documentType XML file", new WorkflowServiceErrorImpl("Error parsing documentType XML file", XML_FILE_PARSE_ERROR)); |
355 | 0 | wsee.initCause(e); |
356 | 0 | throw wsee; |
357 | 0 | } |
358 | 0 | } |
359 | |
|
360 | |
public Element export(ExportDataSet dataSet) { |
361 | 0 | DocumentTypeXmlExporter exporter = new DocumentTypeXmlExporter(); |
362 | 0 | return exporter.export(dataSet); |
363 | |
} |
364 | |
|
365 | |
@Override |
366 | |
public boolean supportPrettyPrint() { |
367 | 0 | return true; |
368 | |
} |
369 | |
|
370 | |
public List getChildDocumentTypes(String documentTypeId) { |
371 | 0 | List childDocumentTypes = new ArrayList(); |
372 | 0 | List childIds = getDocumentTypeDAO().getChildDocumentTypeIds(documentTypeId); |
373 | 0 | for (Iterator iter = childIds.iterator(); iter.hasNext();) { |
374 | 0 | String childDocumentTypeId = (String) iter.next(); |
375 | 0 | childDocumentTypes.add(findById(childDocumentTypeId)); |
376 | 0 | } |
377 | 0 | return childDocumentTypes; |
378 | |
} |
379 | |
|
380 | |
} |