PageRenderTime 88ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/db/objects/Favourite.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 152 lines | 73 code | 25 blank | 54 comment | 3 complexity | 22f87696ae98440a083d622ca91c0fe7 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.db.objects;
  18. import javax.swing.JComponent;
  19. import mpv5.db.common.Context;
  20. import mpv5.db.common.DatabaseObject;
  21. import mpv5.globals.Messages;
  22. import mpv5.ui.frames.MPView;
  23. import mpv5.db.objects.User;
  24. /**
  25. *
  26. *
  27. */
  28. public class Favourite extends DatabaseObject {
  29. private int usersids;
  30. private int itemsids;
  31. public Favourite() {
  32. setContext(Context.getFavourite());
  33. }
  34. public Favourite(DatabaseObject dato) {
  35. setContext(Context.getFavourite());
  36. this.setUsersids(mpv5.db.objects.User.getCurrentUser().__getIDS());
  37. setCname(dato.getDbIdentity());
  38. setItemsids(dato.__getIDS());
  39. }
  40. /**
  41. * @return the userid
  42. */
  43. public int __getUsersids() {
  44. return usersids;
  45. }
  46. /**
  47. * @param userid the userid to set
  48. */
  49. public void setUsersids(int userid) {
  50. this.usersids = userid;
  51. }
  52. /**
  53. * @return the itemid
  54. */
  55. public int __getItemsids() {
  56. return itemsids;
  57. }
  58. /**
  59. * @param itemid the itemid to set
  60. */
  61. public void setItemsids(int itemid) {
  62. this.itemsids = itemid;
  63. }
  64. /**
  65. * Checks if the given do is in the favourites list of the current logged in user
  66. * @param dato
  67. * @return
  68. */
  69. public static boolean isFavourite(DatabaseObject dato) {
  70. if (!mpv5.db.common.QueryHandler.instanceOf().clone(Context.getFavourite()).
  71. checkConstraint(new String[]{"cname", "usersids", "itemsids"},
  72. new Object[]{dato.getDbIdentity(), mpv5.db.objects.User.getCurrentUser().__getIDS(),dato.__getIDS()})) {
  73. return true;
  74. } else {
  75. return false;
  76. }
  77. }
  78. /**
  79. * Checks if the given do is in the favourites list of the current logged in user
  80. * and removes it
  81. * @param dato
  82. */
  83. public static void removeFavourite(DatabaseObject dato) {
  84. mpv5.db.common.QueryHandler.instanceOf().clone(Context.getFavourite()).
  85. delete(new String[]{"cname", "usersids", "itemsids"},
  86. new Object[]{dato.getDbIdentity(), mpv5.db.objects.User.getCurrentUser().__getIDS(),dato.__getIDS()}, Messages.DONE.toString());
  87. }
  88. /**
  89. *
  90. * @return The favourites list of the current logged in user
  91. */
  92. public static Favourite[] getUserFavourites() {
  93. Object[][] data = mpv5.db.common.QueryHandler.instanceOf().clone(Context.getFavourite()).select("cname, usersids, itemsids", new String[]{"usersids", mpv5.db.objects.User.getCurrentUser().__getIDS().toString(), ""});
  94. Favourite[] favs = new Favourite[data.length];
  95. for (int i = 0; i < favs.length; i++) {
  96. Favourite favi = new Favourite();
  97. favi.setCname(String.valueOf(data[i][0]));
  98. favi.setUsersids(Integer.valueOf(data[i][1].toString()));
  99. favi.setItemsids(Integer.valueOf(data[i][2].toString()));
  100. favs[i] = favi;
  101. }
  102. return favs;
  103. }
  104. /**
  105. * Returns the context of the do represented by this fav
  106. * @return
  107. */
  108. public Context getFavContext() {
  109. return Context.getMatchingContext(__getCname());
  110. }
  111. /**
  112. * Flushes the users favourites
  113. * @param user
  114. */
  115. public static void flush(User user) {
  116. mpv5.db.common.QueryHandler.instanceOf().clone(Context.getFavourite()).delete(new String[][]{{"usersids",user.__getIDS().toString(),""}},
  117. Messages.DONE.toString());
  118. }
  119. @Override
  120. public JComponent getView() {
  121. throw new UnsupportedOperationException("Not supported yet.");
  122. }
  123. @Override
  124. public mpv5.utils.images.MPIcon getIcon() {
  125. return null;
  126. }
  127. }