001/*
002 * (C) Copyright 2008 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: CreationContainerListRestlet.java 30586 2008-02-26 14:30:17Z ogrisel $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.restAPI;
021
022import static org.jboss.seam.ScopeType.EVENT;
023
024import java.io.Serializable;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.dom4j.Element;
029import org.dom4j.dom.DOMDocument;
030import org.dom4j.dom.DOMDocumentFactory;
031import org.jboss.seam.annotations.Name;
032import org.jboss.seam.annotations.Scope;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.DocumentModelList;
035import org.nuxeo.ecm.platform.filemanager.api.FileManager;
036import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants;
037import org.nuxeo.runtime.api.Framework;
038import org.restlet.data.CharacterSet;
039import org.restlet.data.MediaType;
040import org.restlet.data.Request;
041import org.restlet.data.Response;
042
043/**
044 * This restlet gets the list of containers that are suitable for new document creation.
045 * <p>
046 * The actual logic is delegated to the FileManagerService.
047 *
048 * @author Olivier Grisel <ogrisel@nuxeo.com>
049 */
050@Name("creationContainerListRestlet")
051@Scope(EVENT)
052public class CreationContainerListRestlet extends BaseNuxeoRestlet implements LiveEditConstants, Serializable {
053
054    private static final Log log = LogFactory.getLog(CreationContainerListRestlet.class);
055
056    private static final long serialVersionUID = 5403775170948512675L;
057
058    @Override
059    public void handle(Request req, Response res) {
060
061        DocumentModelList containers = null;
062        String docType = getQueryParamValue(req, DOC_TYPE, DEFAULT_DOCTYPE);
063        FileManager fileManager = Framework.getService(FileManager.class);
064        containers = fileManager.getCreationContainers(getUserPrincipal(req), docType);
065
066        // build the XML response document holding the containers info
067        DOMDocumentFactory domFactory = new DOMDocumentFactory();
068        DOMDocument resultDocument = (DOMDocument) domFactory.createDocument();
069        Element containersElement = resultDocument.addElement("containers");
070        for (DocumentModel parent : containers) {
071            Element docElement = containersElement.addElement(documentTag);
072            docElement.addElement(docRepositoryTag).setText(parent.getRepositoryName());
073            docElement.addElement(docRefTag).setText(parent.getRef().toString());
074            docElement.addElement(docTitleTag).setText(parent.getTitle());
075            docElement.addElement(docPathTag).setText(parent.getPathAsString());
076        }
077        res.setEntity(resultDocument.asXML(), MediaType.TEXT_XML);
078        res.getEntity().setCharacterSet(CharacterSet.UTF_8);
079    }
080
081}