KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > MenuOption


1 /*
2   Copyright (C) 2003-2005 Know Gate S.L. All rights reserved.
3                            C/Oņa, 107 1š2 28050 Madrid (Spain)
4 cm
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.hipergate;
34
35 import java.util.ArrayList JavaDoc;
36
37 import com.knowgate.debug.DebugFile;
38 import com.knowgate.misc.Gadgets;
39
40 /**
41  * hipergate menu option JiBX handler
42  * @author Sergio Montoro Ten
43  * @version 1.1
44  */

45 public class MenuOption {
46
47   private String JavaDoc name;
48   private String JavaDoc mask;
49   private String JavaDoc title;
50   private String JavaDoc href;
51   private String JavaDoc forbid;
52   private ArrayList JavaDoc suboptions;
53
54   // ---------------------------------------------------------------------------
55

56   /**
57    * Default constructor
58    */

59   public MenuOption() {
60     mask = "0";
61     forbid = href = title = name = null;
62     suboptions = new ArrayList JavaDoc();
63   }
64
65   // ---------------------------------------------------------------------------
66

67   /**
68    * Get suboption
69    * @param nIndex int Suboption index [0..countSubOptions()-1)
70    * @return MenuOption
71    * @throws ArrayIndexOutOfBoundsException if n<0 or n>=countSubOptions()
72    */

73   public MenuOption getSubOption(int nIndex) throws ArrayIndexOutOfBoundsException JavaDoc {
74     return (MenuOption) suboptions.get(nIndex);
75   }
76
77   // ---------------------------------------------------------------------------
78

79   /**
80    * Get suboption by name
81    * @param sname String Suboption name
82    * @return MenuOption
83    * @throws ArrayIndexOutOfBoundsException if no option with such name was found
84    */

85   public MenuOption getSubOption(String JavaDoc sName) throws ArrayIndexOutOfBoundsException JavaDoc {
86     final int nOptCount = countSubOptions();
87     for (int o=0;o<nOptCount;o++) {
88       if (((MenuOption)suboptions.get(o)).getName().equalsIgnoreCase(sName))
89         return (MenuOption) suboptions.get(o);
90     } // next
91
throw new ArrayIndexOutOfBoundsException JavaDoc(sName + " menu option not found");
92   }
93
94   // ---------------------------------------------------------------------------
95

96   /**
97    * Get count of suboptions
98    * @return int
99    */

100   public int countSubOptions() {
101     return suboptions.size();
102   }
103
104   // ---------------------------------------------------------------------------
105

106   /**
107    * Get this option name
108    * @return String
109    */

110   public String JavaDoc getName() {
111     return name;
112   }
113
114   // ---------------------------------------------------------------------------
115

116   /**
117    * <p>Get this option permissions mask</p>
118    * The mask is an Integer value between 0 and 31 or an ArrayList of Integer values or a String either "admin" or "owner"
119    * @return Object May be a String (either "admin" or "owner") or an Integer or <b>null</b>
120    * @throws NumberFormatException If mask value is neither "admin" nor "owner" and it is
121    * not an integer value.
122    */

123   public Object JavaDoc getMask() throws NumberFormatException JavaDoc {
124     if (null==mask)
125       return null;
126     else if (mask.length()==0)
127       return null;
128     else if (mask.equalsIgnoreCase("admin"))
129       return "admin";
130     else if (mask.equalsIgnoreCase("owner"))
131       return "owner";
132     else if (mask.indexOf(",")>0) {
133       String JavaDoc[] aMask = Gadgets.split(mask,',');
134       ArrayList JavaDoc oMask = new ArrayList JavaDoc(aMask.length);
135       for (int m=0; m<aMask.length; m++)
136         oMask.add(new Integer JavaDoc(aMask[m]));
137       return oMask;
138     } else
139       return new Integer JavaDoc(mask);
140   } // getMask
141

142   // ---------------------------------------------------------------------------
143

144   /**
145    * <p>Get permissions mask encoded as a 32bit integer</p>
146    * @return int If mask is empty string or null Then return value is zero,
147    * If mask is "admin" or "owner" Then return value is -1
148    * Else the return value is 1<<mask
149    * If mask has multiple values delmited by commas then the returned value
150    * is the bitwise OR of all values 1<<mask[0] | 1<<mask[1] | 1<<mask[2] ...
151    */

152   public int getBitMask() {
153     int iMask;
154     if (null==mask)
155       return 0;
156     else if (mask.equalsIgnoreCase("admin") || mask.equalsIgnoreCase("owner"))
157       return -1;
158     else if (mask.indexOf(",")>0) {
159       String JavaDoc[] aMask = Gadgets.split(mask,',');
160       iMask = 0;
161       for (int m=0; m<aMask.length; m++)
162         iMask |= 1<<Integer.parseInt(aMask[m]);
163       return iMask;
164     } else {
165       try {
166         iMask=1<<Integer.parseInt(mask);
167       }
168       catch (NumberFormatException JavaDoc ignore) {
169         if (DebugFile.trace) DebugFile.writeln("MenuOption.getBitMask() NumberFormatException "+mask);
170         iMask=0;
171       }
172       return iMask;
173     }
174   } // getBitMask
175

176   // ---------------------------------------------------------------------------
177

178   /**
179    * <p>Determine access to this menu option for an application mask</p>
180    * Get whether or not a user with the given application mask and roles can
181    * access this menu option. Application Masks are 32 bit integers with one
182    * bit flags per application.<br>
183    * <table><tr><td colspan="3"><b>Standard application masks</b></td></tr>
184    * <tr><td><b>Application</b></td><td>Bit Position</td><td>Int Mask</td></tr>
185    * <tr><td>Bug Tracker</td><td>10</td><td>1024</td></tr>
186    * <tr><td>Duty Manager</td><td>11</td><td>1024</td></tr>
187    * <tr><td>Project Manager</td><td>12</td><td>4096</td></tr>
188    * <tr><td>Mailwire</td><td>13</td><td>8192</td></tr>
189    * <tr><td>Web Builder</td><td>14</td><td>16384</td></tr>
190    * <tr><td>Virtual Disk</td><td>15</td><td>32768</td></tr>
191    * <tr><td>Sales</td><td>16</td><td>65536</td></tr>
192    * <tr><td>Collaborative Tools</td><td>17</td><td>131072</td></tr>
193    * <tr><td>Marketing Tools</td><td>18</td><td>262144</td></tr>
194    * <tr><td>Directory</td><td>19</td><td>524288</td></tr>
195    * <tr><td>Shop</td><td>20</td><td>1048576</td></tr>
196    * <tr><td>Hipermail</td><td>21</td><td>2097152</td></tr>
197    * <tr><td>Training</td><td>22</td><td>4194304</td></tr>
198    * <tr><td>Surveys</td><td>23</td><td>8388608</td></tr>
199    * <tr><td>Configuration</td><td>30</td><td>1073741824</td></tr>
200    * </table>
201    * @param iAppMask int Application mask
202    * @param bAdmin boolean <b>true</b> if user has administrator role
203    * @param bOwner boolean <b>true</b> if user is owner of his WorkArea
204    * @return boolean <b>true</b> if user is administrator or owner and mask is
205    * "admin" or "owner" respectively. Or <b>true</b> if getBitMask() bitwise-and iAppMask
206    * is not zero (getBitMask()&iAppMask!=0).
207    */

208   public boolean hasAccess(int iAppMask, boolean bAdmin, boolean bOwner) {
209     if (DebugFile.trace) DebugFile.writeln("Begin MenuOption.hasAccess("+
210                                            String.valueOf(iAppMask)+","+
211                                            String.valueOf(bAdmin)+","+
212                                            String.valueOf(bOwner)+")");
213     final int iBitMask = getBitMask();
214     boolean bRetVal;
215     if (0==iBitMask) {
216       bRetVal = true;
217     }
218     else if (-1==iBitMask) {
219       if (DebugFile.trace) DebugFile.writeln(" mask="+mask);
220       bRetVal = (mask.equalsIgnoreCase("admin") && bAdmin) || (mask.equalsIgnoreCase("owner") && bOwner);
221     } else {
222       if (DebugFile.trace) DebugFile.writeln(" bitmask="+String.valueOf(iBitMask));
223       bRetVal = ((iBitMask&iAppMask)!=0);
224     }
225     if (DebugFile.trace) DebugFile.writeln("End MenuOption.hasAccess() : "+String.valueOf(bRetVal));
226     return bRetVal;
227   } // hasAccess
228

229   // ---------------------------------------------------------------------------
230

231   /**
232    * Get menu option title
233    * @return String &lt;title&gt; tag value or empty string if title is not set
234    */

235   public String JavaDoc getTitle() {
236     if (null!=title)
237       return title;
238     else
239       return "";
240   }
241
242   // ---------------------------------------------------------------------------
243

244   /**
245    * Get menu option href
246    * @return String &lt;href&gt; tag value or empty string if href is not set
247    */

248   public String JavaDoc getHRef() {
249     if (null!=href)
250       return href;
251     else
252       return "";
253   }
254
255   // ---------------------------------------------------------------------------
256

257   /**
258    * Get menu option alternative href used when a user has not enought permissions
259    * to access standard href. Whether to use href or forbid must be determined by
260    * the caller program at runtime.
261    * @return String &lt;forbid&gt; tag value or empty string if forbid is not set
262    */

263   public String JavaDoc getForbid() {
264     if (null!=forbid)
265       return forbid;
266     else
267       return "";
268   }
269 }
270
Popular Tags