PageRenderTime 9ms CodeModel.GetById 22ms RepoModel.GetById 1ms 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
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 newContactsJob implements WSDaemonJob {
  43. private final WSDaemon daemon;
  44. /**
  45. * Create a new job
  46. * @param ddaemon
  47. */
  48. public newContactsJob(WSDaemon ddaemon) {
  49. this.daemon = ddaemon;
  50. }
  51. @Override
  52. public boolean isOneTimeJob() {
  53. return false;
  54. }
  55. @Override
  56. public boolean isDone() {
  57. return false;
  58. }
  59. @Override
  60. public void work(WSConnectionClient client) {
  61. Object itd = WSContactsMapping.getLastWsID(daemon.getWebShop());
  62. try {
  63. Object d = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_NEW_CONTACTS.toString(),
  64. new Object[]{itd}, new Object());
  65. List<Contact> obs = WSIManager.createObjects(d, new Contact());
  66. for (int i = 0; i < obs.size(); i++) {
  67. Contact contact = obs.get(i);
  68. int id = contact.__getIDS();
  69. WSContactsMapping m;
  70. try { // Check if the mapping already exists
  71. m = (WSContactsMapping) WSContactsMapping.getObject(Context.getWebShopContactMapping(),
  72. String.valueOf(id) + "@" + daemon.getWebShopID());
  73. Log.Debug(this,
  74. "Using exiting mapping to: " + contact.__getIDS() + ". Not going to create " + contact);
  75. } catch (NodataFoundException ex) {
  76. contact.setIDS(-1);
  77. contact.setGroupsids(daemon.getWebShop().__getGroupsids());
  78. contact.save();
  79. // If not, create one
  80. m = new WSContactsMapping();
  81. m.setContactsids(contact.__getIDS());
  82. m.setWscontact(String.valueOf(id));
  83. m.setCname(String.valueOf(id) + "@" + daemon.getWebShopID());
  84. m.setWebshopsids(daemon.getWebShopID());
  85. m.setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids());
  86. m.save();
  87. }
  88. }
  89. Object da = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_NEW_ADDRESSES.toString(),
  90. new Object[]{itd}, new Object());
  91. List<Address> aobs = WSIManager.createObjects(da, new Address());
  92. for (Address address : aobs) {
  93. try {
  94. WSContactsMapping m = WSContactsMapping.getMapping(daemon.getWebShopID(),
  95. address.__getContactsids());
  96. address.setContactsids(m.__getContactsids());
  97. address.setGroupsids(daemon.getWebShop().__getGroupsids());
  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