PageRenderTime 113ms CodeModel.GetById 50ms app.highlight 9ms RepoModel.GetById 52ms app.codeStats 0ms

/src/mpv5/webshopinterface/wsdjobs/updatedContactsJob.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 131 lines | 81 code | 24 blank | 26 comment | 7 complexity | bbf8c57af39aad3a1592b38fa8e705cd 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.common.QueryCriteria;
 25import mpv5.db.objects.Address;
 26import mpv5.db.objects.Contact;
 27import mpv5.db.objects.WSContactsMapping;
 28
 29import mpv5.logging.Log;
 30
 31import mpv5.ui.frames.MPView;
 32
 33import mpv5.webshopinterface.WSConnectionClient;
 34import mpv5.webshopinterface.WSDaemon;
 35import mpv5.webshopinterface.WSDaemonJob;
 36import mpv5.webshopinterface.WSIManager;
 37
 38import org.apache.xmlrpc.XmlRpcException;
 39
 40//~--- JDK imports ------------------------------------------------------------
 41
 42import java.util.Date;
 43import java.util.List;
 44import java.util.logging.Level;
 45import java.util.logging.Logger;
 46import mpv5.ui.dialogs.Popup;
 47
 48/**
 49 * This job tries to fetch updated contacts + adresses of them
 50 */
 51public class updatedContactsJob implements WSDaemonJob {
 52
 53    private final WSDaemon daemon;
 54
 55    /**
 56     *  Create a new job
 57     * @param ddaemon
 58     */
 59    public updatedContactsJob(WSDaemon ddaemon) {
 60        this.daemon = ddaemon;
 61    }
 62
 63    @Override
 64    public boolean isOneTimeJob() {
 65        return false;
 66    }
 67
 68    @Override
 69    public boolean isDone() {
 70        return false;
 71    }
 72
 73    @Override
 74    public void work(WSConnectionClient client) {
 75        try {
 76            Object d = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_CHANGED_CONTACTS.toString(),
 77                    new Object[]{}, new Object());
 78            List<Contact> obs = WSIManager.createObjects(d, new Contact());
 79
 80            for (int i = 0; i < obs.size(); i++) {
 81                Contact contact = obs.get(i);
 82                int id = contact.__getIDS();
 83                WSContactsMapping m = null;
 84
 85                try {
 86                    m = WSContactsMapping.getMapping(daemon.getWebShopID(), id);
 87                    contact.setIDS(m.__getContactsids());
 88                    contact.save();
 89                } catch (NodataFoundException ex) {
 90                    throw new UnsupportedOperationException("Invalid contact mapping found: " + id);
 91                }
 92
 93                Object da =
 94                        client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_CHANGED_ADRESSES.toString(),
 95                        new Object[]{m.__getWscontact()}, new Object());
 96                List<Address> aobs = WSIManager.createObjects(da, new Address());
 97
 98                for (Address address : aobs) {
 99                    try {
100                        QueryCriteria qs = new QueryCriteria("cname", address.__getCname());
101                        qs.addAndCondition("prename", address.__getPrename());
102                        qs.addAndCondition("contactsids", address.__getContactsids());
103
104                        List<DatabaseObject> old = DatabaseObject.getObjects(Context.getAddress(), qs);
105
106                        for (int ix = 0; ix < old.size(); ix++) {
107                            old.get(ix).delete();
108                        }
109
110                        address.setContactsids(m.__getContactsids());
111                        address.saveImport();
112                    } catch (NodataFoundException ex) {
113                        Log.Debug(ex);
114                    }
115                }
116            }
117            if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) {
118                Popup.notice(obs, "Updated contacts");
119            }
120        } catch (XmlRpcException ex) {
121            Log.Debug(this, ex.getMessage());
122            if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) {
123                Popup.error(ex);
124            }
125        }
126    }
127}
128
129
130//~ Formatted by Jindent --- http://www.jindent.com
131