001/*
002 * (C) Copyright 2012 Nuxeo SA (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 *     bjalon
016 */
017package org.nuxeo.ecm.mobile.webengine.adapter;
018
019import java.util.HashMap;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.POST;
023import javax.ws.rs.core.Response;
024
025import org.nuxeo.ecm.automation.AutomationService;
026import org.nuxeo.ecm.automation.OperationContext;
027import org.nuxeo.ecm.automation.OperationException;
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.webengine.forms.FormData;
030import org.nuxeo.ecm.webengine.model.WebAdapter;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
035 * @since 5.5
036 */
037@WebAdapter(name = "folderish", type = "Folderish", targetType = "MobileDocument")
038public class FolderishAdapter extends DefaultMobileAdapter {
039
040    @GET
041    public Object doGet() {
042        return getView("index");
043    }
044
045    @POST
046    public Object doUpload() throws OperationException {
047        FormData form = ctx.getForm();
048        Blob blob = form.getFirstBlob();
049        if (blob == null) {
050            throw new IllegalArgumentException("Could not find any uploaded file");
051        }
052        AutomationService as = Framework.getLocalService(AutomationService.class);
053        HashMap<String, Object> params = new HashMap<String, Object>();
054        params.put("overwite", true);
055
056        OperationContext subctx = new OperationContext(ctx.getCoreSession(), null);
057        subctx.setInput(blob);
058        subctx.put("currentDocument", getDocumentModel().getPathAsString());
059
060        as.run(subctx, "FileManager.Import", params);
061        return Response.ok().build();
062    }
063}