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