@POST @Path("/modify") @ApiOperation(value = "Rename a category", notes = "Rename an existing feed category") public Response modifyCategory(@ApiParam(required = true) CategoryModificationRequest req) { Preconditions.checkNotNull(req); Preconditions.checkNotNull(req.getId()); FeedCategory category = feedCategoryDAO.findById(getUser(), req.getId()); if (StringUtils.isNotBlank(req.getName())) { category.setName(req.getName()); } FeedCategory parent = null; if (req.getParentId() != null && !CategoryREST.ALL.equals(req.getParentId()) && !StringUtils.equals(req.getParentId(), String.valueOf(req.getId()))) { parent = feedCategoryDAO.findById(getUser(), Long.valueOf(req.getParentId())); } category.setParent(parent); if (req.getPosition() != null) { List<FeedCategory> categories = feedCategoryDAO.findByParent(getUser(), parent); Collections.sort( categories, new Comparator<FeedCategory>() { @Override public int compare(FeedCategory o1, FeedCategory o2) { return ObjectUtils.compare(o1.getPosition(), o2.getPosition()); } }); int existingIndex = -1; for (int i = 0; i < categories.size(); i++) { if (ObjectUtils.equals(categories.get(i).getId(), category.getId())) { existingIndex = i; } } if (existingIndex != -1) { categories.remove(existingIndex); } categories.add(Math.min(req.getPosition(), categories.size()), category); for (int i = 0; i < categories.size(); i++) { categories.get(i).setPosition(i); } feedCategoryDAO.saveOrUpdate(categories); } else { feedCategoryDAO.saveOrUpdate(category); } feedCategoryDAO.saveOrUpdate(category); cache.invalidateUserRootCategory(getUser()); return Response.ok(Status.OK).build(); }
@POST @Path("/delete") @ApiOperation(value = "Delete a category", notes = "Delete an existing feed category") public Response deleteCategory(@ApiParam(required = true) IDRequest req) { Preconditions.checkNotNull(req); Preconditions.checkNotNull(req.getId()); FeedCategory cat = feedCategoryDAO.findById(getUser(), req.getId()); if (cat != null) { List<FeedSubscription> subs = feedSubscriptionDAO.findByCategory(getUser(), cat); for (FeedSubscription sub : subs) { sub.setCategory(null); } feedSubscriptionDAO.saveOrUpdate(subs); List<FeedCategory> categories = feedCategoryDAO.findAllChildrenCategories(getUser(), cat); for (FeedCategory child : categories) { if (!child.getId().equals(cat.getId()) && child.getParent().getId().equals(cat.getId())) { child.setParent(null); } } feedCategoryDAO.saveOrUpdate(categories); feedCategoryDAO.delete(cat); cache.invalidateUserRootCategory(getUser()); return Response.ok().build(); } else { return Response.status(Status.NOT_FOUND).build(); } }
@POST @Path("/collapse") @ApiOperation( value = "Collapse a category", notes = "Save collapsed or expanded status for a category") public Response collapse(@ApiParam(required = true) CollapseRequest req) { Preconditions.checkNotNull(req); Preconditions.checkNotNull(req.getId()); FeedCategory category = feedCategoryDAO.findById(getUser(), Long.valueOf(req.getId())); if (category == null) { return Response.status(Status.NOT_FOUND).build(); } category.setCollapsed(req.isCollapse()); feedCategoryDAO.saveOrUpdate(category); cache.invalidateUserRootCategory(getUser()); return Response.ok(Status.OK).build(); }
@Path("/add") @POST @ApiOperation( value = "Add a category", notes = "Add a new feed category", responseClass = "java.lang.Long") public Response addCategory(@ApiParam(required = true) AddCategoryRequest req) { Preconditions.checkNotNull(req); Preconditions.checkNotNull(req.getName()); FeedCategory cat = new FeedCategory(); cat.setName(req.getName()); cat.setUser(getUser()); cat.setPosition(0); String parentId = req.getParentId(); if (parentId != null && !ALL.equals(parentId)) { FeedCategory parent = new FeedCategory(); parent.setId(Long.valueOf(parentId)); cat.setParent(parent); } feedCategoryDAO.saveOrUpdate(cat); cache.invalidateUserRootCategory(getUser()); return Response.ok(cat.getId()).build(); }