1 32 33 package it.businesslogic.ireport.connection; 34 import it.businesslogic.ireport.*; 35 import it.businesslogic.ireport.gui.MainFrame; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 42 public class JRCustomConnection extends it.businesslogic.ireport.IReportConnection { 43 44 public static final String CLASSNAME_PROPERTY ="iReportConnectionClassName"; 45 public static final String PROPERTY_PREFIX ="prop_"; 46 47 50 private java.util.HashMap innerConnectionProperties = new java.util.HashMap (); 51 private IReportConnection innerConnection = null; 52 private String iReportConnectionClassName = null; 53 54 55 56 57 public JRCustomConnection() { 58 } 59 60 64 public java.sql.Connection getConnection() { 65 if (getInnerConnection() == null) return super.getConnection(); 66 return getInnerConnection().getConnection(); 67 } 68 69 public boolean isJDBCConnection() { 70 if (getInnerConnection() == null) return super.isJDBCConnection(); 71 return getInnerConnection().isJDBCConnection(); 72 } 73 74 77 public java.util.HashMap getProperties() 78 { 79 HashMap map = new HashMap (); 80 HashMap map2 = getInnerConnectionProperties(); 81 Iterator iterator = map2.keySet().iterator(); 82 while (iterator.hasNext()) 83 { 84 Object key = iterator.next(); 85 map.put(PROPERTY_PREFIX + key, map2.get(key)); 86 } 87 88 if (getIReportConnectionClassName() != null) 89 { 90 map.put(CLASSNAME_PROPERTY, getIReportConnectionClassName()); 91 } 92 return map; 93 } 94 95 public void loadProperties(java.util.HashMap map) 96 { 97 HashMap map2 = new HashMap (); 98 Iterator iterator = map.keySet().iterator(); 99 while (iterator.hasNext()) 100 { 101 String key = ""+iterator.next(); 102 Object value = map.get(key); 103 104 if (key.startsWith(PROPERTY_PREFIX)) 105 { 106 map2.put(key.substring(PROPERTY_PREFIX.length()), value); 107 } 108 else if (key.equals(CLASSNAME_PROPERTY)) 109 { 110 this.setIReportConnectionClassName( ""+map.get( CLASSNAME_PROPERTY ) ); 111 } 112 } 113 114 setInnerConnectionProperties( map2 ); 115 setInnerConnection(null); 116 } 117 118 public String getDescription(){ 119 return "Custom connection: " + getInnerConnection().getDescription(); 120 } 121 122 123 127 public net.sf.jasperreports.engine.JRDataSource getJRDataSource() 128 { 129 if (getInnerConnection() == null) return super.getJRDataSource(); 130 return getInnerConnection().getJRDataSource(); 131 } 132 133 public java.util.Map getSpecialParameters(java.util.Map map) throws net.sf.jasperreports.engine.JRException { 134 135 if (getInnerConnection() == null) return super.getSpecialParameters(map); 136 return getInnerConnection().getSpecialParameters(map); 137 } 138 139 public java.util.Map disposeSpecialParameters(java.util.Map map) { 140 141 if (getInnerConnection() == null) return super.disposeSpecialParameters(map); 142 return getInnerConnection().disposeSpecialParameters(map); 143 } 144 145 public boolean isJRDataSource() { 146 147 if (getInnerConnection() == null) return super.isJRDataSource(); 148 return getInnerConnection().isJRDataSource(); 149 } 150 151 152 public String getIReportConnectionClassName() { 153 return iReportConnectionClassName; 154 } 155 156 public void setIReportConnectionClassName(String iReportConnectionClassName) { 157 this.iReportConnectionClassName = iReportConnectionClassName; 158 } 159 160 public IReportConnection getInnerConnection() { 161 162 if (innerConnection == null) 163 { 164 try { 165 166 innerConnection = (IReportConnection)Class.forName( getIReportConnectionClassName(), true, MainFrame.getMainInstance().getReportClassLoader() ).newInstance(); 167 innerConnection.loadProperties( getInnerConnectionProperties() ); 168 169 } catch (Throwable ex) 170 { 171 ex.printStackTrace(); 172 } 173 } 174 175 return innerConnection; 176 } 177 178 public void setInnerConnection(IReportConnection innerConnection) { 179 this.innerConnection = innerConnection; 180 } 181 182 public java.util.HashMap getInnerConnectionProperties() { 183 return innerConnectionProperties; 184 } 185 186 public void setInnerConnectionProperties(java.util.HashMap innerConnectionProperties) { 187 this.innerConnectionProperties = innerConnectionProperties; 188 } 189 190 public void test() throws Exception 191 { 192 IReportConnection testConnection = (IReportConnection)Class.forName( getIReportConnectionClassName(), true, MainFrame.getMainInstance().getReportClassLoader() ).newInstance(); 193 if (testConnection != null) 194 { 195 testConnection.loadProperties( getInnerConnectionProperties() ); 196 testConnection.test(); 197 } 198 } 199 } 200 | Popular Tags |