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