use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class QuestionController method getComments.
//get the comments for each answer and the question
private 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());
int clSize = (cl == null) ? 0 : cl.size();
if (post.getCommentIds().size() != clSize) {
logger.info("OPAA neshto stava.. {} {}", post.getCommentIds().size(), clSize);
forUpdate.add(reloadFirstPageOfComments(post));
clSize = post.getComments().size();
} else {
post.setComments(cl);
}
// hack to show the "more" button
post.getItemcount().setCount(clSize + 1);
}
if (!forUpdate.isEmpty()) {
pc.updateAll(allPosts);
}
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class ScooldUtils method fullQuestionsSearch.
public List<Post> fullQuestionsSearch(String query, Pager... pager) {
String typeFilter = Config._TYPE + ":(" + String.join(" OR ", Utils.type(Question.class), Utils.type(Reply.class), Utils.type(Comment.class)) + ")";
String qs = StringUtils.isBlank(query) || query.startsWith("*") ? typeFilter : query + " AND " + typeFilter;
List<ParaObject> mixedResults = pc.findQuery("", qs, pager);
Predicate<ParaObject> isQuestion = obj -> obj.getType().equals(Utils.type(Question.class));
Map<String, ParaObject> idsToQuestions = new HashMap<>(mixedResults.stream().filter(isQuestion).collect(Collectors.toMap(q -> q.getId(), q -> q)));
Set<String> toRead = new LinkedHashSet<>();
mixedResults.stream().filter(isQuestion.negate()).forEach(obj -> {
if (!idsToQuestions.containsKey(obj.getParentid())) {
toRead.add(obj.getParentid());
}
});
// find all parent posts but this excludes parents of parents - i.e. won't work for comments in answers
List<Post> parentPostsLevel1 = pc.readAll(new ArrayList<>(toRead));
parentPostsLevel1.stream().filter(isQuestion).forEach(q -> idsToQuestions.put(q.getId(), q));
toRead.clear();
// read parents of parents if any
parentPostsLevel1.stream().filter(isQuestion.negate()).forEach(obj -> {
if (!idsToQuestions.containsKey(obj.getParentid())) {
toRead.add(obj.getParentid());
}
});
List<Post> parentPostsLevel2 = pc.readAll(new ArrayList<>(toRead));
parentPostsLevel2.stream().forEach(q -> idsToQuestions.put(q.getId(), q));
ArrayList<Post> results = new ArrayList<Post>(idsToQuestions.size());
for (ParaObject result : idsToQuestions.values()) {
if (result instanceof Post) {
results.add((Post) result);
}
}
return results;
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class AdminController method importPostsFromSO.
private void importPostsFromSO(List<Map<String, Object>> objs, List<ParaObject> toImport) throws ParseException {
logger.info("Importing {} posts...", objs.size());
for (Map<String, Object> obj : objs) {
Post p;
if ("question".equalsIgnoreCase((String) obj.get("postType"))) {
p = new Question();
p.setTitle((String) obj.get("title"));
String t = StringUtils.stripStart(StringUtils.stripEnd((String) obj.getOrDefault("tags", ""), "|"), "|");
p.setTags(Arrays.asList(t.split("\\|")));
p.setAnswercount(((Integer) obj.getOrDefault("answerCount", 0)).longValue());
Integer answerId = (Integer) obj.getOrDefault("acceptedAnswerId", null);
p.setAnswerid(answerId != null ? "post_" + answerId : null);
} else {
p = new Reply();
Integer parentId = (Integer) obj.getOrDefault("parentId", null);
p.setParentid(parentId != null ? "post_" + parentId : null);
}
p.setId("post_" + (Integer) obj.getOrDefault("id", Utils.getNewId()));
p.setBody((String) obj.get("bodyMarkdown"));
p.setVotes((Integer) obj.getOrDefault("score", 0));
p.setTimestamp(DateUtils.parseDate((String) obj.get("creationDate"), soDateFormat1, soDateFormat2).getTime());
Integer creatorId = (Integer) obj.getOrDefault("ownerUserId", null);
if (creatorId == null || creatorId == -1) {
p.setCreatorid(utils.getSystemUser().getId());
} else {
// add prefix to avoid conflicts
p.setCreatorid(Profile.id("user_" + creatorId));
}
toImport.add(p);
}
pc.createAll(toImport);
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class AdminController method importUsersFromSO.
private void importUsersFromSO(List<Map<String, Object>> objs, List<ParaObject> toImport) throws ParseException {
logger.info("Importing {} users...", objs.size());
for (Map<String, Object> obj : objs) {
User u = new User();
u.setId("user_" + (Integer) obj.get("id"));
u.setTimestamp(DateUtils.parseDate((String) obj.get("creationDate"), soDateFormat1, soDateFormat2).getTime());
u.setActive(true);
u.setCreatorid(((Integer) obj.get("accountId")).toString());
u.setGroups("admin".equalsIgnoreCase((String) obj.get("userTypeId")) ? User.Groups.ADMINS.toString() : User.Groups.USERS.toString());
u.setEmail(u.getId() + "@scoold.com");
u.setIdentifier(u.getEmail());
u.setName((String) obj.get("realName"));
String lastLogin = (String) obj.get("lastLoginDate");
u.setUpdated(StringUtils.isBlank(lastLogin) ? null : DateUtils.parseDate(lastLogin, soDateFormat1, soDateFormat2).getTime());
u.setPicture((String) obj.get("profileImageUrl"));
u.setPassword(Utils.generateSecurityToken(10));
Profile p = Profile.fromUser(u);
p.setVotes((Integer) obj.get("reputation"));
p.setAboutme((String) obj.getOrDefault("title", ""));
p.setLastseen(u.getUpdated());
toImport.add(u);
toImport.add(p);
}
pc.createAll(toImport);
}
use of com.erudika.para.core.ParaObject in project scoold by Erudika.
the class AdminController method importTagsFromSO.
private void importTagsFromSO(List<Map<String, Object>> objs, List<ParaObject> toImport) {
logger.info("Importing {} tags...", objs.size());
for (Map<String, Object> obj : objs) {
Tag t = new Tag((String) obj.get("name"));
t.setCount((Integer) obj.getOrDefault("count", 0));
toImport.add(t);
}
pc.createAll(toImport);
}
Aggregations