001/*
002 * (C) Copyright 2014 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 *     vpasquier <vpasquier@nuxeo.com>
018 *     dmetzler <dmetzler@nuxeo.com>
019 */
020package org.nuxeo.box.api.folder;
021
022import org.nuxeo.box.api.adapter.BoxAdapter;
023import org.nuxeo.box.api.folder.adapter.BoxFolderAdapter;
024import org.nuxeo.box.api.marshalling.dao.BoxFolder;
025import org.nuxeo.box.api.marshalling.exceptions.BoxJSONException;
026import org.nuxeo.box.api.marshalling.jsonparsing.BoxJSONParser;
027import org.nuxeo.box.api.marshalling.jsonparsing.BoxResourceHub;
028import org.nuxeo.box.api.service.BoxService;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.DocumentNotFoundException;
032import org.nuxeo.ecm.core.api.IdRef;
033import org.nuxeo.ecm.webengine.model.WebObject;
034import org.nuxeo.ecm.webengine.model.impl.AbstractResource;
035import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl;
036import org.nuxeo.runtime.api.Framework;
037
038import javax.ws.rs.DELETE;
039import javax.ws.rs.GET;
040import javax.ws.rs.POST;
041import javax.ws.rs.PUT;
042import javax.ws.rs.Path;
043import javax.ws.rs.PathParam;
044import javax.ws.rs.Produces;
045import javax.ws.rs.core.MediaType;
046import java.lang.reflect.InvocationTargetException;
047import java.text.ParseException;
048
049/**
050 * WebObject for a Box Folder
051 *
052 * @since 5.9.2
053 */
054@WebObject(type = "folder")
055@Produces({ MediaType.APPLICATION_JSON })
056public class BoxFolderObject extends AbstractResource<ResourceTypeImpl> {
057
058    BoxService boxService;
059
060    @Override
061    public void initialize(Object... args) {
062        boxService = Framework.getLocalService(BoxService.class);
063    }
064
065    @GET
066    public Object doGet() {
067        return getView("index");
068    }
069
070    @GET
071    @Path("{folderId}")
072    public String doGetFolder(@PathParam("folderId") final String folderId) throws DocumentNotFoundException,
073            BoxJSONException {
074        final CoreSession session = ctx.getCoreSession();
075        final DocumentModel folder = "0".equals(folderId) ? session.getRootDocument() : session.getDocument(new IdRef(
076                folderId));
077        // Adapt nx document to box folder adapter
078        final BoxFolderAdapter folderAdapter = (BoxFolderAdapter) folder.getAdapter(BoxAdapter.class);
079        return boxService.toJSONString(folderAdapter.getBoxItem());
080    }
081
082    @POST
083    public String doPostFolder(String jsonBoxFolder) throws BoxJSONException {
084        final CoreSession session = ctx.getCoreSession();
085        BoxFolder boxFolder = boxService.getBoxFolder(jsonBoxFolder);
086        // Fetching its parent to get parent id
087        String parentId = boxFolder.getParent().getId();
088        DocumentModel documentParent;
089        if ("0".equals(parentId)) {
090            documentParent = session.getRootDocument();
091        } else {
092            documentParent = session.getDocument(new IdRef(boxFolder.getParent().getId()));
093        }
094        // Create the nx document from box folder information
095        DocumentModel newFolder = session.createDocumentModel(documentParent.getPathAsString(), boxFolder.getName(),
096                "Folder");
097        newFolder.setPropertyValue("dc:title", boxFolder.getName());
098        newFolder = session.createDocument(newFolder);
099        // Adapt nx document to box folder adapter
100        final BoxFolderAdapter folderAdapter = (BoxFolderAdapter) newFolder.getAdapter(BoxAdapter.class);
101        // Return the new box folder json
102        return boxService.toJSONString(folderAdapter.getBoxItem());
103    }
104
105    @PUT
106    @Path("{folderId}")
107    public String doPutFolder(@PathParam("folderId") String folderId, String jsonBoxFolder) throws
108            BoxJSONException, ParseException, IllegalAccessException, InvocationTargetException {
109        final CoreSession session = ctx.getCoreSession();
110        // Fetch the nx document with given id
111        final DocumentModel nxDocument = session.getDocument(new IdRef(folderId));
112        // Create box folder from json payload
113        BoxFolder boxFolderUpdated = new BoxJSONParser(new BoxResourceHub()).parseIntoBoxObject(jsonBoxFolder,
114                BoxFolder.class);
115        // Adapt nx document to box folder adapter
116        final BoxFolderAdapter nxDocumentAdapter = (BoxFolderAdapter) nxDocument.getAdapter(BoxAdapter.class);
117        // Update both nx document and box folder adapter
118        nxDocumentAdapter.setBoxItem(boxFolderUpdated);
119        nxDocumentAdapter.save(session);
120        // Return the new box folder json
121        return boxService.toJSONString(nxDocumentAdapter.getBoxItem());
122    }
123
124    @DELETE
125    @Path("{folderId}")
126    public void doDeleteFolder(@PathParam("folderId") String folderId) {
127        final CoreSession session = ctx.getCoreSession();
128        session.removeDocument(new IdRef(folderId));
129        session.save();
130    }
131
132    @Path("{folderId}/items")
133    public Object doGetItems(@PathParam("folderId") String folderId) {
134        return newObject("item", folderId);
135    }
136
137    @Path("{folderId}/collaborations")
138    public Object doGetCollaboration(@PathParam("folderId") String folderId) {
139        return newObject("collaborations", folderId);
140    }
141}