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