PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/db/objects/Group.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 195 lines | 121 code | 17 blank | 57 comment | 13 complexity | b2ba142e864fbf2d3605441e46d45ed6 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 java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.swing.JComponent;
  23. import mpv5.db.common.Context;
  24. import mpv5.db.common.DatabaseObject;
  25. import mpv5.db.common.NodataFoundException;
  26. import mpv5.logging.Log;
  27. import mpv5.ui.dialogs.subcomponents.ControlPanel_Groups;
  28. /**
  29. *
  30. *
  31. */
  32. public class Group extends DatabaseObject {
  33. /**
  34. * Returns 'All Group' or the first group found if 'All group' is gone, and
  35. * if all are deleted, creates a default group
  36. *
  37. * @return A group, never NULL
  38. */
  39. public static Group getDefault() {
  40. try {
  41. return (Group) DatabaseObject.getObject(Context.getGroup(), 1);
  42. } catch (NodataFoundException ex) {
  43. Log.Debug(ex);
  44. try {
  45. return (Group) DatabaseObject.getObjects(Context.getGroup()).get(0);
  46. } catch (NodataFoundException nodataFoundException) {
  47. Group g = new Group();
  48. g.setCname("All Group#");
  49. g.save();
  50. return g;
  51. }
  52. }
  53. }
  54. private String description = "";
  55. private String defaults = "";
  56. private String hierarchypath = "";
  57. public static String GROUPSEPARATOR = " - ";
  58. public Group() {
  59. setContext(Context.getGroup());
  60. }
  61. public Group(String name) {
  62. setContext(Context.getGroup());
  63. setCname(name);
  64. }
  65. /**
  66. * @return the description
  67. */
  68. public String __getDescription() {
  69. return description;
  70. }
  71. /**
  72. * @param description the description to set
  73. */
  74. public void setDescription(String description) {
  75. this.description = description;
  76. }
  77. /**
  78. * @return the defaultvalue
  79. */
  80. public String __getDefaults() {
  81. return defaults;
  82. }
  83. /**
  84. * @param defaultvalue the defaultvalue to set
  85. */
  86. public void setDefaults(String defaultvalue) {
  87. this.defaults = defaultvalue;
  88. }
  89. @Override
  90. public String toString() {
  91. return __getCname();
  92. }
  93. @Override
  94. public boolean delete() {
  95. try {
  96. List<Group> childs = DatabaseObject.getReferencedObjects(this, Context.getGroup());
  97. for (int i = 0; i < childs.size(); i++) {
  98. DatabaseObject databaseObject = childs.get(i);
  99. if (!databaseObject.delete()) {
  100. return false;
  101. }
  102. }
  103. } catch (NodataFoundException ex) {
  104. mpv5.logging.Log.Debug(ex);//Logger.getLogger(Group.class.getName()).log(Level.SEVERE, null, ex);
  105. }
  106. try {
  107. return super.delete();
  108. } catch (Exception e) {
  109. return false;
  110. }
  111. }
  112. @Override
  113. public JComponent getView() {
  114. // MPControlPanel p = (MPControlPanel) MPControlPanel.instanceOf();
  115. // p.openDetails(new ControlPanel_Groups(this));
  116. return new ControlPanel_Groups(this);
  117. }
  118. @Override
  119. public mpv5.utils.images.MPIcon getIcon() {
  120. return null;
  121. }
  122. /**
  123. * @return the hierarchypath
  124. */
  125. public synchronized String __getHierarchypath() {
  126. if (hierarchypath == null || hierarchypath.equals("")) {
  127. int intp = __getIDS();
  128. int lastp = 0;
  129. if (!isExisting()) {
  130. return "/";
  131. }
  132. do {
  133. try {
  134. Group p = (Group) getObject(Context.getGroup(), intp);
  135. hierarchypath = Group.GROUPSEPARATOR + p + hierarchypath;
  136. intp = p.__getGroupsids();
  137. lastp = p.__getIDS();
  138. System.err.println("intp = " + intp + " lastp =" + lastp);
  139. } catch (NodataFoundException ex) {
  140. // Log.Debug(ex);
  141. break;
  142. }
  143. } while (intp > 0 && intp != lastp);
  144. return hierarchypath.replaceFirst(Group.GROUPSEPARATOR, "");
  145. }
  146. return hierarchypath;
  147. }
  148. /**
  149. * @param hierarchypath the hierarchypath to set
  150. */
  151. public void setHierarchypath(String hierarchypath) {
  152. this.hierarchypath = hierarchypath;
  153. }
  154. /**
  155. * Find the children and sub-children of this group
  156. *
  157. * @return
  158. * @throws NodataFoundException
  159. */
  160. public List<Group> getChildGroups() throws NodataFoundException {
  161. List<Group> childs = DatabaseObject.getReferencedObjects(this, Context.getGroup());
  162. for (int i = 0; i < childs.size(); i++) {
  163. Group databaseObject = childs.get(i);
  164. childs.addAll(databaseObject.getChildGroups());
  165. }
  166. if (Log.LOGLEVEL_DEBUG == Log.getLoglevel()) {
  167. Log.Debug(this, childs);
  168. }
  169. return childs;
  170. }
  171. /**
  172. *
  173. * @return True if the group has no parent group
  174. */
  175. public boolean isRoot() {
  176. return __getGroupsids() == 0;
  177. }
  178. }