PageRenderTime 345ms CodeModel.GetById 161ms app.highlight 14ms RepoModel.GetById 167ms app.codeStats 0ms

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