1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.doctype.service.impl; |
17 | |
|
18 | |
import org.apache.commons.collections.CollectionUtils; |
19 | |
import org.jdom.Element; |
20 | |
import org.kuali.rice.core.api.impex.ExportDataSet; |
21 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
22 | |
import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO; |
23 | |
import org.kuali.rice.kew.doctype.service.DocumentTypeService; |
24 | |
import org.kuali.rice.kew.exception.WorkflowServiceErrorException; |
25 | |
import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl; |
26 | |
import org.kuali.rice.kew.xml.DocumentTypeXmlParser; |
27 | |
import org.kuali.rice.kew.xml.export.DocumentTypeXmlExporter; |
28 | |
import org.kuali.rice.krad.util.ObjectUtils; |
29 | |
|
30 | |
import java.io.InputStream; |
31 | |
import java.util.ArrayList; |
32 | |
import java.util.Collection; |
33 | |
import java.util.Iterator; |
34 | |
import java.util.List; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class DocumentTypeServiceImpl implements DocumentTypeService { |
43 | |
|
44 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentTypeServiceImpl.class); |
45 | |
protected static final String XML_FILE_PARSE_ERROR = "general.error.parsexml"; |
46 | |
|
47 | |
private DocumentTypeDAO documentTypeDAO; |
48 | |
|
49 | |
public Collection<DocumentType> find(DocumentType documentType, String docTypeParentName, boolean climbHierarchy) { |
50 | 0 | DocumentType docTypeParent = this.findByName(docTypeParentName); |
51 | 0 | return getDocumentTypeDAO().find(documentType, docTypeParent, climbHierarchy); |
52 | |
} |
53 | |
|
54 | |
public DocumentType findById(String documentTypeId) { |
55 | 0 | if (documentTypeId == null) { |
56 | 0 | return null; |
57 | |
} |
58 | |
|
59 | 0 | return getDocumentTypeDAO().findById(documentTypeId); |
60 | |
} |
61 | |
|
62 | |
public DocumentType findByDocumentId(String documentId) { |
63 | 0 | if (documentId == null) { |
64 | 0 | return null; |
65 | |
} |
66 | 0 | String documentTypeId = getDocumentTypeDAO().findDocumentTypeIdByDocumentId(documentId); |
67 | 0 | return findById(documentTypeId); |
68 | |
} |
69 | |
|
70 | |
public DocumentType findByName(String name) { |
71 | 0 | return this.findByName(name, true); |
72 | |
} |
73 | |
|
74 | |
public DocumentType findByNameCaseInsensitive(String name) { |
75 | 0 | return this.findByName(name, false); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
private DocumentType findByName(String name, boolean caseSensitive) { |
87 | 0 | if (name == null) { |
88 | 0 | return null; |
89 | |
} |
90 | 0 | return getDocumentTypeDAO().findByName(name, caseSensitive); |
91 | |
} |
92 | |
|
93 | |
public void versionAndSave(DocumentType documentType) { |
94 | |
|
95 | |
|
96 | 0 | if (documentType.getDocumentTypeId() != null && documentType.getVersionNumber() != null) { |
97 | 0 | throw new RuntimeException("DocumentType configured for update and not versioning which we support"); |
98 | |
} |
99 | |
|
100 | |
|
101 | 0 | DocumentType oldDocumentType = findByName(documentType.getName()); |
102 | |
|
103 | |
|
104 | 0 | String existingDocTypeId = null; |
105 | 0 | if (oldDocumentType != null) { |
106 | 0 | existingDocTypeId = oldDocumentType.getDocumentTypeId(); |
107 | |
|
108 | 0 | Integer maxVersionNumber = documentTypeDAO.getMaxVersionNumber(documentType.getName()); |
109 | 0 | documentType.setVersion((maxVersionNumber != null) ? new Integer(maxVersionNumber.intValue() + 1) : new Integer(0)); |
110 | 0 | oldDocumentType.setCurrentInd(Boolean.FALSE); |
111 | 0 | if ( LOG.isInfoEnabled() ) { |
112 | 0 | LOG.info("Saving old document type Id " + oldDocumentType.getDocumentTypeId() + " name '" + oldDocumentType.getName() + "' (current = " + oldDocumentType.getCurrentInd() + ")"); |
113 | |
} |
114 | 0 | save(oldDocumentType); |
115 | |
} |
116 | |
|
117 | 0 | if (!CollectionUtils.isEmpty(documentTypeDAO.findAllCurrentByName(documentType.getName()))) { |
118 | 0 | String errorMsg = "Found invalid 'current' document with name '" + documentType.getName() + "'. None should exist."; |
119 | 0 | LOG.error(errorMsg); |
120 | 0 | throw new RuntimeException(errorMsg); |
121 | |
} |
122 | |
|
123 | 0 | documentType.setPreviousVersionId(existingDocTypeId); |
124 | 0 | documentType.setCurrentInd(Boolean.TRUE); |
125 | 0 | save(documentType); |
126 | 0 | if ( LOG.isInfoEnabled() ) { |
127 | 0 | LOG.info("Saved current document type Id " + documentType.getDocumentTypeId() + " name '" + documentType.getName() + "' (current = " + documentType.getCurrentInd() + ")"); |
128 | |
} |
129 | |
|
130 | 0 | if (ObjectUtils.isNotNull(existingDocTypeId)) { |
131 | |
|
132 | 0 | for (Iterator iterator = getChildDocumentTypes(existingDocTypeId).iterator(); iterator.hasNext();) { |
133 | |
|
134 | 0 | DocumentType child = (DocumentType) iterator.next(); |
135 | 0 | child.setDocTypeParentId(documentType.getDocumentTypeId()); |
136 | 0 | save(child); |
137 | 0 | if ( LOG.isInfoEnabled() ) { |
138 | 0 | LOG.info("Saved child document type Id " + child.getDocumentTypeId() + " name '" + child.getName() + "' (parent = " + child.getDocTypeParentId() + ", current = " + child.getCurrentInd() + ")"); |
139 | |
} |
140 | 0 | } |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | 0 | if (documentType.getDocTypeParentId() != null) { |
148 | 0 | DocumentType parent = getDocumentTypeDAO().findById(documentType.getDocTypeParentId()); |
149 | 0 | save(parent); |
150 | 0 | if ( LOG.isInfoEnabled() ) { |
151 | 0 | LOG.info("Saved parent document type Id " + parent.getDocumentTypeId() + " name '" + parent.getName() + "' (current = " + parent.getCurrentInd() + ")"); |
152 | |
} |
153 | |
} |
154 | 0 | } |
155 | |
|
156 | |
public void save(DocumentType documentType) { |
157 | 0 | getDocumentTypeDAO().save(documentType); |
158 | 0 | } |
159 | |
|
160 | |
public DocumentTypeDAO getDocumentTypeDAO() { |
161 | 0 | return documentTypeDAO; |
162 | |
} |
163 | |
|
164 | |
public void setDocumentTypeDAO(DocumentTypeDAO documentTypeDAO) { |
165 | 0 | this.documentTypeDAO = documentTypeDAO; |
166 | 0 | } |
167 | |
|
168 | |
public synchronized List findAllCurrentRootDocuments() { |
169 | 0 | return getDocumentTypeDAO().findAllCurrentRootDocuments(); |
170 | |
} |
171 | |
|
172 | |
public List findAllCurrent() { |
173 | 0 | return getDocumentTypeDAO().findAllCurrent(); |
174 | |
} |
175 | |
|
176 | |
public List<DocumentType> findPreviousInstances(String documentTypeName) { |
177 | 0 | return getDocumentTypeDAO().findPreviousInstances(documentTypeName); |
178 | |
} |
179 | |
|
180 | |
public DocumentType findRootDocumentType(DocumentType docType) { |
181 | 0 | if (docType.getParentDocType() != null) { |
182 | 0 | return findRootDocumentType(docType.getParentDocType()); |
183 | |
} else { |
184 | 0 | return docType; |
185 | |
} |
186 | |
} |
187 | |
|
188 | |
public void loadXml(InputStream inputStream, String principalId) { |
189 | 0 | DocumentTypeXmlParser parser = new DocumentTypeXmlParser(); |
190 | |
try { |
191 | 0 | parser.parseDocumentTypes(inputStream); |
192 | 0 | } catch (Exception e) { |
193 | 0 | WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error parsing documentType XML file", new WorkflowServiceErrorImpl("Error parsing documentType XML file", XML_FILE_PARSE_ERROR)); |
194 | 0 | wsee.initCause(e); |
195 | 0 | throw wsee; |
196 | 0 | } |
197 | 0 | } |
198 | |
|
199 | |
public Element export(ExportDataSet dataSet) { |
200 | 0 | DocumentTypeXmlExporter exporter = new DocumentTypeXmlExporter(); |
201 | 0 | return exporter.export(dataSet); |
202 | |
} |
203 | |
|
204 | |
@Override |
205 | |
public boolean supportPrettyPrint() { |
206 | 0 | return true; |
207 | |
} |
208 | |
|
209 | |
public List getChildDocumentTypes(String documentTypeId) { |
210 | 0 | List childDocumentTypes = new ArrayList(); |
211 | 0 | List childIds = getDocumentTypeDAO().getChildDocumentTypeIds(documentTypeId); |
212 | 0 | for (Iterator iter = childIds.iterator(); iter.hasNext();) { |
213 | 0 | String childDocumentTypeId = (String) iter.next(); |
214 | 0 | childDocumentTypes.add(findById(childDocumentTypeId)); |
215 | 0 | } |
216 | 0 | return childDocumentTypes; |
217 | |
} |
218 | |
|
219 | |
} |