001: package com.xoetrope.carousel.services;
002:
003: import java.io.BufferedReader;
004: import java.io.File;
005: import java.io.FileInputStream;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.io.Reader;
009: import java.util.Enumeration;
010: import java.util.Hashtable;
011: import java.util.Properties;
012: import java.util.Vector;
013:
014: import javax.swing.tree.DefaultMutableTreeNode;
015:
016: import net.xoetrope.editor.project.XEditorProject;
017: import net.xoetrope.data.XDataSource;
018: import net.xoetrope.debug.DebugLogger;
019: import net.xoetrope.editor.project.XEditorProjectProperty;
020: import net.xoetrope.optional.data.XOptionalDataSource;
021: import net.xoetrope.xml.XmlElement;
022: import net.xoetrope.xml.XmlParserFactory;
023: import net.xoetrope.xml.XmlSource;
024: import net.xoetrope.xml.XmlWriter;
025: import net.xoetrope.xml.jaxp.JaxpDocumentFactory;
026: import net.xoetrope.xml.jaxp.JaxpXmlElement;
027: import net.xoetrope.xml.nanoxml.NanoXmlElement;
028: import net.xoetrope.xui.XProject;
029: import net.xoetrope.xui.XProjectManager;
030: import net.xoetrope.xui.build.BuildProperties;
031: import net.xoetrope.xui.data.XBaseModel;
032: import net.xoetrope.xui.data.XModel;
033: import net.xoetrope.xui.helper.Constants;
034:
035: /**
036: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
037: * the GNU Public License (GPL), please see license.txt for more details. If
038: * you make commercial use of this software you must purchase a commercial
039: * license from Xoetrope.</p>
040: * <p> $Revision: 1.7 $</p>
041: */
042: public class ModelManager extends XOptionalDataSource {
043: private Hashtable dataSources;
044: //private static ModelManager instance;
045: private static String modelFilename;
046: private String tableFileName;
047: private String routingFilename;
048: private String saveConfigFilename;
049: private Hashtable databaseNodes;
050: private Properties saveCfgProps;
051:
052: public ModelManager(XEditorProject project) {
053: super (project);
054: dataSources = new Hashtable();
055: databaseNodes = new Hashtable();
056:
057: //RouteManager.setupRouteManager( currentProject );
058: saveConfigFilename = currentProject
059: .getStartupParam("SaveConfigFile");
060: openSaveCfgProps();
061: }
062:
063: private void openSaveCfgProps() {
064: String path = ((XEditorProject) currentProject).getPath()
065: + File.separator + "resources" + File.separator
066: + getSaveConfigFilename();
067: try {
068: File file = new File(path);
069: if (file.exists()) {
070: saveCfgProps = new Properties();
071: FileInputStream fis = new FileInputStream(path);
072: saveCfgProps.load(fis);
073: }
074: } catch (Exception ex) {
075: ex.printStackTrace();
076: }
077: }
078:
079: public Properties getSaveCfgProps() {
080: return saveCfgProps;
081: }
082:
083: protected void addStaticDataSource(String name, String fileName) {
084: NanoXmlElement ele = new NanoXmlElement(Constants.DATASOURCE);
085: ele.setAttribute("name", name);
086: ele.setAttribute("filename", fileName);
087: dataSources.put(name, ele);
088: }
089:
090: protected void addDatabaseDataSource(String name, String fileName) {
091: NanoXmlElement ele = new NanoXmlElement(Constants.DATASOURCE);
092: ele.setAttribute("name", name);
093: ele.setAttribute("filename", fileName);
094: ele.setAttribute("type", "database");
095: dataSources.put(name, ele);
096: }
097:
098: /**
099: * Read a model from the Reader
100: * @param r the Reader
101: */
102: public void read(Reader r) {
103: super .read(r);
104: }
105:
106: /**
107: * If the ShowVisualiser property is set to true then show the visualiser
108: * for debugging
109: */
110: protected void showVisualisation() {
111: }
112:
113: /**
114: * Register a basic datasource element
115: * @param source the XmlElement containg the datasouce reference
116: */
117: public void registerDataSource(XmlElement source) {
118: dataSources.put(source.getAttribute("name"), source);
119: }
120:
121: /**
122: * Register a datasource element of type 'database'
123: * @param source the XmlElement containg the datasouce reference
124: */
125: public void registerDatabaseNode(XmlElement source) {
126: databaseNodes.put(source.getAttribute("name"), source);
127: }
128:
129: /**
130: * Initialise and retrieve the Singleton instance
131: * @return the ModelManager Singleton instance
132: */
133: /*public static ModelManager getInstance()
134: {
135: if ( instance == null ) {
136: instance = new ModelManager();
137: instance.readXmlFile();
138: }
139: return instance;
140: }*/
141:
142: /**
143: * Retrieve the dataSources Hashtable
144: * @return Hashtable containing registered datasources
145: */
146: public Hashtable getDataSources() {
147: return dataSources;
148: }
149:
150: /**
151: * Reset the project model and load the datasets as defined by the file named
152: * by the 'ModelData' startup property
153: */
154: public void readXmlFile() {
155: currentProject.getModel().reset(currentProject);
156: XModel root = currentProject.getModel();
157:
158: XDataSource modelDataSource;
159: boolean sourceExists = false;
160:
161: try {
162: modelFilename = currentProject.getStartupParam("ModelData");
163: if (modelFilename != null) {
164: sourceExists = false;
165: File source = null;
166: if (sourceExists) {
167: read(currentProject.getBufferedReader(source,
168: "UTF8"));
169: } else {
170: try {
171: read(currentProject.getBufferedReader(
172: modelFilename, "UTF8"));
173: } catch (Exception ex3) {
174: if (BuildProperties.DEBUG)
175: DebugLogger
176: .logError("Could not access file:"
177: + modelFilename);
178: }
179: }
180: }
181: } catch (Exception ex) {
182: if (BuildProperties.DEBUG)
183: DebugLogger.logError("Exception in setContent");
184: }
185: loadProjectDataSources();
186: }
187:
188: /**
189: * Reset the current project model and load the datasets as defined by the
190: * file named by the 'ModelData' startup property
191: */
192: private void loadProjectDataSources() {
193: String projectFile = ((XEditorProject) currentProject)
194: .getStartupFilePath();
195: try {
196: BufferedReader br = currentProject.getBufferedReader(
197: projectFile, "UTF8");
198: XmlElement src = XmlSource.read(br);
199: XmlElement xlibEle = src.getFirstChildNamed("XLib");
200: if (xlibEle != null) {
201: XmlElement eleDatasources = xlibEle
202: .getFirstChildNamed("DataSources");
203: if (eleDatasources != null) {
204: Vector vDataSource = eleDatasources
205: .getChildren("DataSource");
206: for (int i = 0; i < vDataSource.size(); i++) {
207: XmlElement eleDS = (XmlElement) vDataSource
208: .get(i);
209:
210: Reader sr = currentProject.getBufferedReader(
211: eleDS.getAttribute("filename"), "UTF8");
212: XmlElement dataSrc = XmlSource.read(sr);
213:
214: String type = eleDS.getAttribute("type");
215: if (type == null || type.compareTo("") == 0)
216: loadTable(dataSrc, currentProject
217: .getModel());
218:
219: eleDS.setAttribute("deploy", "false");
220: registerDataSource(eleDS);
221: }
222: }
223: }
224: br.close();
225: } catch (Exception ex) {
226: ex.printStackTrace();
227: }
228: }
229:
230: public DefaultMutableTreeNode createTreeNodes(XModel model,
231: DefaultMutableTreeNode topNode, String selected) {
232: DefaultMutableTreeNode selectedNode = null;
233: for (int i = 0; i < model.getNumChildren(); i++) {
234: DefaultMutableTreeNode tempNode = addNode(model.get(i),
235: topNode, selected);
236: if (tempNode != null)
237: selectedNode = tempNode;
238: }
239: return selectedNode;
240: }
241:
242: DefaultMutableTreeNode addNode(XModel model,
243: DefaultMutableTreeNode parentNode, String selected) {
244: DefaultMutableTreeNode selectedNode = null;
245: DefaultMutableTreeNode root = null;
246: String text;
247: if ((model.getTagName() != null)
248: && ((model.getTagName().compareTo("item") == 0) || (model
249: .getTagName().compareTo("td") == 0)))
250: text = model.getId() + ":" + model.get();
251: else {
252: text = model.getId();
253:
254: root = new DefaultMutableTreeNode(text);
255: parentNode.add(root);
256: String path = getNodePath(root);
257: if ((selected != null) && (path.compareTo(selected) == 0)) {
258: selectedNode = root;
259: }
260: for (int i = 0; i < model.getNumChildren(); i++) {
261: DefaultMutableTreeNode tempnode = addNode(model.get(i),
262: root, selected);
263: if (tempnode != null)
264: selectedNode = tempnode;
265: }
266: }
267: return selectedNode;
268: }
269:
270: private String getNodePath(DefaultMutableTreeNode node) {
271: String modelName = null;
272: Object path[] = node.getPath();
273:
274: modelName = "";
275: for (int i = 1; i < path.length; i++) {
276: modelName += "/" + path[i].toString();
277: }
278: return modelName.substring(1);
279: }
280:
281: XModel loadStaticData(String fileName) {
282: XModel model = new XBaseModel();
283: Reader sr = null;
284: try {
285: sr = currentProject.getBufferedReader(fileName, "UTF8");
286: XmlElement src = XmlSource.read(sr);
287: loadTable(src, model);
288: sr.close();
289: } catch (Exception ex) {
290: ex.printStackTrace();
291: }
292: return model;
293: }
294:
295: XModel loadDatabaseData(String fileName) {
296: XModel model = new XBaseModel();
297: Reader sr = null;
298: try {
299: sr = currentProject.getBufferedReader(fileName, "UTF8");
300: XmlElement src = XmlSource.read(sr);
301: } catch (Exception ex) {
302: ex.printStackTrace();
303: }
304: return model;
305: }
306:
307: void setFileName(String fileName) {
308: modelFilename = fileName;
309: }
310:
311: String getFileName() {
312: return modelFilename;
313: }
314:
315: void writeDatasets(XEditorProject project) {
316: try {
317: Enumeration e = dataSources.elements();
318: JaxpXmlElement eleroot = JaxpDocumentFactory
319: .createDocument("DataSources");
320:
321: while (e.hasMoreElements()) {
322: XmlElement ele = (XmlElement) e.nextElement();
323: if (ele.getAttribute("deploy").compareTo("false") != 0) {
324: XmlElement elenew = eleroot.createElement(ele
325: .getName());
326: Enumeration attribs = ele.enumerateAttributeNames();
327: while (attribs.hasMoreElements()) {
328: String attName = (String) attribs.nextElement();
329: elenew.setAttribute(attName, ele
330: .getAttribute(attName));
331: }
332: }
333: }
334:
335: FileOutputStream fos = new FileOutputStream(project
336: .getPath()
337: + File.separatorChar
338: + "resources"
339: + File.separatorChar + modelFilename);
340: XmlWriter writer = XmlParserFactory.getInstance()
341: .getWriter(fos);
342: writer.write(eleroot, true, 4);
343: fos.flush();
344: fos.close();
345: } catch (IOException ex) {
346: ex.printStackTrace();
347: }
348: }
349:
350: void writeDatabaseDatasets(XEditorProject project) {
351: try {
352: Enumeration e = databaseNodes.elements();
353: NanoXmlElement eleroot = new NanoXmlElement("DataSources");
354: while (e.hasMoreElements()) {
355: XmlElement ele = (XmlElement) e.nextElement();
356: eleroot.addChild(ele);
357: }
358: XmlElement tableNode = (XmlElement) dataSources
359: .get("Tables");
360: if (tableNode != null) {
361: String filename = project.getPath() + File.separator
362: + "resources" + File.separator
363: + tableNode.getAttribute("filename");
364: FileOutputStream fos = new FileOutputStream(filename);
365: XmlWriter writer = XmlParserFactory.getInstance()
366: .getWriter(fos);
367: writer.write(eleroot, true, 4);
368: fos.flush();
369: fos.close();
370: }
371: } catch (Exception e) {
372: e.printStackTrace();
373: }
374: }
375:
376: void writeSaveConfig(XEditorProject project) {
377: if (saveCfgProps != null) {
378: String path = project.getPath() + File.separator
379: + "resources" + File.separator
380: + getSaveConfigFilename();
381: try {
382: FileOutputStream fos = new FileOutputStream(path);
383: saveCfgProps.store(fos, "XLib save config");
384: } catch (Exception ex) {
385: ex.printStackTrace();
386: }
387: }
388:
389: }
390:
391: String getDataSourceAttrib(String datasrc, String attrib) {
392: XmlElement ele = (XmlElement) dataSources.get(datasrc);
393: return ele.getAttribute(attrib);
394: }
395:
396: String getRoutingFileName() {
397: return (String) ((XEditorProjectProperty) currentProject
398: .getObject(Constants.ROUTES)).property;
399: }
400:
401: public String getServicesFileName() {
402: XmlElement ele = (XmlElement) dataSources
403: .get(Constants.SERVICES);
404: if (ele != null)
405: return ele.getAttribute("filename");
406: else
407: return null;
408: }
409:
410: void setRoutingFileName(String filename) {
411: routingFilename = filename;
412: NanoXmlElement ele = (NanoXmlElement) dataSources
413: .remove(Constants.ROUTES);
414: if (ele == null) {
415: ele = new NanoXmlElement("DataSource");
416: ele.setAttribute("name", Constants.ROUTES);
417: ele.setAttribute("type", "routing");
418: }
419: ele.setAttribute("filename", filename);
420: dataSources.put(Constants.ROUTES, ele);
421: }
422:
423: void setSaveConfigFilename(String file) {
424: saveConfigFilename = file;
425: }
426:
427: public String getSaveConfigFilename() {
428: return saveConfigFilename;
429: }
430:
431: void addDatabaseNode(XmlElement ele) {
432: databaseNodes.put(ele.getAttribute("name"), ele);
433: }
434:
435: public Hashtable getDatabaseNodes() {
436: return databaseNodes;
437: }
438:
439: public void save() {
440: }
441: }
|