|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
   package org.kuali.rice.kew.routeheader.dao.impl;  | 
  |  18 |     | 
     | 
  |  19 |     | 
   import java.math.BigDecimal;  | 
  |  20 |     | 
   import java.util.ArrayList;  | 
  |  21 |     | 
   import java.util.Collection;  | 
  |  22 |     | 
   import java.util.Iterator;  | 
  |  23 |     | 
   import java.util.List;  | 
  |  24 |     | 
   import java.util.Set;  | 
  |  25 |     | 
     | 
  |  26 |     | 
   import javax.persistence.EntityManager;  | 
  |  27 |     | 
   import javax.persistence.EntityNotFoundException;  | 
  |  28 |     | 
   import javax.persistence.PersistenceContext;  | 
  |  29 |     | 
   import javax.persistence.Query;  | 
  |  30 |     | 
     | 
  |  31 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  32 |     | 
   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;  | 
  |  33 |     | 
   import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;  | 
  |  34 |     | 
   import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;  | 
  |  35 |     | 
   import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;  | 
  |  36 |     | 
   import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform;  | 
  |  37 |     | 
   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;  | 
  |  38 |     | 
   import org.kuali.rice.core.util.RiceConstants;  | 
  |  39 |     | 
   import org.kuali.rice.kew.actionitem.ActionItem;  | 
  |  40 |     | 
   import org.kuali.rice.kew.actionlist.service.ActionListService;  | 
  |  41 |     | 
   import org.kuali.rice.kew.docsearch.SearchableAttributeValue;  | 
  |  42 |     | 
   import org.kuali.rice.kew.exception.LockingException;  | 
  |  43 |     | 
   import org.kuali.rice.kew.exception.WorkflowRuntimeException;  | 
  |  44 |     | 
   import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;  | 
  |  45 |     | 
   import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValueContent;  | 
  |  46 |     | 
   import org.kuali.rice.kew.routeheader.dao.DocumentRouteHeaderDAO;  | 
  |  47 |     | 
   import org.kuali.rice.kew.service.KEWServiceLocator;  | 
  |  48 |     | 
   import org.kuali.rice.kew.util.KEWConstants;  | 
  |  49 |     | 
     | 
  |  50 |     | 
     | 
  |  51 |    0 |    public class DocumentRouteHeaderDAOJpaImpl implements DocumentRouteHeaderDAO { | 
  |  52 |     | 
     | 
  |  53 |     | 
           @PersistenceContext(unitName="kew-unit")  | 
  |  54 |     | 
           private EntityManager entityManager;  | 
  |  55 |    0 |        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentRouteHeaderDAOJpaImpl.class);  | 
  |  56 |     | 
     | 
  |  57 |     | 
         | 
  |  58 |     | 
         | 
  |  59 |     | 
     | 
  |  60 |     | 
     | 
  |  61 |     | 
           public EntityManager getEntityManager() { | 
  |  62 |    0 |                    return this.entityManager;  | 
  |  63 |     | 
           }  | 
  |  64 |     | 
     | 
  |  65 |     | 
             | 
  |  66 |     | 
     | 
  |  67 |     | 
     | 
  |  68 |     | 
           public void setEntityManager(EntityManager entityManager) { | 
  |  69 |    0 |                    this.entityManager = entityManager;  | 
  |  70 |    0 |            }  | 
  |  71 |     | 
     | 
  |  72 |     | 
           public void saveRouteHeader(DocumentRouteHeaderValue routeHeader) {            | 
  |  73 |    0 |                DocumentRouteHeaderValueContent documentContent = routeHeader.getDocumentContent();              | 
  |  74 |     | 
     | 
  |  75 |     | 
                 | 
  |  76 |    0 |                if (routeHeader.getRouteHeaderId() == null){ | 
  |  77 |    0 |                        entityManager.persist(routeHeader);  | 
  |  78 |     | 
               } else { | 
  |  79 |    0 |                        OrmUtils.merge(entityManager, routeHeader);  | 
  |  80 |     | 
               }  | 
  |  81 |     | 
             | 
  |  82 |     | 
             | 
  |  83 |    0 |            documentContent.setRouteHeaderId(routeHeader.getRouteHeaderId());  | 
  |  84 |    0 |            entityManager.merge(documentContent);  | 
  |  85 |     | 
             | 
  |  86 |     | 
             | 
  |  87 |     | 
     | 
  |  88 |     | 
     | 
  |  89 |     | 
     | 
  |  90 |     | 
     | 
  |  91 |     | 
     | 
  |  92 |     | 
     | 
  |  93 |     | 
     | 
  |  94 |     | 
     | 
  |  95 |     | 
     | 
  |  96 |     | 
     | 
  |  97 |    0 |        }  | 
  |  98 |     | 
     | 
  |  99 |     | 
       public DocumentRouteHeaderValueContent getContent(Long routeHeaderId) { | 
  |  100 |    0 |                Query query = entityManager.createNamedQuery("DocumentRouteHeaderValueContent.FindByRouteHeaderId"); | 
  |  101 |    0 |                query.setParameter("routeHeaderId", routeHeaderId); | 
  |  102 |    0 |            return (DocumentRouteHeaderValueContent)query.getSingleResult();  | 
  |  103 |     | 
       }  | 
  |  104 |     | 
     | 
  |  105 |     | 
       public void clearRouteHeaderSearchValues(Long routeHeaderId) { | 
  |  106 |    0 |                List<SearchableAttributeValue> searchableAttributeValues = findSearchableAttributeValues(routeHeaderId);  | 
  |  107 |    0 |                for (SearchableAttributeValue searchableAttributeValue:searchableAttributeValues){ | 
  |  108 |    0 |                        entityManager.remove(searchableAttributeValue);  | 
  |  109 |     | 
               }  | 
  |  110 |    0 |        }  | 
  |  111 |     | 
        | 
  |  112 |     | 
       private List<SearchableAttributeValue> findSearchableAttributeValues(Long routeHeaderId){ | 
  |  113 |    0 |                List<SearchableAttributeValue> searchableAttributeValues = new ArrayList<SearchableAttributeValue>();  | 
  |  114 |     | 
                 | 
  |  115 |    0 |                for (int i=1;i<=4; i++){ | 
  |  116 |    0 |                        String namedQuery = "";  | 
  |  117 |    0 |                        switch (i) { | 
  |  118 |    0 |                                    case 1: namedQuery = "SearchableAttributeFloatValue.FindByRouteHeaderId"; break;  | 
  |  119 |    0 |                                    case 2: namedQuery = "SearchableAttributeDateTimeValue.FindByRouteHeaderId"; break;  | 
  |  120 |    0 |                                    case 3: namedQuery = "SearchableAttributeLongValue.FindByRouteHeaderId";break;  | 
  |  121 |    0 |                                    case 4: namedQuery = "SearchableAttributeStringValue.FindByRouteHeaderId"; break;  | 
  |  122 |     | 
                       }  | 
  |  123 |    0 |                        Query query = entityManager.createNamedQuery(namedQuery);  | 
  |  124 |    0 |                        query.setParameter("routeHeaderId", routeHeaderId);            | 
  |  125 |    0 |                        searchableAttributeValues.addAll(query.getResultList());  | 
  |  126 |     | 
               }              | 
  |  127 |     | 
     | 
  |  128 |    0 |                return searchableAttributeValues;  | 
  |  129 |     | 
       }  | 
  |  130 |     | 
     | 
  |  131 |     | 
       public void lockRouteHeader(final Long routeHeaderId, final boolean wait) { | 
  |  132 |    0 |                String sql = getPlatform().getLockRouteHeaderQuerySQL(routeHeaderId, wait);  | 
  |  133 |     | 
               try{ | 
  |  134 |    0 |                        Query query = entityManager.createNativeQuery(sql);  | 
  |  135 |    0 |                        query.setParameter(1, routeHeaderId);  | 
  |  136 |    0 |                        query.getSingleResult();  | 
  |  137 |    0 |                } catch (Exception e){ | 
  |  138 |     | 
                         | 
  |  139 |    0 |                        throw new LockingException("Could not aquire lock on document, routeHeaderId=" + routeHeaderId, e); | 
  |  140 |    0 |                }  | 
  |  141 |    0 |        }  | 
  |  142 |     | 
     | 
  |  143 |     | 
       public DocumentRouteHeaderValue findRouteHeader(Long routeHeaderId) { | 
  |  144 |    0 |                return findRouteHeader(routeHeaderId, false);  | 
  |  145 |     | 
       }  | 
  |  146 |     | 
     | 
  |  147 |     | 
       public DocumentRouteHeaderValue findRouteHeader(Long routeHeaderId, boolean clearCache) { | 
  |  148 |    0 |            Query query = entityManager.createNamedQuery("DocumentRouteHeaderValue.FindByRouteHeaderId"); | 
  |  149 |    0 |            query.setParameter("routeHeaderId", routeHeaderId); | 
  |  150 |     | 
     | 
  |  151 |     | 
             | 
  |  152 |    0 |            if (clearCache) { | 
  |  153 |     | 
                     | 
  |  154 |     | 
           }  | 
  |  155 |     | 
             | 
  |  156 |    0 |                DocumentRouteHeaderValue routeHeader = (DocumentRouteHeaderValue) query.getSingleResult();   | 
  |  157 |     | 
     | 
  |  158 |    0 |                return routeHeader;  | 
  |  159 |     | 
       }  | 
  |  160 |     | 
     | 
  |  161 |     | 
       public Collection<DocumentRouteHeaderValue> findRouteHeaders(Collection<Long> routeHeaderIds) { | 
  |  162 |    0 |                return findRouteHeaders(routeHeaderIds, false);  | 
  |  163 |     | 
       }  | 
  |  164 |     | 
         | 
  |  165 |     | 
       public Collection<DocumentRouteHeaderValue> findRouteHeaders(Collection<Long> routeHeaderIds, boolean clearCache) { | 
  |  166 |    0 |                if (routeHeaderIds == null || routeHeaderIds.isEmpty()) { | 
  |  167 |    0 |                        return null;  | 
  |  168 |     | 
               }  | 
  |  169 |    0 |                Criteria crit = new Criteria(DocumentRouteHeaderValue.class.getName());  | 
  |  170 |    0 |                crit.in("routeHeaderId", routeHeaderIds); | 
  |  171 |     | 
                 | 
  |  172 |     | 
                 | 
  |  173 |    0 |            if (clearCache) { | 
  |  174 |     | 
                     | 
  |  175 |     | 
           }  | 
  |  176 |     | 
                 | 
  |  177 |    0 |                return new QueryByCriteria(entityManager, crit).toQuery().getResultList();  | 
  |  178 |     | 
       }  | 
  |  179 |     | 
         | 
  |  180 |     | 
       public void deleteRouteHeader(DocumentRouteHeaderValue routeHeader) { | 
  |  181 |     | 
                 | 
  |  182 |    0 |                ActionListService actionListSrv = KEWServiceLocator.getActionListService();  | 
  |  183 |    0 |                Collection actionItems = actionListSrv.findByRouteHeaderId(routeHeader.getRouteHeaderId());  | 
  |  184 |    0 |                for (Iterator iter = actionItems.iterator(); iter.hasNext();) { | 
  |  185 |    0 |                        ActionItem actionItem = (ActionItem) iter.next();  | 
  |  186 |     | 
                       try { | 
  |  187 |    0 |                                KEWServiceLocator.getUserOptionsService().saveRefreshUserOption(actionItem.getPrincipalId());  | 
  |  188 |    0 |                        } catch (Exception e) { | 
  |  189 |    0 |                                LOG.error("error saving refreshUserOption", e); | 
  |  190 |    0 |                        }  | 
  |  191 |    0 |                }  | 
  |  192 |     | 
                 | 
  |  193 |    0 |                DocumentRouteHeaderValue attachedRouteHeader = findRouteHeader(routeHeader.getRouteHeaderId());  | 
  |  194 |    0 |                entityManager.remove(attachedRouteHeader);  | 
  |  195 |    0 |        }  | 
  |  196 |     | 
     | 
  |  197 |     | 
       public Long getNextRouteHeaderId() { | 
  |  198 |    0 |            return getPlatform().getNextValSQL("KREW_DOC_HDR_S", entityManager); | 
  |  199 |     | 
       }  | 
  |  200 |     | 
     | 
  |  201 |     | 
       protected DatabasePlatform getPlatform() { | 
  |  202 |    0 |                return (DatabasePlatform) GlobalResourceLoader.getService(RiceConstants.DB_PLATFORM);  | 
  |  203 |     | 
       }  | 
  |  204 |     | 
     | 
  |  205 |     | 
       public Collection findPendingByResponsibilityIds(Set responsibilityIds) { | 
  |  206 |     | 
     | 
  |  207 |    0 |            if (responsibilityIds.isEmpty()) { | 
  |  208 |    0 |                return new ArrayList();  | 
  |  209 |     | 
           }  | 
  |  210 |     | 
     | 
  |  211 |    0 |            String respIds = "("; | 
  |  212 |    0 |            int index = 0;  | 
  |  213 |    0 |            for (Iterator iterator = responsibilityIds.iterator(); iterator.hasNext(); index++) { | 
  |  214 |    0 |                Long responsibilityId = (Long) iterator.next();  | 
  |  215 |    0 |                respIds += responsibilityId + (index == responsibilityIds.size()-1 ? "" : ",");  | 
  |  216 |     | 
           }  | 
  |  217 |    0 |            respIds += ")";  | 
  |  218 |     | 
     | 
  |  219 |    0 |            String query = "SELECT DISTINCT(doc_hdr_id) FROM KREW_ACTN_RQST_T "+  | 
  |  220 |     | 
                   "WHERE (STAT_CD='" +  | 
  |  221 |     | 
                   KEWConstants.ACTION_REQUEST_INITIALIZED+  | 
  |  222 |     | 
                   "' OR STAT_CD='"+  | 
  |  223 |     | 
                   KEWConstants.ACTION_REQUEST_ACTIVATED+  | 
  |  224 |     | 
                   "') AND RSP_ID IN "+respIds;  | 
  |  225 |     | 
     | 
  |  226 |    0 |            LOG.debug("Query to find pending documents for requeue: " + query); | 
  |  227 |     | 
             | 
  |  228 |    0 |            List<Long> idList = new ArrayList<Long>();  | 
  |  229 |    0 |            for (Object tempId : entityManager.createNativeQuery(query).getResultList()) { | 
  |  230 |    0 |                    idList.add(((BigDecimal) tempId).longValueExact());  | 
  |  231 |     | 
           }  | 
  |  232 |     | 
     | 
  |  233 |    0 |            return idList;   | 
  |  234 |     | 
       }  | 
  |  235 |     | 
     | 
  |  236 |     | 
       public boolean hasSearchableAttributeValue(Long documentId, String searchableAttributeKey, String searchableAttributeValue) { | 
  |  237 |    0 |                return hasSearchableAttributeValue(documentId, searchableAttributeKey, searchableAttributeValue, "SearchableAttributeDateTimeValue.FindByKey")  | 
  |  238 |     | 
                       || hasSearchableAttributeValue(documentId, searchableAttributeKey, searchableAttributeValue, "SearchableAttributeStringValue.FindByKey")  | 
  |  239 |     | 
                       || hasSearchableAttributeValue(documentId, searchableAttributeKey, searchableAttributeValue, "SearchableAttributeLongValue.FindByKey")  | 
  |  240 |     | 
                       || hasSearchableAttributeValue(documentId, searchableAttributeKey, searchableAttributeValue, "SearchableAttributeFloatValue.FindByKey");  | 
  |  241 |     | 
       }  | 
  |  242 |     | 
         | 
  |  243 |     | 
       private boolean hasSearchableAttributeValue(Long documentId, String searchableAttributeKey, String searchableAttributeValue, String namedQuery) { | 
  |  244 |    0 |                Query query = entityManager.createNamedQuery(namedQuery);  | 
  |  245 |    0 |            query.setParameter("routeHeaderId", documentId); | 
  |  246 |    0 |            query.setParameter("searchableAttributeKey", searchableAttributeKey); | 
  |  247 |    0 |            Collection results = query.getResultList();  | 
  |  248 |    0 |            if (!results.isEmpty()) { | 
  |  249 |    0 |                for (Iterator iterator = results.iterator(); iterator.hasNext();) { | 
  |  250 |    0 |                    SearchableAttributeValue attribute = (SearchableAttributeValue) iterator.next();  | 
  |  251 |    0 |                    if (StringUtils.equals(attribute.getSearchableAttributeDisplayValue(), searchableAttributeValue)) { | 
  |  252 |    0 |                        return true;  | 
  |  253 |     | 
                   }  | 
  |  254 |    0 |                }  | 
  |  255 |     | 
           }  | 
  |  256 |    0 |            return false;              | 
  |  257 |     | 
       }  | 
  |  258 |     | 
     | 
  |  259 |     | 
       public String getServiceNamespaceByDocumentId(Long documentId) { | 
  |  260 |    0 |                if (documentId == null) { | 
  |  261 |    0 |                        throw new IllegalArgumentException("Encountered a null document ID."); | 
  |  262 |     | 
               }  | 
  |  263 |     | 
                 | 
  |  264 |    0 |                String serviceNamespace = null;  | 
  |  265 |     | 
                 | 
  |  266 |     | 
               try { | 
  |  267 |    0 |                String sql = "SELECT DT.SVC_NMSPC FROM KREW_DOC_TYP_T DT, KREW_DOC_HDR_T DH "+  | 
  |  268 |     | 
                       "WHERE DH.DOC_TYP_ID=DT.DOC_TYP_ID AND "+  | 
  |  269 |     | 
                       "DH.DOC_HDR_ID=?";  | 
  |  270 |     | 
                     | 
  |  271 |    0 |                Query query = entityManager.createNativeQuery(sql);  | 
  |  272 |    0 |                query.setParameter(1, documentId);  | 
  |  273 |     | 
                 | 
  |  274 |    0 |                serviceNamespace = (String)query.getSingleResult();  | 
  |  275 |    0 |                } catch (EntityNotFoundException enfe) { | 
  |  276 |    0 |                        throw new WorkflowRuntimeException(enfe.getMessage());  | 
  |  277 |    0 |                    }  | 
  |  278 |     | 
                 | 
  |  279 |    0 |                return serviceNamespace;  | 
  |  280 |     | 
       }  | 
  |  281 |     | 
     | 
  |  282 |     | 
       public String getDocumentStatus(Long documentId) { | 
  |  283 |    0 |                DocumentRouteHeaderValue document = findRouteHeader(documentId);  | 
  |  284 |     | 
     | 
  |  285 |    0 |                    return document.getDocRouteStatus();  | 
  |  286 |     | 
       }  | 
  |  287 |     | 
         | 
  |  288 |     | 
       public String getAppDocId(Long documentId) { | 
  |  289 |    0 |                Query query = entityManager.createNamedQuery("DocumentRouteHeaderValue.GetAppDocId"); | 
  |  290 |    0 |            query.setParameter("routeHeaderId", documentId); | 
  |  291 |    0 |            return (String) query.getSingleResult();   | 
  |  292 |     | 
             }  | 
  |  293 |     | 
         | 
  |  294 |     | 
       public void save(SearchableAttributeValue searchableAttributeValue) {            | 
  |  295 |    0 |                if (searchableAttributeValue.getSearchableAttributeValueId() == null){ | 
  |  296 |    0 |                        entityManager.persist(searchableAttributeValue);  | 
  |  297 |     | 
               } else { | 
  |  298 |    0 |                        entityManager.merge(searchableAttributeValue);  | 
  |  299 |     | 
               }  | 
  |  300 |    0 |        }  | 
  |  301 |     | 
     | 
  |  302 |     | 
           public Collection findByDocTypeAndAppId(String documentTypeName,  | 
  |  303 |     | 
                           String appId) { | 
  |  304 |     | 
               try { | 
  |  305 |    0 |                String sql =   | 
  |  306 |     | 
                            "SELECT DISTINCT " +  | 
  |  307 |     | 
                           "    (docHdr.doc_hdr_id) " +  | 
  |  308 |     | 
                           "FROM " +  | 
  |  309 |     | 
                           "    KREW_DOC_HDR_T docHdr, " +  | 
  |  310 |     | 
                           "    KREW_DOC_TYP_T docTyp " +  | 
  |  311 |     | 
                           "WHERE " +  | 
  |  312 |     | 
                           "    docHdr.APP_DOC_ID     = ? " +  | 
  |  313 |     | 
                           "    AND docHdr.DOC_TYP_ID = docTyp.DOC_TYP_ID " +  | 
  |  314 |     | 
                           "    AND docTyp.DOC_TYP_NM = ?";  | 
  |  315 |     | 
                     | 
  |  316 |    0 |                Query query = entityManager.createNativeQuery(sql);  | 
  |  317 |    0 |                query.setParameter(1, appId);  | 
  |  318 |    0 |                query.setParameter(2, documentTypeName);  | 
  |  319 |    0 |                Collection<Long> idCollection = new ArrayList<Long>();  | 
  |  320 |    0 |                for (Object tempId : query.getResultList()) { | 
  |  321 |    0 |                        idCollection.add(((BigDecimal)tempId).longValueExact());  | 
  |  322 |     | 
               }  | 
  |  323 |    0 |                return idCollection;  | 
  |  324 |    0 |                } catch (EntityNotFoundException enfe) { | 
  |  325 |    0 |                        throw new WorkflowRuntimeException(enfe.getMessage());  | 
  |  326 |     | 
                   }  | 
  |  327 |     | 
           }  | 
  |  328 |     | 
     | 
  |  329 |     | 
     | 
  |  330 |     | 
   }  |