001/*
002 * (C) Copyright 2006-20011 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.ecm.platform.reporting.jaxrs;
020
021import java.util.UUID;
022
023import javax.ws.rs.GET;
024import javax.ws.rs.Path;
025import javax.ws.rs.PathParam;
026import javax.ws.rs.Produces;
027import javax.ws.rs.core.Response;
028
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.IdRef;
032import org.nuxeo.ecm.platform.reporting.api.ReportInstance;
033import org.nuxeo.ecm.platform.reporting.api.ReportService;
034import org.nuxeo.ecm.webengine.model.WebObject;
035import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Root module to give access to Report Listing and report execution
040 *
041 * @author Tiry (tdelprat@nuxeo.com)
042 */
043@Path("/reports")
044@WebObject(type = "reports")
045public class ReportModule extends ModuleRoot {
046
047    protected ReportInstance findReport(String idOrKey) {
048
049        boolean useIdResolution = true;
050        try {
051            UUID.fromString(idOrKey);
052        } catch (IllegalArgumentException e) {
053            useIdResolution = false;
054        }
055
056        CoreSession session = getContext().getCoreSession();
057        if (useIdResolution) {
058            IdRef ref = new IdRef(idOrKey);
059            if (!session.exists(ref)) {
060                return null;
061            }
062            DocumentModel reportDoc = session.getDocument(ref);
063            return reportDoc.getAdapter(ReportInstance.class);
064        } else {
065            ReportService rs = Framework.getLocalService(ReportService.class);
066            return rs.getReportInstanceByKey(session, idOrKey);
067        }
068    }
069
070    @GET
071    @Path("list")
072    @Produces("text/html")
073    public Object listReports() {
074        // TODO get report list by model
075        return null;
076    }
077
078    @Path(value = "{reportIdOrName}")
079    public Object getReport(@PathParam("reportIdOrName") String reportIdOrName) {
080        ReportInstance report = findReport(reportIdOrName);
081        if (report == null) {
082            return Response.status(404).build();
083        }
084        return ctx.newObject("report", report);
085    }
086
087}