PageRenderTime 19ms CodeModel.GetById 13ms RepoModel.GetById 0ms 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
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.common.QueryCriteria;
  23. import mpv5.db.objects.Address;
  24. import mpv5.db.objects.Contact;
  25. import mpv5.db.objects.WSContactsMapping;
  26. import mpv5.logging.Log;
  27. import mpv5.ui.frames.MPView;
  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 updated contacts + adresses of them
  41. */
  42. public class updatedContactsJob implements WSDaemonJob {
  43. private final WSDaemon daemon;
  44. /**
  45. * Create a new job
  46. * @param ddaemon
  47. */
  48. public updatedContactsJob(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. try {
  62. Object d = client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_CHANGED_CONTACTS.toString(),
  63. new Object[]{}, new Object());
  64. List<Contact> obs = WSIManager.createObjects(d, new Contact());
  65. for (int i = 0; i < obs.size(); i++) {
  66. Contact contact = obs.get(i);
  67. int id = contact.__getIDS();
  68. WSContactsMapping m = null;
  69. try {
  70. m = WSContactsMapping.getMapping(daemon.getWebShopID(), id);
  71. contact.setIDS(m.__getContactsids());
  72. contact.save();
  73. } catch (NodataFoundException ex) {
  74. throw new UnsupportedOperationException("Invalid contact mapping found: " + id);
  75. }
  76. Object da =
  77. client.getClient().invokeGetCommand(WSConnectionClient.COMMANDS.GET_CHANGED_ADRESSES.toString(),
  78. new Object[]{m.__getWscontact()}, new Object());
  79. List<Address> aobs = WSIManager.createObjects(da, new Address());
  80. for (Address address : aobs) {
  81. try {
  82. QueryCriteria qs = new QueryCriteria("cname", address.__getCname());
  83. qs.addAndCondition("prename", address.__getPrename());
  84. qs.addAndCondition("contactsids", address.__getContactsids());
  85. List<DatabaseObject> old = DatabaseObject.getObjects(Context.getAddress(), qs);
  86. for (int ix = 0; ix < old.size(); ix++) {
  87. old.get(ix).delete();
  88. }
  89. address.setContactsids(m.__getContactsids());
  90. address.saveImport();
  91. } catch (NodataFoundException ex) {
  92. Log.Debug(ex);
  93. }
  94. }
  95. }
  96. if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) {
  97. Popup.notice(obs, "Updated contacts");
  98. }
  99. } catch (XmlRpcException ex) {
  100. Log.Debug(this, ex.getMessage());
  101. if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) {
  102. Popup.error(ex);
  103. }
  104. }
  105. }
  106. }
  107. //~ Formatted by Jindent --- http://www.jindent.com