PageRenderTime 67ms CodeModel.GetById 39ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/webshopinterface/wsdjobs/newOrdersJob.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 199 lines | 137 code | 34 blank | 28 comment | 10 complexity | 16fcf2afef25e478075b31e7c1a1dd61 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.Contact;
 25import mpv5.db.objects.Item;
 26import mpv5.db.objects.SubItem;
 27import mpv5.db.objects.WSContactsMapping;
 28import mpv5.db.objects.WSItemsMapping;
 29
 30import mpv5.globals.Messages;
 31
 32import mpv5.logging.Log;
 33
 34import mpv5.ui.dialogs.Popup;
 35import mpv5.ui.frames.MPView;
 36
 37import mpv5.webshopinterface.WSConnectionClient;
 38import mpv5.webshopinterface.WSDaemon;
 39import mpv5.webshopinterface.WSDaemonJob;
 40import mpv5.webshopinterface.WSIManager;
 41
 42import org.apache.xmlrpc.XmlRpcException;
 43
 44//~--- JDK imports ------------------------------------------------------------
 45
 46import java.util.Date;
 47import java.util.List;
 48import java.util.Vector;
 49import java.util.logging.Level;
 50import java.util.logging.Logger;
 51
 52/**
 53 * This job tries to fetch new orders
 54 */
 55public class newOrdersJob implements WSDaemonJob {
 56
 57    private final WSDaemon daemon;
 58
 59    /**
 60     *  Create a new job
 61     * @param ddaemon
 62     */
 63    public newOrdersJob(WSDaemon ddaemon) {
 64        this.daemon = ddaemon;
 65    }
 66
 67    @Override
 68    public boolean isOneTimeJob() {
 69        return false;
 70    }
 71
 72    @Override
 73    public boolean isDone() {
 74        return false;
 75    }
 76
 77    @Override
 78    public void work(WSConnectionClient client) {
 79        WSContactsMapping md;
 80        Contact c;
 81        List<Item> savedOrders = new Vector<Item>();
 82
 83        try {
 84            Object itd = WSItemsMapping.getLastWsID(daemon.getWebShop());
 85            Object d = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_NEW_ORDERS.toString(),
 86                    new Object[]{itd}, new Object());
 87            List<Item> obs = WSIManager.createObjects(d, new Item());
 88
 89            for (Item order : obs) {
 90                int id = order.__getContactsids();
 91
 92                if (id == -1) {    // Guestaccount
 93                    Object d2 = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_CONTACT.toString(),
 94                            new Object[]{order.__getIDS(),
 95                                Boolean.FALSE}, new Object());
 96                    List<Contact> obs2 = WSIManager.createObjects(d2, new Contact());
 97
 98                    c = obs2.get(0);
 99                    c.setNotes(c.__getNotes() + "\nAuto generated guest account for webshop: "
100                            + daemon.getWebShop().__getCname());
101                    c.setMailaddress("NO-MAILS " + c.__getMailaddress());
102                    c.setGroupsids(daemon.getWebShop().__getGroupsids());
103                    c.saveImport();
104                } else {
105                    try {    // Look the contact up
106                        md = (WSContactsMapping) WSContactsMapping.getObject(Context.getWebShopContactMapping(),
107                                String.valueOf(id) + "@" + daemon.getWebShopID());
108
109                        try {
110                            c = (Contact) DatabaseObject.getObject(Context.getContact(), md.__getContactsids());
111                        } catch (NodataFoundException nodataFoundException) {
112                            throw new UnsupportedOperationException("Invalid contact mapping found: " + md);
113                        }
114                    } catch (NodataFoundException ex) {
115
116                        // Not yet created
117                        Object d2 =
118                                client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_CONTACT.toString(),
119                                new Object[]{id,
120                                    Boolean.TRUE}, new Object());
121                        List<Contact> obs2 = WSIManager.createObjects(d2, new Contact());
122
123                        c = obs2.get(0);
124                        c.setGroupsids(daemon.getWebShop().__getGroupsids());
125                        c.saveImport();
126                        md = new WSContactsMapping();
127                        md.setContactsids(c.__getIDS());
128                        md.setWscontact(String.valueOf(id));
129                        md.setCname(String.valueOf(id) + "@" + daemon.getWebShopID());
130                        md.setWebshopsids(daemon.getWebShopID());
131                        md.setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids());
132                        md.save();
133                    }
134                }
135
136                int wsitemids = order.__getIDS();
137
138                order.setContactsids(c.__getIDS());
139                order.setDescription(order.__getDescription() + "\nImport: " + daemon.getWebShop());
140                order.setGroupsids(daemon.getWebShop().__getGroupsids());
141                order.save();
142                savedOrders.add(order);
143
144                WSItemsMapping m = new WSItemsMapping();
145
146                m.setItemsids(order.__getIDS());
147                m.setWsitem(String.valueOf(wsitemids));
148                m.setCname(String.valueOf(order.__getIDS() + "@" + daemon.getWebShopID()));
149                m.setWebshopsids(daemon.getWebShopID());
150                m.setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids());
151                m.saveImport();
152
153                // Fetch the order details
154                Object da = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_ORDER_ROWS.toString(),
155                        new Object[]{String.valueOf(wsitemids)}, new Object());
156                List<SubItem> aobs = WSIManager.createObjects(da, new SubItem());
157
158                for (SubItem orderRow : aobs) {
159                    try {
160                        WSItemsMapping mk = (WSItemsMapping) DatabaseObject.getObject(Context.getWebShopItemMapping(),
161                                String.valueOf(orderRow.__getItemsids()));
162
163                        orderRow.setItemsids(mk.__getItemsids());
164                        orderRow.setGroupsids(daemon.getWebShop().__getGroupsids());
165                        orderRow.saveImport();
166                    } catch (NodataFoundException ex) {
167                        Log.Debug(ex);
168                    }
169                }
170            }
171
172            if (obs.size() > 0) {
173                mpv5.YabsViewProxy.instance().addMessage(obs.size() + " " + Messages.ORDERS_RECEIVED + " " + daemon.getWebShop());
174
175                if (Popup.Y_N_dialog(obs.size() + " " + Messages.ORDERS_RECEIVED + " " + daemon.getWebShop() + "\n"
176                        + Messages.LOAD_NOW)) {
177                    for (Item s : savedOrders) {
178                        try {
179                            mpv5.YabsViewProxy.instance().getIdentifierView().addTab(DatabaseObject.getObject(Context.getItem(),
180                                    s.__getIDS()));
181                        } catch (NodataFoundException ex) {
182                            Log.Debug(ex);    // Something must have failed during the import process
183                            Popup.error(ex);
184                        }
185                    }
186                }
187            }
188        } catch (XmlRpcException ex) {
189            Log.Debug(this, ex.getMessage());
190            if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) {
191                Popup.error(ex);
192            }
193        }
194    }
195}
196
197
198//~ Formatted by Jindent --- http://www.jindent.com
199