001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Arnaud Kervern
018 */
019package org.nuxeo.ecm.agenda.operations;
020
021import java.util.Date;
022
023import org.nuxeo.ecm.agenda.AgendaService;
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.automation.jaxrs.io.documents.PaginableDocumentModelListImpl;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModelList;
033import org.nuxeo.ecm.platform.query.core.DocumentModelListPageProvider;
034
035/**
036 * Operation to list events between two dates to display them into a calendar
037 *
038 * @author <a href="mailto:akervern@nuxeo.com">Arnaud Kervern</a>
039 * @since 5.6
040 */
041@Operation(id = ListAgendaEvents.ID, category = Constants.CAT_DOCUMENT, label = "List Events", description = "List Events between two dates")
042public class ListAgendaEvents {
043
044    protected static final String ID = "VEVENT.List";
045
046    @Context
047    protected OperationContext context;
048
049    @Context
050    protected AgendaService agendaService;
051
052    @Context
053    protected CoreSession session;
054
055    @Param(name = "contextPath")
056    protected String contextPath;
057
058    @Param(name = "dtStart", required = false)
059    protected Date dtStart;
060
061    @Param(name = "dtEnd", required = false)
062    protected Date dtEnd;
063
064    @Param(name = "limit", required = false)
065    protected int limit = 5;
066
067    @Param(name = "documentLinkBuilder", required = false)
068    protected String documentLinkBuilder;
069
070    @OperationMethod
071    public PaginableDocumentModelListImpl run() {
072        DocumentModelList events;
073        if (dtStart != null) {
074            events = agendaService.listEvents(session, contextPath, dtStart, dtEnd);
075        } else {
076            events = agendaService.listEvents(session, contextPath, limit);
077        }
078        return new PaginableDocumentModelListImpl(new DocumentModelListPageProvider(events), documentLinkBuilder);
079    }
080}