001/*
002 * (C) Copyright 2006-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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
018 */
019package org.nuxeo.ecm.automation.client.adapters;
020
021import java.io.IOException;
022
023import org.nuxeo.ecm.automation.client.Session;
024import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshalling;
025import org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers.PojoMarshaller;
026
027/**
028 * This automation client business service provides access to Business operations @{link BusinessCreateOperation} and
029 * @{link BusinessUpdateOperation}
030 *
031 * @since 5.7
032 */
033public class BusinessService<T> {
034
035    protected final Session session;
036
037    public BusinessService(Session session) {
038        this.session = session;
039    }
040
041    public Session getSession() {
042        return session;
043    }
044
045    /**
046     * Check if Client JsonMarshalling already contains o Marshaller
047     *
048     * @param o the pojo to add to Client JsonMarshalling
049     */
050    private void checkMarshaller(T o) {
051        if (JsonMarshalling.getMarshaller(o.getClass()) == null) {
052            JsonMarshalling.addMarshaller(PojoMarshaller.forClass(o.getClass()));
053        }
054    }
055
056    /**
057     * This method is calling @{BusinessCreateOperation}
058     *
059     * @param o the object to send (pojo client side)
060     * @param name the id/name of the NX document
061     * @param parentPath the path of the NX document parent
062     * @return the pojo returned by the server
063     */
064    @SuppressWarnings("unchecked")
065    public T create(T o, String name, String parentPath) throws IOException {
066        checkMarshaller(o);
067        return (T) session.newRequest("Business.BusinessCreateOperation").setInput(o).set("name", name).set(
068                "parentPath", parentPath).execute();
069    }
070
071    /**
072     * This method is calling @{BusinessUpdateOperation}
073     *
074     * @param o the object to send (pojo client side)
075     * @return the pojo returned by the server
076     */
077    public T update(T o) throws IOException {
078        checkMarshaller(o);
079        return (T) session.newRequest("Business.BusinessUpdateOperation").setInput(o).execute();
080    }
081
082    /**
083     * This method is calling @{BusinessFetchOperation}
084     *
085     * @param o the object to send (pojo client side)
086     * @return the pojo returned by the server
087     */
088    public T fetch(T o) throws IOException {
089        return (T) session.newRequest("Business.BusinessFetchOperation").setInput(o).execute();
090    }
091}