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;
}
}
}
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);
}
Aggregations