//#############################################################################
//#                                                                           #
//#  Copyright (C) <2015>  <IMS MAXIMS>                                       #
//#                                                                           #
//#  This program is free software: you can redistribute it and/or modify     #
//#  it under the terms of the GNU Affero General Public License as           #
//#  published by the Free Software Foundation, either version 3 of the       #
//#  License, or (at your option) any later version.                          # 
//#                                                                           #
//#  This program is distributed in the hope that it will be useful,          #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of           #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
//#  GNU Affero General Public License for more details.                      #
//#                                                                           #
//#  You should have received a copy of the GNU Affero General Public License #
//#  along with this program.  If not, see <http://www.gnu.org/licenses/>.    #
//#                                                                           #
//#  IMS MAXIMS provides absolutely NO GUARANTEE OF THE CLINICAL SAFTEY of    #
//#  this program.  Users of this software do so entirely at their own risk.  #
//#  IMS MAXIMS only ensures the Clinical Safety of unaltered run-time        #
//#  software that it builds, deploys and maintains.                          #
//#                                                                           #
//#############################################################################
//#EOH
// This code was generated by Rory Fitzpatrick using IMS Development Environment (version 1.71 build 3946.24554)
// Copyright (C) 1995-2010 IMS MAXIMS. All rights reserved.

package ims.clinical.forms.edischargepreviewsthkcomponent;

import com.ims.query.builder.client.QueryBuilderClient;
import com.ims.query.builder.client.SeedValue;
import com.ims.query.builder.client.exceptions.QueryBuilderClientException;

import ims.configuration.gen.ConfigFlag;
import ims.core.vo.CareContextShortVo;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;
	private String urlQueryServer = ConfigFlag.GEN.QUERY_SERVER_URL.getValue();
	private String urlReportServer = ConfigFlag.GEN.REPORT_SERVER_URL.getValue();

	@Override
	protected void onFormOpen(Object[] args) throws ims.framework.exceptions.PresentationLogicException
	{
		// TODO Add your code here.
	}
	/**
	* initialise
	*/
	public void initialise()
	{
		String url = buildReport();
		form.htmPreview().setHTML("<p align='center'>" + url );
	}
	private String buildReport()
	{		
		CareContextShortVo currentCareContext = form.getGlobalContext().Core.getCurrentCareContext();
		Object[] obj = null;
		QueryBuilderClient client = new QueryBuilderClient(urlQueryServer, engine.getSessionId());
		String url = "";
		
		Integer reportID = 247;
		obj = domain.getSystemReportAndTemplate(reportID);
		client.addSeed(new SeedValue("CARE_CONTEXT",  currentCareContext.getID_CareContext(), Integer.class));
		
		if(obj == null || obj.length < 2)
		{
			engine.showMessage("I could not get the report and template !");
			return "";
		}
		
		if(obj[0] == null || obj[1] == null)
		{
			engine.showMessage("The report has not been deployed !");
			return "";
		}
				
		try
		{
			url = client.buildReportAsUrlInlineFrame((String)obj[0], (String)obj[1], urlReportServer, "PDF", "", 1);
			if (url != null)
			{
				return url;
			}
		}
		catch (QueryBuilderClientException e1)
		{
			engine.showMessage("Error creating report: " + e1.getMessage());
			return "";
		}
				
		return "";
	}

	public void setReadOnly()
	{
		// TODO: Add your code here.
	}
}
