001/*
002 * (C) Copyright 2013 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 *     dmetzler
016 */
017package org.nuxeo.ecm.restapi.server.jaxrs.adapters;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import javax.ws.rs.Consumes;
023import javax.ws.rs.GET;
024import javax.ws.rs.POST;
025import javax.ws.rs.PUT;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.Produces;
029import javax.ws.rs.core.GenericEntity;
030import javax.ws.rs.core.Response;
031import javax.ws.rs.core.Response.Status;
032
033import org.nuxeo.ecm.automation.core.operations.business.adapter.BusinessAdapter;
034import org.nuxeo.ecm.automation.core.util.Paginable;
035import org.nuxeo.ecm.automation.io.services.codec.ObjectCodec;
036import org.nuxeo.ecm.automation.io.services.codec.ObjectCodecService;
037import org.nuxeo.ecm.automation.jaxrs.DefaultJsonAdapter;
038import org.nuxeo.ecm.automation.jaxrs.io.documents.PaginableWithDelegate;
039import org.nuxeo.ecm.core.api.CoreSession;
040import org.nuxeo.ecm.core.api.DocumentModel;
041import org.nuxeo.ecm.core.api.DocumentModelList;
042import org.nuxeo.ecm.core.rest.DocumentObject;
043import org.nuxeo.ecm.webengine.WebException;
044import org.nuxeo.ecm.webengine.model.WebAdapter;
045import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
046import org.nuxeo.runtime.api.Framework;
047
048/**
049 * Basic CRUD with a BusinessAdapter
050 *
051 * @since 5.7.2
052 */
053@WebAdapter(name = BOAdapter.NAME, type = "BOService", targetType = "Document")
054@Produces({ "application/json+nxentity", "application/json+esentity", "application/json" })
055public class BOAdapter extends DefaultAdapter {
056
057    public static final String NAME = "bo";
058
059    @GET
060    @Path("{adapterName}")
061    public Object doGetAdapter(@PathParam("adapterName") String adapterName) {
062        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
063        if (doc != null) {
064            BusinessAdapter adapter = getAdapter(adapterName, doc);
065            return new DefaultJsonAdapter(adapter);
066        }
067
068        DocumentModelList list = getTarget().getAdapter(DocumentModelList.class);
069        if (list != null) {
070            return doGetAdapterOnList(list, adapterName);
071        }
072
073        return Response.status(Status.BAD_REQUEST).entity("Adapter can only be executed on Document or DocumentList").build();
074
075    }
076
077    /**
078     * @param list
079     * @param adapterName
080     * @return
081     * @since 5.8
082     */
083    @SuppressWarnings({ "unchecked", "rawtypes" })
084    private Object doGetAdapterOnList(DocumentModelList list, String adapterName) {
085
086        List<BusinessAdapter> adapters;
087        if (list instanceof Paginable) {
088            adapters = new PaginableWithDelegate((Paginable<DocumentModel>) list);
089        } else {
090            adapters = new ArrayList<BusinessAdapter>();
091        }
092
093        for (DocumentModel docItem : list) {
094            adapters.add(getAdapter(adapterName, docItem));
095        }
096        GenericEntity<List<BusinessAdapter>> entity = new GenericEntity<List<BusinessAdapter>>(adapters) {
097        };
098        return entity;
099    }
100
101    @PUT
102    @Path("{adapterName}")
103    @Consumes({ "application/json+nxentity", "application/json" })
104    public Object doPostAdapter(@PathParam("adapterName") String adapterName, BusinessAdapter input) {
105        ctx.getCoreSession().saveDocument(input.getDocument());
106
107        ctx.getCoreSession().save();
108
109        return new DefaultJsonAdapter(input);
110    }
111
112    @POST
113    @Path("{adapterName}/{docName}")
114    public Object doPutAdapter(@PathParam("adapterName") String adapterName, @PathParam("docName") String docName,
115            BusinessAdapter input) {
116        DocumentModel document = input.getDocument();
117
118        DocumentObject dobj = (DocumentObject) getTarget();
119        DocumentModel parentDoc = dobj.getDocument();
120
121        document.setPathInfo(parentDoc.getPathAsString(), docName);
122        CoreSession session = ctx.getCoreSession();
123        document = session.createDocument(document);
124        session.save();
125        BusinessAdapter adapter = document.getAdapter(input.getClass());
126        return new DefaultJsonAdapter(adapter);
127    }
128
129    private BusinessAdapter getAdapter(String adapterName, DocumentModel doc) {
130        ObjectCodecService cs = Framework.getLocalService(ObjectCodecService.class);
131        ObjectCodec<?> codec = cs.getCodec(adapterName);
132        if (codec != null) {
133            return (BusinessAdapter) doc.getAdapter(codec.getJavaType());
134        } else {
135            throw new WebException(String.format("Unable to find [%s] adapter", adapterName));
136        }
137
138    }
139
140}