001: package com.jat.business.privileges;
002:
003: import java.util.Enumeration;
004: import java.util.Vector;
005:
006: import com.jat.business.BusinessException;
007: import com.jat.business.JatUser;
008: import com.jat.core.config.Config;
009: import com.jat.core.init.Initable;
010: import com.jat.util.OrderVector;
011: import com.jat.util.StringOrderable;
012:
013: /**
014: * <p>Title: JAT</p>
015: * <p>Description: </p>
016: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
017: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
018: * @author stf
019: * @version 1.0
020: * @since 1.2
021: */
022:
023: public class PrivilegeFactory implements Initable {
024:
025: public PrivilegeFactory() {
026: if (manager == null)
027: manager = this ;
028: }
029:
030: public static PrivilegeFactory getDefault() {
031: return manager;
032: }
033:
034: public PrivilegeList getPrivileges(JatUser user)
035: throws BusinessException {
036: return this .loader.getPrivileges(user);
037: }
038:
039: public PrivilegeList getPrivileges(String profile)
040: throws BusinessException {
041: return this .loader.getPrivileges(profile);
042: }
043:
044: /**
045: * Return a list of profile names
046: * @return a list of profile name as String
047: */
048: public Enumeration profiles() throws BusinessException {
049: if (this .profiles == null)
050: this .profiles = this .loader.profiles();
051: return this .profiles.elements();
052: }
053:
054: public Profile getProfile(String name) throws BusinessException {
055: if (!this .profiles.contains(name))
056: return null;
057: Profile profile = this .getEmptyProfile(name);
058: profile.setPrivileges(this .getPrivileges(name));
059: return profile;
060: }
061:
062: public Profile getEmptyProfile(String name) {
063: Profile profile = new Profile(this .loader.getDataSource(), name);
064: return profile;
065: }
066:
067: public Privilege getEmptyPrivilege() {
068: Privilege privilege = new Privilege(this .loader.getDataSource());
069: return privilege;
070: }
071:
072: public void save(Profile profile) throws Exception {
073: if (this .getProfile(profile.getName()) != null) {
074: this .loader.update(profile);
075: } else {
076: this .loader.insert(profile);
077: }
078: this .loader.persist();
079: this .profiles = null;
080: this .allPrivileges = null;
081: }
082:
083: public void remove(Profile profile) throws Exception {
084: this .loader.remove(profile);
085: this .loader.persist();
086: this .profiles = null;
087: this .allPrivileges = null;
088: }
089:
090: /**
091: * Return all privileges
092: * @return a list of Privilege
093: * @throws BusinessException
094: */
095: public Enumeration getDistinctPrivileges() throws BusinessException {
096: if (this .allPrivileges == null) {
097: Vector names = new Vector();
098: for (Enumeration e = this .profiles(); e.hasMoreElements();) {
099: String profile = (String) e.nextElement();
100: PrivilegeList pl = this .getPrivileges(profile);
101: for (Enumeration e2 = pl.privileges(); e2
102: .hasMoreElements();) {
103: Privilege p = (Privilege) e2.nextElement();
104: if (!this .containsPrivilege(names, p))
105: names.addElement(p);
106: }
107: this .allPrivileges = OrderVector.orderAscendent(names);
108: }
109: }
110: return this .allPrivileges.elements();
111: }
112:
113: private boolean containsPrivilege(Vector vect, Privilege priv) {
114: for (Enumeration e = vect.elements(); e.hasMoreElements();) {
115: Privilege p = (Privilege) e.nextElement();
116: if (p.equals(priv))
117: return true;
118: }
119: return false;
120: }
121:
122: public void init() throws Exception {
123: String loaderName = "";
124: loaderName = Config.getCurrent().getValue("authentication",
125: "privilegeLoader");
126: this .loader = (PrivilegesLoader) Class.forName(loaderName)
127: .newInstance();
128: this .profiles = null;
129: this .allPrivileges = null;
130: }
131:
132: private Vector allPrivileges;
133: private Vector profiles;
134: private PrivilegesLoader loader = null;
135: private static PrivilegeFactory manager;
136: }
|