Search in sources :

Example 6 with OpenEntityManager

use of org.apereo.portal.jpa.OpenEntityManager in project uPortal by Jasig.

the class JpaMarketplaceRatingDao method getRating.

/**
 * @param marketplaceRatingPK the primary key of the entity you want
 * @return an attached entity if found, null otherwise
 */
@PortalTransactionalReadOnly
@OpenEntityManager(unitName = PERSISTENCE_UNIT_NAME)
public IMarketplaceRating getRating(MarketplaceRatingPK marketplaceRatingPK) {
    final MarketplaceRatingPK tempRatingPK = marketplaceRatingPK;
    MarketplaceRatingImpl temp = new MarketplaceRatingImpl();
    temp.setMarketplaceRatingPK(marketplaceRatingPK);
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(temp)) {
        temp = entityManager.merge(temp);
        return temp;
    } else {
        final TypedQuery<MarketplaceRatingImpl> query = this.createQuery(this.createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<MarketplaceRatingImpl>>() {

            @Override
            public CriteriaQuery<MarketplaceRatingImpl> apply(CriteriaBuilder input) {
                final CriteriaQuery<MarketplaceRatingImpl> criteriaQuery = input.createQuery(MarketplaceRatingImpl.class);
                final Root<MarketplaceRatingImpl> definitionRoot = criteriaQuery.from(MarketplaceRatingImpl.class);
                Predicate conditionUser = input.equal(definitionRoot.get("marketplaceRatingPK").get("userName"), tempRatingPK.getUserName());
                Predicate conditionPortlet = input.equal(definitionRoot.get("marketplaceRatingPK").get("portletDefinition"), tempRatingPK.getPortletDefinition());
                Predicate allConditions = input.and(conditionPortlet, conditionUser);
                criteriaQuery.where(allConditions);
                return criteriaQuery;
            }
        }));
        List<MarketplaceRatingImpl> results = query.getResultList();
        if (!results.isEmpty()) {
            return results.get(0);
        } else {
            return null;
        }
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Function(com.google.common.base.Function) EntityManager(javax.persistence.EntityManager) OpenEntityManager(org.apereo.portal.jpa.OpenEntityManager) Predicate(javax.persistence.criteria.Predicate) OpenEntityManager(org.apereo.portal.jpa.OpenEntityManager)

Example 7 with OpenEntityManager

use of org.apereo.portal.jpa.OpenEntityManager in project uPortal by Jasig.

the class JpaMarketplaceRatingDao method getRatingsByFname.

/**
 * @since 5.0
 * @return Set of ratings per portlet definition
 */
@Override
@PortalTransactionalReadOnly
@OpenEntityManager(unitName = PERSISTENCE_UNIT_NAME)
public Set<IMarketplaceRating> getRatingsByFname(String fname) {
    // Build criteria to fetch MarketplaceRatingImpl based on the incoming portlet name.
    final EntityManager entityManager = this.getEntityManager();
    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    final CriteriaQuery<IMarketplaceRating> getByPortlet = cb.createQuery(IMarketplaceRating.class);
    final Root<MarketplaceRatingImpl> imr = getByPortlet.from(MarketplaceRatingImpl.class);
    getByPortlet.select(imr);
    // Define the path to the portlet fName
    final Path<MarketplaceRatingPK> mrPK = imr.get("marketplaceRatingPK");
    final Path<PortletDefinitionImpl> mrIPD = mrPK.get("portletDefinition");
    final ParameterExpression<String> portletFName = cb.parameter(String.class, "portletFName");
    getByPortlet.where(cb.equal(mrIPD.get("fname"), portletFName));
    TypedQuery<IMarketplaceRating> tq = entityManager.createQuery(getByPortlet);
    tq.setParameter("portletFName", fname);
    List<IMarketplaceRating> resultList = tq.getResultList();
    return new HashSet<>(resultList);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) OpenEntityManager(org.apereo.portal.jpa.OpenEntityManager) IMarketplaceRating(org.apereo.portal.portlet.marketplace.IMarketplaceRating) HashSet(java.util.HashSet) OpenEntityManager(org.apereo.portal.jpa.OpenEntityManager)

Aggregations

OpenEntityManager (org.apereo.portal.jpa.OpenEntityManager)7 HashSet (java.util.HashSet)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)2 DialectAwareTransactional (org.apereo.portal.spring.tx.DialectAwareTransactional)2 CacheKey (org.apereo.portal.utils.cache.CacheKey)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 Function (com.google.common.base.Function)1 Predicate (javax.persistence.criteria.Predicate)1 CompositeEntityIdentifier (org.apereo.portal.groups.CompositeEntityIdentifier)1 IEntityGroup (org.apereo.portal.groups.IEntityGroup)1 IMarketplaceRating (org.apereo.portal.portlet.marketplace.IMarketplaceRating)1 IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)1