mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-21 18:09:20 +00:00
ALF-9156 Convert the Calendar delete event webscript to be Java backed
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28853 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -1,98 +0,0 @@
|
||||
<import resource="classpath:/alfresco/templates/webscripts/org/alfresco/slingshot/calendar/lib/calendar.lib.js">
|
||||
/**
|
||||
* Delete event action
|
||||
* @method DELETE
|
||||
* @param uri {string} /{siteid}/{eventname}
|
||||
*/
|
||||
var result = deleteEvent();
|
||||
status.code = result;
|
||||
|
||||
function deleteEvent()
|
||||
{
|
||||
var params = getTemplateParams();
|
||||
if (params === null)
|
||||
{
|
||||
return status.STATUS_BAD_REQUEST;
|
||||
}
|
||||
|
||||
var site = siteService.getSite(params.siteid);
|
||||
if (site === null)
|
||||
{
|
||||
return status.STATUS_NOT_FOUND;
|
||||
}
|
||||
|
||||
var eventsFolder = getCalendarContainer(site);
|
||||
if (eventsFolder === null)
|
||||
{
|
||||
return status.STATUS_NOT_FOUND;
|
||||
}
|
||||
|
||||
var event = eventsFolder.childByNamePath(params.eventname);
|
||||
var editedEvent = event;
|
||||
|
||||
if (event === null)
|
||||
{
|
||||
return status.STATUS_NOT_FOUND;
|
||||
}
|
||||
|
||||
if (editedEvent.properties["ia:recurrenceRule"] != null)
|
||||
{
|
||||
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");
|
||||
|
||||
return status.STATUS_NO_CONTENT;
|
||||
}
|
||||
|
||||
var whatEvent = event.properties["ia:whatEvent"];
|
||||
|
||||
if (!event.remove())
|
||||
{
|
||||
return status.STATUS_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var data =
|
||||
{
|
||||
title: whatEvent,
|
||||
page: decodeURIComponent(args["page"])
|
||||
}
|
||||
activities.postActivity("org.alfresco.calendar.event-deleted", params.siteid, "calendar", jsonUtils.toJSONString(data));
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
if (logger.isLoggingEnabled())
|
||||
{
|
||||
logger.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Success
|
||||
return status.STATUS_NO_CONTENT;
|
||||
}
|
||||
|
||||
function getTemplateParams()
|
||||
{
|
||||
// Grab the URI parameters
|
||||
var siteid = "" + url.templateArgs.siteid;
|
||||
var eventname = "" + url.templateArgs.eventname;
|
||||
var date = args["date"];
|
||||
|
||||
if (siteid === null || siteid.length === 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (eventname === null || eventname.length === 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
"siteid": siteid,
|
||||
"eventname": eventname,
|
||||
"date": date
|
||||
};
|
||||
}
|
@@ -1450,6 +1450,7 @@
|
||||
parent="webscript" abstract="true">
|
||||
<property name="nodeService" ref="NodeService"/>
|
||||
<property name="siteService" ref="SiteService"/>
|
||||
<property name="activityService" ref="activityService"/>
|
||||
<property name="calendarService" ref="CalendarService"/>
|
||||
</bean>
|
||||
|
||||
@@ -1459,4 +1460,10 @@
|
||||
parent="abstractCalendarWebScript">
|
||||
</bean>
|
||||
|
||||
<!-- Removes a Calendar Event from the site calendar -->
|
||||
<bean id="webscript.org.alfresco.slingshot.calendar.event.delete"
|
||||
class="org.alfresco.repo.web.scripts.calendar.CalendarEntryDelete"
|
||||
parent="abstractCalendarWebScript">
|
||||
</bean>
|
||||
|
||||
</beans>
|
@@ -18,9 +18,11 @@
|
||||
*/
|
||||
package org.alfresco.repo.web.scripts.calendar;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.service.cmr.activities.ActivityService;
|
||||
import org.alfresco.service.cmr.calendar.CalendarService;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.site.SiteInfo;
|
||||
@@ -28,6 +30,7 @@ import org.alfresco.service.cmr.site.SiteService;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.DeclarativeWebScript;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
@@ -36,9 +39,12 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
*/
|
||||
public abstract class AbstractCalendarWebScript extends DeclarativeWebScript
|
||||
{
|
||||
public static final String CALENDAR_SERVICE_ACTIVITY_APP_NAME = "calendar";
|
||||
|
||||
// Injected services
|
||||
protected NodeService nodeService;
|
||||
protected SiteService siteService;
|
||||
protected ActivityService activityService;
|
||||
protected CalendarService calendarService;
|
||||
|
||||
public void setNodeService(NodeService nodeService)
|
||||
@@ -51,11 +57,44 @@ public abstract class AbstractCalendarWebScript extends DeclarativeWebScript
|
||||
this.siteService = siteService;
|
||||
}
|
||||
|
||||
public void setActivityService(ActivityService activityService)
|
||||
{
|
||||
this.activityService = activityService;
|
||||
}
|
||||
|
||||
public void setCalendarService(CalendarService calendarService)
|
||||
{
|
||||
this.calendarService = calendarService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the date from the String, trying the various formats
|
||||
* (New and Legacy) until one works...
|
||||
*/
|
||||
protected Date extractDate(String date)
|
||||
{
|
||||
// Try as ISO8601
|
||||
|
||||
// Try YYYY/MM/DD
|
||||
|
||||
// Try YYYY-MM-DD
|
||||
|
||||
|
||||
|
||||
// TODO Implement
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normally the Calendar webscripts return a 200 with JSON
|
||||
* containing the error message. Override this to switch to
|
||||
* using HTTP status codes instead
|
||||
*/
|
||||
protected boolean useJSONErrors()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Equivalent of <i>jsonError</i> in the old JavaScript controllers
|
||||
*/
|
||||
@@ -78,7 +117,15 @@ public abstract class AbstractCalendarWebScript extends DeclarativeWebScript
|
||||
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
|
||||
if(templateVars == null)
|
||||
{
|
||||
return buildError("No parameters supplied");
|
||||
String error = "No parameters supplied";
|
||||
if(useJSONErrors())
|
||||
{
|
||||
return buildError(error);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
|
||||
}
|
||||
}
|
||||
|
||||
// Get the site short name. Try quite hard to do so...
|
||||
@@ -93,14 +140,30 @@ public abstract class AbstractCalendarWebScript extends DeclarativeWebScript
|
||||
}
|
||||
if(siteName == null)
|
||||
{
|
||||
return buildError("No site given");
|
||||
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)
|
||||
{
|
||||
return buildError("Could not find site: " + siteName);
|
||||
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
|
||||
|
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.repo.calendar.CalendarModel;
|
||||
import org.alfresco.repo.calendar.CalendarServiceImpl;
|
||||
import org.alfresco.service.cmr.calendar.CalendarEntry;
|
||||
import org.alfresco.service.cmr.site.SiteInfo;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.GUID;
|
||||
import org.alfresco.util.ISO8601DateFormat;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
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.delete webscript.
|
||||
*
|
||||
* @author Nick Burch
|
||||
* @since 4.0
|
||||
*/
|
||||
public class CalendarEntryDelete extends AbstractCalendarWebScript
|
||||
{
|
||||
private static Log logger = LogFactory.getLog(CalendarEntryDelete.class);
|
||||
|
||||
/**
|
||||
* This WebScript uses HTTP status codes for errors
|
||||
*/
|
||||
@Override
|
||||
protected boolean useJSONErrors() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(SiteInfo site, String eventName,
|
||||
WebScriptRequest req, Status status, Cache cache) {
|
||||
CalendarEntry entry = calendarService.getCalendarEntry(
|
||||
site.getShortName(), eventName
|
||||
);
|
||||
|
||||
if(entry == null)
|
||||
{
|
||||
status.setCode(Status.STATUS_NOT_FOUND);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Special case for "deleting" an instance of a recurring event
|
||||
if(req.getParameter("date") != null && entry.getRecurrenceRule() != null)
|
||||
{
|
||||
// Get the date to be ignored
|
||||
Map<QName,Serializable> props = new HashMap<QName, Serializable>();
|
||||
Date date = extractDate(req.getParameter("date"));
|
||||
props.put(CalendarModel.PROP_IGNORE_EVENT_DATE, date);
|
||||
|
||||
// Create a child node of the event
|
||||
nodeService.createNode(
|
||||
entry.getNodeRef(), CalendarModel.ASSOC_IGNORE_EVENT_LIST,
|
||||
QName.createQName(GUID.generate()), CalendarModel.TYPE_IGNORE_EVENT, props
|
||||
);
|
||||
|
||||
// Mark as ignored
|
||||
status.setCode(Status.STATUS_NO_CONTENT, "Recurring entry ignored");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Delete the calendar entry
|
||||
calendarService.deleteCalendarEntry(entry);
|
||||
|
||||
// Record this in the activity feed
|
||||
try
|
||||
{
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("siteid", site.getShortName());
|
||||
json.put("eventname", entry.getSystemName());
|
||||
json.put("date", ISO8601DateFormat.format(entry.getStart()));
|
||||
|
||||
activityService.postActivity(
|
||||
"org.alfresco.calendar.event-deleted",
|
||||
site.getShortName(),
|
||||
CALENDAR_SERVICE_ACTIVITY_APP_NAME,
|
||||
json.toString()
|
||||
);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
// Warn, but carry on
|
||||
logger.warn("Error adding event deletion to activities feed", e);
|
||||
}
|
||||
|
||||
// All done
|
||||
status.setCode(Status.STATUS_NO_CONTENT, "Entry deleted");
|
||||
return null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user