001: package org.contineo.core.security;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.HashSet;
006: import java.util.Set;
007:
008: import org.contineo.core.security.dao.MenuDAO;
009: import org.contineo.util.Context;
010:
011: /**
012: * This class represents menus.
013: *
014: * @author Michael Scholz
015: * @author Marco Meschieri
016: * @version 1.0
017: */
018: public class Menu {
019:
020: public static final int MENUID_HOME = 1;
021:
022: public static final int MENUID_PERSONAL = 4;
023:
024: public static final int MENUID_DOCUMENTS = 5;
025:
026: public static final int MENUID_MESSAGES = 13;
027:
028: public static final int MENUID_EDITME = 19;
029:
030: public static final int MENUTYPE_DIRECTORY = 3;
031:
032: public static final int MENUTYPE_FILE = 5;
033:
034: public static final int MENUTYPE_ACTION = 1;
035:
036: public static final int MENUTYPE_HOME = 0;
037:
038: // Special menu for browsing the document archive
039: public static final String MENU_BROWSE = "Browse.do";
040:
041: protected int menuId = 0;
042:
043: protected String menuText = "";
044:
045: protected int menuParent = 0;
046:
047: protected int menuSort = 0;
048:
049: protected String menuIcon = "";
050:
051: protected String menuPath = "";
052:
053: protected int menuType = 2;
054:
055: protected int menuHier = 0;
056:
057: protected String menuRef = "";
058:
059: protected long size = 0;
060:
061: protected Set<MenuGroup> menuGroups = new HashSet<MenuGroup>();
062:
063: public Menu() {
064: }
065:
066: public int getMenuId() {
067: return menuId;
068: }
069:
070: public String getMenuText() {
071: return menuText;
072: }
073:
074: public int getMenuParent() {
075: return menuParent;
076: }
077:
078: public int getMenuSort() {
079: return menuSort;
080: }
081:
082: public String getMenuIcon() {
083: return menuIcon;
084: }
085:
086: public String getMenuPath() {
087: return menuPath;
088: }
089:
090: public int getMenuType() {
091: return menuType;
092: }
093:
094: public int getMenuHier() {
095: return menuHier;
096: }
097:
098: public String getMenuRef() {
099: return menuRef;
100: }
101:
102: public Set<MenuGroup> getMenuGroups() {
103: return menuGroups;
104: }
105:
106: protected void setMenuId(int id) {
107: menuId = id;
108: }
109:
110: public void setMenuText(String text) {
111: menuText = text;
112: }
113:
114: public void setMenuParent(int parent) {
115: menuParent = parent;
116: }
117:
118: public void setMenuSort(int sort) {
119: menuSort = sort;
120: }
121:
122: public void setMenuIcon(String icon) {
123: menuIcon = icon;
124: }
125:
126: public void setMenuPath(String path) {
127: menuPath = path;
128: }
129:
130: public void setMenuType(int type) {
131: menuType = type;
132: }
133:
134: public void setMenuHier(int hier) {
135: menuHier = hier;
136: }
137:
138: public void setMenuRef(String ref) {
139: menuRef = ref;
140: }
141:
142: public void setMenuGroups(Set<MenuGroup> mgroup) {
143: menuGroups = mgroup;
144: }
145:
146: public String[] getMenuGroupNames() {
147: ArrayList<String> ids = new ArrayList<String>();
148: for (MenuGroup mg : menuGroups) {
149: ids.add(mg.getGroupName());
150: }
151: return (String[]) ids.toArray(new String[] {});
152: }
153:
154: /**
155: * Adds MenuGroup object given in a String array to the ArrayList of
156: * MenuGroups.
157: *
158: * @param groups
159: */
160: public void setMenuGroup(String[] groups) {
161: menuGroups.clear();
162: for (int i = 0; i < groups.length; i++) {
163: MenuGroup mg = new MenuGroup();
164: mg.setGroupName(groups[i]);
165: mg.setWriteEnable(1);
166: menuGroups.add(mg);
167: }
168: }
169:
170: public Menu copy() {
171: Menu menu = new Menu();
172: menu.setMenuId(this .getMenuId());
173: menu.setMenuText(this .getMenuText());
174: menu.setMenuParent(this .getMenuParent());
175: menu.setMenuSort(this .getMenuSort());
176: menu.setMenuIcon(this .getMenuIcon());
177: menu.setMenuPath(this .getMenuPath());
178: menu.setMenuType(this .getMenuType());
179: menu.setMenuHier(this .getMenuHier());
180: menu.setMenuRef(this .getMenuRef());
181: menu.setMenuGroups(this .getMenuGroups());
182: return menu;
183: }
184:
185: /**
186: * Returns a Collection of menus being a parent of the given menu. The
187: * collection is sorted by the hierarchy level (menuHier) of the menus.
188: *
189: * @return
190: */
191: public Collection<Menu> getParents() {
192: Collection<Menu> coll = new ArrayList<Menu>();
193:
194: try {
195: String[] menus = menuPath.split("/");
196:
197: if (menus.length > 0) {
198: if (menus[0].length() > 0) {
199: MenuDAO menuDao = (MenuDAO) Context.getInstance()
200: .getBean(MenuDAO.class);
201: Menu home = menuDao
202: .findByPrimaryKey(Menu.MENUID_HOME);
203: coll.add(home);
204:
205: for (int i = 1; i < menus.length; i++) {
206: try {
207: Menu menu = menuDao
208: .findByPrimaryKey(Integer
209: .parseInt(menus[i]));
210: coll.add(menu);
211: } catch (NumberFormatException nfe) {
212: ;
213: }
214: }
215: }
216: }
217: } catch (Exception e) {
218: ;
219: }
220:
221: return coll;
222: }
223:
224: public MenuGroup getMenuGroup(String groupName) {
225: for (MenuGroup mg : menuGroups) {
226: if (mg.getGroupName().equals(groupName))
227: return mg;
228: }
229: return null;
230: }
231:
232: @Override
233: public boolean equals(Object arg0) {
234: if (!(arg0 instanceof Menu))
235: return false;
236: Menu other = (Menu) arg0;
237: return other.getMenuId() == this .getMenuId();
238: }
239:
240: @Override
241: public int hashCode() {
242: return new Integer(menuId).hashCode();
243: }
244:
245: /**
246: * The size of the file associated to this menu expressed in bytes
247: */
248: public long getMenuSize() {
249: return size;
250: }
251:
252: public void setMenuSize(long size) {
253: this.size = size;
254: }
255:
256: }
|