use of org.alfresco.service.cmr.discussion.PostInfo in project alfresco-remote-api by Alfresco.
the class AbstractDiscussionWebScript method renderTopic.
/*
* Was topicpost.lib.js getTopicPostData / getTopicPostDataFromTopicAndPosts
*
* TODO Switch the FTL to prefer the Info object rather than the ScriptNode
*/
protected Map<String, Object> renderTopic(TopicInfo topic, SiteInfo site) {
// Fetch the primary post
PostInfo primaryPost = discussionService.getPrimaryPost(topic);
if (primaryPost == null) {
throw new WebScriptException(Status.STATUS_PRECONDITION_FAILED, "First (primary) post was missing from the topic, can't fetch");
}
// Fetch the most recent reply
PostInfo mostRecentPost = discussionService.getMostRecentPost(topic);
// Find out how many replies there are
int numReplies;
if (mostRecentPost.getNodeRef().equals(primaryPost.getNodeRef())) {
// Only the one post in the topic
mostRecentPost = null;
numReplies = 0;
} else {
// Use this trick to get the number of posts in the topic,
// but without needing to get lots of data and objects
PagingRequest paging = new PagingRequest(1);
paging.setRequestTotalCountMax(MAX_QUERY_ENTRY_COUNT);
PagingResults<PostInfo> posts = discussionService.listPosts(topic, paging);
// The primary post is in the list, so exclude from the reply count
numReplies = posts.getTotalResultCount().getFirst() - 1;
}
// Build the details
Map<String, Object> item = new HashMap<String, Object>();
item.put(KEY_IS_TOPIC_POST, true);
item.put(KEY_TOPIC, topic.getNodeRef());
item.put(KEY_POST, primaryPost.getNodeRef());
item.put(KEY_CAN_EDIT, canUserEditPost(primaryPost, site));
item.put(KEY_AUTHOR, buildPerson(topic.getCreator()));
// The reply count is one less than all posts (first is the primary one)
item.put("totalReplyCount", numReplies);
// Add the topic site
item.put("site", topic.getShortSiteName());
// We want details on the most recent post
if (mostRecentPost != null) {
item.put("lastReply", mostRecentPost.getNodeRef());
item.put("lastReplyBy", buildPerson(mostRecentPost.getCreator()));
}
// Include the tags
item.put("tags", topic.getTags());
// All done
return item;
}
use of org.alfresco.service.cmr.discussion.PostInfo in project alfresco-remote-api by Alfresco.
the class ForumTopicsFilteredGet method wrap.
/**
* Wrap up search results as {@link TopicInfo} instances
*
* @param finalResults ResultSet
* @param paging PagingRequest
*/
protected PagingResults<TopicInfo> wrap(final ResultSet finalResults, PagingRequest paging) {
int maxItems = paging.getMaxItems();
Comparator<TopicInfo> lastPostDesc = new Comparator<TopicInfo>() {
@Override
public int compare(TopicInfo t1, TopicInfo t2) {
Date t1LastPostDate = t1.getCreatedAt();
if (discussionService.getMostRecentPost(t1) != null) {
t1LastPostDate = discussionService.getMostRecentPost(t1).getCreatedAt();
}
Date t2LastPostDate = t2.getCreatedAt();
if (discussionService.getMostRecentPost(t2) != null) {
t2LastPostDate = discussionService.getMostRecentPost(t2).getCreatedAt();
}
return t2LastPostDate.compareTo(t1LastPostDate);
}
};
final Set<TopicInfo> topics = new TreeSet<TopicInfo>(lastPostDesc);
for (ResultSetRow row : finalResults) {
Pair<TopicInfo, PostInfo> pair = discussionService.getForNodeRef(row.getNodeRef());
TopicInfo topic = pair.getFirst();
if (topic != null) {
String path = nodeService.getPath(topic.getNodeRef()).toDisplayPath(nodeService, permissionService);
String site = path.split("/")[3];
TopicInfoImpl tii = (TopicInfoImpl) topic;
tii.setShortSiteName(site);
topics.add(tii);
if (topics.size() >= maxItems) {
break;
}
}
}
// Wrap
return new PagingResults<TopicInfo>() {
@Override
public boolean hasMoreItems() {
try {
return finalResults.hasMore();
} catch (UnsupportedOperationException e) {
// Not all search results support paging
return false;
}
}
@Override
public Pair<Integer, Integer> getTotalResultCount() {
int skipCount = 0;
int itemsRemainingAfterThisPage = 0;
try {
skipCount = finalResults.getStart();
} catch (UnsupportedOperationException e) {
}
try {
itemsRemainingAfterThisPage = finalResults.length();
} catch (UnsupportedOperationException e) {
}
final int totalItemsInUnpagedResultSet = skipCount + itemsRemainingAfterThisPage;
return new Pair<Integer, Integer>(totalItemsInUnpagedResultSet, totalItemsInUnpagedResultSet);
}
@Override
public List<TopicInfo> getPage() {
return new ArrayList<TopicInfo>(topics);
}
@Override
public String getQueryExecutionId() {
return null;
}
};
}
use of org.alfresco.service.cmr.discussion.PostInfo in project alfresco-remote-api by Alfresco.
the class AbstractDiscussionWebScript method executeImpl.
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
if (templateVars == null) {
String error = "No parameters supplied";
throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
}
// Parse the JSON, if supplied
JSONObject json = null;
String contentType = req.getContentType();
if (contentType != null && contentType.indexOf(';') != -1) {
contentType = contentType.substring(0, contentType.indexOf(';'));
}
if (MimetypeMap.MIMETYPE_JSON.equals(contentType)) {
JSONParser parser = new JSONParser();
try {
json = (JSONObject) parser.parse(req.getContent().getContent());
} catch (IOException io) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + io.getMessage());
} catch (ParseException pe) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + pe.getMessage());
}
}
// Did they request it by node reference or site?
NodeRef nodeRef = null;
SiteInfo site = null;
TopicInfo topic = null;
PostInfo post = null;
if (templateVars.containsKey("site")) {
// Site, and optionally topic
String siteName = templateVars.get("site");
site = siteService.getSite(siteName);
if (site == null) {
String error = "Could not find site: " + siteName;
throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
}
// Did they give a topic name too?
if (templateVars.containsKey("path")) {
String name = templateVars.get("path");
topic = discussionService.getTopic(site.getShortName(), name);
if (topic == null) {
String error = "Could not find topic '" + name + "' for site '" + site.getShortName() + "'";
throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
}
nodeRef = topic.getNodeRef();
} else {
// The NodeRef is the container (if it exists)
if (siteService.hasContainer(siteName, DiscussionServiceImpl.DISCUSSION_COMPONENT)) {
nodeRef = siteService.getContainer(siteName, DiscussionServiceImpl.DISCUSSION_COMPONENT);
}
}
} else if (templateVars.containsKey("store_type") && templateVars.containsKey("store_id") && templateVars.containsKey("id")) {
// NodeRef, normally Topic or Discussion
StoreRef store = new StoreRef(templateVars.get("store_type"), templateVars.get("store_id"));
nodeRef = new NodeRef(store, templateVars.get("id"));
if (!nodeService.exists(nodeRef)) {
String error = "Could not find node: " + nodeRef;
throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
}
// Try to build the appropriate object for it
Pair<TopicInfo, PostInfo> objects = discussionService.getForNodeRef(nodeRef);
if (objects != null) {
topic = objects.getFirst();
post = objects.getSecond();
}
// See if it's actually attached to a site
if (topic != null) {
NodeRef container = topic.getContainerNodeRef();
if (container != null) {
NodeRef maybeSite = nodeService.getPrimaryParent(container).getParentRef();
if (maybeSite != null) {
// Try to make it a site, will return Null if it isn't one
site = siteService.getSite(maybeSite);
}
}
}
} else {
String error = "Unsupported template parameters found";
throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
}
// Have the real work done
return executeImpl(site, nodeRef, topic, post, req, json, status, cache);
}
use of org.alfresco.service.cmr.discussion.PostInfo in project alfresco-remote-api by Alfresco.
the class ForumPostRepliesPost method executeImpl.
@Override
protected Map<String, Object> executeImpl(SiteInfo site, NodeRef nodeRef, TopicInfo topic, PostInfo post, WebScriptRequest req, JSONObject json, Status status, Cache cache) {
// mean to create the reply on the primary post
if (post == null) {
post = discussionService.getPrimaryPost(topic);
if (post == null) {
throw new WebScriptException(Status.STATUS_PRECONDITION_FAILED, "First (primary) post was missing from the topic, can't fetch");
}
} else if (topic == null) {
String error = "Node was of the wrong type, only Topic and Post are supported";
throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
}
// Have the reply created
PostInfo reply = doCreatePost(post, topic, req, json);
// Add the activity entry for the reply change
addActivityEntry("reply", "created", topic, reply, site, req, json);
// Build the common model parts
Map<String, Object> model = buildCommonModel(site, topic, reply, req);
// Build the JSON for the new reply post
model.put(KEY_POSTDATA, renderPost(reply, site));
// All done
return model;
}
use of org.alfresco.service.cmr.discussion.PostInfo in project alfresco-remote-api by Alfresco.
the class ForumPostRepliesPost method doCreatePost.
private PostInfo doCreatePost(PostInfo post, TopicInfo topic, WebScriptRequest req, JSONObject json) {
// Fetch the details from the JSON
String title = null;
if (json.containsKey("title")) {
title = (String) json.get("title");
}
String contents = null;
if (json.containsKey("content")) {
contents = (String) json.get("content");
}
// Create the reply
PostInfo reply = discussionService.createReply(post, contents);
// Set the title if needed (it normally isn't)
if (title != null && title.length() > 0) {
nodeService.setProperty(reply.getNodeRef(), ContentModel.PROP_TITLE, title);
reply = discussionService.getPost(topic, reply.getSystemName());
}
// All done
return reply;
}
Aggregations