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.actionlist.dao.impl;
017
018import org.kuali.rice.kew.actionlist.dao.ActionListDAO;
019import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
020
021import javax.persistence.EntityGraph;
022import javax.persistence.EntityManager;
023import javax.persistence.TypedQuery;
024import java.util.Arrays;
025import java.util.List;
026
027/**
028 * JPA implementation of the action list DAO for functions not easily handled by the data layer.
029 *
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class ActionListDAOJpaImpl implements ActionListDAO {
033
034    protected EntityManager entityManager;
035
036    public void setEntityManager(EntityManager entityManager) {
037        this.entityManager = entityManager;
038    }
039
040    /**
041     * {@inheritDoc}
042     */
043    @Override
044    public int getCount(String principalId) {
045        TypedQuery<Long> query = entityManager.createNamedQuery("ActionItem.DistinctDocumentsForPrincipalId",Long.class);
046        query.setParameter("principalId",principalId);
047        return query.getSingleResult().intValue();
048    }
049
050    /**
051     * {@inheritDoc}
052     */
053    @Override
054    public List<Object> getMaxActionItemDateAssignedAndCountForUser(String principalId) {
055        TypedQuery<Object[]> query = (TypedQuery<Object[]>) entityManager.createNamedQuery("ActionItem.GetMaxDateAndCountForPrincipalId", (new Object[0]).getClass() );
056        query.setParameter("principalId",principalId);
057        return Arrays.asList( query.getSingleResult() );
058    }
059
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    public DocumentRouteHeaderValue getMinimalRouteHeader(String documentId) {
065        // This graph is defined on the DocumentRouteHeaderValue class.
066        EntityGraph<DocumentRouteHeaderValue> entityGraph =
067                (EntityGraph<DocumentRouteHeaderValue>) entityManager.createEntityGraph("DocumentRouteHeaderValue.ActionListAttributesOnly");
068        TypedQuery<DocumentRouteHeaderValue> query = entityManager.createQuery("SELECT rh FROM DocumentRouteHeaderValue rh WHERE rh.documentId = :documentId", DocumentRouteHeaderValue.class );
069        // By using the graph - all properties but those on the graph should have
070        // a lazy proxy in place.  Attempting to access any of those *should* cause the
071        // rest of the properties to load.
072        query.setHint("javax.persistence.fetchgraph", entityGraph);
073        query.setParameter("documentId", documentId);
074        List<DocumentRouteHeaderValue> result = query.getResultList();
075        if ( result.isEmpty() ) {
076            return null;
077        }
078        return result.get(0);
079    }
080
081}