PageRenderTime 214ms CodeModel.GetById 115ms app.highlight 8ms RepoModel.GetById 89ms app.codeStats 0ms

/src/mpv5/webshopinterface/WSConnectionClient.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 136 lines | 60 code | 29 blank | 47 comment | 1 complexity | f3b7e239cfdc2d384344389a990ec147 MD5 | raw file
  1
  2/*
  3*  This file is part of YaBS.
  4*
  5*      YaBS is free software: you can redistribute it and/or modify
  6*      it under the terms of the GNU General Public License as published by
  7*      the Free Software Foundation, either version 3 of the License, or
  8*      (at your option) any later version.
  9*
 10*      YaBS is distributed in the hope that it will be useful,
 11*      but WITHOUT ANY WARRANTY; without even the implied warranty of
 12*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13*      GNU General Public License for more details.
 14*
 15*      You should have received a copy of the GNU General Public License
 16*      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 17 */
 18package mpv5.webshopinterface;
 19
 20//~--- non-JDK imports --------------------------------------------------------
 21
 22import mpv5.globals.Constants;
 23
 24import mpv5.logging.Log;
 25
 26import mpv5.ui.dialogs.Popup;
 27
 28import mpv5.utils.xml.XMLRpcClient;
 29
 30import org.apache.xmlrpc.XmlRpcException;
 31
 32//~--- JDK imports ------------------------------------------------------------
 33
 34import java.net.URL;
 35
 36import java.util.Arrays;
 37import java.util.List;
 38import java.util.Vector;
 39
 40/**
 41 * This class acts as connector to webshops using the WebShopAPI specified at
 42 * http://code.google.com/p/mp-rechnungs-und-kundenverwaltung/wiki/WebShopInterfaceSpecs
 43 */
 44public class WSConnectionClient {
 45    private XMLRpcClient client;
 46
 47    /**
 48     * Create a new connection to the specified url.<br/>
 49     * e.g. new URL("http://127.0.0.1:8080/xmlrpc")
 50     * @param host
 51     * @param requCompression
 52     * @throws NoCompatibleHostFoundException
 53     */
 54    public WSConnectionClient(final URL host, boolean requCompression, String user, String pw)
 55            throws NoCompatibleHostFoundException {
 56        if (!connect(host, requCompression, user, pw)) {
 57            throw new NoCompatibleHostFoundException(host);
 58        }
 59    }
 60
 61    /**
 62     * Contains all known xml-rpc commands
 63     */
 64    public static enum COMMANDS {
 65        GETVERSION("getYWSIVersion"), GET_NEW_CONTACTS("getNewContacts"), GET_CONTACT("getContact"),
 66        GET_NEW_ADDRESSES("getNewAddresses"), GET_ADDRESSES("getAddresses"), GET_NEW_ORDERS("getNewOrders"),
 67        GET_ORDER_ROWS("getOrderRows"),
 68
 69        // //////////////////////////////////////////////////////////////////////
 70        GET_NEW_SYSTEM_MESSAGES("getNewSystemMessages"), GET_CHANGED_CONTACTS("getUpdatedContacts"),
 71        GET_CHANGED_ADRESSES("getUpdatedAdresses"), GET_CHANGED_ORDERS("getUpdatedOrders"),
 72        GET_CHANGED_ORDER_ROWS("getUpdatedOrderRows"),
 73
 74        // //////////////////////////////////////////////////////////////////////
 75        ADD_NEW_PRODUCT("addNewProduct"), ADD_NEW_CONTACT("addNewContact"), SET_ORDER_STATUS("setOrderStatus"),
 76        SET_DISABLED_CONTACT("setDisabledContact"), SET_DISABLED_PRODUCT("setDisabledProduct");
 77
 78        String command;
 79
 80        private COMMANDS(String command) {
 81            this.command = command;
 82        }
 83
 84        /**
 85         * Overriden to return the actual command
 86         * @return The command specified in this enum
 87         */
 88        @Override
 89        public String toString() {
 90            return command;
 91        }
 92    }
 93
 94    /**
 95     * @return the client
 96     */
 97    public XMLRpcClient getClient() {
 98        return client;
 99    }
100
101    /**
102     * Test the shop & runs system.listMethods
103     * @return The shop impl version
104     * @throws XmlRpcException If the test fails
105     */
106    @SuppressWarnings("unchecked")
107    public String test() throws XmlRpcException {
108        Object   o          = client.execute("system.listMethods", new Object[0]);
109        Object[] methodList = (Object[]) o;
110
111        Arrays.sort(methodList);
112        Popup.notice(Arrays.asList(methodList), "Supported methods:\n");
113
114        Object[] params = new Object[] { Constants.RELEASE_VERSION };
115        Integer  v      = (Integer) getClient().execute(COMMANDS.GETVERSION.toString(), params);
116
117        return "Server XML RPC Yabs Version : " + v;
118    }
119
120    private boolean connect(URL host, boolean requCompression, String user, String pw) {
121        client = new XMLRpcClient(host, requCompression, user, pw);
122
123        try {
124            Log.Debug(this, test());
125
126            return true;
127        } catch (XmlRpcException ex) {
128            Log.Debug(this, ex.getMessage());
129
130            return false;
131        }
132    }
133}
134
135
136//~ Formatted by Jindent --- http://www.jindent.com