use of org.alfresco.service.cmr.site.SiteInfo in project alfresco-remote-api by Alfresco.
the class UserCalendarEntriesGet method executeImpl.
@Override
protected Map<String, Object> executeImpl(SiteInfo singleSite, String eventName, WebScriptRequest req, JSONObject json, Status status, Cache cache) {
// Did they restrict by date?
Date fromDate = parseDate(req.getParameter("from"));
Date toDate = parseDate(req.getParameter("to"));
// What should we do about repeating events? First or all?
boolean repeatingFirstOnly = true;
String repeatingEvents = req.getParameter("repeating");
if (repeatingEvents != null) {
if ("first".equals(repeatingEvents)) {
repeatingFirstOnly = true;
} else if ("all".equals(repeatingEvents)) {
repeatingFirstOnly = false;
}
} else {
// the format of the from date, which differs between uses!
if (fromDate != null) {
String fromDateS = req.getParameter("from");
if (fromDateS.indexOf('-') != -1) {
// Apparently this is the site calendar dashlet...
repeatingFirstOnly = true;
}
if (fromDateS.indexOf('/') != -1) {
// This is something else, wants all events in range
repeatingFirstOnly = false;
}
}
}
// One site, or all the user's ones?
List<SiteInfo> sites = new ArrayList<SiteInfo>();
if (singleSite != null) {
// Just one
sites.add(singleSite);
} else {
// All their sites (with optional limit)
int max = 0;
String strMax = req.getParameter("size");
if (strMax != null && strMax.length() != 0) {
max = Integer.parseInt(strMax);
}
sites = siteService.listSites(AuthenticationUtil.getRunAsUser(), max);
}
// We need to know the Site Names, and the NodeRefs of the calendar containers
String[] siteShortNames = new String[sites.size()];
Map<NodeRef, SiteInfo> containerLookup = new HashMap<NodeRef, SiteInfo>();
for (int i = 0; i < sites.size(); i++) {
SiteInfo site = sites.get(i);
siteShortNames[i] = site.getShortName();
try {
containerLookup.put(siteService.getContainer(site.getShortName(), CalendarServiceImpl.CALENDAR_COMPONENT), site);
} catch (AccessDeniedException e) {
// You can see the site, but not the calendar, so skip it
// This means you won't have any events in it anyway
}
}
// Get the entries for the list
PagingRequest paging = buildPagingRequest(req);
PagingResults<CalendarEntry> entries = calendarService.listCalendarEntries(siteShortNames, fromDate, toDate, paging);
boolean resortNeeded = false;
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (CalendarEntry entry : entries.getPage()) {
// Build the object
Map<String, Object> result = new HashMap<String, Object>();
boolean isAllDay = CalendarEntryDTO.isAllDay(entry);
boolean removeTimezone = isAllDay && !entry.isOutlook();
result.put(RESULT_EVENT, entry);
result.put(RESULT_NAME, entry.getSystemName());
result.put(RESULT_TITLE, entry.getTitle());
result.put("description", entry.getDescription());
result.put("where", entry.getLocation());
result.put(RESULT_START, removeTimeZoneIfRequired(entry.getStart(), isAllDay, removeTimezone));
result.put(RESULT_END, removeTimeZoneIfRequired(entry.getEnd(), isAllDay, removeTimezone));
String legacyDateFormat = "yyyy-MM-dd";
String legacyTimeFormat = "HH:mm";
result.put("legacyDateFrom", removeTimeZoneIfRequired(entry.getStart(), isAllDay, removeTimezone, legacyDateFormat));
result.put("legacyTimeFrom", removeTimeZoneIfRequired(entry.getStart(), isAllDay, removeTimezone, legacyTimeFormat));
result.put("legacyDateTo", removeTimeZoneIfRequired(entry.getEnd(), isAllDay, removeTimezone, legacyDateFormat));
result.put("legacyTimeTo", removeTimeZoneIfRequired(entry.getEnd(), isAllDay, removeTimezone, legacyTimeFormat));
result.put("duration", buildDuration(entry));
result.put("tags", entry.getTags());
result.put("isoutlook", entry.isOutlook());
result.put("allday", CalendarEntryDTO.isAllDay(entry));
// Identify the site
SiteInfo site = containerLookup.get(entry.getContainerNodeRef());
result.put("site", site);
result.put("siteName", site.getShortName());
result.put("siteTitle", site.getTitle());
// Check the permissions the user has on the entry
AccessStatus canEdit = permissionService.hasPermission(entry.getNodeRef(), PermissionService.WRITE);
AccessStatus canDelete = permissionService.hasPermission(entry.getNodeRef(), PermissionService.DELETE);
result.put("canEdit", (canEdit == AccessStatus.ALLOWED));
result.put("canDelete", (canDelete == AccessStatus.ALLOWED));
// Replace nulls with blank strings for the JSON
for (String key : result.keySet()) {
if (result.get(key) == null) {
result.put(key, "");
}
}
// Save this one
results.add(result);
// Handle recurring as needed
boolean orderChanged = handleRecurring(entry, result, results, fromDate, toDate, repeatingFirstOnly);
if (orderChanged) {
resortNeeded = true;
}
}
// If the recurring events meant dates changed, re-sort
if (resortNeeded) {
Collections.sort(results, getEventDetailsSorter());
}
// All done
Map<String, Object> model = new HashMap<String, Object>();
model.put("events", results);
return model;
}
use of org.alfresco.service.cmr.site.SiteInfo in project alfresco-remote-api by Alfresco.
the class AbstractCommentsWebScript method postActivity.
/**
* Post an activity entry for the comment added or deleted
*
* @param json
* - is not sent null with this activity type - only for delete
* @param req
* @param nodeRef
* @param activityType
*/
protected void postActivity(JSONObject json, WebScriptRequest req, NodeRef nodeRef, String activityType) {
String jsonActivityData = "";
String siteId = "";
String page = "";
String title = "";
if (nodeRef == null) {
// to post activity for parent node
return;
}
String strNodeRef = nodeRef.toString();
SiteInfo siteInfo = getSiteInfo(req, COMMENT_CREATED_ACTIVITY.equals(activityType));
// post an activity item, but only if we've got a site
if (siteInfo == null || siteInfo.getShortName() == null || siteInfo.getShortName().length() == 0) {
return;
} else {
siteId = siteInfo.getShortName();
}
// json is not sent null with this activity type - only for delete
if (COMMENT_CREATED_ACTIVITY.equals(activityType)) {
try {
org.json.JSONObject params = new org.json.JSONObject(getOrNull(json, JSON_KEY_PAGE_PARAMS));
String strParams = "";
Iterator<?> itr = params.keys();
while (itr.hasNext()) {
String strParam = itr.next().toString();
strParams += strParam + "=" + params.getString(strParam) + "&";
}
page = getOrNull(json, JSON_KEY_PAGE) + "?" + (strParams != "" ? strParams.substring(0, strParams.length() - 1) : "");
title = getOrNull(json, JSON_KEY_ITEM_TITLE);
} catch (Exception e) {
logger.warn("Error parsing JSON", e);
}
} else {
// COMMENT_DELETED_ACTIVITY
title = req.getParameter(JSON_KEY_ITEM_TITLE);
page = req.getParameter(JSON_KEY_PAGE) + "?" + JSON_KEY_NODEREF + "=" + strNodeRef;
}
try {
JSONWriter jsonWriter = new JSONStringer().object();
jsonWriter.key(JSON_KEY_TITLE).value(title);
jsonWriter.key(JSON_KEY_PAGE).value(page);
jsonWriter.key(JSON_KEY_NODEREF).value(strNodeRef);
jsonActivityData = jsonWriter.endObject().toString();
activityService.postActivity(activityType, siteId, COMMENTS_TOPIC_NAME, jsonActivityData);
} catch (Exception e) {
logger.warn("Error adding comment to activities feed", e);
}
}
use of org.alfresco.service.cmr.site.SiteInfo in project alfresco-remote-api by Alfresco.
the class AbstractCalendarWebScript 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";
if (useJSONErrors()) {
return buildError(error);
} else {
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) {
return buildError("Invalid JSON: " + io.getMessage());
} catch (org.json.simple.parser.ParseException je) {
return buildError("Invalid JSON: " + je.getMessage());
}
}
// Get the site short name. Try quite hard to do so...
String siteName = templateVars.get("siteid");
if (siteName == null) {
siteName = templateVars.get("site");
}
if (siteName == null) {
siteName = req.getParameter("site");
}
if (siteName == null && json != null) {
if (json.containsKey("siteid")) {
siteName = (String) json.get("siteid");
} else if (json.containsKey("site")) {
siteName = (String) json.get("site");
}
}
if (siteName == null) {
String error = "No site given";
if (useJSONErrors()) {
return buildError("No site given");
} else {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
}
}
// Grab the requested site
SiteInfo site = siteService.getSite(siteName);
if (site == null) {
String error = "Could not find site: " + siteName;
if (useJSONErrors()) {
return buildError(error);
} else {
throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
}
}
// Event name is optional
String eventName = templateVars.get("eventname");
// Have the real work done
return executeImpl(site, eventName, req, json, status, cache);
}
use of org.alfresco.service.cmr.site.SiteInfo in project alfresco-remote-api by Alfresco.
the class AbstractBlogWebScript 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;
BlogPostInfo blog = null;
if (templateVars.containsKey("site")) {
// Site, and Optionally Blog Post
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 blog post name too?
if (templateVars.containsKey("path")) {
String name = templateVars.get("path");
blog = blogService.getBlogPost(siteName, name);
if (blog == null) {
String error = "Could not find blog '" + name + "' for site '" + site.getShortName() + "'";
throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
}
nodeRef = blog.getNodeRef();
} else {
// The NodeRef is the container (if it exists)
if (siteService.hasContainer(siteName, BlogServiceImpl.BLOG_COMPONENT)) {
nodeRef = siteService.getContainer(siteName, BlogServiceImpl.BLOG_COMPONENT);
}
}
} else if (templateVars.containsKey("store_type") && templateVars.containsKey("store_id") && templateVars.containsKey("id")) {
// NodeRef, should be a Blog Post
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
blog = blogService.getForNodeRef(nodeRef);
// See if it's actually attached to a site
if (blog != null) {
NodeRef container = blog.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, blog, req, json, status, cache);
}
use of org.alfresco.service.cmr.site.SiteInfo in project alfresco-remote-api by Alfresco.
the class RepoService method getFavouriteSites.
public List<FavouriteSite> getFavouriteSites(TestPerson user) {
List<FavouriteSite> favouriteSites = new ArrayList<FavouriteSite>();
PagingResults<SiteInfo> pagingResults = getFavouriteSites(user.getId(), new PagingRequest(0, Integer.MAX_VALUE));
for (SiteInfo siteInfo : pagingResults.getPage()) {
String siteId = siteInfo.getShortName();
String siteGuid = siteInfo.getNodeRef().getId();
TestSite site = user.getDefaultAccount().getSite(siteId);
FavouriteSite favouriteSite = null;
if (site.isMember(user.getId())) {
favouriteSite = new FavouriteSite(site);
} else {
favouriteSite = new FavouriteSite(null, siteId, siteGuid, null, null, null, null, null);
}
favouriteSites.add(favouriteSite);
}
return favouriteSites;
}
Aggregations