package com.foc.web.modules.workflow;

import com.foc.business.workflow.signing.WFTransactionWrapper;
import com.foc.desc.FocObject;
import com.foc.vaadin.FocWebVaadinWindow;
import com.foc.vaadin.gui.components.FVTextArea;
import com.foc.vaadin.gui.layouts.validationLayout.FVValidationLayout;
import com.foc.vaadin.gui.xmlForm.FocXMLLayout;
import com.foc.vaadin.gui.xmlForm.IValidationListener;

@SuppressWarnings("serial")
public class WFTransactionWrapper_Comment_Standard_Form extends FocXMLLayout {
	
	private boolean sign = false;//isSigning otherwise would be rejecting all previous signatures
	
	public WFTransactionWrapper getTransactionWrapper() {
		return (WFTransactionWrapper) getFocData();
	}

//	public Workflow getWorkflow() {
//		return getWFTransactionWrapper() != null ? getWFTransactionWrapper().iWorkflow_getWorkflow() : null;
//	}
//	
//	public StatusHolder getStatusHolder() {
//		return getWFTransactionWrapper() != null ? getWFTransactionWrapper().getStatusHolder() : null;
//	}

	public String getWrittenComment() {
		String writtenComment = "";
		FVTextArea textArea = (FVTextArea) getComponentByName("MY_COMMENT");
		if(textArea != null) {
			writtenComment = textArea.getValue();
		}
		return writtenComment;
	}
	
	@Override
	public void showValidationLayout(boolean showBackButton, int position) {
		super.showValidationLayout(showBackButton, position);
		FVValidationLayout vLay = getValidationLayout();
		if(vLay != null) {
			vLay.setExitWithoutPrompt(true);
			vLay.addValidationListener(new IValidationListener() {
				
				@Override
				public void validationDiscard(FVValidationLayout validationLayout) {
				}
				
				@Override
				public boolean validationCommit(FVValidationLayout validationLayout) {
					return false;
				}
				
				@Override
				public boolean validationCheckData(FVValidationLayout validationLayout) {
					boolean error = false;
					return error;
				}
				
				@Override
				public void validationAfter(FVValidationLayout validationLayout, boolean commited) {
					if(getMainWindow() != null && commited) {
						WFTransactionWrapper wrapper = getTransactionWrapper();
						if(wrapper != null) {
							//If Cancel Mode we popup the comment dialog 
							if(!isSign()){
								wrapper.undoAllSignatures(getWrittenComment());
							}else if(isSign()){
								FocObject focObject = wrapper.getFocObject();
								if(focObject != null) {
									focObject.workflow_SignIfAllowed(getWrittenComment());
								}
							}
							if(getMainWindow() != null && getMainWindow() instanceof FocWebVaadinWindow) {
								((FocWebVaadinWindow)getMainWindow()).refreshPendingSignatureCount();
							}
						}
						
						//If from dash board there is no console and we don't need to goBack() 	
						getMainWindow().refreshCentralPanelAndRightPanel();
					}
				}
			});
		}
	}

	public boolean isSign() {
		return sign;
	}
	
	public void setIsSigning(boolean sign) {
		this.sign = sign;
	}

}

@@ -34,16 +34,74 @@
 low;%0D%0A%0D%0A
+import com.foc.Globals;%0D%0Aimport com.foc.IFocEnvironment;%0D%0A
 import c
@@ -184,24 +184,52 @@
 FocObject;%0D%0A
+import com.foc.util.Utils;%0D%0A
 import com.f
@@ -2021,22 +2021,194 @@
 error = 
-false;
+!isSign() && Utils.isStringEmpty(getWrittenComment());%0D%0A%09%09%09%09%09if(error) %7B%0D%0A%09%09%09%09%09%09Globals.showNotification(%22%D9%8A%D8%B1%D8%AC%D9%89 %D8%A7%D8%AF%D8%AE%D8%A7%D9%84 %D8%A7%D9%84%D9%85%D9%84%D8%A7%D8%AD%D8%B8%D8%A9%22, %22%22, IFocEnvironment.TYPE_WARNING_MESSAGE);%0D%0A%09%09%09%09%09%7D
 %0D%0A%09%09%09%09%09r

Make the Comment mandatory when rejecting signatures