/src/mpv5/db/objects/ProductGroup.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 228 lines · 149 code · 24 blank · 55 comment · 16 complexity · 1216c34c8e577d584fa3d2763b1cbf0f 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.ArrayList;
  19. import java.util.Enumeration;
  20. import java.util.List;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javax.swing.JComponent;
  24. import javax.swing.tree.DefaultMutableTreeNode;
  25. import javax.swing.tree.DefaultTreeModel;
  26. import mpv5.db.common.Context;
  27. import mpv5.db.common.DatabaseObject;
  28. import mpv5.db.common.NodataFoundException;
  29. import mpv5.db.common.QueryHandler;
  30. import mpv5.globals.Messages;
  31. import mpv5.logging.Log;
  32. import mpv5.ui.dialogs.subcomponents.ControlPanel_Groups;
  33. import mpv5.ui.dialogs.subcomponents.ControlPanel_ProductGroups;
  34. import mpv5.ui.frames.MPView;
  35. import mpv5.ui.panels.MPControlPanel;
  36. /**
  37. *
  38. *
  39. */
  40. public class ProductGroup extends DatabaseObject {
  41. private String description = "";
  42. private String defaults = "";
  43. private String hierarchypath = "";
  44. private int productgroupsids;
  45. public ProductGroup() {
  46. setContext(Context.getProductGroup());
  47. }
  48. public ProductGroup(String name) {
  49. this();
  50. setCname(name);
  51. }
  52. /**
  53. * @return the description
  54. */
  55. public String __getDescription() {
  56. return description;
  57. }
  58. /**
  59. * @param description the description to set
  60. */
  61. public void setDescription(String description) {
  62. this.description = description;
  63. }
  64. /**
  65. * @return the defaultvalue
  66. */
  67. public String __getDefaults() {
  68. return defaults;
  69. }
  70. /**
  71. * @param defaultvalue the defaultvalue to set
  72. */
  73. public void setDefaults(String defaultvalue) {
  74. this.defaults = defaultvalue;
  75. }
  76. @Override
  77. public String toString() {
  78. return __getCname();
  79. }
  80. @Override
  81. public boolean delete() {
  82. try {
  83. List<ProductGroup> childs = DatabaseObject.getReferencedObjects(this, Context.getProductGroup());
  84. for (int i = 0; i < childs.size(); i++) {
  85. DatabaseObject databaseObject = childs.get(i);
  86. if (!databaseObject.delete()) {
  87. return false;
  88. }
  89. }
  90. } catch (NodataFoundException ex) {
  91. mpv5.logging.Log.Debug(ex);//Logger.getLogger(ProductGroup.class.getName()).log(Level.SEVERE, null, ex);
  92. }
  93. try {
  94. return super.delete();
  95. } catch (Exception e) {
  96. return false;
  97. }
  98. }
  99. @Override
  100. public JComponent getView() {
  101. return new ControlPanel_ProductGroups(this);
  102. }
  103. @Override
  104. public mpv5.utils.images.MPIcon getIcon() {
  105. return null;
  106. }
  107. /**
  108. * @return the hierarchypath
  109. */
  110. public String __getHierarchypath() {
  111. if (hierarchypath == null || hierarchypath.equals("")) {
  112. int intp = __getIDS();
  113. do {
  114. try {
  115. ProductGroup p = (ProductGroup) getObject(Context.getProductGroup(), intp);
  116. hierarchypath = p + Group.GROUPSEPARATOR + hierarchypath;
  117. // System.err.println(hierarchypath);
  118. intp = p.__getProductgroupsids();
  119. } catch (NodataFoundException ex) {
  120. break;
  121. }
  122. } while (intp >= 1);
  123. }
  124. return hierarchypath + this;
  125. }
  126. /**
  127. * @param hierarchypath the hierarchypath to set
  128. */
  129. public void setHierarchypath(String hierarchypath) {
  130. this.hierarchypath = hierarchypath;
  131. }
  132. /**
  133. * @return the productgroupsids
  134. */
  135. public int __getProductgroupsids() {
  136. return productgroupsids;
  137. }
  138. /**
  139. * @param productgroupsids the productgroupsids to set
  140. */
  141. public void setProductgroupsids(int productgroupsids) {
  142. this.productgroupsids = productgroupsids;
  143. }
  144. /**
  145. * Create a tree model
  146. * @param data
  147. * @param rootNode
  148. * @return
  149. */
  150. public static DefaultTreeModel toTreeModel(ArrayList<ProductGroup> data, ProductGroup rootNode) {
  151. DefaultMutableTreeNode node1 = null;
  152. if (data.size() > 0) {
  153. DatabaseObject clone = rootNode.clone();
  154. clone.ReadOnly(true);
  155. clone.setCname("/");
  156. node1 = new DefaultMutableTreeNode(clone);
  157. // data.remove(rootNode);//remove root if in list
  158. try {
  159. mpv5.YabsViewProxy.instance().setWaiting(true);
  160. node1 = addToParents(node1, data);
  161. } catch (Exception e) {
  162. Log.Debug(e);
  163. } finally {
  164. mpv5.YabsViewProxy.instance().setWaiting(false);
  165. }
  166. }
  167. DefaultTreeModel model = new DefaultTreeModel(node1);
  168. return model;
  169. }
  170. @SuppressWarnings("unchecked")
  171. private static DefaultMutableTreeNode addToParents(DefaultMutableTreeNode firstnode, ArrayList<ProductGroup> dobjlist) {
  172. for (int i = 0; i < dobjlist.size(); i++) {
  173. ProductGroup dobj = dobjlist.get(i);
  174. if (dobj.__getProductgroupsids() <= 0 && firstnode.isRoot()) {
  175. // Log.Debug(ArrayUtilities.class, "Node is root child, adding it to root and removing it from the list.");
  176. firstnode.add(new DefaultMutableTreeNode(dobj));
  177. dobjlist.remove(dobj);//First level groups
  178. i--;
  179. } else {
  180. int parentid = dobj.__getProductgroupsids();
  181. if (((ProductGroup) firstnode.getUserObject()).__getIDS().intValue() == parentid) {
  182. // Log.Debug(ArrayUtilities.class, "Node is child of parentnode, adding and removing it from the list.");
  183. firstnode.add(new DefaultMutableTreeNode(dobj));
  184. dobjlist.remove(dobj);
  185. i--;
  186. } else {
  187. // Log.Debug(ArrayUtilities.class, "Node is no child of parentnode, iterating over the parent node..");
  188. @SuppressWarnings("unchecked")
  189. Enumeration<DefaultMutableTreeNode> nodes = firstnode.children();
  190. while (nodes.hasMoreElements()) {
  191. addToParents(nodes.nextElement(), dobjlist);
  192. }
  193. }
  194. }
  195. }
  196. return firstnode;
  197. }
  198. @Override
  199. public void ensureUniqueness() {
  200. if (!QueryHandler.instanceOf().clone(Context.getProductGroup()).checkUniqueness("cname", getCname())) {
  201. throw new UnsupportedOperationException(Messages.VALUE_ALREADY_EXISTS + " " + getCname());
  202. }
  203. }
  204. }