PageRenderTime 103ms CodeModel.GetById 41ms app.highlight 10ms RepoModel.GetById 50ms app.codeStats 0ms

/src/mpv5/webshopinterface/wsdjobs/newContactsJob.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 138 lines | 87 code | 24 blank | 27 comment | 6 complexity | d0de0c174dc854bd629528a446061e94 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.wsdjobs;
 19
 20//~--- non-JDK imports --------------------------------------------------------
 21import mpv5.db.common.Context;
 22import mpv5.db.common.DatabaseObject;
 23import mpv5.db.common.NodataFoundException;
 24import mpv5.db.objects.Address;
 25import mpv5.db.objects.Contact;
 26import mpv5.db.objects.WSContactsMapping;
 27
 28import mpv5.logging.Log;
 29
 30import mpv5.ui.frames.MPView;
 31
 32import mpv5.utils.text.RandomStringUtils;
 33
 34import mpv5.webshopinterface.WSConnectionClient;
 35import mpv5.webshopinterface.WSDaemon;
 36import mpv5.webshopinterface.WSDaemonJob;
 37import mpv5.webshopinterface.WSIManager;
 38
 39import org.apache.xmlrpc.XmlRpcException;
 40
 41//~--- JDK imports ------------------------------------------------------------
 42
 43import java.util.Date;
 44import java.util.List;
 45import java.util.logging.Level;
 46import java.util.logging.Logger;
 47import mpv5.ui.dialogs.Popup;
 48
 49/**
 50 * This job tries to fetch new contacts + adresses of them
 51 */
 52public class newContactsJob implements WSDaemonJob {
 53
 54    private final WSDaemon daemon;
 55
 56    /**
 57     *  Create a new job
 58     * @param ddaemon
 59     */
 60    public newContactsJob(WSDaemon ddaemon) {
 61        this.daemon = ddaemon;
 62    }
 63
 64    @Override
 65    public boolean isOneTimeJob() {
 66        return false;
 67    }
 68
 69    @Override
 70    public boolean isDone() {
 71        return false;
 72    }
 73
 74    @Override
 75    public void work(WSConnectionClient client) {
 76        Object itd = WSContactsMapping.getLastWsID(daemon.getWebShop());
 77
 78        try {
 79            Object d = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_NEW_CONTACTS.toString(),
 80                    new Object[]{itd}, new Object());
 81            List<Contact> obs = WSIManager.createObjects(d, new Contact());
 82
 83            for (int i = 0; i < obs.size(); i++) {
 84                Contact contact = obs.get(i);
 85                int id = contact.__getIDS();
 86                WSContactsMapping m;
 87
 88                try {    // Check if the mapping already exists
 89                    m = (WSContactsMapping) WSContactsMapping.getObject(Context.getWebShopContactMapping(),
 90                            String.valueOf(id) + "@" + daemon.getWebShopID());
 91                    Log.Debug(this,
 92                            "Using exiting mapping to: " + contact.__getIDS() + ". Not going to create " + contact);
 93                } catch (NodataFoundException ex) {
 94                    contact.setIDS(-1);
 95                    contact.setGroupsids(daemon.getWebShop().__getGroupsids());
 96                    contact.save();
 97
 98                    // If not, create one
 99                    m = new WSContactsMapping();
100                    m.setContactsids(contact.__getIDS());
101                    m.setWscontact(String.valueOf(id));
102                    m.setCname(String.valueOf(id) + "@" + daemon.getWebShopID());
103                    m.setWebshopsids(daemon.getWebShopID());
104                    m.setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids());
105                    m.save();
106                }
107            }
108
109            Object da = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_NEW_ADDRESSES.toString(),
110                    new Object[]{itd}, new Object());
111            List<Address> aobs = WSIManager.createObjects(da, new Address());
112
113            for (Address address : aobs) {
114                try {
115                    WSContactsMapping m = WSContactsMapping.getMapping(daemon.getWebShopID(),
116                            address.__getContactsids());
117
118                    address.setContactsids(m.__getContactsids());
119                    address.setGroupsids(daemon.getWebShop().__getGroupsids());
120                    address.saveImport();
121                } catch (NodataFoundException ex) {
122                    Log.Debug(this, ex.getMessage());
123                }
124            }
125
126            if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) {
127                Popup.notice(obs, "Saved contacts");
128            }
129        } catch (XmlRpcException ex) {
130            Log.Debug(this, ex.getMessage());
131            if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) {
132                Popup.error(ex);
133            }
134        }
135    }
136}
137//~ Formatted by Jindent --- http://www.jindent.com
138