/src/mpv5/db/objects/WSContactsMapping.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 186 lines · 107 code · 18 blank · 61 comment · 6 complexity · 82cf92ea0f61b97861b6326de32a2f43 MD5 · raw file

  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.db.objects;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import java.util.Vector;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javax.swing.JComponent;
  24. import mpv5.db.common.Context;
  25. import mpv5.db.common.DatabaseObject;
  26. import mpv5.db.common.NodataFoundException;
  27. import mpv5.db.common.QueryCriteria;
  28. import mpv5.db.common.QueryHandler;
  29. import mpv5.db.common.ReturnValue;
  30. import mpv5.logging.Log;
  31. import mpv5.usermanagement.MPSecurityManager;
  32. import mpv5.utils.images.MPIcon;
  33. /**
  34. * Maps Contacts to WebShop Contacts
  35. */
  36. public class WSContactsMapping extends DatabaseObject {
  37. /**
  38. * Fetches a mapping from db
  39. * @param webShopID
  40. * @param contactsids
  41. * @return
  42. * @throws NodataFoundException
  43. */
  44. public static WSContactsMapping getMapping(int webShopID, int contactsids) throws NodataFoundException {
  45. QueryCriteria qs = new QueryCriteria("webshopid", webShopID);
  46. qs.addAndCondition("contactsids", contactsids);
  47. List old = DatabaseObject.getObjects(Context.getWebShopContactMapping(), qs);
  48. return (WSContactsMapping) old.get(0);
  49. }
  50. /**
  51. * Fetches all contacts from the db which:
  52. * <li>Have no current mapping to the given webshop
  53. * <li>Are in the same group as the webshop (ignored if shop is in "All" group)
  54. * @param webShop
  55. * @return A list of Contacts
  56. */
  57. public static List<Contact> getUnmappedContacts(WebShop webShop) {
  58. String query = Context.getContact().prepareSQLString("SELECT contacts.ids FROM contacts WHERE contacts.ids NOT IN " +
  59. "(SELECT contactsids FROM wscontactsmapping WHERE webshopsids = " + webShop.__getIDS() + ") ");
  60. if (webShop.__getGroupsids() > 1) {
  61. query += " AND groupsids = " + webShop.__getGroupsids();
  62. }
  63. ReturnValue ads = QueryHandler.instanceOf().clone(Context.getContact()).freeSelectQuery(query, MPSecurityManager.VIEW, null);
  64. Object[][] data = ads.getData();
  65. List<Contact> l = new Vector<Contact>();
  66. for (int i = 0; i < data.length; i++) {
  67. int ig = Integer.valueOf(data[i][0].toString());
  68. try {
  69. l.add((Contact) DatabaseObject.getObject(Context.getContact(), ig));
  70. } catch (NodataFoundException ex) {
  71. Log.Debug(WSContactsMapping.class, ex.getMessage());
  72. }
  73. }
  74. Log.Debug(WSContactsMapping.class, "Found unmapped contacts: " + l);
  75. return l;
  76. }
  77. /**
  78. * Tries to find the highest WS id, in the following order:
  79. * <li>If the IDs are {@link Number} values, returns the highest number
  80. * <li>If the values are non-number values, returns the highest values such as Collections.sort(List<String>, String.CASE_INSENSITIVE_ORDER) would return as last value
  81. * @param webShop
  82. * @return The String representation of the highest found number
  83. */
  84. public static String getLastWsID(WebShop webShop) {
  85. String query = Context.getWebShopContactMapping().prepareSQLString("SELECT wscontact FROM wscontactsmapping WHERE webshopsids = " + webShop.__getIDS());
  86. ReturnValue ads = QueryHandler.instanceOf().clone(Context.getWebShopContactMapping()).freeSelectQuery(query, MPSecurityManager.VIEW, null);
  87. Object[][] data = ads.getData();
  88. List<String> l = new Vector<String>();
  89. boolean stringvals = false;
  90. Integer hn = 0;
  91. for (int i = 0; i < data.length; i++) {
  92. try {
  93. Integer d = Integer.valueOf(data[i][0].toString());
  94. if (d > hn) {
  95. hn = d;
  96. }
  97. } catch (NumberFormatException numberFormatException) {
  98. stringvals = true;
  99. }
  100. l.add(String.valueOf(data[i][0].toString()));
  101. }
  102. if (stringvals) {
  103. Collections.sort(l, String.CASE_INSENSITIVE_ORDER);
  104. Log.Debug(WSContactsMapping.class, "Last String id: " + l.get(l.size()));
  105. return String.valueOf(l.get(l.size()));
  106. } else {
  107. Log.Debug(WSContactsMapping.class, "Last Number id: " + hn);
  108. return String.valueOf(hn);
  109. }
  110. }
  111. private int webshopsids;
  112. private int contactsids;
  113. private String wscontact = "";
  114. /**
  115. * Create a new mapping
  116. */
  117. public WSContactsMapping() {
  118. setContext(Context.getWebShopContactMapping());
  119. }
  120. @Override
  121. public JComponent getView() {
  122. return null;
  123. }
  124. @Override
  125. public MPIcon getIcon() {
  126. return null;
  127. }
  128. /**
  129. * @return the webshopsids
  130. */
  131. public int __getWebshopsids() {
  132. return webshopsids;
  133. }
  134. /**
  135. * @param webshopsids the webshopsids to set
  136. */
  137. public void setWebshopsids(int webshopsids) {
  138. this.webshopsids = webshopsids;
  139. }
  140. /**
  141. * @return the contactsids
  142. */
  143. public int __getContactsids() {
  144. return contactsids;
  145. }
  146. /**
  147. * @param contactsids the contactsids to set
  148. */
  149. public void setContactsids(int contactsids) {
  150. this.contactsids = contactsids;
  151. }
  152. /**
  153. * @return the wscontact
  154. */
  155. public String __getWscontact() {
  156. return wscontact;
  157. }
  158. /**
  159. * @param wscontact the wscontact to set
  160. */
  161. public void setWscontact(String wscontact) {
  162. this.wscontact = wscontact;
  163. }
  164. @Override
  165. public boolean save() {
  166. return super.save(true);
  167. }
  168. }