Search in sources :

Example 1 with FeedItem

use of fi.otavanopisto.muikku.plugins.feed.model.FeedItem in project muikku by otavanopisto.

the class FeedItemDAO method findByFeeds.

public List<FeedItem> findByFeeds(List<Feed> feeds, int numItems, FeedSortOrder order) {
    EntityManager entityManager = getEntityManager();
    if (feeds == null || feeds.size() == 0) {
        return Collections.emptyList();
    }
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<FeedItem> criteria = criteriaBuilder.createQuery(FeedItem.class);
    Root<FeedItem> root = criteria.from(FeedItem.class);
    criteria.select(root);
    criteria.where(root.get(FeedItem_.feed).in(feeds));
    switch(order) {
        case ASCENDING:
            criteria.orderBy(criteriaBuilder.asc(root.get(FeedItem_.publicationDate)));
            break;
        case DESCENDING:
            criteria.orderBy(criteriaBuilder.desc(root.get(FeedItem_.publicationDate)));
            break;
    }
    return entityManager.createQuery(criteria).setMaxResults(numItems).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) FeedItem(fi.otavanopisto.muikku.plugins.feed.model.FeedItem)

Example 2 with FeedItem

use of fi.otavanopisto.muikku.plugins.feed.model.FeedItem in project muikku by otavanopisto.

the class FeedRESTService method findFeedByNames.

@GET
@Path("/feeds/{NAMES}")
@RESTPermit(handling = Handling.UNSECURED)
public Response findFeedByNames(@PathParam("NAMES") String names, @QueryParam("numItems") @DefaultValue("10") int numItems, @QueryParam("order") @DefaultValue("DESCENDING") FeedSortOrder order) {
    if (StringUtils.isBlank(names)) {
        return Response.status(Status.NOT_FOUND).build();
    }
    Set<String> nameSet = Stream.of(names.split(",")).collect(Collectors.toSet());
    List<Feed> feeds = new ArrayList<>();
    for (String name : nameSet) {
        Feed feed = feedDAO.findByName(name);
        if (feed != null) {
            feeds.add(feed);
        }
    }
    List<FeedItem> feedItems = feedItemDao.findByFeeds(feeds, numItems, order);
    return Response.ok(feedItems).build();
}
Also used : FeedItem(fi.otavanopisto.muikku.plugins.feed.model.FeedItem) ArrayList(java.util.ArrayList) Feed(fi.otavanopisto.muikku.plugins.feed.model.Feed) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Aggregations

FeedItem (fi.otavanopisto.muikku.plugins.feed.model.FeedItem)2 Feed (fi.otavanopisto.muikku.plugins.feed.model.Feed)1 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)1 ArrayList (java.util.ArrayList)1 EntityManager (javax.persistence.EntityManager)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1