001/*
002 * (C) Copyright 2006-2009 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 * $Id$
018 */
019
020package org.nuxeo.project.sample.restAPI;
021
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.List;
025import org.dom4j.dom.DOMDocument;
026import org.dom4j.dom.DOMDocumentFactory;
027import org.nuxeo.ecm.core.api.CoreInstance;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.api.PropertyException;
032import org.nuxeo.ecm.platform.query.api.PageProvider;
033import org.nuxeo.ecm.platform.query.api.PageProviderDefinition;
034import org.nuxeo.ecm.platform.query.api.PageProviderService;
035import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider;
036import org.nuxeo.ecm.platform.ui.web.restAPI.BaseNuxeoRestlet;
037import org.nuxeo.runtime.api.Framework;
038import org.restlet.data.MediaType;
039import org.restlet.data.Request;
040import org.restlet.data.Response;
041
042/**
043 * This Restlet get the latest created Book document and send back a custom JSon object to the client.
044 *
045 * @author ldoguin
046 */
047public class LastBookRestlet extends BaseNuxeoRestlet {
048
049    public static final String LAST_BOOK_PROVIDER = "LAST_BOOK";
050
051    /**
052     * override the handle method to do custom json serialization.
053     */
054    @Override
055    public void handle(Request req, Response res) {
056        DOMDocumentFactory domfactory = new DOMDocumentFactory();
057        DOMDocument result = (DOMDocument) domfactory.createDocument();
058
059        try (CoreSession session = CoreInstance.openCoreSession(null, getUserPrincipal(req))) {
060
061            PageProviderService pps = Framework.getService(PageProviderService.class);
062
063            PageProviderDefinition ppd = pps.getPageProviderDefinition(LAST_BOOK_PROVIDER);
064            HashMap<String, Serializable> props = new HashMap<String, Serializable>();
065            props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) session);
066
067            PageProvider<?> provider = pps.getPageProvider(LAST_BOOK_PROVIDER, ppd, null, null, Long.valueOf(1),
068                    Long.valueOf(0), props);
069
070            // fetch result
071            List<?> dms = provider.getCurrentPage();
072            if (dms.size() == 0) {
073                handleError(result, res, "No result available");
074                return;
075            }
076            DocumentModel lastBookDm = (DocumentModel) dms.get(0);
077            // serialize and write in response
078            String json = serialize(lastBookDm);
079            res.setEntity(json, MediaType.TEXT_PLAIN);
080        } catch (NuxeoException e) {
081            handleError(res, e);
082        }
083    }
084
085    private String serialize(DocumentModel lastBookDm) throws PropertyException {
086        String jSonString = "{\"book\": {" + "\"title\": \"" + lastBookDm.getPropertyValue("dc:title") + "\","
087                + "\"isbn\": \"" + lastBookDm.getPropertyValue("bk:isbn") + "\"," + "\"rating\": \""
088                + lastBookDm.getPropertyValue("bk:rating") + "\"," + "\"publicationDate\": \""
089                + lastBookDm.getPropertyValue("bk:publicationDate") + "\"," + "\"keywords\": \""
090                + lastBookDm.getPropertyValue("bk:keywords") + "\"}}";
091        return jSonString;
092    }
093
094}