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