use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class AdminController method backup.
@GetMapping(value = "/export", produces = "application/zip")
public ResponseEntity<StreamingResponseBody> backup(HttpServletRequest req, HttpServletResponse response) {
Profile authUser = utils.getAuthUser(req);
if (!utils.isAdmin(authUser)) {
return new ResponseEntity<StreamingResponseBody>(HttpStatus.UNAUTHORIZED);
}
String fileName = App.identifier(Config.getConfigParam("access_key", "scoold")) + "_" + Utils.formatDate("YYYYMMdd_HHmmss", Locale.US);
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".zip");
return new ResponseEntity<StreamingResponseBody>(out -> {
// export all fields, even those which are JSON-ignored
ObjectWriter writer = JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build().writer().without(SerializationFeature.INDENT_OUTPUT).without(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
long count = 0;
int partNum = 0;
// find all objects even if there are more than 10000 users in the system
Pager pager = new Pager(1, "_docid", false, Config.MAX_ITEMS_PER_PAGE);
List<ParaObject> objects;
do {
objects = pc.findQuery("", "*", pager);
ZipEntry zipEntry = new ZipEntry(fileName + "_part" + (++partNum) + ".json");
zipOut.putNextEntry(zipEntry);
writer.writeValue(zipOut, objects);
count += objects.size();
} while (!objects.isEmpty());
logger.info("Exported {} objects to {}. Downloaded by {} (pager.count={})", count, fileName + ".zip", authUser.getCreatorid() + " " + authUser.getName(), pager.getCount());
} catch (final IOException e) {
logger.error("Failed to export data.", e);
}
}, HttpStatus.OK);
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class ApiController method createComment.
@PostMapping("/comments")
public Comment createComment(HttpServletRequest req, HttpServletResponse res) {
Map<String, Object> entity = readEntity(req);
if (entity.isEmpty()) {
badReq("Missing request body.");
}
String comment = (String) entity.get("comment");
String parentid = (String) entity.get(Config._PARENTID);
String creatorid = (String) entity.get(Config._CREATORID);
ParaObject parent = pc.read(parentid);
if (parent == null) {
badReq("Parent object not found. Provide a valid parentid.");
return null;
}
if (!StringUtils.isBlank(creatorid)) {
Profile authUser = pc.read(Profile.id(creatorid));
if (authUser != null) {
req.setAttribute(AUTH_USER_ATTRIBUTE, authUser);
}
}
Model model = new ExtendedModelMap();
commentController.createAjax(comment, parentid, req, model);
Comment created = (Comment) model.getAttribute("showComment");
if (created == null || StringUtils.isBlank(comment)) {
badReq("Failed to create comment.");
return null;
}
res.setStatus(HttpStatus.CREATED.value());
return created;
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class ScooldUtils method getComments.
// get the comments for each answer and the question
public void getComments(List<Post> allPosts) {
Map<String, List<Comment>> allComments = new HashMap<String, List<Comment>>();
List<String> allCommentIds = new ArrayList<String>();
List<Post> forUpdate = new ArrayList<Post>(allPosts.size());
// get the comment ids of the first 5 comments for each post
for (Post post : allPosts) {
// not set => read comments if any and embed ids in post object
if (post.getCommentIds() == null) {
forUpdate.add(reloadFirstPageOfComments(post));
allComments.put(post.getId(), post.getComments());
} else {
// ids are set => add them to list for bulk read
allCommentIds.addAll(post.getCommentIds());
}
}
if (!allCommentIds.isEmpty()) {
// read all comments for all posts on page in bulk
for (ParaObject comment : pc.readAll(allCommentIds)) {
List<Comment> postComments = allComments.get(comment.getParentid());
if (postComments == null) {
allComments.put(comment.getParentid(), new ArrayList<Comment>());
}
allComments.get(comment.getParentid()).add((Comment) comment);
}
}
// embed comments in each post for use within the view
for (Post post : allPosts) {
List<Comment> cl = allComments.get(post.getId());
long clSize = (cl == null) ? 0 : cl.size();
if (post.getCommentIds().size() != clSize) {
forUpdate.add(reloadFirstPageOfComments(post));
clSize = post.getComments().size();
} else {
post.setComments(cl);
if (clSize == post.getItemcount().getLimit() && pc.getCount(Utils.type(Comment.class), Collections.singletonMap("parentid", post.getId())) > clSize) {
// hack to show the "more" button
clSize++;
}
}
post.getItemcount().setCount(clSize);
}
if (!forUpdate.isEmpty()) {
pc.updateAll(allPosts);
}
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class VoteController method processVoteRequest.
boolean processVoteRequest(boolean isUpvote, ParaObject votable, HttpServletRequest req) {
Profile author = null;
Profile authUser = utils.getAuthUser(req);
boolean result = false;
boolean update = false;
if (votable == null || authUser == null) {
return false;
}
try {
List<ParaObject> voteObjects = pc.readAll(Arrays.asList(votable.getCreatorid(), new Vote(authUser.getId(), votable.getId(), Votable.VoteValue.UP).getId(), new Vote(authUser.getId(), votable.getId(), Votable.VoteValue.DOWN).getId()));
author = (Profile) voteObjects.stream().filter((p) -> p instanceof Profile).findFirst().orElse(null);
Integer votes = votable.getVotes() != null ? votable.getVotes() : 0;
boolean upvoteExists = voteObjects.stream().anyMatch((v) -> v instanceof Vote && ((Vote) v).isUpvote());
boolean downvoteExists = voteObjects.stream().anyMatch((v) -> v instanceof Vote && ((Vote) v).isDownvote());
boolean isVoteCorrection = (isUpvote && downvoteExists) || (!isUpvote && upvoteExists);
if (isUpvote && voteUp(votable, authUser.getId())) {
votes++;
result = true;
update = updateReputationOnUpvote(votable, votes, authUser, author, isVoteCorrection);
} else if (!isUpvote && voteDown(votable, authUser.getId())) {
votes--;
result = true;
hideCommentAndReport(votable, votes, votable.getId(), req);
update = updateReputationOnDownvote(votable, votes, authUser, author, isVoteCorrection);
}
} catch (Exception ex) {
logger.error(null, ex);
result = false;
}
utils.addBadgeOnce(authUser, SUPPORTER, authUser.getUpvotes() >= SUPPORTER_IFHAS);
utils.addBadgeOnce(authUser, CRITIC, authUser.getDownvotes() >= CRITIC_IFHAS);
utils.addBadgeOnce(authUser, VOTER, authUser.getTotalVotes() >= VOTER_IFHAS);
if (update) {
pc.updateAll(Arrays.asList(author, authUser));
}
return result;
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class PeopleController method get.
@GetMapping(path = { "", "/bulk-edit" })
public String get(@RequestParam(required = false, defaultValue = Config._TIMESTAMP) String sortby, @RequestParam(required = false, defaultValue = "*") String q, HttpServletRequest req, Model model) {
if (!utils.isDefaultSpacePublic() && !utils.isAuthenticated(req)) {
return "redirect:" + SIGNINLINK + "?returnto=" + PEOPLELINK;
}
if (req.getRequestURI().endsWith("/bulk-edit")) {
return "redirect:" + PEOPLELINK + "?bulk-edit=true";
}
Profile authUser = utils.getAuthUser(req);
Pager itemcount = utils.getPager("page", req);
itemcount.setSortby(sortby);
// [space query filter] + original query string
String qs = utils.sanitizeQueryString(q, req);
if (req.getParameter("bulkedit") != null && utils.isAdmin(authUser)) {
qs = q;
} else {
qs = qs.replaceAll("properties\\.space:", "properties.spaces:");
}
if (!qs.endsWith("*")) {
// admins are members of every space and always visible
qs += " OR properties.groups:(admins)";
}
List<Profile> userlist = pc.findQuery(Utils.type(Profile.class), qs, itemcount);
model.addAttribute("path", "people.vm");
model.addAttribute("title", utils.getLang(req).get("people.title"));
model.addAttribute("peopleSelected", "navbtn-hover");
model.addAttribute("itemcount", itemcount);
model.addAttribute("userlist", userlist);
if (req.getParameter("bulkedit") != null && utils.isAdmin(authUser)) {
List<ParaObject> spaces = pc.findQuery("scooldspace", "*", new Pager(Config.DEFAULT_LIMIT));
model.addAttribute("spaces", spaces);
}
return "base";
}
Aggregations