/* Description :--- Below custom plug-in can be used to drop and recreate the table. Input :--- tableName, scriptFileLocation, Entity ID of DBinfo(dbInfoId). Output :--- Tables will be dropped and recreated. */ import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.InputStream; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.sql.*; import java.util.Iterator; import java.util.Map; import java.util.Collection; import javax.security.auth.Subject; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import com.adeptia.indigo.security.AuthUtil; import com.adeptia.indigo.services.transport.connector.DatabaseConnectionInfo; import com.adeptia.indigo.storage.Entity; import com.adeptia.indigo.storage.EntityManager; import com.adeptia.indigo.storage.EntityManagerFactory; import com.adeptia.indigo.storage.StorageException; import com.adeptia.indigo.utils.IndigoException; import com.adeptia.indigo.utils.JdbcUtils; String _tableName = context.get("tableName"); String _scriptFileLocation=context.get("scriptFileLocation");//Write the context variable here in which you are storing the table name's. String dbInfoId= context.get("dbInfoId"); InputStream inputStream=null; String _tableScript=""; Writer writer=null; Reader reader=null; try { inputStream = new FileInputStream(_scriptFileLocation); if (inputStream != null) { writer = new StringWriter(); char[] buffer = new char[1024]; reader = new InputStreamReader(inputStream); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } _tableScript = writer.toString(); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { inputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } } if(!_tableScript.equalsIgnoreCase("") && !_tableName.equalsIgnoreCase("")){ String truncateQuery="TRUNCATE TABLE "+_tableName.trim() +";"; String dropQuery="DROP TABLE "+_tableName.trim() +";"; //String str = ""; //System.out.println(_tableScript +"@@@@@@@@@@@@@@@@@@@"+truncateQuery); Connection _con = null; Statement _statement =null; Subject _subject = AuthUtil.getAdminSubject(); DatabaseConnectionInfo dbInfo = null ; try{ Entity beanObject1 = (Entity) DatabaseConnectionInfo.class.newInstance(); EntityManager entityManager1 = EntityManagerFactory.getEntityManager( beanObject1.getClass() , _subject ); Iterator it = entityManager1.retrieve(); while( it.hasNext() ) { DatabaseConnectionInfo temp = (DatabaseConnectionInfo) it.next() ; String entityId = temp.getId() ; if( entityId.equals(dbInfoId) ) // Enter the EntityId of database info. { dbInfo = temp ; break ; } } _con = JdbcUtils.getConnection(dbInfo , _subject ); _statement = _con.createStatement() ; _statement.executeUpdate(truncateQuery) ; _statement.executeUpdate(dropQuery); _statement.executeUpdate(_tableScript); } catch(SQLException e) { e.printStackTrace(); throw e; } catch (ClassNotFoundException e) { e.printStackTrace(); throw e; } finally{ if(_statement != null){ _statement.close(); } if(_con != null){ _con.close(); } } }