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