use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class AdminController method forceDelete.
@PostMapping
public String forceDelete(@RequestParam Boolean confirmdelete, @RequestParam String id, HttpServletRequest req) {
Profile authUser = utils.getAuthUser(req);
if (confirmdelete && utils.isAdmin(authUser)) {
ParaObject object = pc.read(id);
if (object != null) {
object.delete();
logger.info("{} #{} deleted {} #{}", authUser.getName(), authUser.getId(), object.getClass().getName(), object.getId());
}
}
return "redirect:" + Optional.ofNullable(req.getParameter("returnto")).orElse(ADMINLINK);
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class AdminController method importFromSOArchive.
private List<ParaObject> importFromSOArchive(ZipInputStream zipIn, ZipEntry zipEntry, ObjectReader mapReader, Map<String, String> comments2authors) throws IOException, ParseException {
if (zipEntry.getName().endsWith(".json")) {
List<Map<String, Object>> objs = mapReader.readValue(new FilterInputStream(zipIn) {
public void close() throws IOException {
zipIn.closeEntry();
}
});
List<ParaObject> toImport = new LinkedList<>();
switch(zipEntry.getName()) {
case "posts.json":
importPostsFromSO(objs, toImport);
break;
case "tags.json":
importTagsFromSO(objs, toImport);
break;
case "comments.json":
importCommentsFromSO(objs, toImport, comments2authors);
break;
case "users.json":
importUsersFromSO(objs, toImport);
break;
case "users2badges.json":
// nice to have...
break;
case "accounts.json":
importAccountsFromSO(objs);
break;
default:
break;
}
// IN PRO: rewrite all image links to relative local URLs
return toImport;
} else {
// IN PRO: store files in ./uploads
return Collections.emptyList();
}
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class AdminController method restore.
@PostMapping("/import")
public String restore(@RequestParam("file") MultipartFile file, @RequestParam(required = false, defaultValue = "false") Boolean isso, HttpServletRequest req, HttpServletResponse res) {
Profile authUser = utils.getAuthUser(req);
if (!utils.isAdmin(authUser)) {
res.setStatus(403);
return null;
}
ObjectReader reader = ParaObjectUtils.getJsonMapper().readerFor(new TypeReference<List<Map<String, Object>>>() {
});
Map<String, String> comments2authors = new LinkedHashMap<>();
int count = 0;
int importBatchSize = Config.getConfigInt("import_batch_size", 100);
String filename = file.getOriginalFilename();
Sysprop s = new Sysprop();
s.setType("scooldimport");
try (InputStream inputStream = file.getInputStream()) {
if (StringUtils.endsWithIgnoreCase(filename, ".zip")) {
try (ZipInputStream zipIn = new ZipInputStream(inputStream)) {
ZipEntry zipEntry;
List<ParaObject> toCreate = new LinkedList<ParaObject>();
while ((zipEntry = zipIn.getNextEntry()) != null) {
if (isso) {
count += importFromSOArchive(zipIn, zipEntry, reader, comments2authors).size();
} else if (zipEntry.getName().endsWith(".json")) {
List<Map<String, Object>> objects = reader.readValue(new FilterInputStream(zipIn) {
public void close() throws IOException {
zipIn.closeEntry();
}
});
objects.forEach(o -> toCreate.add(ParaObjectUtils.setAnnotatedFields(o)));
if (toCreate.size() >= importBatchSize) {
pc.createAll(toCreate);
toCreate.clear();
}
count += objects.size();
} else {
logger.error("Expected JSON but found unknown file type to import: {}", zipEntry.getName());
}
}
if (!toCreate.isEmpty()) {
pc.createAll(toCreate);
}
if (isso) {
updateSOCommentAuthors(comments2authors);
}
}
} else if (StringUtils.endsWithIgnoreCase(filename, ".json")) {
List<Map<String, Object>> objects = reader.readValue(inputStream);
List<ParaObject> toCreate = new LinkedList<ParaObject>();
objects.forEach(o -> toCreate.add(ParaObjectUtils.setAnnotatedFields(o)));
count = objects.size();
pc.createAll(toCreate);
}
s.setCreatorid(authUser.getCreatorid());
s.setName(authUser.getName());
s.addProperty("count", count);
s.addProperty("file", filename);
logger.info("Imported {} objects to {}. Executed by {}", count, Config.getConfigParam("access_key", "scoold"), authUser.getCreatorid() + " " + authUser.getName());
if (count > 0) {
pc.create(s);
}
} catch (Exception e) {
logger.error("Failed to import " + filename, e);
return "redirect:" + ADMINLINK + "?error=true&imported=" + count;
}
return "redirect:" + ADMINLINK + "?success=true&imported=" + count;
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class VoteController method processVoteRequest.
boolean processVoteRequest(boolean isUpvote, String type, String id, HttpServletRequest req) {
if (StringUtils.isBlank(id) || StringUtils.isBlank(type)) {
return false;
}
ParaObject votable = pc.read(id);
Profile author = null;
Profile authUser = utils.getAuthUser(req);
boolean result = false;
boolean updateAuthUser = false;
boolean updateVoter = false;
if (votable == null || authUser == null) {
return false;
}
try {
author = pc.read(votable.getCreatorid());
Integer votes = votable.getVotes() != null ? votable.getVotes() : 0;
if (isUpvote && (result = pc.voteUp(votable, authUser.getId()))) {
votes++;
authUser.incrementUpvotes();
updateAuthUser = true;
int reward;
if (votable instanceof Post) {
Post p = (Post) votable;
if (p.isReply()) {
utils.addBadge(author, GOODANSWER, votes >= GOODANSWER_IFHAS, false);
reward = ANSWER_VOTEUP_REWARD_AUTHOR;
} else if (p.isQuestion()) {
utils.addBadge(author, GOODQUESTION, votes >= GOODQUESTION_IFHAS, false);
reward = QUESTION_VOTEUP_REWARD_AUTHOR;
} else {
reward = VOTEUP_REWARD_AUTHOR;
}
} else {
reward = VOTEUP_REWARD_AUTHOR;
}
if (author != null && reward > 0) {
author.addRep(reward);
updateVoter = true;
}
} else if (!isUpvote && (result = pc.voteDown(votable, authUser.getId()))) {
votes--;
authUser.incrementDownvotes();
updateAuthUser = true;
if (votable instanceof Comment && votes <= -5) {
//treat comment as offensive or spam - hide
((Comment) votable).setHidden(true);
} else if (votable instanceof Post && votes <= -5) {
Post p = (Post) votable;
//mark post for closing
Report rep = new Report();
rep.setParentid(id);
rep.setLink(p.getPostLink(false, false));
rep.setDescription(utils.getLang(req).get("posts.forclosing"));
rep.setSubType(Report.ReportType.OTHER);
rep.setAuthorName("System");
rep.create();
}
if (author != null) {
author.removeRep(POST_VOTEDOWN_PENALTY_AUTHOR);
updateVoter = true;
//small penalty to voter
authUser.removeRep(POST_VOTEDOWN_PENALTY_VOTER);
}
}
} catch (Exception ex) {
logger.error(null, ex);
}
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 (updateVoter) {
pc.update(author);
}
if (updateAuthUser) {
pc.update(authUser);
}
if (updateAuthUser && updateVoter) {
pc.updateAll(Arrays.asList(author, authUser));
}
return result;
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class AdminController method importCommentsFromSO.
private void importCommentsFromSO(List<Map<String, Object>> objs, List<ParaObject> toImport, Map<String, String> comments2authors) throws ParseException {
logger.info("Importing {} comments...", objs.size());
for (Map<String, Object> obj : objs) {
Comment c = new Comment();
c.setId("comment_" + (Integer) obj.get("id"));
c.setComment((String) obj.get("text"));
Integer parentId = (Integer) obj.getOrDefault("postId", null);
c.setParentid(parentId != null ? "post_" + parentId : null);
c.setTimestamp(DateUtils.parseDate((String) obj.get("creationDate"), soDateFormat1, soDateFormat2).getTime());
Integer creatorId = (Integer) obj.getOrDefault("userId", null);
String userid = "user_" + creatorId;
c.setCreatorid(creatorId != null ? Profile.id(userid) : utils.getSystemUser().getId());
comments2authors.put(c.getId(), userid);
toImport.add(c);
}
pc.createAll(toImport);
}
Aggregations