mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
ALF-9156 Convert the last of the Calendar CRUD webscripts to Java (except for recurring parts)
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28866 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -27,6 +27,7 @@ import java.util.Map;
|
||||
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.service.cmr.activities.ActivityService;
|
||||
import org.alfresco.service.cmr.calendar.CalendarEntry;
|
||||
import org.alfresco.service.cmr.calendar.CalendarService;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.site.SiteInfo;
|
||||
@@ -79,7 +80,7 @@ public abstract class AbstractCalendarWebScript extends DeclarativeWebScript
|
||||
* Gets the date from the String, trying the various formats
|
||||
* (New and Legacy) until one works...
|
||||
*/
|
||||
protected Date extractDate(String date)
|
||||
protected Date parseDate(String date)
|
||||
{
|
||||
// Is there one at all?
|
||||
if(date == null || date.length() == 0)
|
||||
@@ -126,6 +127,51 @@ public abstract class AbstractCalendarWebScript extends DeclarativeWebScript
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid date '" + date + "'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the Start and End details, along with the All Day flag
|
||||
* from the JSON, and returns if the event is all day or not
|
||||
*/
|
||||
protected boolean extractDates(CalendarEntry entry, JSONObject json) throws JSONException
|
||||
{
|
||||
boolean isAllDay = false;
|
||||
|
||||
if(json.has("startAt") && json.has("endAt"))
|
||||
{
|
||||
// New style ISO8601 dates
|
||||
entry.setStart(parseDate(json.getString("startAt")));
|
||||
entry.setEnd(parseDate(json.getString("endAt")));
|
||||
if(json.has("allday"))
|
||||
{
|
||||
// TODO Handle All Day events properly, including timezones
|
||||
isAllDay = true;
|
||||
}
|
||||
}
|
||||
else if(json.has("allday"))
|
||||
{
|
||||
// Old style all-day event
|
||||
entry.setStart(parseDate(getOrNull(json, "from")));
|
||||
entry.setEnd(parseDate(getOrNull(json, "to")));
|
||||
isAllDay = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Old style regular event
|
||||
entry.setStart(parseDate(json.getString("from") + " " + json.getString("start")));
|
||||
entry.setEnd(parseDate(json.getString("to") + " " + json.getString("end")));
|
||||
}
|
||||
|
||||
return isAllDay;
|
||||
}
|
||||
|
||||
protected String getOrNull(JSONObject json, String key) throws JSONException
|
||||
{
|
||||
if(json.has(key))
|
||||
{
|
||||
return json.getString(key);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normally the Calendar webscripts return a 200 with JSON
|
||||
* containing the error message. Override this to switch to
|
||||
|
@@ -71,7 +71,7 @@ public class CalendarEntryDelete extends AbstractCalendarWebScript
|
||||
{
|
||||
// Get the date to be ignored
|
||||
Map<QName,Serializable> props = new HashMap<QName, Serializable>();
|
||||
Date date = extractDate(req.getParameter("date"));
|
||||
Date date = parseDate(req.getParameter("date"));
|
||||
props.put(CalendarModel.PROP_IGNORE_EVENT_DATE, date);
|
||||
|
||||
// Create a child node of the event
|
||||
|
@@ -61,30 +61,7 @@ public class CalendarEntryPost extends AbstractCalendarWebScript
|
||||
entry.setSharePointDocFolder(getOrNull(json, "docfolder"));
|
||||
|
||||
// Handle the dates
|
||||
if(json.has("startAt") && json.has("endAt"))
|
||||
{
|
||||
// New style ISO8601 dates
|
||||
entry.setStart(extractDate(json.getString("startAt")));
|
||||
entry.setEnd(extractDate(json.getString("endAt")));
|
||||
if(json.has("allday"))
|
||||
{
|
||||
// TODO Handle All Day events properly, including timezones
|
||||
isAllDay = true;
|
||||
}
|
||||
}
|
||||
else if(json.has("allday"))
|
||||
{
|
||||
// Old style all-day event
|
||||
entry.setStart(extractDate(getOrNull(json, "from")));
|
||||
entry.setEnd(extractDate(getOrNull(json, "to")));
|
||||
isAllDay = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Old style regular event
|
||||
entry.setStart(extractDate(json.getString("from") + " " + json.getString("start")));
|
||||
entry.setEnd(extractDate(json.getString("to") + " " + json.getString("end")));
|
||||
}
|
||||
isAllDay = extractDates(entry, json);
|
||||
|
||||
// Handle tags
|
||||
if(json.has("tags"))
|
||||
@@ -162,13 +139,4 @@ public class CalendarEntryPost extends AbstractCalendarWebScript
|
||||
model.put("result", result);
|
||||
return model;
|
||||
}
|
||||
|
||||
private String getOrNull(JSONObject json, String key) throws JSONException
|
||||
{
|
||||
if(json.has(key))
|
||||
{
|
||||
return json.getString(key);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2011 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.repo.web.scripts.calendar;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import org.alfresco.service.cmr.calendar.CalendarEntry;
|
||||
import org.alfresco.service.cmr.site.SiteInfo;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
* This class is the controller for the slingshot calendar event.put webscript.
|
||||
*
|
||||
* @author Nick Burch
|
||||
* @since 4.0
|
||||
*/
|
||||
public class CalendarEntryPut extends AbstractCalendarWebScript
|
||||
{
|
||||
private static Log logger = LogFactory.getLog(CalendarEntryPut.class);
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(SiteInfo site, String eventName,
|
||||
WebScriptRequest req, JSONObject json, Status status, Cache cache) {
|
||||
CalendarEntry entry = calendarService.getCalendarEntry(
|
||||
site.getShortName(), eventName
|
||||
);
|
||||
|
||||
if(entry == null)
|
||||
{
|
||||
return buildError("Could not find event: " + eventName);
|
||||
}
|
||||
|
||||
// TODO Handle All Day events properly, including timezones
|
||||
boolean isAllDay = false;
|
||||
|
||||
try
|
||||
{
|
||||
// Doc folder is a bit special
|
||||
String docFolder = json.getString("docfolder");
|
||||
|
||||
if(entry.getRecurrenceRule() != null)
|
||||
{
|
||||
// TODO Handle editing recurring rules
|
||||
// Needs stuff with ignored events
|
||||
/*
|
||||
var prop = new Array();
|
||||
var fromParts = params.date.split("-");
|
||||
prop["ia:date"] = new Date(fromParts[0],fromParts[1] - 1,fromParts[2]);
|
||||
editedEvent.createNode(null, "ia:ignoreEvent", prop, "ia:ignoreEventList");
|
||||
|
||||
var timestamp = new Date().getTime();
|
||||
var random = Math.round(Math.random() * 10000);
|
||||
|
||||
event = eventsFolder.createNode(timestamp + "-" + random + ".ics", "ia:calendarEvent");
|
||||
event.properties["ia:isOutlook"] = true;
|
||||
|
||||
*/
|
||||
|
||||
// TODO Special doc folder stuff
|
||||
if("*NOT_CHANGE*".equals(docFolder))
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
|
||||
// Doc folder is a bit special
|
||||
if("*NOT_CHANGE*".equals(docFolder))
|
||||
{
|
||||
// Nothing to change
|
||||
}
|
||||
else
|
||||
{
|
||||
entry.setSharePointDocFolder(docFolder);
|
||||
}
|
||||
|
||||
|
||||
// Grab the properties
|
||||
entry.setTitle(getOrNull(json, "what"));
|
||||
entry.setDescription(getOrNull(json, "desc"));
|
||||
entry.setLocation(getOrNull(json, "where"));
|
||||
|
||||
// Handle the dates
|
||||
isAllDay = extractDates(entry, json);
|
||||
|
||||
// Handle tags
|
||||
if(json.has("tags"))
|
||||
{
|
||||
entry.getTags().clear();
|
||||
|
||||
StringTokenizer st = new StringTokenizer(json.getString("tags"), " ");
|
||||
while(st.hasMoreTokens())
|
||||
{
|
||||
entry.getTags().add(st.nextToken());
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(JSONException je)
|
||||
{
|
||||
return buildError("Invalid JSON: " + je.getMessage());
|
||||
}
|
||||
|
||||
if(entry == null)
|
||||
{
|
||||
return buildError("Could not find event: " + eventName);
|
||||
}
|
||||
|
||||
|
||||
// Have it edited
|
||||
entry = calendarService.updateCalendarEntry(entry);
|
||||
|
||||
|
||||
// Generate the activity feed for this
|
||||
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
|
||||
String dateOpt = "?date=" + fmt.format(entry.getStart());
|
||||
try
|
||||
{
|
||||
JSONObject activity = new JSONObject();
|
||||
activity.put("title", entry.getTitle());
|
||||
activity.put("page", req.getParameter("page") + dateOpt);
|
||||
|
||||
activityService.postActivity(
|
||||
"org.alfresco.calendar.event-updated",
|
||||
site.getShortName(),
|
||||
CALENDAR_SERVICE_ACTIVITY_APP_NAME,
|
||||
activity.toString()
|
||||
);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
// Warn, but carry on
|
||||
logger.warn("Error adding event deletion to activities feed", e);
|
||||
}
|
||||
|
||||
|
||||
// Build the return object
|
||||
Map<String, Object> result = new HashMap<String, Object>();
|
||||
result.put("summary", entry.getTitle());
|
||||
result.put("description", entry.getDescription());
|
||||
result.put("location", entry.getLocation());
|
||||
result.put("dtstart", entry.getStart());
|
||||
result.put("dtend", entry.getEnd());
|
||||
result.put("uri", "calendar/event/" + site.getShortName() + "/" +
|
||||
entry.getSystemName() + dateOpt);
|
||||
|
||||
result.put("tags", generateTagString(entry));
|
||||
result.put("allday", isAllDay);
|
||||
result.put("docfolder", entry.getSharePointDocFolder());
|
||||
|
||||
// Replace nulls with blank strings for the JSON
|
||||
for(String key : result.keySet())
|
||||
{
|
||||
if(result.get(key) == null)
|
||||
{
|
||||
result.put(key, "");
|
||||
}
|
||||
}
|
||||
|
||||
// All done
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("result", result);
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* We use lists for tags internally, and the other webscripts
|
||||
* return arrays too. This one is different, and it needs to
|
||||
* a single space separated string. This does the conversion
|
||||
*/
|
||||
protected String generateTagString(CalendarEntry entry)
|
||||
{
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if(entry.getTags() != null)
|
||||
{
|
||||
for(String tag : entry.getTags())
|
||||
{
|
||||
if(sb.length() > 0) sb.append(' ');
|
||||
sb.append(tag);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@@ -379,7 +379,7 @@ public class CalendarRestApiTest extends BaseWebScriptTest
|
||||
// No from/to/start/end, does dtstart and dtend instead
|
||||
assertEquals("2011-06-28T11:30", entry.getString("dtstart"));
|
||||
assertEquals("2011-06-28T13:30", entry.getString("dtend"));
|
||||
assertEquals("", entry.getString("allday"));
|
||||
assertEquals("false", entry.getString("allday"));
|
||||
// No isoutlook on create/edit
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user