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