| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.kew.doctype.dao.impl; |
| 18 | |
|
| 19 | |
import org.apache.log4j.Logger; |
| 20 | |
import org.kuali.rice.core.framework.persistence.jpa.OrmUtils; |
| 21 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria; |
| 22 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria; |
| 23 | |
import org.kuali.rice.kew.doctype.DocumentTypeAttribute; |
| 24 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
| 25 | |
import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO; |
| 26 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
| 27 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
| 28 | |
|
| 29 | |
import javax.persistence.EntityManager; |
| 30 | |
import javax.persistence.PersistenceContext; |
| 31 | |
import javax.persistence.Query; |
| 32 | |
import java.util.ArrayList; |
| 33 | |
import java.util.Collection; |
| 34 | |
import java.util.Iterator; |
| 35 | |
import java.util.List; |
| 36 | |
|
| 37 | |
|
| 38 | 0 | public class DocumentTypeDAOJpaImpl implements DocumentTypeDAO { |
| 39 | |
|
| 40 | 0 | public static final Logger LOG = Logger.getLogger(DocumentTypeDAOJpaImpl.class); |
| 41 | |
|
| 42 | |
@PersistenceContext(unitName="kew-unit") |
| 43 | |
private EntityManager entityManager; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public EntityManager getEntityManager() { |
| 50 | 0 | return this.entityManager; |
| 51 | |
} |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public void setEntityManager(EntityManager entityManager) { |
| 57 | 0 | this.entityManager = entityManager; |
| 58 | 0 | } |
| 59 | |
|
| 60 | |
public void delete(DocumentType documentType) { |
| 61 | 0 | DocumentType docType = findById(documentType.getDocumentTypeId()); |
| 62 | 0 | entityManager.remove(documentType); |
| 63 | 0 | } |
| 64 | |
|
| 65 | |
public DocumentType findById(Long docTypeId) { |
| 66 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
| 67 | 0 | crit.eq("documentTypeId", docTypeId); |
| 68 | 0 | return (DocumentType) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult(); |
| 69 | |
} |
| 70 | |
|
| 71 | |
public DocumentType findByName(String name){ |
| 72 | 0 | return findByName(name, true); |
| 73 | |
} |
| 74 | |
|
| 75 | |
public DocumentType findByName(String name, boolean caseSensitive) { |
| 76 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
| 77 | 0 | if(!caseSensitive){ |
| 78 | 0 | crit.like("UPPER(__JPA_ALIAS[[0]]__.name)", ("%" + name.trim() + "%").toUpperCase()); |
| 79 | |
|
| 80 | |
}else{ |
| 81 | 0 | crit.eq("name", name); |
| 82 | |
} |
| 83 | 0 | crit.eq("currentInd", Boolean.TRUE); |
| 84 | 0 | DocumentType docType = (DocumentType) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult(); |
| 85 | 0 | return docType; |
| 86 | |
} |
| 87 | |
|
| 88 | |
public Integer getMaxVersionNumber(String docTypeName) { |
| 89 | 0 | return getMostRecentDocType(docTypeName).getVersion(); |
| 90 | |
} |
| 91 | |
|
| 92 | |
public List getChildDocumentTypeIds(Long parentDocumentTypeId) { |
| 93 | 0 | List childrenIds = new ArrayList(); |
| 94 | |
try { |
| 95 | 0 | String sql = "select DOC_TYP_ID from KREW_DOC_TYP_T where CUR_IND = 1 and PARNT_ID = " + parentDocumentTypeId; |
| 96 | 0 | Query query = entityManager.createNativeQuery(sql); |
| 97 | 0 | List resultIds = query.getResultList(); |
| 98 | 0 | for (Object id:resultIds){ |
| 99 | 0 | childrenIds.add(new Long(id.toString())); |
| 100 | |
} |
| 101 | |
|
| 102 | 0 | } catch (Exception e) { |
| 103 | 0 | LOG.error("Error occured fetching children document type ids for document type " + parentDocumentTypeId, e); |
| 104 | 0 | throw new RuntimeException(e); |
| 105 | 0 | } |
| 106 | |
|
| 107 | 0 | return childrenIds; |
| 108 | |
} |
| 109 | |
|
| 110 | |
public DocumentType getMostRecentDocType(String docTypeName) { |
| 111 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
| 112 | 0 | crit.eq("name", docTypeName); |
| 113 | 0 | crit.orderBy("version", false); |
| 114 | |
|
| 115 | 0 | Iterator docTypes = new QueryByCriteria(entityManager, crit).toQuery().getResultList().iterator(); |
| 116 | 0 | while (docTypes.hasNext()) { |
| 117 | 0 | return (DocumentType) docTypes.next(); |
| 118 | |
} |
| 119 | 0 | return null; |
| 120 | |
} |
| 121 | |
|
| 122 | |
public void save(DocumentType documentType) { |
| 123 | 0 | if (documentType.getDocumentTypeId() == null){ |
| 124 | 0 | entityManager.persist(documentType); |
| 125 | |
} else { |
| 126 | 0 | OrmUtils.merge(entityManager, documentType); |
| 127 | |
} |
| 128 | 0 | } |
| 129 | |
|
| 130 | |
public List findByDocumentId(String documentId) { |
| 131 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
| 132 | 0 | crit.eq("documentId", documentId); |
| 133 | 0 | return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
| 134 | |
} |
| 135 | |
|
| 136 | |
public Collection<DocumentType> find(DocumentType documentType, DocumentType docTypeParent, boolean climbHierarchy) { |
| 137 | 0 | LOG.debug("documentType: "+ documentType); |
| 138 | 0 | LOG.debug("docTypeParent: "+ docTypeParent); |
| 139 | 0 | LOG.debug("climbHierarchy: " + climbHierarchy); |
| 140 | |
|
| 141 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
| 142 | 0 | if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getLabel())) { |
| 143 | 0 | crit.like("UPPER(__JPA_ALIAS[[0]]__.label)", documentType.getLabel().trim().toUpperCase()); |
| 144 | |
} |
| 145 | 0 | if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getName())) { |
| 146 | 0 | String docTypeName = documentType.getName(); |
| 147 | 0 | crit.like("UPPER(__JPA_ALIAS[[0]]__.name)", ("%" + docTypeName.trim() + "%").toUpperCase()); |
| 148 | |
} |
| 149 | 0 | if (documentType != null && documentType.getActive() != null) { |
| 150 | 0 | crit.eq("active", documentType.getActive()); |
| 151 | |
} |
| 152 | 0 | if (documentType != null && documentType.getDocumentTypeId() != null) { |
| 153 | 0 | crit.eq("documentTypeId", documentType.getDocumentTypeId()); |
| 154 | |
} |
| 155 | 0 | if (documentType != null && documentType.getActualApplicationId() != null){ |
| 156 | 0 | crit.eq("applicationId", documentType.getActualApplicationId()); |
| 157 | |
} |
| 158 | 0 | if (docTypeParent != null) { |
| 159 | 0 | if (!"".equals(docTypeParent.getName()) && docTypeParent.getName() != null) { |
| 160 | 0 | Criteria parentCrit = new Criteria(DocumentType.class.getName()); |
| 161 | |
|
| 162 | 0 | addParentIdOrCriteria(docTypeParent.getDocumentTypeId(), parentCrit); |
| 163 | 0 | if (climbHierarchy) { |
| 164 | 0 | assembleChildrenCriteria(docTypeParent.getChildrenDocTypes(), parentCrit); |
| 165 | |
} |
| 166 | 0 | parentCrit.eq("currentInd", Boolean.TRUE); |
| 167 | 0 | crit.and(parentCrit); |
| 168 | 0 | } |
| 169 | |
} else { |
| 170 | 0 | if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getName())) { |
| 171 | 0 | DocumentType searchDocumentType = findByName(documentType.getName()); |
| 172 | 0 | if ((searchDocumentType != null) && climbHierarchy) { |
| 173 | 0 | LOG.debug("searchDocumentType: "+ searchDocumentType); |
| 174 | 0 | Criteria criteria = new Criteria(DocumentType.class.getName()); |
| 175 | |
|
| 176 | 0 | addParentIdOrCriteria(searchDocumentType.getDocumentTypeId(), criteria); |
| 177 | 0 | assembleChildrenCriteria(searchDocumentType.getChildrenDocTypes(), criteria); |
| 178 | 0 | criteria.eq("currentInd", Boolean.TRUE); |
| 179 | 0 | crit.or(criteria); |
| 180 | |
} |
| 181 | |
} |
| 182 | |
} |
| 183 | 0 | crit.eq("currentInd", Boolean.TRUE); |
| 184 | 0 | return new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
| 185 | |
} |
| 186 | |
|
| 187 | |
private void addParentIdOrCriteria(Long parentId, Criteria mainCriteria) { |
| 188 | 0 | Criteria parentCriteria = new Criteria(DocumentType.class.getName()); |
| 189 | 0 | parentCriteria.eq("docTypeParentId", parentId); |
| 190 | 0 | mainCriteria.or(parentCriteria); |
| 191 | 0 | } |
| 192 | |
|
| 193 | |
private void assembleChildrenCriteria(Collection childrenDocTypes, Criteria crit) { |
| 194 | 0 | if (childrenDocTypes != null) { |
| 195 | 0 | Iterator childrenDocTypesIter = childrenDocTypes.iterator(); |
| 196 | 0 | while (childrenDocTypesIter.hasNext()) { |
| 197 | 0 | DocumentType child = (DocumentType) childrenDocTypesIter.next(); |
| 198 | 0 | addParentIdOrCriteria(child.getDocumentTypeId(), crit); |
| 199 | 0 | assembleChildrenCriteria(child.getChildrenDocTypes(), crit); |
| 200 | 0 | } |
| 201 | |
} |
| 202 | 0 | } |
| 203 | |
|
| 204 | |
public DocumentType getMostRecentDocType(Long documentTypeId) { |
| 205 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
| 206 | 0 | crit.eq("documentTypeId", documentTypeId); |
| 207 | 0 | crit.orderBy("version", false); |
| 208 | |
|
| 209 | 0 | Iterator docTypes = new QueryByCriteria(entityManager, crit).toQuery().getResultList().iterator(); |
| 210 | 0 | while (docTypes.hasNext()) { |
| 211 | 0 | return (DocumentType) docTypes.next(); |
| 212 | |
} |
| 213 | 0 | return null; |
| 214 | |
} |
| 215 | |
|
| 216 | |
public List findAllCurrentRootDocuments() { |
| 217 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
| 218 | 0 | crit.isNull("docTypeParentId"); |
| 219 | 0 | return findAllCurrent(crit); |
| 220 | |
} |
| 221 | |
|
| 222 | |
public List findAllCurrent() { |
| 223 | 0 | return findAllCurrent(new Criteria(DocumentType.class.getName())); |
| 224 | |
} |
| 225 | |
|
| 226 | |
public List findAllCurrentByName(String name) { |
| 227 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
| 228 | 0 | crit.eq("name", name); |
| 229 | 0 | return findAllCurrent(crit); |
| 230 | |
} |
| 231 | |
|
| 232 | |
public List<DocumentType> findPreviousInstances(String name) { |
| 233 | 0 | Criteria crit = new Criteria(DocumentType.class.getName()); |
| 234 | 0 | crit.eq("name", name); |
| 235 | 0 | crit.eq("currentInd", Boolean.FALSE); |
| 236 | 0 | return findAll(crit); |
| 237 | |
} |
| 238 | |
|
| 239 | |
private List findAllCurrent(Criteria crit) { |
| 240 | 0 | crit.eq("currentInd", Boolean.TRUE); |
| 241 | 0 | return findAll(crit); |
| 242 | |
} |
| 243 | |
|
| 244 | |
private List findAll(Criteria crit) { |
| 245 | 0 | return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
| 246 | |
} |
| 247 | |
|
| 248 | |
public List findDocumentTypeAttributes(RuleAttribute ruleAttribute) { |
| 249 | 0 | Criteria crit = new Criteria(DocumentTypeAttribute.class.getName()); |
| 250 | 0 | if (ruleAttribute.getRuleAttributeId() != null) { |
| 251 | 0 | crit.eq("ruleAttributeId", ruleAttribute.getRuleAttributeId()); |
| 252 | |
} |
| 253 | 0 | return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList(); |
| 254 | |
} |
| 255 | |
|
| 256 | |
public Long findDocumentTypeIdByDocumentId(String documentId) { |
| 257 | 0 | Criteria crit = new Criteria(DocumentRouteHeaderValue.class.getName()); |
| 258 | 0 | crit.eq("documentId", documentId); |
| 259 | |
|
| 260 | 0 | final DocumentRouteHeaderValue docHeader = (DocumentRouteHeaderValue)new QueryByCriteria(entityManager, crit).toQuery().getSingleResult(); |
| 261 | 0 | return (docHeader != null) ? docHeader.getDocumentTypeId() : null; |
| 262 | |
} |
| 263 | |
|
| 264 | |
} |