/*
 * Created on Aug 26, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package org.biomoby.registry.rdfagent.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.GregorianCalendar;

/**
 * @author Eddie Kawas
 * 
 */

public class Report {

	private StringBuffer buffer = null;

	private String email = "";

	private String admin = "";
	
	private String registry = "";

	/**
	 * Constructor
	 * 
	 */
	public Report(String administrator, String email, String registry, boolean removed) {
		buffer = new StringBuffer();
		this.admin = administrator;
		this.email = email;
		this.registry = registry;
		init(removed);
	}

	/*
	 * method to create the header for the message
	 */
	private void init(boolean removed) {
		addToReport("");
		addToReport("Date: " + new GregorianCalendar().getTime().toString());
		addToReport( "" );
		addToReport("This report was generated by the registry located at");
		addToReport( "\t" + this.registry + ".");
		
		if (removed) {
			addToReport("\tThis report outlines the reason(s) that your service may");
			addToReport("have either been removed or is at risk of removal.");
			addToReport( "" );
		} else {
			addToReport("\tThis report is only an informative message, letting you know");
			addToReport("that your service was found to be modified recently.");
		}
		addToReport("");
		addToReport("\tIf your service has been removed, enclosed in this report you");
		addToReport("will find an RDF document describing the offending service.");
		addToReport("This document can be used to re-register your service, so");
		addToReport("long as you have 'fixed' any problems associated it.");
		addToReport("");
		addToReport("\tFor instance, if your service didn't conform to the API,");
		addToReport("the most likely reason is that you have inputs or outputs");
		addToReport("that do not have article names associated with them.");
		addToReport("Other problems include, but aren't limited to, non-existing");
		addToReport("signature urls, empty required fields, etc. A common problem");
		addToReport("is that some people have RDF documents that contain illegal");
		addToReport("RDF-XML. If you suspect that your service description contains");
		addToReport("illegal RDF-XML, a useful validating site is:");
		addToReport("");
		addToReport("\t\thttp://www.w3.org/RDF/Validator/ which validates structure of the RDF");
		addToReport("\t\t\t\t\tor");
		addToReport("\t\thttp://bioinfo.icapture.ubc.ca/ekawas/agent/RDFAgent_test.html which checks the");
		addToReport("\t\tcontained services");
		addToReport("");
		addToReport("\tHere you can enter your RDF and the servlet will parse it");
		addToReport("and tell you what was wrong. If you aren't up to debugging RDF,");
		addToReport("you could always re-register your service using Dashboard, ");
		addToReport("or an online service registration applet, etc.");
		addToReport("");
		addToReport("\tPlease don't hesitate to email the moby list if you have");
		addToReport("other problems!");
		addToReport("");
		addToReport("Cheers!");
		addToReport("");
		addToReport( this.admin);
		addToReport( this.email);
		addToReport("");
		addToReport("");

	}

	/**
	 * 
	 * @param string
	 *            the string to add to the report. Each string added will have a
	 *            newline appended to it.
	 */
	public void addToReport(String string) {
		buffer.append(string + System.getProperty("line.separator"));
	}

	/**
	 * 
	 * @return a string representing the the report 
	 */
	public String getReport() {
		return buffer.toString();
	}

	/**
	 * 
	 * @param directory the directory to save the report to
	 * @param filename the file to save the report to 
	 * @return true if the report was saved, false otherwise
	 */
	public boolean saveReport(String directory, String filename) {
		File file = new File(directory, filename);
		if (file.exists()) {
			// append to it
			try {
				Log.info("Attempting to append to an existing report");
				BufferedWriter out = new BufferedWriter(new FileWriter(file,
						true));
				out.write(System.getProperty("line.separator"));
				out.write(getReport());
				out.close();
				Log.info("Successfully saved the report to " + directory
						+ filename + ".");
				return true;

			} catch (IOException e) {
				Log.exception(this.getClass().getName(),
						"saveReport(String, String)", e);
				Log.severe(e.getLocalizedMessage());
				Log.info("Couldn't save report. Agent attempted to append "
						+ "to an existing report and failed.");
				return false;
			}
		} else {
			// create a new one
			try {
				Log.info("Attempting to save the report to " + directory
						+ filename + ".");
				BufferedWriter out = new BufferedWriter(new FileWriter(file));
				out.write(getReport());
				out.close();
				Log.info("Successfully saved the report"+ ".");
				return true;
			} catch (IOException e) {
				Log.exception(this.getClass().getName(),
						"saveReport(String, String)", e);
				Log.severe(e.getLocalizedMessage());
				Log.info("Couldn't save report. Agent attempted to create "
						+ "a new report and failed.");
				return false;
			}
		}
	}
}
