001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.mda.db;
024:
025: import java.io.IOException;
026: import java.sql.Connection;
027: import java.sql.SQLException;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031:
032: import org.apache.log4j.Logger;
033:
034: import biz.hammurapi.config.ConfigurationException;
035: import biz.hammurapi.rules.Rule;
036: import biz.hammurapi.sql.IdentityGenerator;
037: import biz.hammurapi.sql.IdentityManager;
038: import biz.hammurapi.sql.IdentityRetriever;
039: import biz.hammurapi.sql.SQLProcessor;
040: import biz.hammurapi.web.ActionsBase;
041: import biz.hammurapi.web.HammurapiWebException;
042: import biz.hammurapi.web.eval.EvaluationResult;
043: import biz.hammurapi.web.eval.EvaluatorFactory;
044: import biz.hammurapi.web.eval.ResourceLocator;
045: import biz.hammurapi.web.mda.db.model.Column;
046: import biz.hammurapi.web.mda.db.model.Table;
047: import biz.hammurapi.web.menu.sql.HelpTopicImpl;
048: import biz.hammurapi.web.menu.sql.MenuEngine;
049: import biz.hammurapi.web.menu.sql.XmenuImpl;
050: import biz.hammurapi.web.util.GuidGenerator;
051:
052: /**
053: * Rule to generate system objects from table.
054: * @author Pavel
055: *
056: */
057: public class TableRule extends Rule {
058: private static final Logger logger = Logger
059: .getLogger(TableRule.class);
060:
061: private ResourceLocator resourceLocator;
062: private EvaluatorFactory evaluatorFactory;
063: private SQLProcessor sqlProcessor;
064: private IdentityManager identityManager;
065: private GuidGenerator guidGenerator;
066: private String tablePackage;
067:
068: public void start() throws ConfigurationException {
069: super .start();
070: resourceLocator = (ResourceLocator) get("/@resource-locator");
071: if (resourceLocator == null) {
072: throw new ConfigurationException(
073: "Resource locator not found");
074: }
075: evaluatorFactory = (EvaluatorFactory) get("/@evaluator-factory");
076: if (evaluatorFactory == null) {
077: throw new ConfigurationException(
078: "Evaluator factory not found");
079: }
080: sqlProcessor = (SQLProcessor) get("/@sql-processor");
081: if (sqlProcessor == null) {
082: throw new ConfigurationException("SQL Processor not found");
083: }
084: identityManager = (IdentityManager) get("/@identity-manager");
085: if (identityManager == null) {
086: throw new ConfigurationException(
087: "Identity manager not found");
088: }
089: guidGenerator = (GuidGenerator) get("/@guid-generator");
090: if (guidGenerator == null) {
091: guidGenerator = new GuidGenerator();
092: }
093: tablePackage = (String) get("/@table-package");
094: }
095:
096: public Object infer(Table table) {
097: // System.out.println("Processing table "+table.getName());
098: Integer parent = (Integer) get("/@parent-menu");
099:
100: // Menu entries
101:
102: try {
103: // List
104: if (table.hasView("list")) {
105: XmenuImpl listMenu = createMenu("list", table);
106: if (parent != null) {
107: listMenu.setParent(parent);
108: }
109: listMenu.setScope("Public");
110: listMenu.setName(table.getLabel());
111: insertMenu(listMenu);
112:
113: parent = new Integer(listMenu.getId());
114:
115: // Help
116: MenuEngine engine = new MenuEngine(sqlProcessor);
117: if (!ActionsBase.isBlank(table.getDescription())) {
118: HelpTopicImpl helpTopic = new HelpTopicImpl(true);
119: helpTopic.setMenuId(listMenu.getId());
120: helpTopic.setType("Content");
121: helpTopic.setName("main");
122: helpTopic.setContent(table.getDescription());
123: engine.insertHelpTopic(helpTopic);
124: }
125:
126: // Menu entries
127:
128: // Columns
129: Iterator it = table.getColumns().iterator();
130: while (it.hasNext()) {
131: Column column = (Column) it.next();
132: if (!ActionsBase.isBlank(column.getDescription())) {
133: HelpTopicImpl helpTopic = new HelpTopicImpl(
134: true);
135: helpTopic.setMenuId(listMenu.getId());
136: helpTopic.setType("Content");
137: helpTopic.setName("#Attributes#"
138: + column.getLabel());
139: helpTopic.setContent(column.getDescription());
140: engine.insertHelpTopic(helpTopic);
141: }
142: }
143: }
144:
145: // View
146: if (table.hasView("view")) {
147: XmenuImpl viewMenu = createMenu("view", table);
148: viewMenu.setParent(parent);
149: viewMenu.setScope("Hidden");
150: insertMenu(viewMenu);
151: }
152:
153: // Create
154: if (table.hasView("create")) {
155: XmenuImpl createMenu = createMenu("create", table);
156: createMenu.setParent(parent);
157: createMenu.setScope("Public");
158: insertMenu(createMenu);
159: }
160:
161: // Edit
162: if (table.hasView("edit")) {
163: XmenuImpl editMenu = createMenu("edit", table);
164: editMenu.setParent(parent);
165: editMenu.setScope("Hidden");
166: insertMenu(editMenu);
167: }
168:
169: // Delete
170: if (table.hasView("delete")) {
171: XmenuImpl deleteMenu = createMenu("delete", table);
172: deleteMenu.setParent(parent);
173: deleteMenu.setScope("Hidden");
174: deleteMenu.setType("action");
175: deleteMenu.setXid("delete");
176: insertMenu(deleteMenu);
177: }
178: return new GenerationResult("Generation successful");
179: } catch (Throwable e) {
180: logger.error(e);
181: return new GenerationResult(e);
182: }
183: }
184:
185: public static class GenerationResult {
186: private Object result;
187:
188: public GenerationResult(Object result) {
189: this .result = result;
190: }
191:
192: public Object getResult() {
193: return result;
194: }
195: }
196:
197: /**
198: * Creates menu and populates standard values.
199: * @param name
200: * @param prefix
201: * @param table
202: * @return
203: * @throws HammurapiWebException
204: */
205: private XmenuImpl createMenu(String name, Table table)
206: throws HammurapiWebException {
207: logger.info("Generating " + name);
208: XmenuImpl menu = new XmenuImpl(true);
209: String xidPrefix = tablePackage == null ? table.getJavaName()
210: + "." : tablePackage + "." + table.getJavaName() + ".";
211: menu.setXid(xidPrefix + name);
212: menu.setName(name);
213: menu.setDescription(name + " " + table.getLabel());
214: menu.setTitle(menu.getDescription());
215: Map context = new HashMap();
216: context.put("result", table);
217: context.put("xidPrefix", xidPrefix);
218: context.put("view", name);
219: EvaluationResult matchHandler = evaluatorFactory.evaluate(name
220: + "MatchHandler", context, getClass().getClassLoader(),
221: resourceLocator);
222: if (matchHandler != null) {
223: menu.setMatchHandler(matchHandler.getOutput());
224: }
225: String[] content = instantiateTemplate(name + "Content",
226: context);
227: if (content != null) {
228: menu.setContentType(content[0]);
229: menu.setContent(content[1]);
230: }
231: String[] formHandler = instantiateTemplate(
232: name + "FormHandler", context);
233: if (formHandler != null) {
234: menu.setFormHandlerType(formHandler[0]);
235: menu.setFormHandlerCode(formHandler[1]);
236: }
237: menu.setType("item");
238: menu.setGuid(guidGenerator.nextGUID());
239:
240: return menu;
241: }
242:
243: /**
244: * Finds a template by extension and evaluates it.
245: * @param name Short template name. E.g. "main"
246: * @param context
247: * @return
248: * @throws IOException
249: * @throws HammurapiWebException
250: */
251: private String[] instantiateTemplate(String name, Map context)
252: throws HammurapiWebException {
253: Iterator it = evaluatorFactory.getNames().iterator();
254: while (it.hasNext()) {
255: String language = (String) it.next();
256: String extension = evaluatorFactory.getExtension(language);
257: EvaluationResult result = evaluatorFactory.evaluate(name
258: + extension, context, getClass().getClassLoader(),
259: resourceLocator);
260: if (result != null) {
261: return new String[] { language, result.getOutput() };
262: }
263: }
264: return null;
265: }
266:
267: private void insertMenu(XmenuImpl menu) throws SQLException {
268: Connection con = sqlProcessor.getConnection();
269: if (identityManager instanceof IdentityGenerator) {
270: menu.setId(((IdentityGenerator) identityManager).generate(
271: con, "XMENU"));
272: }
273: menu.insert(new SQLProcessor(con, null), "XMENU");
274: if (identityManager instanceof IdentityRetriever) {
275: menu.setId(((IdentityRetriever) identityManager)
276: .retrieve(con));
277: }
278: sqlProcessor.releaseConnection(con);
279: }
280: }
|