001: package com.jat.business.privileges;
002:
003: import com.jat.business.JatUser;
004: import com.jat.business.BusinessException;
005: import com.jat.integration.DataSource;
006: import com.jat.business.BusinessObjectFactory;
007: import com.jat.business.BusinessObjectList;
008: import com.jat.business.BusinessObjectPropertyList;
009: import com.jat.core.config.Config;
010: import com.jat.integration.*;
011: import java.util.Vector;
012: import com.jat.business.BusinessObjectProperties;
013: import java.util.Enumeration;
014: import com.jat.business.BusinessObject;
015: import com.jat.business.MultiBusinessObjectProperties;
016: import com.jat.core.log.LogManager;
017:
018: /**
019: * <p>Title: JAT</p>
020: * <p>Description: </p>
021: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
022: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p> * @author stf
023: * @version 1.0
024: * @since 1.2
025: */
026:
027: public class DataSourcePrivilegesLoader extends BusinessObjectFactory
028: implements PrivilegesLoader {
029:
030: public final static String SECTION = "data_source_privileges_loader";
031: public final static String DATA_SOURCE = "data_source";
032: public final static String OPERATION_LOAD_PROFILES = "load_profiles_operation";
033: public final static String OPERATION_LOAD_PRIVILEGES = "load_privileges_operation";
034: public final static String OPERATION_INSERT_PROFILE = "insert_profile_operation";
035: public final static String OPERATION_INSERT_PRIVILEGES = "insert_privileges_operation";
036: public final static String OPERATION_REMOVE_PRIVILEGES = "remove_privileges_operation";
037: public final static String OPERATION_REMOVE_PROFILE = "remove_profile_operation";
038:
039: public DataSourcePrivilegesLoader() throws BusinessException {
040: try {
041: dataSource = Config.getCurrent().getValue(SECTION,
042: DATA_SOURCE);
043: loadPrivilegesOperation = Config.getCurrent().getValue(
044: SECTION, OPERATION_LOAD_PROFILES);
045: loadProfilesOperation = Config.getCurrent().getValue(
046: SECTION, OPERATION_LOAD_PROFILES);
047: insertProfileOperation = Config.getCurrent().getValue(
048: SECTION, OPERATION_INSERT_PROFILE);
049: insertPrivilegesOperation = Config.getCurrent().getValue(
050: SECTION, OPERATION_INSERT_PRIVILEGES);
051: removePrivilegesOperation = Config.getCurrent().getValue(
052: SECTION, OPERATION_REMOVE_PRIVILEGES);
053: removeProfileOperation = Config.getCurrent().getValue(
054: SECTION, OPERATION_REMOVE_PROFILE);
055: } catch (Exception ex) {
056: throw new BusinessException(ex);
057: }
058: }
059:
060: public String getDataSource() {
061: return this .dataSource;
062: }
063:
064: public PrivilegeList getPrivileges(JatUser user)
065: throws BusinessException {
066: try {
067: return (PrivilegeList) super .getBusinessObjectList(
068: dataSource, this .loadPrivilegesOperation, user
069: .getProperties());
070: } catch (IntegrationException ex) {
071: throw new BusinessException(ex);
072: }
073: }
074:
075: public PrivilegeList getPrivileges(String profile)
076: throws BusinessException {
077: BusinessObjectProperties bop = new BusinessObjectProperties();
078: bop.put("PROFILE", profile);
079: try {
080: return (PrivilegeList) super .getBusinessObjectList(
081: dataSource, this .loadPrivilegesOperation, bop);
082: } catch (IntegrationException ex) {
083: throw new BusinessException(ex);
084: }
085:
086: }
087:
088: public Vector profiles() throws BusinessException {
089: try {
090: BusinessObjectList bol = super .getBusinessObjectList(
091: dataSource, this .loadProfilesOperation);
092: Vector ret = new Vector();
093: for (Enumeration e = bol.elements(); e.hasMoreElements();)
094: ret.addElement(((BusinessObject) e.nextElement())
095: .getField("PROFILE"));
096: return ret;
097: } catch (IntegrationException ex) {
098: throw new BusinessException(ex);
099: }
100: }
101:
102: public void persist() {
103: // nothing to do
104: }
105:
106: public void remove(Profile profile) throws Exception {
107: LogManager.sendDebug(this .getClass().getName()
108: + "::remove: start for profile: " + profile);
109: this .removePrivileges(profile);
110: BusinessObjectProperties bop = new BusinessObjectProperties();
111: bop.put("PROFILE", profile.getName());
112: DataSourceFactory.getFactory().getDataSource(this .dataSource)
113: .execute(this .removeProfileOperation, bop);
114: LogManager.sendDebug(this .getClass().getName()
115: + "::remove: end");
116: }
117:
118: public void update(Profile profile) throws Exception {
119: LogManager.sendDebug(this .getClass().getName()
120: + "::update: start for profile: " + profile);
121: this .removePrivileges(profile);
122: this .insertPrivileges(profile);
123: LogManager.sendDebug(this .getClass().getName()
124: + "::update: end");
125: }
126:
127: public void insert(Profile profile) throws IntegrationException {
128: LogManager.sendDebug(this .getClass().getName()
129: + "::insert: start for profile: " + profile);
130: BusinessObjectProperties bop = new BusinessObjectProperties();
131: bop.put("PROFILE", profile.getName());
132: DataSourceFactory.getFactory().getDataSource(this .dataSource)
133: .execute(this .insertProfileOperation, bop);
134: this .insertPrivileges(profile);
135: LogManager.sendDebug(this .getClass().getName()
136: + "::insert: end");
137: }
138:
139: protected void insertPrivileges(Profile profile)
140: throws IntegrationException {
141: MultiBusinessObjectProperties multi = new MultiBusinessObjectProperties();
142: for (Enumeration e = profile.getPrivileges().elements(); e
143: .hasMoreElements();) {
144: Privilege priv = (Privilege) e.nextElement();
145: priv.putField("PROFILE", profile.getName());
146: multi.put(this .insertPrivilegesOperation, priv);
147: }
148: DataSourceFactory.getFactory().getDataSource(this .dataSource)
149: .execute(multi);
150: }
151:
152: protected void removePrivileges(Profile profile)
153: throws IntegrationException {
154: BusinessObjectProperties bop = new BusinessObjectProperties();
155: bop.put("PROFILE", profile.getName());
156: DataSourceFactory.getFactory().getDataSource(this .dataSource)
157: .execute(this .removePrivilegesOperation, bop);
158: }
159:
160: protected BusinessObjectList getBusinessObjectList(
161: String dataSourceName, BusinessObjectPropertyList properties) {
162: return new PrivilegeList(dataSourceName, properties);
163: }
164:
165: private String dataSource;
166:
167: private String loadPrivilegesOperation;
168: private String loadProfilesOperation;
169: private String insertProfileOperation;
170: private String insertPrivilegesOperation;
171: private String removePrivilegesOperation;
172: private String removeProfileOperation;
173: }
|