package com.foc.web.modules.business;

import com.foc.business.printing.PrnLayout;
import com.foc.business.printing.gui.PrintingAction;
import com.vaadin.server.DownloadStream;
import com.vaadin.server.StreamResource;

public class PrnLayout_BrowserWindowOpenerStreamResource extends StreamResource {

	private PrintingAction printingAction = null;
	private PrnLayout      prnLayout      = null;
	private boolean        wordDoc = false;
	
	public PrnLayout_BrowserWindowOpenerStreamResource(PrnLayout prnLayout, PrintingAction printingaction) {
		this(prnLayout, printingaction, false);
	}
	
	public PrnLayout_BrowserWindowOpenerStreamResource(PrnLayout prnLayout, PrintingAction printingAction, boolean wordDoc) {
		super(null, "printnig_"+System.currentTimeMillis()+ (wordDoc ? ".docx" : ".pdf"));
		this.prnLayout = prnLayout;
		this.printingAction = printingAction;
		this.wordDoc = wordDoc;
		setCacheTime(1);
	}
	
	public void dispose(){
		printingAction = null;
		prnLayout = null;
	}
	
	protected boolean isWithLogo() {
		return true;
	}
	
	public PrintingAction getPrintingAction() {
		return printingAction;
	}
	
	@Override
	public DownloadStream getStream() {
		DownloadStream downloadStream = null;
		if(getPrintingAction() != null && getPrintingAction().getLauncher() != null){
			if(prnLayout != null){
				
//				if(this.prnLayout_Table.getPrintLogoCheckBox() != null){
//				  boolean withLogo = this.prnLayout_Table.getPrintLogoCheckBox().getValue();
//				  this.prnLayout_Table.getPrintingAction().getLauncher().setWithLogo(withLogo);
//				}
				
			  getPrintingAction().getLauncher().setWithLogo(isWithLogo());
				getPrintingAction().setLaunchedAutomatically(false);
				
				byte[] bytes = null;
				if(wordDoc){
					bytes = getPrintingAction().getLauncher().printWordDocument(prnLayout);
				}else{
					bytes = getPrintingAction().getLauncher().web_FillReport(prnLayout, prnLayout.getFileName());
				}
				if(bytes != null){
					if(wordDoc){
						setMIMEType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
					}else{
						setMIMEType("application/pdf");
					}
		  		setStreamSource(new FStreamSource(bytes));
				}
				downloadStream = super.getStream();
			}
		}
		return downloadStream;
	}
	
}
@@ -432,16 +432,23 @@
 wordDoc 
+       
 = false;
@@ -2320,17 +2320,300 @@
 Stream;%0D%0A%09%7D%0D
+%0A%0D%0A%09public PrnLayout getPrnLayout() %7B%0D%0A%09%09return prnLayout;%0D%0A%09%7D%0D%0A%0D%0A%09public void setPrnLayout(PrnLayout prnLayout) %7B%0D%0A%09%09this.prnLayout = prnLayout;%0D%0A%09%7D%0D%0A%0D%0A%09public boolean isWordDoc() %7B%0D%0A%09%09return wordDoc;%0D%0A%09%7D%0D%0A%0D%0A%09public void setWordDoc(boolean wordDoc) %7B%0D%0A%09%09this.wordDoc = wordDoc;%0D%0A%09%7D%0D
 %0A%09%0D%0A%7D

Add accessors to PrnLayout_BrowserWindowOpenerStreamResource