Search in sources :

Example 1 with Category

use of org.ambraproject.rhino.model.Category in project rhino by PLOS.

the class TaxonomyServiceImpl method getArticleCategoryAssignmentFlags.

private List<ArticleCategoryAssignmentFlag> getArticleCategoryAssignmentFlags(Article article, Category category, Optional<Long> userProfileId) {
    return hibernateTemplate.execute(session -> {
        Query query = userProfileId.map(userProfileIdValue -> session.createQuery("" + "FROM ArticleCategoryAssignmentFlag " + "WHERE article = :article AND category = :category AND userProfileId = :userProfileId").setParameter("userProfileId", userProfileIdValue)).orElseGet(() -> session.createQuery("" + "FROM ArticleCategoryAssignmentFlag " + "WHERE article = :article AND category = :category AND userProfileId IS NULL"));
        query.setParameter("article", article);
        query.setParameter("category", category);
        return (List<ArticleCategoryAssignmentFlag>) query.list();
    });
}
Also used : AmbraService(org.ambraproject.rhino.service.impl.AmbraService) Article(org.ambraproject.rhino.model.Article) Collection(java.util.Collection) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) TaxonomyClassificationService(org.ambraproject.rhino.service.taxonomy.TaxonomyClassificationService) ArticleCategoryAssignment(org.ambraproject.rhino.model.ArticleCategoryAssignment) WeightedTerm(org.ambraproject.rhino.service.taxonomy.WeightedTerm) ArticleCategoryAssignmentFlag(org.ambraproject.rhino.model.ArticleCategoryAssignmentFlag) List(java.util.List) ArticleRevision(org.ambraproject.rhino.model.ArticleRevision) Category(org.ambraproject.rhino.model.Category) Document(org.w3c.dom.Document) LocalDate(java.time.LocalDate) TaxonomyService(org.ambraproject.rhino.service.taxonomy.TaxonomyService) Query(org.hibernate.Query) Optional(java.util.Optional) Query(org.hibernate.Query) List(java.util.List)

Example 2 with Category

use of org.ambraproject.rhino.model.Category in project rhino by PLOS.

the class ArticleCrudController method flagArticleCategory.

@Transactional(rollbackFor = { Throwable.class })
@RequestMapping(value = "/articles/{doi}/categories", params = { "flag" }, method = RequestMethod.POST)
@ResponseBody
public Map<String, String> flagArticleCategory(@PathVariable("doi") String articleDoi, @RequestParam(value = "categoryTerm", required = true) String categoryTerm, @RequestParam(value = "userId", required = false) String userId, @RequestParam(value = "flag", required = true) String action) throws IOException {
    ArticleIdentifier articleId = ArticleIdentifier.create(DoiEscaping.unescape(articleDoi));
    Article article = articleCrudService.readArticle(articleId);
    Optional<Long> userIdObj = Optional.ofNullable(userId).map(Long::parseLong);
    Collection<Category> categories = taxonomyService.getArticleCategoriesWithTerm(article, categoryTerm);
    switch(action) {
        case "add":
            for (Category category : categories) {
                taxonomyService.flagArticleCategory(article, category, userIdObj);
            }
            break;
        case "remove":
            for (Category category : categories) {
                taxonomyService.deflagArticleCategory(article, category, userIdObj);
            }
            break;
        default:
            throw new RestClientException("action must be 'add' or 'remove'", HttpStatus.BAD_REQUEST);
    }
    // ajax call expects returned data so provide an empty map for the body
    return ImmutableMap.of();
}
Also used : ArticleIdentifier(org.ambraproject.rhino.identity.ArticleIdentifier) Category(org.ambraproject.rhino.model.Category) Article(org.ambraproject.rhino.model.Article) RestClientException(org.ambraproject.rhino.rest.RestClientException) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with Category

use of org.ambraproject.rhino.model.Category in project rhino by PLOS.

the class TaxonomyClassificationServiceImpl method persistCategories.

private void persistCategories(List<WeightedTerm> terms, Article article) {
    Set<String> termStrings = terms.stream().map(WeightedTerm::getPath).collect(Collectors.toSet());
    Collection<Category> existingCategories = hibernateTemplate.execute(session -> {
        Query query = session.createQuery("FROM Category WHERE path IN (:terms)");
        query.setParameterList("terms", termStrings);
        return (Collection<Category>) query.list();
    });
    Map<String, Category> existingCategoryMap = Maps.uniqueIndex(existingCategories, Category::getPath);
    Collection<ArticleCategoryAssignment> existingAssignments = getAssignmentsForArticle(article);
    Map<Category, ArticleCategoryAssignment> assignmentMap = Maps.uniqueIndex(existingAssignments, ArticleCategoryAssignment::getCategory);
    // Make it mutable. We will remove assignments as they are updated.
    assignmentMap = new HashMap<>(assignmentMap);
    for (WeightedTerm term : terms) {
        Category category = existingCategoryMap.get(term.getPath());
        if (category == null) {
            /*
         * A new category from the taxonomy server, which is not yet persisted in our system. Create it now.
         *
         * This risks a race condition if two articles are being populated concurrently and both have the same new
         * category, which can cause a "MySQLIntegrityConstraintViolationException: Duplicate entry" error.
         */
            category = new Category();
            category.setPath(term.getPath());
            hibernateTemplate.save(category);
        }
        ArticleCategoryAssignment assignment = assignmentMap.remove(category);
        if (assignment == null) {
            hibernateTemplate.save(new ArticleCategoryAssignment(category, article, term.getWeight()));
        } else {
            assignment.setWeight(term.getWeight());
            hibernateTemplate.update(assignment);
        }
    }
    // Each assignment that was not removed from assignmentMap is not among the new terms, so it should be deleted.
    assignmentMap.values().forEach(hibernateTemplate::delete);
}
Also used : ArticleCategoryAssignment(org.ambraproject.rhino.model.ArticleCategoryAssignment) Category(org.ambraproject.rhino.model.Category) Query(org.hibernate.Query) WeightedTerm(org.ambraproject.rhino.service.taxonomy.WeightedTerm) Collection(java.util.Collection)

Aggregations

Category (org.ambraproject.rhino.model.Category)3 Collection (java.util.Collection)2 Article (org.ambraproject.rhino.model.Article)2 ArticleCategoryAssignment (org.ambraproject.rhino.model.ArticleCategoryAssignment)2 WeightedTerm (org.ambraproject.rhino.service.taxonomy.WeightedTerm)2 Query (org.hibernate.Query)2 IOException (java.io.IOException)1 LocalDate (java.time.LocalDate)1 List (java.util.List)1 Optional (java.util.Optional)1 ArticleIdentifier (org.ambraproject.rhino.identity.ArticleIdentifier)1 ArticleCategoryAssignmentFlag (org.ambraproject.rhino.model.ArticleCategoryAssignmentFlag)1 ArticleRevision (org.ambraproject.rhino.model.ArticleRevision)1 RestClientException (org.ambraproject.rhino.rest.RestClientException)1 AmbraService (org.ambraproject.rhino.service.impl.AmbraService)1 TaxonomyClassificationService (org.ambraproject.rhino.service.taxonomy.TaxonomyClassificationService)1 TaxonomyService (org.ambraproject.rhino.service.taxonomy.TaxonomyService)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Transactional (org.springframework.transaction.annotation.Transactional)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1