View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.actionlist.dao.impl;
17  
18  import org.kuali.rice.kew.actionlist.dao.ActionListDAO;
19  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
20  
21  import javax.persistence.EntityGraph;
22  import javax.persistence.EntityManager;
23  import javax.persistence.TypedQuery;
24  import java.util.Arrays;
25  import java.util.List;
26  
27  /**
28   * JPA implementation of the action list DAO for functions not easily handled by the data layer.
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class ActionListDAOJpaImpl implements ActionListDAO {
33  
34      protected EntityManager entityManager;
35  
36      public void setEntityManager(EntityManager entityManager) {
37          this.entityManager = entityManager;
38      }
39  
40      /**
41       * {@inheritDoc}
42       */
43      @Override
44      public int getCount(String principalId) {
45          TypedQuery<Long> query = entityManager.createNamedQuery("ActionItem.DistinctDocumentsForPrincipalId",Long.class);
46          query.setParameter("principalId",principalId);
47          return query.getSingleResult().intValue();
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public List<Object> getMaxActionItemDateAssignedAndCountForUser(String principalId) {
55          TypedQuery<Object[]> query = (TypedQuery<Object[]>) entityManager.createNamedQuery("ActionItem.GetMaxDateAndCountForPrincipalId", (new Object[0]).getClass() );
56          query.setParameter("principalId",principalId);
57          return Arrays.asList( query.getSingleResult() );
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public DocumentRouteHeaderValue getMinimalRouteHeader(String documentId) {
65          // This graph is defined on the DocumentRouteHeaderValue class.
66          EntityGraph<DocumentRouteHeaderValue> entityGraph =
67                  (EntityGraph<DocumentRouteHeaderValue>) entityManager.createEntityGraph("DocumentRouteHeaderValue.ActionListAttributesOnly");
68          TypedQuery<DocumentRouteHeaderValue> query = entityManager.createQuery("SELECT rh FROM DocumentRouteHeaderValue rh WHERE rh.documentId = :documentId", DocumentRouteHeaderValue.class );
69          // By using the graph - all properties but those on the graph should have
70          // a lazy proxy in place.  Attempting to access any of those *should* cause the
71          // rest of the properties to load.
72          query.setHint("javax.persistence.fetchgraph", entityGraph);
73          query.setParameter("documentId", documentId);
74          List<DocumentRouteHeaderValue> result = query.getResultList();
75          if ( result.isEmpty() ) {
76              return null;
77          }
78          return result.get(0);
79      }
80  
81  }