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.item;
019
020import com.google.common.base.Objects;
021import org.nuxeo.box.api.BoxConstants;
022import org.nuxeo.box.api.adapter.BoxAdapter;
023import org.nuxeo.box.api.folder.adapter.BoxFolderAdapter;
024import org.nuxeo.box.api.marshalling.dao.BoxCollection;
025import org.nuxeo.box.api.marshalling.exceptions.BoxJSONException;
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.IdRef;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.webengine.WebException;
032import org.nuxeo.ecm.webengine.model.WebObject;
033import org.nuxeo.ecm.webengine.model.impl.AbstractResource;
034import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl;
035import org.nuxeo.runtime.api.Framework;
036
037import javax.ws.rs.GET;
038import javax.ws.rs.Produces;
039import javax.ws.rs.QueryParam;
040import javax.ws.rs.core.MediaType;
041
042/**
043 * WebObject for a Box Folder Item
044 *
045 * @since 5.9.2
046 */
047@WebObject(type = "item")
048@Produces({ MediaType.APPLICATION_JSON })
049public class BoxItemObject extends AbstractResource<ResourceTypeImpl> {
050
051    BoxFolderAdapter folderAdapter;
052
053    BoxService boxService;
054
055    @Override
056    public void initialize(Object... args) {
057        boxService = Framework.getLocalService(BoxService.class);
058        try {
059            String folderId = (String) args[0];
060            CoreSession session = ctx.getCoreSession();
061            DocumentModel folder = session.getDocument(new IdRef(folderId));
062            folderAdapter = (BoxFolderAdapter) folder.getAdapter(BoxAdapter.class);
063        } catch (NuxeoException e) {
064            throw WebException.wrap(e);
065        }
066        setRoot(true);
067    }
068
069    @GET
070    public String doGetItems(@QueryParam("offset") String offset, @QueryParam("limit") String limit,
071            @QueryParam("fields") String fields) throws BoxJSONException {
072        CoreSession session = ctx.getCoreSession();
073        BoxCollection itemCollection = folderAdapter.getItemCollection(session,
074                Objects.firstNonNull(limit, BoxConstants.BOX_LIMIT),
075                Objects.firstNonNull(offset, BoxConstants.BOX_OFFSET),
076                Objects.firstNonNull(fields, BoxConstants.BOX_FIELDS));
077        return boxService.toJSONString(itemCollection);
078    }
079
080}