JavaAgent JFreeChart Conversion to Backing Bean


		//Run the Program
		public void NotesMain() {

		try 
		{

//get the current database being used

Database database= (Database) FacesContext.getCurrentInstance()

.getApplication().getVariableResolver()

.resolveVariable(FacesContext.getCurrentInstance(), "database");

System.out.println("Database Obtained..." + database);

//Call database view

View view = database.getView("By Category");


//Build a collection for each Departement slice in chart

//2012.03.26.10.PM

DocumentCollection reColl = (DocumentCollection) database.getAllDocuments();

//Declare/initialize DepartmentOne String
String DepartMentOneName = "Research Team"; 
//Search the String
reColl.FTSearch(DepartMentOneName);
//get number a count of times DepartMentOneName loaded
int Doc1Counts = reColl.getCount();
//Build a collection for each Departement slice in chart

//2012.03.26.10.PM
DocumentCollection devColl = (DocumentCollection) database.getAllDocuments();
//Declare/initialize DepartmentTwo String
String DepartMentTwoName = "Development Team";
//Search the String
devColl.FTSearch(DepartMentTwoName);
//get number a count of times DepartMentTwoName loaded
int Doc2Counts = devColl.getCount();

//ensuring no nulls are loaded...
if (devColl !=null || reColl !=null)

//TO DO: Limit the number of records being returned, if your database is massive


{

//TO DO: Loop through collection, load an array and feed to Chart
//fill map
HashMap<String, Number> map = new HashMap<String, Number>();
map.put(DepartMentOneName, new Integer(Doc1Counts));
map.put(DepartMentTwoName, new Integer(Doc2Counts));



//TO DO: Get userid into the file name, perhaps add a date to make it unique
//format date in this fashion YYYY.MM.DD.HH.MM.SS.
//Append AM or PM

String UserInfo = "SuperUser";

//2011.06.14.2.05.PM
writeChartToDisk("Diagram", "C:\\temp\\XML_DATA\\"+UserInfo+"pietest.jpg", map); 
// and re-saves, it won't affect the sort order in the view

view.setAutoUpdate(false);

reColl.recycle(); // recycle the one we're done with
devColl.recycle(); // recycle the one we're done with
view.recycle(); //clear up mem for the view
database.recycle();// kill the database

}else{

try {

 //send user to a page of choice and not a blank image
FacesContext.getCurrentInstance().getExternalContext().redirect("../docu.nsf/index.html");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

		} catch (Exception e)
		{
			e.printStackTrace();
		} 
	}
	private void writeChartToDisk(String title, String fileName, Map<String, Number> map)
	{
		//put map values in to a DefaultPieDataset
		Iterator<String> iterator = map.keySet().iterator();
		DefaultPieDataset pieDataset = new DefaultPieDataset();
		while (iterator.hasNext())
		{
			Object o = iterator.next();
			Object o2 = map.get(o);
			pieDataset.setValue((String) o,  (Number) o2);
		}
		
		//Create the actual chart
		JFreeChart chart = ChartFactory.createPieChart(title, pieDataset, true, true, true);
		
		//Write chart to disk as JPG file, and ask explorer.exe to show it.
		//You could extract the file to the html directory of the domino server or attach to a notes-document
		try
		{
		  	FileOutputStream fos = new FileOutputStream( fileName);
			ChartUtilities.writeChartAsJPEG(fos, 1, chart, 750, 400);
			fos.flush();
			fos.close();
			
			Runtime run = Runtime.getRuntime();
			run.exec("explorer.exe  " + fileName );

		} catch (Exception e)
		{
			
			//For Testing purposes as a Java Application
			//remove NotesMain() method and add a 'static void main method
			//commenting out the AgentOutput StackTrace print for errors will be necessary
			//static is also reflected in this method 'static void writeChartToDisk(String title, String fileName, Map map)'
			//to allow the Java Application to load properly, add where necessary
			//e.printStackTrace(getAgentOutput());
		}
	}
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...