001: package com.jat.presentation.controller;
002:
003: import java.io.IOException;
004: import java.util.Enumeration;
005: import java.util.Hashtable;
006: import java.util.Vector;
007:
008: import com.jat.core.config.Config;
009: import com.jat.core.log.LogManager;
010:
011: /**
012: * <p>Title: JAT</p>
013: * <p>Description: </p>
014: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
015: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
016: * @author stf
017: * @version 1.0
018: * @since 1.2
019: */
020:
021: public class ConfigActionLoader implements ActionLoader {
022:
023: public final static String ACTION_KEY = "action";
024: public final static String ACTION_NAME = ".name";
025: public final static String ACTION_CLASS_NAME = ".class_name";
026: public final static String ACTION_NEXT_PAGE = ".next_page";
027: public final static String ACTION_ERROR_PAGE = ".error_page";
028: public final static String ACTION_LOG_REQUIRED = ".log_required";
029: public final static String ACTION_PRIVILEGE = ".privilege";
030: public final static String ACTION_METHOD = ".method";
031: public final static String ACTION_FLOW = ".previous_page";
032:
033: public ConfigActionLoader() {
034: }
035:
036: public Hashtable getActionList() throws Exception {
037: LogManager.sendDebug(this .getClass().getName()
038: + "::getActionList: start");
039: Hashtable ret = new Hashtable();
040: Config config = this .getConfig();
041: Vector keys = config.getSubKeys(SECTION, ACTION_KEY);
042: for (Enumeration e = keys.elements(); e.hasMoreElements();) {
043: String key = (String) e.nextElement();
044: String className = config.getValue(SECTION, key
045: + ACTION_CLASS_NAME);
046: Action action = (Action) Class.forName(className)
047: .newInstance();
048: action.setName(config.getValue(SECTION, key + ACTION_NAME));
049: action.setNextPage(config.getValue(SECTION, key
050: + ACTION_NEXT_PAGE));
051: action.setErrorPage(config.getValue(SECTION, key
052: + ACTION_ERROR_PAGE));
053: action
054: .setLogRequired(config.getValue(SECTION,
055: key + ACTION_LOG_REQUIRED)
056: .equalsIgnoreCase("TRUE"));
057: try {
058: action.setPrivilege(config.getValue(SECTION, key
059: + ACTION_PRIVILEGE));
060: } catch (Exception ignored) {
061: }
062: action.setMethods(config.getSubValues(SECTION, key
063: + ACTION_METHOD));
064: try {
065: Vector flows = config.getSubValues(SECTION, key
066: + ACTION_FLOW);
067: if (flows != null && flows.size() > 0)
068: action.setPreviousPages(flows);
069: } catch (Exception ignored) {
070: }
071: LogManager.sendDebug(this .getClass().getName()
072: + "::getActionList: Action: " + action);
073: ret.put(action.getName(), action);
074: }
075: LogManager.sendDebug(this .getClass().getName()
076: + "::getActionList: end");
077: return ret;
078: }
079:
080: public void persist() throws Exception {
081: LogManager.sendDebug(this .getClass().getName()
082: + "::persist: start");
083: Config config = this .getConfig();
084: config.save();
085: LogManager.sendDebug(this .getClass().getName()
086: + "::persist: end");
087: }
088:
089: public void remove(Action action) throws Exception {
090: LogManager.sendDebug(this .getClass().getName()
091: + "::remove: start for action: " + action);
092: String key = this .getIndexKey(action.getName());
093: this .removeAction(key);
094: LogManager.sendDebug(this .getClass().getName()
095: + "::remove: end");
096: }
097:
098: public void modify(String name, Action action) throws Exception {
099: LogManager.sendDebug(this .getClass().getName()
100: + "::modify: start for name '" + name
101: + "' and action: " + action);
102: String key = this .getIndexKey(name);
103: this .removeAction(key);
104: this .addAction(action, key);
105: LogManager.sendDebug(this .getClass().getName()
106: + "::modify: end");
107: }
108:
109: public void add(Action action) throws Exception {
110: LogManager.sendDebug(this .getClass().getName()
111: + "::add: start for action: " + action);
112: int index = 1;
113: String key = null;
114: while (key == null) {
115: String k = ACTION_KEY + (index++);
116: if (!this .getConfig().existsValue(SECTION, k + ACTION_NAME))
117: key = k;
118: }
119: this .addAction(action, key);
120: LogManager.sendDebug(this .getClass().getName() + "::add: end");
121: }
122:
123: private void addAction(Action action, String key) throws Exception {
124: try {
125: Config config = this .getConfig();
126: config.addValue(SECTION, key + ACTION_NAME, action
127: .getName());
128: config.addValue(SECTION, key + ACTION_CLASS_NAME, action
129: .getClass().getName());
130: config.addValue(SECTION, key + ACTION_NEXT_PAGE, action
131: .getNextPage());
132: config.addValue(SECTION, key + ACTION_ERROR_PAGE, action
133: .getErrorPage());
134: config.addValue(SECTION, key + ACTION_LOG_REQUIRED, ""
135: + action.isLogRequired());
136: if (action.getPrivilege() != null)
137: config.addValue(SECTION, key + ACTION_PRIVILEGE, action
138: .getPrivilege());
139: int index = 1;
140: for (Enumeration e = action.getMethods().elements(); e
141: .hasMoreElements();)
142: config.addValue(SECTION, key + ACTION_METHOD
143: + (index++), (String) e.nextElement());
144: for (Enumeration e = action.getPreviousPages().elements(); e
145: .hasMoreElements();)
146: config.addValue(SECTION, key + ACTION_FLOW + (index++),
147: (String) e.nextElement());
148: } catch (Exception ex) {
149: throw new Exception(this .getClass().getName()
150: + "::addAction: exception: " + ex);
151: }
152: }
153:
154: private void removeAction(String key) throws Exception {
155: try {
156: Config config = this .getConfig();
157: config.removeKeys(SECTION, key);
158: } catch (Exception ex) {
159: throw new Exception(this .getClass().getName()
160: + "::removeAction: exception: " + ex);
161: }
162: }
163:
164: private Config getConfig() throws Exception {
165: String name = Config.getCurrent().getValue(SECTION,
166: "config.name");
167: Config config = Config.getCurrent().getConfig(name);
168: if (config == null) {
169: LogManager.sendError(this .getClass().getName()
170: + "::getConfig: exception: Config file " + name
171: + " not found");
172: throw new Exception("Config file " + name + " not found");
173: }
174: return config;
175: }
176:
177: private String getIndexKey(String actionName) throws Exception {
178: for (Enumeration e = this .getConfig().getSubKeys(SECTION,
179: ACTION_KEY).elements(); e.hasMoreElements();) {
180: String key = (String) e.nextElement();
181: if (this .getConfig().getValue(SECTION, key + ACTION_NAME)
182: .equalsIgnoreCase(actionName))
183: return key;
184: }
185: throw new Exception("config key not found for " + actionName);
186: }
187:
188: }
|