KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > util > ProjectUtilities


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.client.util;
20
21 import java.util.*;
22
23 import cowsultants.itracker.ejb.client.models.*;
24 import cowsultants.itracker.ejb.client.resources.*;
25
26 public class ProjectUtilities {
27     // Options use bitmasks and are stored as a single integer in the db
28
public static final int OPTION_SURPRESS_HISTORY_HTML = 1;
29     public static final int OPTION_ALLOW_ASSIGN_TO_CLOSE = 2;
30     public static final int OPTION_PREDEFINED_RESOLUTIONS = 4;
31     public static final int OPTION_ALLOW_SELF_REGISTERED_CREATE = 8;
32     public static final int OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL = 16;
33     public static final int OPTION_NO_ATTACHMENTS = 32;
34     public static final int OPTION_LITERAL_HISTORY_HTML = 64;
35
36     public static final int STATUS_DELETED = -1;
37     public static final int STATUS_ACTIVE = 1;
38     public static final int STATUS_VIEWABLE = 2;
39     public static final int STATUS_LOCKED = 3;
40
41     private static HashMap statusNames = new HashMap();
42
43     public ProjectUtilities() {
44     }
45
46     public static String JavaDoc getStatusName(int value) {
47         return getStatusName(value, ITrackerResources.getLocale());
48     }
49
50     public static String JavaDoc getStatusName(int value, Locale locale) {
51         return ITrackerResources.getString(ITrackerResources.KEY_BASE_PROJECT_STATUS + value, locale);
52     }
53
54     public static HashMap getStatusNames() {
55         return getStatusNames(ITrackerResources.getLocale());
56     }
57
58     public static HashMap getStatusNames(Locale locale) {
59         HashMap statuses = (HashMap) statusNames.get(locale);
60         if(statuses == null) {
61             statuses = new HashMap();
62             statuses.put(Integer.toString(STATUS_DELETED), getStatusName(STATUS_DELETED, locale));
63             statuses.put(Integer.toString(STATUS_ACTIVE), getStatusName(STATUS_ACTIVE, locale));
64             statuses.put(Integer.toString(STATUS_VIEWABLE), getStatusName(STATUS_VIEWABLE, locale));
65             statuses.put(Integer.toString(STATUS_LOCKED), getStatusName(STATUS_LOCKED, locale));
66         }
67         statusNames.put(locale, statuses);
68         return statuses;
69     }
70
71     public static boolean hasOption(int option, int currentOptions) {
72         return ((option & currentOptions) == option);
73     }
74
75     public static Integer JavaDoc[] getOptions(int currentOptions) {
76         Vector options = new Vector();
77         if(hasOption(OPTION_SURPRESS_HISTORY_HTML, currentOptions)) {
78             options.add(new Integer JavaDoc(OPTION_SURPRESS_HISTORY_HTML));
79         }
80         if(hasOption(OPTION_ALLOW_ASSIGN_TO_CLOSE, currentOptions)) {
81             options.add(new Integer JavaDoc(OPTION_ALLOW_ASSIGN_TO_CLOSE));
82         }
83         if(hasOption(OPTION_PREDEFINED_RESOLUTIONS, currentOptions)) {
84             options.add(new Integer JavaDoc(OPTION_PREDEFINED_RESOLUTIONS));
85         }
86         if(hasOption(OPTION_ALLOW_SELF_REGISTERED_CREATE, currentOptions)) {
87             options.add(new Integer JavaDoc(OPTION_ALLOW_SELF_REGISTERED_CREATE));
88         }
89         if(hasOption(OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL, currentOptions)) {
90             options.add(new Integer JavaDoc(OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL));
91         }
92         if(hasOption(OPTION_NO_ATTACHMENTS, currentOptions)) {
93             options.add(new Integer JavaDoc(OPTION_NO_ATTACHMENTS));
94         }
95         if(hasOption(OPTION_LITERAL_HISTORY_HTML, currentOptions)) {
96             options.add(new Integer JavaDoc(OPTION_LITERAL_HISTORY_HTML));
97         }
98         Integer JavaDoc[] optionsArray = new Integer JavaDoc[options.size()];
99         options.copyInto(optionsArray);
100         return optionsArray;
101     }
102
103     /**
104       * Returns true if the user has any access at all for the requested project.
105       * @param projectId the id of the project to check
106       * @param permissions a HashMap of user permissions
107       * @return true if the user has any access to the project
108       */

109     public static boolean hasProjectAccess(Integer JavaDoc projectId, HashMap permissions) {
110         if(permissions == null) {
111             return false;
112         }
113
114         Boolean JavaDoc superUser = (Boolean JavaDoc) permissions.get(Integer.toString(-1));
115         if(superUser != null && superUser.booleanValue()) {
116             return true;
117         }
118
119         if(projectId != null && projectId.intValue() > 0) {
120             HashSet projectPermissions = (HashSet) permissions.get(projectId);
121
122             if(projectPermissions != null && projectPermissions.size() > 0) {
123                 return true;
124             }
125         }
126         return false;
127     }
128 }
129
Popular Tags