PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/webshopinterface/wsdjobs/getContactJob.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 139 lines | 87 code | 25 blank | 27 comment | 6 complexity | 63d6383182427739823fcc872a7d9c59 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.webshopinterface.wsdjobs;
  18. //~--- non-JDK imports --------------------------------------------------------
  19. import mpv5.db.common.Context;
  20. import mpv5.db.common.DatabaseObject;
  21. import mpv5.db.common.NodataFoundException;
  22. import mpv5.db.objects.Address;
  23. import mpv5.db.objects.Contact;
  24. import mpv5.db.objects.WSContactsMapping;
  25. import mpv5.logging.Log;
  26. import mpv5.ui.frames.MPView;
  27. import mpv5.utils.text.RandomStringUtils;
  28. import mpv5.webshopinterface.WSConnectionClient;
  29. import mpv5.webshopinterface.WSDaemon;
  30. import mpv5.webshopinterface.WSDaemonJob;
  31. import mpv5.webshopinterface.WSIManager;
  32. import org.apache.xmlrpc.XmlRpcException;
  33. //~--- JDK imports ------------------------------------------------------------
  34. import java.util.Date;
  35. import java.util.List;
  36. import java.util.logging.Level;
  37. import java.util.logging.Logger;
  38. import mpv5.ui.dialogs.Popup;
  39. /**
  40. * This job tries to fetch new contacts + adresses of them
  41. */
  42. public class getContactJob implements WSDaemonJob {
  43. private final WSDaemon daemon;
  44. private final String wscontactid;
  45. /**
  46. * Create a new job
  47. * @param ddaemon
  48. */
  49. public getContactJob(WSDaemon ddaemon, String wscontactid) {
  50. this.daemon = ddaemon;
  51. this.wscontactid = wscontactid;
  52. }
  53. @Override
  54. public boolean isOneTimeJob() {
  55. return false;
  56. }
  57. @Override
  58. public boolean isDone() {
  59. return false;
  60. }
  61. @Override
  62. public void work(WSConnectionClient client) {
  63. try {
  64. Object d = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_CONTACT.toString(),
  65. new Object[]{wscontactid,
  66. Boolean.TRUE}, new Object());
  67. List<Contact> obs = WSIManager.createObjects(d, new Contact());
  68. for (int i = 0; i < obs.size(); i++) {
  69. Contact contact = obs.get(i);
  70. int id = contact.__getIDS();
  71. WSContactsMapping m;
  72. try { // Check if the mapping already exists
  73. m = (WSContactsMapping) WSContactsMapping.getObject(Context.getWebShopContactMapping(),
  74. String.valueOf(id) + "@" + daemon.getWebShopID());
  75. Log.Debug(this,
  76. "Using exiting mapping to: " + contact.__getIDS() + ". Not going to create " + contact);
  77. } catch (NodataFoundException ex) {
  78. contact.setGroupsids(daemon.getWebShop().__getGroupsids());
  79. contact.saveImport();
  80. // If not, create one
  81. m = new WSContactsMapping();
  82. m.setContactsids(contact.__getIDS());
  83. m.setWscontact(String.valueOf(id));
  84. m.setCname(String.valueOf(id) + "@" + daemon.getWebShopID());
  85. m.setWebshopsids(daemon.getWebShopID());
  86. m.setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids());
  87. m.save();
  88. }
  89. }
  90. Object da = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_ADDRESSES.toString(),
  91. new Object[]{wscontactid}, new Object());
  92. List<Address> aobs = WSIManager.createObjects(da, new Address());
  93. for (Address address : aobs) {
  94. try {
  95. WSContactsMapping m = WSContactsMapping.getMapping(daemon.getWebShopID(),
  96. address.__getContactsids());
  97. address.setContactsids(m.__getContactsids());
  98. address.saveImport();
  99. } catch (NodataFoundException ex) {
  100. Log.Debug(this, ex.getMessage());
  101. }
  102. }
  103. if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) {
  104. Popup.notice(obs, "Saved contacts");
  105. }
  106. } catch (XmlRpcException ex) {
  107. Log.Debug(this, ex.getMessage());
  108. if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) {
  109. Popup.error(ex);
  110. }
  111. }
  112. }
  113. }
  114. //~ Formatted by Jindent --- http://www.jindent.com