Wednesday, September 26, 2012

generate report from java


package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.ReportEngine;

public class BirtTest {

     public static void main(String[] args) {
          //Variables used to control BIRT Engine instance
          EngineConfig conf = null;
          ReportEngine eng = null;
          IReportRunnable design = null;
          IRunAndRenderTask task = null;
          HTMLRenderContext renderContext = null;
          HashMap contextMap = null;
          HTMLRenderOption options = null;
          //Now, setup the BIRT engine configuration. The Engine Home is hardcoded
          //here, this is probably better set in an environment variable or in
          //a configuration file. No other options need to be set
          conf = new EngineConfig();
          conf.setEngineHome("C:/eclipse-birt/eclipse/birt-runtime-4_2_0/ReportEngine");
       
          //Create new Report engine based off of the configuration
          eng = new ReportEngine( conf );
       
          //With our new engine, lets try to open the report design
          try
          {
               design = eng.openReportDesign("C:/sanker/birt/birt-test/new_report.rptdesign");
          }
          catch (Exception e)
          {
               System.err.println("An error occured during the opening of the report file!");
               e.printStackTrace();
               System.exit(-1);
          }
       
          //With the file open, create the Run and Render task to run the report
          task = eng.createRunAndRenderTask(design);
       
          //Set Render context to handle url and image locataions, and apply to the
          //task
          renderContext = new HTMLRenderContext();
          renderContext.setImageDirectory("image");
          contextMap = new HashMap();
          contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );
          task.setAppContext( contextMap );
          ResultSet rs = getResultSet(); // a function that executes the query and returns resultset
          task.setParameterValue("myResultSet", rs);
          //This will set the output file location, the format to rener to, and
          //apply to the task
          options = new HTMLRenderOption();
          options.setOutputFileName("C:/sanker/birt/output/output.html");
          options.setOutputFormat("html");
          task.setRenderOption(options);
       
          try
          {
               task.run();
          }
          catch (Exception e)
          {
               e.printStackTrace();
          }
       
          eng.destroy();
     }
   
     static ResultSet getResultSet() {
    ResultSet rs = null;
    try {
Connection con = getConnection();
Statement stmt = con.createStatement();
stmt.execute("select id, name from A");
rs = stmt.getResultSet();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    return rs;
     }
   
     static Connection getConnection() {
    try {
Class.forName("oracle.jdbc.driver.OracleDriver");
return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","sanker","password");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    return null;
     }
}


/*******************************************************/
package com.test;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.api.script.IUpdatableDataSetRow;
import org.eclipse.birt.report.engine.api.script.ScriptException;
import org.eclipse.birt.report.engine.api.script.eventadapter.ScriptedDataSetEventAdapter;
import org.eclipse.birt.report.engine.api.script.instance.IDataSetInstance;

public class MyScriptedSetAdapter extends ScriptedDataSetEventAdapter {

ResultSet resultSet;

@Override
public boolean fetch(IDataSetInstance dataSet, IUpdatableDataSetRow row)
throws ScriptException {
try {
if(resultSet.isAfterLast())
return false;// to indicate that there are no more rows left to process
row.setColumnValue("ID", resultSet.getInt(1));
row.setColumnValue("NAME", resultSet.getString(2));
resultSet.next();
return true; // to indicate that more rows to follow
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}

@Override
public void open(IDataSetInstance dataSet) throws ScriptException {
super.open(dataSet);
try {
if(resultSet != null)
resultSet.next();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void beforeOpen(IDataSetInstance dataSet, IReportContext reportContext) throws ScriptException {
super.beforeOpen(dataSet, reportContext);
//retrieve the parameter myResultSet set from the java class in Step 1
System.out.println("myResultSet="+reportContext.getParameterValue("myResultSet"));
resultSet = (ResultSet)reportContext.getParameterValue("myResultSet");
}
}

Saturday, March 3, 2012

Find missing number in a list in java


Problem :- 

         Assume that there is a list of numbers that has numbers from 1 to n. Out of the list, one number is missing. Find the missing number from the list.

Solution :-

       The solution (written in java) with least time complexity is to use BitSet and set bit in the bitset for each corresponding number in the list. The first bit that is not set will be the missing number in the list.

void findMissingNum(int[] ar) {
if(ar != null) {
BitSet bs = new BitSet(ar.length);
for(int no : ar) {
bs.set(no);
}
System.out.println("Missing number=" + bs.nextClearBit(1));
}
}