001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.doctype.dao.impl;
017
018import java.sql.Connection;
019import java.sql.PreparedStatement;
020import java.sql.ResultSet;
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.Iterator;
024import java.util.List;
025
026import org.apache.log4j.Logger;
027import org.apache.ojb.broker.PersistenceBroker;
028import org.apache.ojb.broker.accesslayer.ReportQueryRsIterator;
029import org.apache.ojb.broker.query.Criteria;
030import org.apache.ojb.broker.query.QueryByCriteria;
031import org.apache.ojb.broker.query.QueryFactory;
032import org.apache.ojb.broker.query.ReportQueryByCriteria;
033import org.kuali.rice.kew.doctype.DocumentTypeAttributeBo;
034import org.kuali.rice.kew.doctype.bo.DocumentType;
035import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO;
036import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
037import org.kuali.rice.kew.rule.bo.RuleAttribute;
038import org.springmodules.orm.ojb.OjbFactoryUtils;
039import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
040
041
042public class DocumentTypeDAOOjbImpl extends PersistenceBrokerDaoSupport implements DocumentTypeDAO {
043
044        public static final Logger LOG = Logger.getLogger(DocumentTypeDAOOjbImpl.class);
045
046        public void delete(DocumentType documentType) {
047                this.getPersistenceBrokerTemplate().delete(documentType);
048        }
049
050        public DocumentType findById(String docTypeId) {
051                Criteria crit = new Criteria();
052                crit.addEqualTo("documentTypeId", docTypeId);
053                return (DocumentType) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentType.class, crit));
054        }
055
056        public DocumentType findByName(String name) {
057                return findByName(name, true);
058        }
059
060    public DocumentType findByName(String name, boolean caseSensitive) {
061        Criteria crit = new Criteria();
062        if (!caseSensitive) {
063            if (name.contains("*") || name.contains("%")) {
064                name.replace("*", "%");
065                crit.addLike("UPPER(name)", name.trim().toUpperCase());
066            } else {
067                crit.addEqualTo("UPPER(name)", name.trim().toUpperCase());
068            }
069        } else {
070            if (name.contains("*")) {
071                name.replace("*", "%");
072                crit.addLike("name", name.trim().toUpperCase());
073            } else {
074                crit.addEqualTo("name", name);
075            }
076
077        }
078        crit.addEqualTo("currentInd", Boolean.TRUE);
079        DocumentType docType = (DocumentType) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(
080                DocumentType.class, crit));
081        return docType;
082    }
083
084    public Integer getMaxVersionNumber(String docTypeName) {
085                return getMostRecentDocType(docTypeName).getVersion();
086        }
087
088        public List<String> getChildDocumentTypeIds(String parentDocumentTypeId) {
089                List<String> childrenIds = new ArrayList<String>();
090                PersistenceBroker broker = getPersistenceBroker(false);
091                Connection conn = null;
092                PreparedStatement st = null;
093                ResultSet rs = null;
094                try {
095                        conn = broker.serviceConnectionManager().getConnection();
096                        st = conn.prepareStatement("select DOC_TYP_ID from KREW_DOC_TYP_T where CUR_IND = 1 and PARNT_ID = ?");
097                        st.setString(1, parentDocumentTypeId);
098                        rs = st.executeQuery();
099                        while (rs.next()) {
100                                childrenIds.add(rs.getString("DOC_TYP_ID"));
101                        }
102                } catch (Exception e) {
103                        LOG.error("Error occured fetching children document type ids for document type " + parentDocumentTypeId, e);
104                        throw new RuntimeException(e);
105                } finally {
106                        try {
107                                st.close();
108                        } catch (Exception e) {
109                                LOG.warn("Failed to close Statement", e);
110                        }
111
112                        try {
113                                rs.close();
114                        } catch (Exception e) {
115                                LOG.warn("Failed to close Resultset", e);
116                        }
117
118                if (broker != null) {
119                        try {
120                                OjbFactoryUtils.releasePersistenceBroker(broker, this.getPersistenceBrokerTemplate().getPbKey());
121                        } catch (Exception e) {
122                                LOG.error("Failed closing connection: " + e.getMessage(), e);
123                        }
124                }
125                }
126                return childrenIds;
127        }
128
129        protected DocumentType getMostRecentDocType(String docTypeName) {
130                Criteria crit = new Criteria();
131                crit.addEqualTo("name", docTypeName);
132                QueryByCriteria query = new QueryByCriteria(DocumentType.class, crit);
133                query.addOrderByDescending("version");
134
135                Iterator docTypes = this.getPersistenceBrokerTemplate().getCollectionByQuery(query).iterator();
136                while (docTypes.hasNext()) {
137                        return (DocumentType) docTypes.next();
138                }
139                return null;
140        }
141
142        public void save(DocumentType documentType) {
143                this.getPersistenceBrokerTemplate().store(documentType);
144        }
145
146        public List findByDocumentId(String documentId) {
147                Criteria crit = new Criteria();
148                crit.addEqualTo("documentId", documentId);
149                return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentType.class, crit));
150        }
151
152        public Collection<DocumentType> find(DocumentType documentType, DocumentType docTypeParent, boolean climbHierarchy) {
153                LOG.debug("documentType: "+ documentType);
154                LOG.debug("docTypeParent: "+ docTypeParent);
155                LOG.debug("climbHierarchy: " + climbHierarchy);
156
157                Criteria crit = new Criteria();
158                if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getLabel())) {
159                        crit.addLike("UPPER(label)", documentType.getLabel().trim().toUpperCase());
160                }
161                if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getName())) {
162                        String docTypeName = documentType.getName();
163                        crit.addLike("UPPER(name)", ("%" + docTypeName.trim() + "%").toUpperCase());
164                }
165                if (documentType != null && documentType.getActive() != null) {
166                        crit.addEqualTo("active", documentType.getActive());
167                }
168                if (documentType != null && documentType.getDocumentTypeId() != null) {
169                        crit.addEqualTo("documentTypeId", documentType.getDocumentTypeId());
170                }
171                if (documentType != null && documentType.getActualApplicationId() != null){
172                        crit.addEqualTo("actualApplicationIde", documentType.getActualApplicationId());
173                }
174                if (docTypeParent != null) {
175                        if (!"".equals(docTypeParent.getName()) && docTypeParent.getName() != null) {
176                                Criteria parentCrit = new Criteria();
177                                //addParentNameOrCriteria(docTypeParent.getName(), parentCrit);
178                                addParentIdOrCriteria(docTypeParent.getDocumentTypeId(), parentCrit);
179                                if (climbHierarchy) {
180                                        assembleChildrenCriteria(docTypeParent.getChildrenDocTypes(), parentCrit);
181                                }
182                                parentCrit.addEqualTo("currentInd", Boolean.TRUE);
183                                crit.addAndCriteria(parentCrit);
184                        }
185                } else {
186                        if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getName())) {
187                                DocumentType searchDocumentType = findByName(documentType.getName());
188                                if ((searchDocumentType != null) && climbHierarchy) {
189                                        LOG.debug("searchDocumentType: "+ searchDocumentType);
190                                        Criteria criteria = new Criteria();
191                                        //addParentNameOrCriteria(searchDocumentType.getName(), criteria);
192                    addParentIdOrCriteria(searchDocumentType.getDocumentTypeId(), criteria);
193                    assembleChildrenCriteria(searchDocumentType.getChildrenDocTypes(), criteria);
194                                        criteria.addEqualTo("currentInd", Boolean.TRUE);
195                                        crit.addOrCriteria(criteria);
196                                }
197                        }
198                }
199                crit.addEqualTo("currentInd", Boolean.TRUE);
200                return (Collection<DocumentType>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentType.class, crit));
201        }
202
203    private void addParentIdOrCriteria(String parentId, Criteria mainCriteria) {
204        Criteria parentCriteria = new Criteria();
205        parentCriteria.addEqualTo("docTypeParentId", parentId);
206        mainCriteria.addOrCriteria(parentCriteria);
207    }
208
209        private void assembleChildrenCriteria(Collection childrenDocTypes, Criteria crit) {
210                if (childrenDocTypes != null) {
211                        Iterator childrenDocTypesIter = childrenDocTypes.iterator();
212                        while (childrenDocTypesIter.hasNext()) {
213                                DocumentType child = (DocumentType) childrenDocTypesIter.next();
214                                addParentIdOrCriteria(child.getDocumentTypeId(), crit);
215                                assembleChildrenCriteria(child.getChildrenDocTypes(), crit);
216                        }
217                }
218        }
219
220        public List<DocumentType> findAllCurrentRootDocuments() {
221                Criteria crit = new Criteria();
222                crit.addIsNull("docTypeParentId");
223                return findAllCurrent(crit);
224        }
225
226    public List<DocumentType> findAllCurrent() {
227        return findAllCurrent(new Criteria());
228    }
229
230    public List<DocumentType> findAllCurrentByName(String name) {
231        Criteria crit = new Criteria();
232        crit.addEqualTo("name", name);
233        return findAllCurrent(crit);
234    }
235
236    public List<DocumentType> findPreviousInstances(String documentTypeName) {
237        Criteria crit = new Criteria();
238        crit.addEqualTo("name", documentTypeName);
239        crit.addEqualTo("currentInd", Boolean.FALSE);
240        return findAll(crit);
241    }
242
243    private List<DocumentType> findAllCurrent(Criteria crit) {
244        crit.addEqualTo("currentInd", Boolean.TRUE);
245        return findAll(crit);
246    }
247
248    private List<DocumentType> findAll(Criteria crit) {
249        return (List<DocumentType>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentType.class, crit));
250    }
251
252    public List findDocumentTypeAttributes(RuleAttribute ruleAttribute) {
253        Criteria crit = new Criteria();
254        crit.addEqualTo("ruleAttributeId", ruleAttribute.getId());
255        return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentTypeAttributeBo.class, crit));
256    }
257
258    public String findDocumentTypeIdByDocumentId(String documentId) {
259        Criteria crit = new Criteria();
260        crit.addEqualTo("documentId", documentId);
261        ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentRouteHeaderValue.class, crit);
262        query.setAttributes(new String[] { "documentTypeId" });
263
264        ReportQueryRsIterator iter = (ReportQueryRsIterator)getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
265        try {
266            while (iter.hasNext()) {
267                Object[] row = (Object[]) iter.next();
268                return (String)row[0];
269            }
270        } finally {
271            iter.releaseDbResources();
272        }
273        return null;
274    }
275    
276    public String findDocumentTypeIdByName(String documentTypeName) {
277        Criteria crit = new Criteria();
278        crit.addEqualTo("name", documentTypeName);
279        crit.addEqualTo("currentInd", Boolean.TRUE);
280        ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentType.class, crit);
281        query.setAttributes(new String[] { "documentTypeId" });
282
283        ReportQueryRsIterator iter = (ReportQueryRsIterator)getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
284        try {
285                while (iter.hasNext()) {
286                        Object[] row = (Object[]) iter.next();
287                        return (String)row[0];
288                }
289        } finally {
290                iter.releaseDbResources();
291        }
292        return null;
293    }
294    
295    public String findDocumentTypeNameById(String documentTypeId) {
296        Criteria crit = new Criteria();
297        crit.addEqualTo("documentTypeId", documentTypeId);
298        ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentType.class, crit);
299        query.setAttributes(new String[] { "name" });
300
301        ReportQueryRsIterator iter = (ReportQueryRsIterator)getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
302        try {
303            while (iter.hasNext()) {
304                Object[] row = (Object[]) iter.next();
305                return (String)row[0];
306            }
307        } finally {
308            iter.releaseDbResources();
309        }
310        return null;
311    }
312
313}