Skip to content

Commit

Permalink
changed to use LocalDateTime instead for calculating Today minus
Browse files Browse the repository at this point in the history
a number of days for searching portlet ratings
  • Loading branch information
phillips1021 committed Dec 6, 2017
1 parent 2587721 commit 3ec0bb9
Showing 1 changed file with 66 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apereo.portal.portlet.dao.IPortletDefinitionDao;
import org.apereo.portal.portlet.marketplace.IMarketplaceRating;
import org.apereo.portal.portlet.om.IPortletDefinition;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -119,7 +120,7 @@ public IMarketplaceRating createOrUpdateRating(
temp.setMarketplaceRatingPK(tempPK);
temp.setRating(rating);
temp.setReview(review);
temp.setRatingDate( new Date() );
temp.setRatingDate(new Date());
return this.createOrUpdateRating(temp);
}

Expand Down Expand Up @@ -305,53 +306,82 @@ public void aggregateMarketplaceRating() {
}
}
}

@Override
public Set<IMarketplaceRating> getAllRatingsForPortletInLastXDays(int numberOfDaysBack, final String fname) {

final Calendar queryDate = Calendar.getInstance();
queryDate.add(Calendar.DAY_OF_YEAR, 0-numberOfDaysBack);
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 ratingDatePredicate = input.greaterThanOrEqualTo(definitionRoot.<java.util.Date>get("ratingDate"), queryDate.getTime());
Predicate conditionPortlet = input.equal(definitionRoot.get("marketplaceRatingPK").get("portletDefinition"), portletDefinitionDao.getPortletDefinitionByFname(fname));
Predicate allConditions = input.and(conditionPortlet, ratingDatePredicate);
criteriaQuery.select(definitionRoot).where(allConditions);

return criteriaQuery;
}
}));
public Set<IMarketplaceRating> getAllRatingsForPortletInLastXDays(
int numberOfDaysBack, final String fname) {

LocalDateTime todayMinusNumberOfDaysBack = LocalDateTime.now().minusDays(numberOfDaysBack);

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 ratingDatePredicate =
input.greaterThanOrEqualTo(
definitionRoot.<java.util.Date>get(
"ratingDate"),
todayMinusNumberOfDaysBack.toDate());
Predicate conditionPortlet =
input.equal(
definitionRoot
.get("marketplaceRatingPK")
.get("portletDefinition"),
portletDefinitionDao
.getPortletDefinitionByFname(
fname));
Predicate allConditions =
input.and(conditionPortlet, ratingDatePredicate);
criteriaQuery.select(definitionRoot).where(allConditions);

return criteriaQuery;
}
}));

return new HashSet<IMarketplaceRating>(query.getResultList());

}


@Override
@PortalTransactionalReadOnly
@OpenEntityManager(unitName = PERSISTENCE_UNIT_NAME)
public Set<IMarketplaceRating> getAllRatingsInLastXDays(int numberOfDaysBack) {

final Calendar queryDate = Calendar.getInstance();
queryDate.add(Calendar.DAY_OF_YEAR, 0-numberOfDaysBack);
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 ratingDatePredicate = input.greaterThanOrEqualTo(definitionRoot.<java.util.Date>get("ratingDate"), queryDate.getTime());

criteriaQuery.select(definitionRoot).where(ratingDatePredicate);

return criteriaQuery;
}
}));
queryDate.add(Calendar.DAY_OF_YEAR, 0 - numberOfDaysBack);
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 ratingDatePredicate =
input.greaterThanOrEqualTo(
definitionRoot.<java.util.Date>get(
"ratingDate"),
queryDate.getTime());

criteriaQuery
.select(definitionRoot)
.where(ratingDatePredicate);

return criteriaQuery;
}
}));

return new HashSet<IMarketplaceRating>(query.getResultList());
}


}

0 comments on commit 3ec0bb9

Please sign in to comment.