001/*
002 * Copyright (c) 2006-2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Vladimir Pasquier <vpasquier@nuxeo.com>
011 */
012package org.nuxeo.ecm.automation.client.adapters;
013
014import java.io.IOException;
015
016import org.nuxeo.ecm.automation.client.Session;
017import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshalling;
018import org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers.PojoMarshaller;
019
020/**
021 * This automation client business service provides access to Business operations @{link BusinessCreateOperation} and
022 * @{link BusinessUpdateOperation}
023 *
024 * @since 5.7
025 */
026public class BusinessService<T> {
027
028    protected final Session session;
029
030    public BusinessService(Session session) {
031        this.session = session;
032    }
033
034    public Session getSession() {
035        return session;
036    }
037
038    /**
039     * Check if Client JsonMarshalling already contains o Marshaller
040     *
041     * @param o the pojo to add to Client JsonMarshalling
042     */
043    private void checkMarshaller(T o) {
044        if (JsonMarshalling.getMarshaller(o.getClass()) == null) {
045            JsonMarshalling.addMarshaller(PojoMarshaller.forClass(o.getClass()));
046        }
047    }
048
049    /**
050     * This method is calling @{BusinessCreateOperation}
051     *
052     * @param o the object to send (pojo client side)
053     * @param name the id/name of the NX document
054     * @param parentPath the path of the NX document parent
055     * @return the pojo returned by the server
056     */
057    @SuppressWarnings("unchecked")
058    public T create(T o, String name, String parentPath) throws IOException {
059        checkMarshaller(o);
060        return (T) session.newRequest("Business.BusinessCreateOperation").setInput(o).set("name", name).set(
061                "parentPath", parentPath).execute();
062    }
063
064    /**
065     * This method is calling @{BusinessUpdateOperation}
066     *
067     * @param o the object to send (pojo client side)
068     * @return the pojo returned by the server
069     */
070    public T update(T o) throws IOException {
071        checkMarshaller(o);
072        return (T) session.newRequest("Business.BusinessUpdateOperation").setInput(o).execute();
073    }
074
075    /**
076     * This method is calling @{BusinessFetchOperation}
077     *
078     * @param o the object to send (pojo client side)
079     * @return the pojo returned by the server
080     */
081    public T fetch(T o) throws IOException {
082        return (T) session.newRequest("Business.BusinessFetchOperation").setInput(o).execute();
083    }
084}