001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * EJBQLConnection.java
028: *
029: * Created on 4 giugno 2003, 18.15
030: *
031: */
032:
033: package it.businesslogic.ireport.connection;
034:
035: import it.businesslogic.ireport.*;
036: import it.businesslogic.ireport.IReportConnectionEditor;
037: import it.businesslogic.ireport.connection.gui.EJBQLConnectionEditor;
038: import it.businesslogic.ireport.gui.MainFrame;
039: import it.businesslogic.ireport.util.*;
040: import java.net.URLClassLoader;
041: import java.util.HashMap;
042: import java.util.List;
043: import javax.swing.*;
044:
045: import javax.persistence.EntityManager;
046: import javax.persistence.EntityManagerFactory;
047: import javax.persistence.Persistence;
048:
049: /**
050: *
051: * @author Administrator
052: */
053: public class EJBQLConnection extends
054: it.businesslogic.ireport.IReportConnection {
055:
056: private String name;
057:
058: private java.util.HashMap map = null;
059: private String persistenceUnit;
060:
061: private EntityManager em = null;
062: private EntityManagerFactory emf = null;
063:
064: private int usedby = 0;
065:
066: /** Creates a new instance of JDBCConnection */
067:
068: public EJBQLConnection() {
069: map = new java.util.HashMap();
070: }
071:
072: /** This method return an instanced connection to the database.
073: * If isJDBCConnection() return false => getConnection() return null
074: *
075: */
076: public java.sql.Connection getConnection() {
077: return null;
078: }
079:
080: public boolean isJDBCConnection() {
081: return false;
082: }
083:
084: public boolean isJRDataSource() {
085: return false;
086: }
087:
088: /*
089: * This method return all properties used by this connection
090: */
091: public java.util.HashMap getProperties() {
092: return map;
093: }
094:
095: public void loadProperties(java.util.HashMap map) {
096: this .map = map;
097: }
098:
099: /**
100: * This method return an instanced JRDataDource to the database.
101: * If isJDBCConnection() return true => getJRDataSource() return false
102: */
103: public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
104: return null;
105: }
106:
107: public EntityManager getEntityManager() throws Exception {
108: if (em == null) {
109: if (emf == null) {
110: ClassLoader cl = Thread.currentThread()
111: .getContextClassLoader();
112: if (cl instanceof ReportClassLoader) {
113: List items = ((ReportClassLoader) cl)
114: .getCachedItems();
115:
116: java.net.URL[] urls = new java.net.URL[items.size()];
117: for (int i = 0; i < items.size(); ++i) {
118: urls[i] = new java.io.File("" + items.get(i))
119: .toURL();
120: }
121: URLClassLoader urlClassLoader = new URLClassLoader(
122: urls, cl);
123: Thread.currentThread().setContextClassLoader(
124: urlClassLoader);
125: }
126:
127: emf = Persistence.createEntityManagerFactory(Misc.nvl(
128: getProperties().get("PersistenceUnit"), null),
129: new HashMap());
130: //if (emf == null) throw new Exception("Unable to create the EntityManagerFactory for persistence unit " + Misc.nvl(getProperties().get("PersistenceUnit"), null));
131: //Thread.currentThread().setContextClassLoader().
132: }
133: em = emf.createEntityManager();
134: }
135: usedby++;
136: return em;
137: }
138:
139: public void closeEntityManager() {
140: try {
141: if (em != null) {
142: usedby--;
143: if (usedby == 0) {
144: em.close();
145: em = null;
146: }
147: }
148: } catch (Exception ex) {
149: }
150: }
151:
152: public String getDescription() {
153: return I18n.getString("connectionType.ejbql",
154: "EJBQL connection");
155: }
156:
157: public IReportConnectionEditor getIReportConnectionEditor() {
158: return new EJBQLConnectionEditor();
159: }
160:
161: public void test() throws Exception {
162: try {
163: SwingUtilities.invokeLater(new Runnable() {
164: public void run() {
165:
166: Thread.currentThread().setContextClassLoader(
167: MainFrame.getMainInstance()
168: .getReportClassLoader());
169:
170: try {
171:
172: getEntityManager();
173: closeEntityManager();
174: JOptionPane
175: .showMessageDialog(
176: MainFrame.getMainInstance(),
177: I18n
178: .getString(
179: "messages.connectionDialog.connectionTestSuccessful",
180: "Connection test successful!"),
181: "",
182: JOptionPane.INFORMATION_MESSAGE);
183:
184: } catch (Exception ex) {
185: ex.printStackTrace();
186: JOptionPane.showMessageDialog(MainFrame
187: .getMainInstance(), ex.getMessage(),
188: I18n.getString("message.title.error",
189: "Error"),
190: JOptionPane.ERROR_MESSAGE);
191: return;
192: } finally {
193:
194: }
195:
196: }
197: });
198: } catch (Exception ex) {
199: }
200: }
201: }
|