KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > DatabaseDefinitionsController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.StringReader JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import javax.xml.transform.TransformerException JavaDoc;
37
38 import org.apache.log4j.Logger;
39 import org.apache.xerces.parsers.DOMParser;
40 import org.apache.xpath.XPathAPI;
41 import org.exolab.castor.jdo.Database;
42 import org.exolab.castor.jdo.OQLQuery;
43 import org.exolab.castor.jdo.QueryResults;
44 import org.infoglue.cms.entities.content.ContentVersion;
45 import org.infoglue.cms.entities.kernel.BaseEntityVO;
46 import org.infoglue.cms.entities.management.CategoryAttribute;
47 import org.infoglue.cms.entities.management.Repository;
48 import org.infoglue.cms.entities.management.impl.simple.RepositoryImpl;
49 import org.infoglue.cms.entities.structure.SiteNodeVersion;
50 import org.infoglue.cms.entities.workflow.Event;
51 import org.infoglue.cms.entities.workflow.EventVO;
52 import org.infoglue.cms.entities.workflow.impl.simple.EventImpl;
53 import org.infoglue.cms.exception.Bug;
54 import org.infoglue.cms.exception.SystemException;
55 import org.infoglue.cms.io.FileHelper;
56 import org.infoglue.cms.security.InfoGluePrincipal;
57 import org.infoglue.cms.util.CmsPropertyHandler;
58 import org.w3c.dom.Document JavaDoc;
59 import org.w3c.dom.Element JavaDoc;
60 import org.w3c.dom.Node JavaDoc;
61 import org.w3c.dom.NodeList JavaDoc;
62 import org.xml.sax.InputSource JavaDoc;
63
64 /**
65  * @author Mattias Bogeblad
66  *
67  * This class implements all operations we can do on the cmEvent-entity.
68  */

69
70 public class DatabaseDefinitionsController
71 {
72     private final static Logger logger = Logger.getLogger(DatabaseDefinitionsController.class.getName());
73
74     public static DatabaseDefinitionsController getController()
75     {
76         return new DatabaseDefinitionsController();
77     }
78     
79     /**
80      * Returns a parsed database definitions document
81      */

82     private Document JavaDoc getDatabaseDefinitionsDocument()
83     {
84         Document JavaDoc document = null;
85         
86         try
87         {
88             String JavaDoc databaseDefinitions = CmsPropertyHandler.getContextRootPath() + File.separator + "WEB-INF" + File.separator + "classes" + File.separator + "databaseDefinitions.xml";
89             
90             File JavaDoc file = new File JavaDoc(databaseDefinitions);
91
92             if(file != null && file.exists())
93             {
94                 InputSource JavaDoc xmlSource = new InputSource JavaDoc(new FileInputStream JavaDoc(file));
95
96                 DOMParser parser = new DOMParser();
97                 parser.parse(xmlSource);
98                 document = parser.getDocument();
99             }
100         }
101         catch(Exception JavaDoc e)
102         {
103             logger.warn("An error occurred when trying to fetch the asset keys:" + e.getMessage(), e);
104         }
105
106         return document;
107     }
108
109     /**
110      * Returns a List of DatabaseDefintions from the file
111      */

112     
113     public Map JavaDoc getDatabaseDefinitions()
114     {
115         Map JavaDoc databases = new HashMap JavaDoc();
116         
117         Document JavaDoc document = getDatabaseDefinitionsDocument();
118         
119         if(document != null)
120         {
121             NodeList JavaDoc databaseNodeList = document.getDocumentElement().getElementsByTagName("database");
122             
123             for(int i=0; i < databaseNodeList.getLength(); i++)
124             {
125                 Element JavaDoc databaseElement = (Element JavaDoc)databaseNodeList.item(i);
126                 String JavaDoc id = databaseElement.getAttribute("id");
127                 NodeList JavaDoc propertyNodeList = databaseElement.getElementsByTagName("property");
128                 
129                 Map JavaDoc database = new HashMap JavaDoc();
130                 database.put("id", id);
131                 
132                 for(int j=0; j < propertyNodeList.getLength(); j++)
133                 {
134                     Element JavaDoc propertyElement = (Element JavaDoc)propertyNodeList.item(j);
135                     String JavaDoc name = propertyElement.getAttribute("name");
136                     String JavaDoc value = propertyElement.getAttribute("value");
137                     
138                     database.put(name, value);
139                 }
140                 
141                 databases.put(id, database);
142             }
143         }
144         
145         return databases;
146     }
147
148     /**
149      * Returns a List of DatabaseDefintions from the file
150      */

151     
152     public Map JavaDoc getDatabaseDefinition(String JavaDoc id)
153     {
154         Map JavaDoc definitions = getDatabaseDefinitions();
155         
156         return (Map JavaDoc)definitions.get(id);
157     }
158
159     
160     /**
161      * Returns a Castor database definition xml string
162      */

163     
164     public File JavaDoc getCastorDatabaseDefinitionFile(String JavaDoc id)
165     {
166         Map JavaDoc databaseDefinition = DatabaseDefinitionsController.getController().getDatabaseDefinition("default");
167         String JavaDoc engine = (String JavaDoc)databaseDefinition.get("driverEngine");
168         String JavaDoc driverClass = (String JavaDoc)databaseDefinition.get("driverClass");
169         String JavaDoc url = (String JavaDoc)databaseDefinition.get("url");
170         String JavaDoc user = (String JavaDoc)databaseDefinition.get("user");
171         String JavaDoc password = (String JavaDoc)databaseDefinition.get("password");
172         String JavaDoc useUnicode = (String JavaDoc)databaseDefinition.get("useUnicode");
173         String JavaDoc encoding = (String JavaDoc)databaseDefinition.get("encoding");
174         String JavaDoc maxConnections = (String JavaDoc)databaseDefinition.get("maxConnections");
175         String JavaDoc mapping = (String JavaDoc)databaseDefinition.get("mapping");
176         
177         url = url.replaceAll("&", "&amp;");
178         
179         /*
180          <jdo-conf>
181          <database name="INFOGLUE_CMS" engine="\"" + engine + "\"">
182             <jndi name="java:comp/env/jdbc/mydb" />
183          </database>
184             <transaction-demarcation mode=\"local\"/>
185       </jdo-conf>
186          */

187         
188         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
189         sb.append("<jdo-conf>");
190         sb.append(" <database name=\"INFOGLUE_CMS\" engine=\"" + engine + "\">");
191         sb.append(" <data-source class-name=\"org.apache.commons.dbcp.BasicDataSource\">");
192         sb.append(" <param name=\"driver-class-name\" value=\"" + driverClass + "\"/>");
193         sb.append(" <param name=\"username\" value=\"" + user + "\"/>");
194         sb.append(" <param name=\"password\" value=\"" + password + "\"/>");
195         sb.append(" <param name=\"url\" value=\"" + url + "\"/>");
196         sb.append(" <param name=\"max-active\" value=\"" + maxConnections + "\"/>");
197         sb.append(" <param name=\"connection-properties\" value=\"useUnicode=" + useUnicode + ";characterEncoding=" + encoding + "\"/>");
198         sb.append(" </data-source>");
199         sb.append(" <mapping HREF=\"classes/" + mapping + "\"/>");
200         sb.append(" </database>");
201         sb.append(" <transaction-demarcation mode=\"local\"/>");
202         sb.append("</jdo-conf>");
203         
204         String JavaDoc xml = sb.toString();
205         
206         String JavaDoc databaseDefinitions = CmsPropertyHandler.getContextRootPath() + File.separator + "WEB-INF" + File.separator + "classes" + File.separator + "currentDatabase.xml";
207         
208         File JavaDoc file = new File JavaDoc(databaseDefinitions);
209         
210         try
211         {
212             FileHelper.writeToFile(file, xml, false);
213         }
214         catch (Exception JavaDoc e)
215         {
216             logger.error("Could not write currentDatabase.xml:" + e.getMessage(), e);
217         }
218         
219         return file;
220     }
221 }
222
Popular Tags