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.dto.DTOConverter; |
33 | |
import org.kuali.rice.kew.dto.DocumentTypeDTO; |
34 | |
import org.kuali.rice.kew.exception.WorkflowServiceErrorException; |
35 | |
import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl; |
36 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
37 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
38 | |
import org.kuali.rice.kew.util.KEWConstants; |
39 | |
import org.kuali.rice.kew.xml.DocumentTypeXmlParser; |
40 | |
import org.kuali.rice.kew.xml.export.DocumentTypeXmlExporter; |
41 | |
import org.kuali.rice.kns.util.ObjectUtils; |
42 | |
import org.kuali.rice.ksb.api.KsbApiServiceLocator; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | public class DocumentTypeServiceImpl implements DocumentTypeService { |
51 | |
|
52 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentTypeServiceImpl.class); |
53 | |
protected static final String XML_FILE_PARSE_ERROR = "general.error.parsexml"; |
54 | |
|
55 | |
public static final String DOCUMENT_TYPE_ID_CACHE_GROUP = "DocumentTypeId"; |
56 | |
public static final String DOCUMENT_TYPE_NAME_CACHE_GROUP = "DocumentTypeName"; |
57 | |
public static final String DOCUMENT_TYPE_DTO_ID_CACHE_GROUP = "DocumentTypeDTOId"; |
58 | |
public static final String DOCUMENT_TYPE_DTO_NAME_CACHE_GROUP = "DocumentTypeDTOName"; |
59 | |
|
60 | |
public static final String DOCUMENT_TYPE_ID_CACHE_PREFIX = DOCUMENT_TYPE_ID_CACHE_GROUP + ":"; |
61 | |
public static final String DOCUMENT_TYPE_NAME_CACHE_PREFIX = DOCUMENT_TYPE_NAME_CACHE_GROUP + ":"; |
62 | |
public static final String DOCUMENT_TYPE_DTO_ID_CACHE_PREFIX = DOCUMENT_TYPE_DTO_ID_CACHE_GROUP + ":"; |
63 | |
public static final String DOCUMENT_TYPE_DTO_NAME_CACHE_PREFIX = DOCUMENT_TYPE_DTO_NAME_CACHE_GROUP + ":"; |
64 | |
public static final String CURRENT_ROOTS_IN_CACHE_KEY = "DocumentType:CurrentRootsInCache"; |
65 | |
|
66 | |
|
67 | |
private DocumentTypeDAO documentTypeDAO; |
68 | |
|
69 | |
public Collection<DocumentType> find(DocumentType documentType, String docTypeParentName, boolean climbHierarchy) { |
70 | 0 | DocumentType docTypeParent = this.findByName(docTypeParentName); |
71 | 0 | Collection<DocumentType> documentTypes = getDocumentTypeDAO().find(documentType, docTypeParent, climbHierarchy); |
72 | |
|
73 | 0 | for (Object documentType1 : documentTypes) |
74 | |
{ |
75 | 0 | insertIntoCache((DocumentType) documentType1); |
76 | |
} |
77 | 0 | return documentTypes; |
78 | |
} |
79 | |
|
80 | |
public DocumentType findById(Long documentTypeId) { |
81 | 0 | if (documentTypeId == null) { |
82 | 0 | return null; |
83 | |
} |
84 | 0 | DocumentType documentType = fetchFromCacheById(documentTypeId); |
85 | 0 | if (documentType == null) { |
86 | 0 | documentType = getDocumentTypeDAO().findById(documentTypeId); |
87 | 0 | insertIntoCache(documentType); |
88 | |
} |
89 | 0 | return documentType; |
90 | |
} |
91 | |
|
92 | |
public DocumentType findByDocumentId(String documentId) { |
93 | 0 | if (documentId == null) { |
94 | 0 | return null; |
95 | |
} |
96 | 0 | Long documentTypeId = getDocumentTypeDAO().findDocumentTypeIdByDocumentId(documentId); |
97 | 0 | return findById(documentTypeId); |
98 | |
} |
99 | |
|
100 | |
public DocumentType findByName(String name) { |
101 | 0 | return this.findByName(name, true, true); |
102 | |
} |
103 | |
|
104 | |
public DocumentType findByNameCaseInsensitive(String name) { |
105 | 0 | return this.findByName(name, false,true); |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
protected DocumentType findByName(String name, boolean caseSensitive) { |
118 | 0 | if (name == null) { |
119 | 0 | return null; |
120 | |
} |
121 | 0 | DocumentType documentType = fetchFromCacheByName(name); |
122 | 0 | if (documentType == null) { |
123 | 0 | documentType = getDocumentTypeDAO().findByName(name, caseSensitive); |
124 | 0 | insertIntoCache(documentType); |
125 | |
} |
126 | 0 | return documentType; |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
protected DocumentType findByName(String name, boolean caseSensitive, boolean checkCache) { |
139 | 0 | if (name == null) { |
140 | 0 | return null; |
141 | |
} |
142 | |
|
143 | 0 | DocumentType documentType = null; |
144 | 0 | if(checkCache){ |
145 | 0 | documentType = fetchFromCacheByName(name); |
146 | |
} |
147 | 0 | if (documentType == null) { |
148 | 0 | documentType = getDocumentTypeDAO().findByName(name, caseSensitive); |
149 | 0 | insertIntoCache(documentType); |
150 | |
} |
151 | 0 | return documentType; |
152 | |
} |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
protected DocumentType fetchFromCacheByName(String documentTypeName) { |
159 | 0 | return (DocumentType) KsbApiServiceLocator.getCacheAdministrator().getFromCache(getNameCacheKey(documentTypeName)); |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
protected DocumentType fetchFromCacheById(Long documentTypeId) { |
167 | 0 | return (DocumentType) KsbApiServiceLocator.getCacheAdministrator().getFromCache(getIdCacheKey(documentTypeId)); |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
protected String getIdCacheKey(Long documentTypeId) { |
174 | 0 | return DOCUMENT_TYPE_ID_CACHE_PREFIX + documentTypeId.toString(); |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
protected String getNameCacheKey(String documentTypeName) { |
181 | 0 | return DOCUMENT_TYPE_NAME_CACHE_PREFIX + documentTypeName; |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
protected void insertIntoCache(DocumentType documentType) { |
194 | 0 | if (documentType == null) { |
195 | 0 | return; |
196 | |
} |
197 | |
|
198 | 0 | if (documentType.getCurrentInd().booleanValue()) { |
199 | 0 | KsbApiServiceLocator.getCacheAdministrator().putInCache(getNameCacheKey(documentType.getName()), documentType, DOCUMENT_TYPE_NAME_CACHE_GROUP); |
200 | |
} |
201 | |
|
202 | 0 | KsbApiServiceLocator.getCacheAdministrator().putInCache(getIdCacheKey(documentType.getDocumentTypeId()), documentType, DOCUMENT_TYPE_ID_CACHE_GROUP); |
203 | 0 | } |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
protected DocumentTypeDTO fetchDTOFromCacheByName(String documentTypeName) { |
210 | 0 | return (DocumentTypeDTO) KsbApiServiceLocator.getCacheAdministrator().getFromCache(getDTONameCacheKey(documentTypeName)); |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
protected DocumentTypeDTO fetchDTOFromCacheById(Long documentTypeId) { |
218 | 0 | return (DocumentTypeDTO) KsbApiServiceLocator.getCacheAdministrator().getFromCache(getDTOIdCacheKey(documentTypeId)); |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
protected String getDTOIdCacheKey(Long documentTypeId) { |
225 | 0 | return DOCUMENT_TYPE_DTO_ID_CACHE_PREFIX + documentTypeId.toString(); |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
protected String getDTONameCacheKey(String documentTypeName) { |
232 | 0 | return DOCUMENT_TYPE_DTO_NAME_CACHE_PREFIX + documentTypeName; |
233 | |
} |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
protected void insertDTOIntoCache(DocumentTypeDTO documentType) { |
245 | 0 | if (documentType == null) { |
246 | 0 | return; |
247 | |
} |
248 | |
|
249 | 0 | if ( documentType.getDocTypeCurrentInd().equals(KEWConstants.ACTIVE_CD) ) { |
250 | 0 | KsbApiServiceLocator.getCacheAdministrator().putInCache(getDTONameCacheKey(documentType.getName()), documentType, DOCUMENT_TYPE_DTO_NAME_CACHE_GROUP); |
251 | |
} |
252 | |
|
253 | 0 | KsbApiServiceLocator.getCacheAdministrator().putInCache(getDTOIdCacheKey(documentType.getDocTypeId()), documentType, DOCUMENT_TYPE_DTO_ID_CACHE_GROUP); |
254 | 0 | } |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
public void flushCache() { |
260 | |
|
261 | |
|
262 | |
|
263 | 0 | LOG.info("clearing DocumentType cache because of local update"); |
264 | 0 | KsbApiServiceLocator.getCacheAdministrator().flushGroup(DOCUMENT_TYPE_ID_CACHE_GROUP); |
265 | 0 | KsbApiServiceLocator.getCacheAdministrator().flushGroup(DOCUMENT_TYPE_NAME_CACHE_GROUP); |
266 | 0 | KsbApiServiceLocator.getCacheAdministrator().flushGroup(DOCUMENT_TYPE_DTO_ID_CACHE_GROUP); |
267 | 0 | KsbApiServiceLocator.getCacheAdministrator().flushGroup(DOCUMENT_TYPE_DTO_NAME_CACHE_GROUP); |
268 | 0 | KsbApiServiceLocator.getCacheAdministrator().flushGroup(DocumentTypePermissionService.DOC_TYPE_PERM_CACHE_GROUP); |
269 | 0 | KsbApiServiceLocator.getCacheAdministrator().flushEntry(CURRENT_ROOTS_IN_CACHE_KEY); |
270 | 0 | } |
271 | |
|
272 | |
public void clearCacheForAttributeUpdate(RuleAttribute ruleAttribute) { |
273 | 0 | if (ruleAttribute.getRuleAttributeId() != null) { |
274 | 0 | List documentTypeAttributes = this.documentTypeDAO.findDocumentTypeAttributes(ruleAttribute); |
275 | 0 | if (documentTypeAttributes.size() != 0) { |
276 | 0 | flushCache(); |
277 | |
} |
278 | |
} |
279 | 0 | } |
280 | |
|
281 | |
public void versionAndSave(DocumentType documentType) { |
282 | |
try { |
283 | |
|
284 | |
|
285 | 0 | if (documentType.getDocumentTypeId() != null && documentType.getVersionNumber() != null) { |
286 | 0 | throw new RuntimeException("DocumentType configured for update and not versioning which we support"); |
287 | |
} |
288 | |
|
289 | |
|
290 | 0 | DocumentType oldDocumentType = findByName(documentType.getName(), true, false); |
291 | |
|
292 | |
|
293 | 0 | Long existingDocTypeId = null; |
294 | 0 | if (oldDocumentType != null) { |
295 | 0 | existingDocTypeId = oldDocumentType.getDocumentTypeId(); |
296 | 0 | if (existingDocTypeId.longValue() > 0) { |
297 | |
|
298 | 0 | Integer maxVersionNumber = documentTypeDAO.getMaxVersionNumber(documentType.getName()); |
299 | 0 | documentType.setVersion((maxVersionNumber != null) ? new Integer(maxVersionNumber.intValue() + 1) : new Integer(0)); |
300 | 0 | oldDocumentType.setCurrentInd(Boolean.FALSE); |
301 | 0 | if ( LOG.isInfoEnabled() ) { |
302 | 0 | LOG.info("Saving old document type Id " + oldDocumentType.getDocumentTypeId() + " name '" + oldDocumentType.getName() + "' (current = " + oldDocumentType.getCurrentInd() + ")"); |
303 | |
} |
304 | 0 | save(oldDocumentType, false); |
305 | |
} |
306 | |
} |
307 | |
|
308 | 0 | if (!CollectionUtils.isEmpty(documentTypeDAO.findAllCurrentByName(documentType.getName()))) { |
309 | 0 | String errorMsg = "Found invalid 'current' document with name '" + documentType.getName() + "'. None should exist."; |
310 | 0 | LOG.error(errorMsg); |
311 | 0 | throw new RuntimeException(errorMsg); |
312 | |
} |
313 | |
|
314 | 0 | documentType.setPreviousVersionId(existingDocTypeId); |
315 | 0 | documentType.setCurrentInd(Boolean.TRUE); |
316 | 0 | save(documentType, false); |
317 | 0 | if ( LOG.isInfoEnabled() ) { |
318 | 0 | LOG.info("Saved current document type Id " + documentType.getDocumentTypeId() + " name '" + documentType.getName() + "' (current = " + documentType.getCurrentInd() + ")"); |
319 | |
} |
320 | |
|
321 | 0 | if (ObjectUtils.isNotNull(existingDocTypeId)) { |
322 | |
|
323 | 0 | for (Iterator iterator = getChildDocumentTypes(existingDocTypeId).iterator(); iterator.hasNext();) { |
324 | |
|
325 | 0 | DocumentType child = (DocumentType) iterator.next(); |
326 | 0 | child.setDocTypeParentId(documentType.getDocumentTypeId()); |
327 | 0 | save(child, false); |
328 | 0 | if ( LOG.isInfoEnabled() ) { |
329 | 0 | LOG.info("Saved child document type Id " + child.getDocumentTypeId() + " name '" + child.getName() + "' (parent = " + child.getDocTypeParentId() + ", current = " + child.getCurrentInd() + ")"); |
330 | |
} |
331 | 0 | } |
332 | |
} |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | 0 | if (documentType.getDocTypeParentId() != null) { |
339 | 0 | DocumentType parent = getDocumentTypeDAO().findById(documentType.getDocTypeParentId()); |
340 | 0 | save(parent, false); |
341 | 0 | if ( LOG.isInfoEnabled() ) { |
342 | 0 | LOG.info("Saved parent document type Id " + parent.getDocumentTypeId() + " name '" + parent.getName() + "' (current = " + parent.getCurrentInd() + ")"); |
343 | |
} |
344 | |
} |
345 | |
|
346 | |
|
347 | 0 | flushCache(); |
348 | 0 | KEWServiceLocator.getRuleService().notifyCacheOfDocumentTypeChange(documentType); |
349 | |
} finally { |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | 0 | flushCache(); |
360 | 0 | } |
361 | 0 | } |
362 | |
|
363 | |
public void save(DocumentType documentType, boolean flushCache) { |
364 | 0 | getDocumentTypeDAO().save(documentType); |
365 | 0 | if (flushCache) { |
366 | |
|
367 | 0 | flushCache(); |
368 | 0 | KEWServiceLocator.getRuleService().notifyCacheOfDocumentTypeChange(documentType); |
369 | 0 | flushCache(); |
370 | |
} |
371 | 0 | } |
372 | |
|
373 | |
public void save(DocumentType documentType) { |
374 | 0 | save(documentType, true); |
375 | 0 | } |
376 | |
|
377 | |
public DocumentTypeDAO getDocumentTypeDAO() { |
378 | 0 | return documentTypeDAO; |
379 | |
} |
380 | |
|
381 | |
public void setDocumentTypeDAO(DocumentTypeDAO documentTypeDAO) { |
382 | 0 | this.documentTypeDAO = documentTypeDAO; |
383 | 0 | } |
384 | |
|
385 | |
public DocumentTypeDTO getDocumentTypeVO(Long documentTypeId) { |
386 | 0 | DocumentTypeDTO dto = fetchDTOFromCacheById(documentTypeId); |
387 | 0 | if ( dto == null ) { |
388 | 0 | DocumentType docType = findById(documentTypeId); |
389 | 0 | if ( docType == null ) { |
390 | 0 | return null; |
391 | |
} |
392 | 0 | dto = DTOConverter.convertDocumentType(docType); |
393 | 0 | insertDTOIntoCache(dto); |
394 | |
} |
395 | |
|
396 | 0 | return dto; |
397 | |
} |
398 | |
|
399 | |
public DocumentTypeDTO getDocumentTypeVO(String documentTypeName) { |
400 | 0 | DocumentTypeDTO dto = fetchDTOFromCacheByName(documentTypeName); |
401 | 0 | if ( dto == null ) { |
402 | 0 | DocumentType docType = findByName(documentTypeName); |
403 | 0 | if ( docType == null ) { |
404 | 0 | return null; |
405 | |
} |
406 | 0 | dto = DTOConverter.convertDocumentType(docType); |
407 | 0 | insertDTOIntoCache(dto); |
408 | |
} |
409 | 0 | return dto; |
410 | |
} |
411 | |
|
412 | |
public synchronized List findAllCurrentRootDocuments() { |
413 | 0 | List currentRootsInCache = (List) KsbApiServiceLocator.getCacheAdministrator().getFromCache(CURRENT_ROOTS_IN_CACHE_KEY); |
414 | |
|
415 | 0 | if (currentRootsInCache == null) { |
416 | 0 | currentRootsInCache = getDocumentTypeDAO().findAllCurrentRootDocuments(); |
417 | 0 | KsbApiServiceLocator.getCacheAdministrator().putInCache(CURRENT_ROOTS_IN_CACHE_KEY, currentRootsInCache); |
418 | |
} |
419 | 0 | return currentRootsInCache; |
420 | |
} |
421 | |
|
422 | |
public List findAllCurrent() { |
423 | 0 | return getDocumentTypeDAO().findAllCurrent(); |
424 | |
} |
425 | |
|
426 | |
public List<DocumentType> findPreviousInstances(String documentTypeName) { |
427 | 0 | return getDocumentTypeDAO().findPreviousInstances(documentTypeName); |
428 | |
} |
429 | |
|
430 | |
public DocumentType findRootDocumentType(DocumentType docType) { |
431 | 0 | if (docType.getParentDocType() != null) { |
432 | 0 | return findRootDocumentType(docType.getParentDocType()); |
433 | |
} else { |
434 | 0 | return docType; |
435 | |
} |
436 | |
} |
437 | |
|
438 | |
public void loadXml(InputStream inputStream, String principalId) { |
439 | 0 | DocumentTypeXmlParser parser = new DocumentTypeXmlParser(); |
440 | |
try { |
441 | 0 | parser.parseDocumentTypes(inputStream); |
442 | 0 | } catch (Exception e) { |
443 | 0 | WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error parsing documentType XML file", new WorkflowServiceErrorImpl("Error parsing documentType XML file", XML_FILE_PARSE_ERROR)); |
444 | 0 | wsee.initCause(e); |
445 | 0 | throw wsee; |
446 | 0 | } |
447 | 0 | } |
448 | |
|
449 | |
public Element export(ExportDataSet dataSet) { |
450 | 0 | DocumentTypeXmlExporter exporter = new DocumentTypeXmlExporter(); |
451 | 0 | return exporter.export(dataSet); |
452 | |
} |
453 | |
|
454 | |
@Override |
455 | |
public boolean supportPrettyPrint() { |
456 | 0 | return true; |
457 | |
} |
458 | |
|
459 | |
public List getChildDocumentTypes(Long documentTypeId) { |
460 | 0 | List childDocumentTypes = new ArrayList(); |
461 | 0 | List childIds = getDocumentTypeDAO().getChildDocumentTypeIds(documentTypeId); |
462 | 0 | for (Iterator iter = childIds.iterator(); iter.hasNext();) { |
463 | 0 | Long childDocumentTypeId = (Long) iter.next(); |
464 | 0 | childDocumentTypes.add(findById(childDocumentTypeId)); |
465 | 0 | } |
466 | 0 | return childDocumentTypes; |
467 | |
} |
468 | |
|
469 | |
} |