/src/mpv5/webshopinterface/wsdjobs/getContactJob.java
Java | 139 lines | 87 code | 25 blank | 27 comment | 6 complexity | 63d6383182427739823fcc872a7d9c59 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 getContactJob implements WSDaemonJob { 53 54 private final WSDaemon daemon; 55 private final String wscontactid; 56 57 /** 58 * Create a new job 59 * @param ddaemon 60 */ 61 public getContactJob(WSDaemon ddaemon, String wscontactid) { 62 this.daemon = ddaemon; 63 this.wscontactid = wscontactid; 64 } 65 66 @Override 67 public boolean isOneTimeJob() { 68 return false; 69 } 70 71 @Override 72 public boolean isDone() { 73 return false; 74 } 75 76 @Override 77 public void work(WSConnectionClient client) { 78 try { 79 Object d = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_CONTACT.toString(), 80 new Object[]{wscontactid, 81 Boolean.TRUE}, new Object()); 82 List<Contact> obs = WSIManager.createObjects(d, new Contact()); 83 84 for (int i = 0; i < obs.size(); i++) { 85 Contact contact = obs.get(i); 86 int id = contact.__getIDS(); 87 WSContactsMapping m; 88 89 try { // Check if the mapping already exists 90 m = (WSContactsMapping) WSContactsMapping.getObject(Context.getWebShopContactMapping(), 91 String.valueOf(id) + "@" + daemon.getWebShopID()); 92 Log.Debug(this, 93 "Using exiting mapping to: " + contact.__getIDS() + ". Not going to create " + contact); 94 } catch (NodataFoundException ex) { 95 contact.setGroupsids(daemon.getWebShop().__getGroupsids()); 96 contact.saveImport(); 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_ADDRESSES.toString(), 110 new Object[]{wscontactid}, 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.saveImport(); 120 } catch (NodataFoundException ex) { 121 Log.Debug(this, ex.getMessage()); 122 } 123 } 124 125 if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) { 126 Popup.notice(obs, "Saved contacts"); 127 } 128 } catch (XmlRpcException ex) { 129 Log.Debug(this, ex.getMessage()); 130 if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) { 131 Popup.error(ex); 132 } 133 } 134 } 135} 136 137 138//~ Formatted by Jindent --- http://www.jindent.com 139