//#############################################################################
//#                                                                           #
//#  Copyright (C) <2014>  <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/>.    #
//#                                                                           #
//#############################################################################
//#EOH
// This code was generated by Catalin Tomozei using IMS Development Environment (version 1.65 build 3182.26548)
// Copyright (C) 1995-2008 IMS MAXIMS plc. All rights reserved.

package ims.core.forms.upload;

import ims.core.vo.enums.UploadComponentWorkingMode;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;
	String[] allowType = {"doc", "docx","rtf","txt","jpg","png","bmp","gif","tif","html", "pdf"};

	public String getValue() 
	{
		return form.getLocalContext().getImportedFile();
	}

	public void initialize() 
	{
		setWorkingMode(UploadComponentWorkingMode.COMPONENT);
		clearLocalContexts();
	}

	public String getAbsolutePath() 
	{
		return form.getLocalContext().getAbsolutePath();	
	}	

	public void setWorkingMode(UploadComponentWorkingMode mode) 
	{
		if (mode.equals(UploadComponentWorkingMode.APPLET))
		{
			initialiseUploadApplet();
		}
		else if (mode.equals(UploadComponentWorkingMode.COMPONENT))
		{
			initialiseUploadControl();
		}
	}
	
	public void deleteUploadedFile(String absPath)
	{
	}
	/*	String html="";
		form.htmUpload().setHTML("");
		html += "<applet code='applet/Delete.class'";
		html += "	width='1' height='1'";
		html += "	mayscript='mayscript'>";			
		html += "		<param name='absPath'"; 
		html += "			value='"+ absPath + "'>";
		html += "</applet>";	
		
		form.htmUpload().setHTML(html);
	}
	
	*/
	private void initialiseUploadApplet() 
	{
		/*String html="";
		form.htmUpload().setHTML("");
			
		html += "<applet code='applet/Upload.class'";
		html += "	width='285' height='22'";
		html += "	archive='upload.jar, jodconverter-2.2.1.jar, xstream-1.2.2.jar, unoil-2.3.0.jar, slf4j-jdk14-1.4.3.jar, slf4j-api-1.4.3.jar, ridl-2.3.0.jar, jurt-2.3.0.jar, juh-2.3.0-2.3.0.jar, commons-io-1.3.1.jar, commons-cli-1.0.jar'";
		html += "	scriptable='true'";	
		html += "	mayscript='true'>";		
		html += "		<param name='uploadPath'"; 
		html += "			value='uploads/'>";
		html += "		<param name='servletURL'"; 
		html += "			value='http://localhost:8080/CARE_UK/Upload'>";					 
		html += "</applet>";

		form.fileupld().setVisible(false);
		form.htmUpload().setVisible(true);
		form.htmUpload().setHTML(html);*/
	}
	
	private void initialiseUploadControl()
	{
		form.fileupld().setVisible(true);		
		/*form.htmUpload().setVisible(false);
		form.htm1().setHTML("");*/
	}

	private void clearLocalContexts() 
	{
		form.getLocalContext().setImportedFile(null);	
		form.getLocalContext().setAbsolutePath(null);		
	}
	
	@Override
	protected void onfileupldFileSelected(String fileName) 
	{								
		form.getLocalContext().setImportedFile(fileName);				
		form.fireCustomControlValueChanged();
	}

	@Override
	protected void onfileupldFileUploaded(String fileName) 
	{
		form.getLocalContext().setImportedFile(fileName);				
		form.fireCustomControlValueChanged();
	}

	public void setVisible(Boolean visible)
	{
		if (visible == null)
			return;
		form.lbl1().setVisible(visible);
		form.fileupld().setVisible(visible);
		
	}			
}
