<commit_before>
<%@page import="org.opencps.util.MessageKeys"%>
<%
/**
 * OpenCPS is the open source Core Public Services software
 * Copyright (C) 2016-present OpenCPS community
 * 
 * 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
 * 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/>.
 */
%>


<%@page import="org.opencps.dossiermgt.service.DossierLocalServiceUtil"%>
<%@page import="org.opencps.dossiermgt.model.Dossier"%>
<%@page import="org.opencps.util.PortletPropsValues"%>
<%@page import="org.opencps.dossiermgt.model.DossierPart"%>
<%@page import="org.opencps.dossiermgt.service.DossierPartLocalServiceUtil"%>
<%@page import="org.opencps.dossiermgt.search.DossierFileDisplayTerms"%>
<%@page import="com.liferay.portal.kernel.servlet.SessionErrors"%>
<%@page import="com.liferay.portal.kernel.servlet.SessionMessages"%>
<%@page import="org.opencps.dossiermgt.search.DossierDisplayTerms"%>
<%@page import="org.opencps.dossiermgt.model.DossierFile"%>
<%@page import="org.opencps.util.WebKeys"%>
<%@page import="org.opencps.util.PortletConstants"%>
<%@page import="javax.portlet.WindowState"%>
<%@page import="javax.portlet.PortletRequest"%>
<%@page import="com.liferay.portlet.PortletURLFactoryUtil"%>
<%@page import="com.liferay.portal.kernel.language.UnicodeLanguageUtil"%>
<%@page import="org.opencps.dossiermgt.service.DossierFileLocalServiceUtil"%>
<%@page import="org.opencps.accountmgt.model.Business"%>
<%@page import="org.opencps.dossiermgt.bean.AccountBean"%>
<%@page import="org.opencps.util.AccountUtil"%>
<%@page import="org.opencps.accountmgt.service.BusinessLocalServiceUtil"%>
<%@page import="org.opencps.accountmgt.service.CitizenLocalServiceUtil"%>
<%@page import="org.opencps.accountmgt.model.Citizen"%>
<%@page import="org.opencps.backend.util.AutoFillFormData"%>
<%@page import="org.opencps.backend.util.BackendUtils"%>
<%@page import="org.opencps.util.JsonUtils"%>
<%@page import="com.liferay.portal.security.auth.AuthTokenUtil"%>

<%@ include file="/init.jsp"%>

<%
	boolean success = false;

	boolean isViewForm = true;

	try{
		success = !SessionMessages.isEmpty(renderRequest) && SessionErrors.isEmpty(renderRequest);
	}catch(Exception e){
		
	}
	
	long primaryKey = ParamUtil.getLong(request, "primaryKey");
	
	long dossierId = ParamUtil.getLong(request, DossierDisplayTerms.DOSSIER_ID);
	
	long dossierPartId = ParamUtil.getLong(request, DossierFileDisplayTerms.DOSSIER_PART_ID);
	
	long dossierFileId = ParamUtil.getLong(request, DossierFileDisplayTerms.DOSSIER_FILE_ID);
	
	long fileGroupId = ParamUtil.getLong(request, DossierDisplayTerms.FILE_GROUP_ID);
	
	long groupDossierPartId = ParamUtil.getLong(request, "groupDossierPartId");

	String groupName = ParamUtil.getString(request, DossierFileDisplayTerms.GROUP_NAME);
	
	String modalDialogId = ParamUtil.getString(request, "modalDialogId");
	
	String redirectURL = ParamUtil.getString(request, "redirectURL");
	
	String sampleData = StringPool.BLANK;
	
	if(primaryKey > 0){
		dossierFileId = primaryKey;
	}
	
	
	DossierPart dossierPart = null;
	
	if(dossierPartId > 0){
		try{
			dossierPart = DossierPartLocalServiceUtil.getDossierPart(dossierPartId);
			sampleData = dossierPart.getSampleData();
		}catch(Exception e){
			
		}
	}
	
	Dossier dossier = null;
	
	if(dossierId > 0){
		try{
			dossier = DossierLocalServiceUtil.getDossier(dossierId);
		}catch(Exception e){
			
		}
	}
	
	
	AccountBean accBean = AccountUtil.getAccountBean(request);
	
	Citizen ownerCitizen = null;
	
	Business ownerBusiness = null;
	
	if (accBean.isCitizen()) {
		ownerCitizen = (Citizen) accBean.getAccountInstance();
		if(dossier != null && (dossier.getDossierStatus().equals(PortletConstants.DOSSIER_STATUS_NEW) || 
						dossier.getDossierStatus().equals(PortletConstants.DOSSIER_STATUS_WAITING))){
			isViewForm = false;
		}
	} else if (accBean.isBusiness()) {
		ownerBusiness = (Business) accBean.getAccountInstance();
		if(dossier != null && (dossier.getDossierStatus().equals(PortletConstants.DOSSIER_STATUS_NEW) || 
						dossier.getDossierStatus().equals(PortletConstants.DOSSIER_STATUS_WAITING))){
			isViewForm = false;
		}
	}else if(accBean.isEmployee()){
		if(dossierPart.getPartType() != PortletConstants.DOSSIER_PART_TYPE_MULTIPLE_RESULT && 
						dossierPart.getPartType() != PortletConstants.DOSSIER_PART_TYPE_RESULT){
			isViewForm = true;
		}else{
			isViewForm = false;
		}
	}
	
	String formData = "";
	
	String auTock = AuthTokenUtil.getToken(request);
	  
	String alpacaSchema = dossierPart != null && Validator.isNotNull(dossierPart.getFormScript()) ? 
	      dossierPart.getFormScript().replaceAll("\\?p_auth=REPLACEKEY", "/group-id/"+ themeDisplay.getScopeGroupId()+"?p_auth="+auTock) : StringPool.BLANK;


	DossierFile dossierFile = null;
	
	if(dossierFileId > 0){
		try{
			dossierFile = DossierFileLocalServiceUtil.getDossierFile(dossierFileId);
		}catch(Exception e){
			
		}
		
		if(dossierFile != null && Validator.isNotNull(dossierFile.getFormData())){
			formData = JsonUtils.quote(dossierFile.getFormData());
		}
	}else{
		formData = AutoFillFormData.dataBinding(sampleData, ownerCitizen, ownerBusiness, dossierId);
	}
	
	String portleName = WebKeys.DOSSIER_MGT_PORTLET;
	
	if(renderResponse.getNamespace().equals(StringPool.UNDERLINE + WebKeys.PROCESS_ORDER_PORTLET + StringPool.UNDERLINE)){
		portleName = WebKeys.PROCESS_ORDER_PORTLET;
	}
	
%>

<liferay-ui:success  key="<%=MessageKeys.DEFAULT_SUCCESS_KEY %>" message="<%=MessageKeys.DEFAULT_SUCCESS_KEY %>"/>

<portlet:actionURL var="updateDynamicFormDataURL" name="updateDynamicFormData"/>

<aui:form 
	name="fm" action="<%=updateDynamicFormDataURL.toString() %>" 
	method="post"
>
	<aui:input name="redirectURL" type="hidden" value="<%=currentURL %>"/>
	<aui:input name="<%=DossierDisplayTerms.DOSSIER_ID %>" type="hidden" value="<%=dossierId %>"/>
	<aui:input name="<%=DossierFileDisplayTerms.DOSSIER_FILE_ID %>" type="hidden" value="<%=dossierFile != null ? dossierFile.getDossierFileId() : 0 %>"/>
	<aui:input name="<%=DossierFileDisplayTerms.DOSSIER_PART_ID %>" type="hidden" value="<%=dossierPartId %>"/>
	<aui:input name="<%=DossierFileDisplayTerms.FORM_DATA %>" type="hidden" value=""/>
	<aui:input name="<%=DossierFileDisplayTerms.GROUP_NAME %>" type="hidden" value="<%=groupName %>"/>
	<aui:input name="<%=DossierDisplayTerms.FILE_GROUP_ID %>" type="hidden" value="<%=fileGroupId%>"/>
	<aui:input name="<%=DossierFileDisplayTerms.DOSSIER_FILE_TYPE %>" type="hidden" value="<%=String.valueOf(renderResponse.getNamespace().equals(StringPool.UNDERLINE + WebKeys.DOSSIER_MGT_PORTLET + StringPool.UNDERLINE) ? PortletConstants.DOSSIER_FILE_TYPE_INPUT : PortletConstants.DOSSIER_FILE_TYPE_OUTPUT) %>"/>
	<aui:input name="groupDossierPartId" type="hidden" value="<%=groupDossierPartId%>"/>
	
	<aui:fieldset id="dynamicForm"></aui:fieldset>
	<aui:fieldset>
		<c:choose>
			<c:when test="<%=!isViewForm %>">
				<c:if test="<%=Validator.isNotNull(alpacaSchema) %>">
					<aui:button type="button" value="save" name="save" cssClass="saveForm"/>
				</c:if>
					
				<c:if test="<%=dossierFileId > 0%>">
					<aui:button type="button" value="preview" name="preview"/>
					
					<aui:button type="button" value="create-file" name="create-file"/>
				</c:if>
			</c:when><commit_msg>hidden create-file button on dynamic form<commit_after>
<%@page import="org.opencps.util.MessageKeys"%>
<%
/**
 * OpenCPS is the open source Core Public Services software
 * Copyright (C) 2016-present OpenCPS community
 * 
 * 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
 * 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/>.
 */
%>


<%@page import="org.opencps.dossiermgt.service.DossierLocalServiceUtil"%>
<%@page import="org.opencps.dossiermgt.model.Dossier"%>
<%@page import="org.opencps.util.PortletPropsValues"%>
<%@page import="org.opencps.dossiermgt.model.DossierPart"%>
<%@page import="org.opencps.dossiermgt.service.DossierPartLocalServiceUtil"%>
<%@page import="org.opencps.dossiermgt.search.DossierFileDisplayTerms"%>
<%@page import="com.liferay.portal.kernel.servlet.SessionErrors"%>
<%@page import="com.liferay.portal.kernel.servlet.SessionMessages"%>
<%@page import="org.opencps.dossiermgt.search.DossierDisplayTerms"%>
<%@page import="org.opencps.dossiermgt.model.DossierFile"%>
<%@page import="org.opencps.util.WebKeys"%>
<%@page import="org.opencps.util.PortletConstants"%>
<%@page import="javax.portlet.WindowState"%>
<%@page import="javax.portlet.PortletRequest"%>
<%@page import="com.liferay.portlet.PortletURLFactoryUtil"%>
<%@page import="com.liferay.portal.kernel.language.UnicodeLanguageUtil"%>
<%@page import="org.opencps.dossiermgt.service.DossierFileLocalServiceUtil"%>
<%@page import="org.opencps.accountmgt.model.Business"%>
<%@page import="org.opencps.dossiermgt.bean.AccountBean"%>
<%@page import="org.opencps.util.AccountUtil"%>
<%@page import="org.opencps.accountmgt.service.BusinessLocalServiceUtil"%>
<%@page import="org.opencps.accountmgt.service.CitizenLocalServiceUtil"%>
<%@page import="org.opencps.accountmgt.model.Citizen"%>
<%@page import="org.opencps.backend.util.AutoFillFormData"%>
<%@page import="org.opencps.backend.util.BackendUtils"%>
<%@page import="org.opencps.util.JsonUtils"%>
<%@page import="com.liferay.portal.security.auth.AuthTokenUtil"%>

<%@ include file="/init.jsp"%>

<%
	boolean success = false;

	boolean isViewForm = true;

	try{
		success = !SessionMessages.isEmpty(renderRequest) && SessionErrors.isEmpty(renderRequest);
	}catch(Exception e){
		
	}
	
	long primaryKey = ParamUtil.getLong(request, "primaryKey");
	
	long dossierId = ParamUtil.getLong(request, DossierDisplayTerms.DOSSIER_ID);
	
	long dossierPartId = ParamUtil.getLong(request, DossierFileDisplayTerms.DOSSIER_PART_ID);
	
	long dossierFileId = ParamUtil.getLong(request, DossierFileDisplayTerms.DOSSIER_FILE_ID);
	
	long fileGroupId = ParamUtil.getLong(request, DossierDisplayTerms.FILE_GROUP_ID);
	
	long groupDossierPartId = ParamUtil.getLong(request, "groupDossierPartId");

	String groupName = ParamUtil.getString(request, DossierFileDisplayTerms.GROUP_NAME);
	
	String modalDialogId = ParamUtil.getString(request, "modalDialogId");
	
	String redirectURL = ParamUtil.getString(request, "redirectURL");
	
	String sampleData = StringPool.BLANK;
	
	if(primaryKey > 0){
		dossierFileId = primaryKey;
	}
	
	
	DossierPart dossierPart = null;
	
	if(dossierPartId > 0){
		try{
			dossierPart = DossierPartLocalServiceUtil.getDossierPart(dossierPartId);
			sampleData = dossierPart.getSampleData();
		}catch(Exception e){
			
		}
	}
	
	Dossier dossier = null;
	
	if(dossierId > 0){
		try{
			dossier = DossierLocalServiceUtil.getDossier(dossierId);
		}catch(Exception e){
			
		}
	}
	
	
	AccountBean accBean = AccountUtil.getAccountBean(request);
	
	Citizen ownerCitizen = null;
	
	Business ownerBusiness = null;
	
	if (accBean.isCitizen()) {
		ownerCitizen = (Citizen) accBean.getAccountInstance();
		if(dossier != null && (dossier.getDossierStatus().equals(PortletConstants.DOSSIER_STATUS_NEW) || 
						dossier.getDossierStatus().equals(PortletConstants.DOSSIER_STATUS_WAITING))){
			isViewForm = false;
		}
	} else if (accBean.isBusiness()) {
		ownerBusiness = (Business) accBean.getAccountInstance();
		if(dossier != null && (dossier.getDossierStatus().equals(PortletConstants.DOSSIER_STATUS_NEW) || 
						dossier.getDossierStatus().equals(PortletConstants.DOSSIER_STATUS_WAITING))){
			isViewForm = false;
		}
	}else if(accBean.isEmployee()){
		if(dossierPart.getPartType() != PortletConstants.DOSSIER_PART_TYPE_MULTIPLE_RESULT && 
						dossierPart.getPartType() != PortletConstants.DOSSIER_PART_TYPE_RESULT){
			isViewForm = true;
		}else{
			isViewForm = false;
		}
	}
	
	String formData = "";
	
	String auTock = AuthTokenUtil.getToken(request);
	  
	String alpacaSchema = dossierPart != null && Validator.isNotNull(dossierPart.getFormScript()) ? 
	      dossierPart.getFormScript().replaceAll("\\?p_auth=REPLACEKEY", "/group-id/"+ themeDisplay.getScopeGroupId()+"?p_auth="+auTock) : StringPool.BLANK;


	DossierFile dossierFile = null;
	
	if(dossierFileId > 0){
		try{
			dossierFile = DossierFileLocalServiceUtil.getDossierFile(dossierFileId);
		}catch(Exception e){
			
		}
		
		if(dossierFile != null && Validator.isNotNull(dossierFile.getFormData())){
			formData = JsonUtils.quote(dossierFile.getFormData());
		}
	}else{
		formData = AutoFillFormData.dataBinding(sampleData, ownerCitizen, ownerBusiness, dossierId);
	}
	
	String portleName = WebKeys.DOSSIER_MGT_PORTLET;
	
	if(renderResponse.getNamespace().equals(StringPool.UNDERLINE + WebKeys.PROCESS_ORDER_PORTLET + StringPool.UNDERLINE)){
		portleName = WebKeys.PROCESS_ORDER_PORTLET;
	}
	
%>

<liferay-ui:success  key="<%=MessageKeys.DEFAULT_SUCCESS_KEY %>" message="<%=MessageKeys.DEFAULT_SUCCESS_KEY %>"/>

<portlet:actionURL var="updateDynamicFormDataURL" name="updateDynamicFormData"/>

<aui:form 
	name="fm" action="<%=updateDynamicFormDataURL.toString() %>" 
	method="post"
>
	<aui:input name="redirectURL" type="hidden" value="<%=currentURL %>"/>
	<aui:input name="<%=DossierDisplayTerms.DOSSIER_ID %>" type="hidden" value="<%=dossierId %>"/>
	<aui:input name="<%=DossierFileDisplayTerms.DOSSIER_FILE_ID %>" type="hidden" value="<%=dossierFile != null ? dossierFile.getDossierFileId() : 0 %>"/>
	<aui:input name="<%=DossierFileDisplayTerms.DOSSIER_PART_ID %>" type="hidden" value="<%=dossierPartId %>"/>
	<aui:input name="<%=DossierFileDisplayTerms.FORM_DATA %>" type="hidden" value=""/>
	<aui:input name="<%=DossierFileDisplayTerms.GROUP_NAME %>" type="hidden" value="<%=groupName %>"/>
	<aui:input name="<%=DossierDisplayTerms.FILE_GROUP_ID %>" type="hidden" value="<%=fileGroupId%>"/>
	<aui:input name="<%=DossierFileDisplayTerms.DOSSIER_FILE_TYPE %>" type="hidden" value="<%=String.valueOf(renderResponse.getNamespace().equals(StringPool.UNDERLINE + WebKeys.DOSSIER_MGT_PORTLET + StringPool.UNDERLINE) ? PortletConstants.DOSSIER_FILE_TYPE_INPUT : PortletConstants.DOSSIER_FILE_TYPE_OUTPUT) %>"/>
	<aui:input name="groupDossierPartId" type="hidden" value="<%=groupDossierPartId%>"/>
	
	<aui:fieldset id="dynamicForm"></aui:fieldset>
	<aui:fieldset>
		<c:choose>
			<c:when test="<%=!isViewForm %>">
				<c:if test="<%=Validator.isNotNull(alpacaSchema) %>">
					<aui:button type="button" value="save" name="save" cssClass="saveForm"/>
				</c:if>
					
				<c:if test="<%=dossierFileId > 0%>">
					<aui:button type="button" value="preview" name="preview"/>
					
<%-- 					<aui:button type="button" value="create-file" name="create-file"/> --%>
				</c:if>
			</c:when>
{'fraction_non_alphanumeric': 0.1500064077918749, 'fraction_numerical': 0.0014097142124823785, 'mean_word_length': 6.1493357764544205, 'pattern_counts': {'":': 0, '<': 160, '<?xml version=': 0, '>': 170, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5566078', 'n_tokens_mistral': 5926, 'n_tokens_neox': 5361, 'n_words': 834}