<commit_before>/*
 * $Log: BrowseJdbcTableExecute.java,v $
 * Revision 1.10  2009-10-19 14:01:23  m168309
 * includeFieldDefinition=true
 *
 * Revision 1.9  2009/09/25 12:28:49  <NAME> <<EMAIL>>
 * added WHERE facility
 *
 * Revision 1.8  2008/11/12 12:34:24  <NAME> <<EMAIL>>
 * fixed error in cookies
 *
 * Revision 1.7  2008/11/05 12:22:06  <NAME> <<EMAIL>>
 * impose limits to prevent memory exception
 *
 * Revision 1.6  2008/10/20 13:02:44  <NAME> <<EMAIL>>
 * also show not compressed blobs and not serialized blobs
 *
 * Revision 1.5  2008/06/18 09:17:42  <NAME> <<EMAIL>>
 * corrected rownums together with order by (MO)
 *
 * Revision 1.4  2008/05/22 07:34:00  <NAME> <<EMAIL>>
 * use inherited error() method
 *
 * Revision 1.3  2007/10/08 13:41:35  <NAME> <<EMAIL>>
 * changed ArrayList to List where possible
 *
 * Revision 1.2  2007/05/24 09:54:47  <NAME> <<EMAIL>>
 * made cloudscape compatible
 *
 * Revision 1.1  2007/05/21 12:24:57  <NAME> <<EMAIL>>
 * added browseJdbcTable functions
 *
 */
package nl.nn.adapterframework.webcontrol.action;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.xml.transform.Transformer;

import nl.nn.adapterframework.jdbc.DirectQuerySender;
import nl.nn.adapterframework.jdbc.JdbcFacade;
import nl.nn.adapterframework.jms.JmsRealmFactory;
import nl.nn.adapterframework.util.AppConstants;
import nl.nn.adapterframework.util.ClassUtils;
import nl.nn.adapterframework.util.StringTagger;
import nl.nn.adapterframework.util.XmlUtils;
import nl.nn.adapterframework.webcontrol.IniDynaActionForm;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class BrowseJdbcTableExecute extends ActionBase {
	public static final String version = "$RCSfile: BrowseJdbcTableExecute.java,v $ $Revision: 1.10 $ $Date: 2009-10-19 14:01:23 $";
	public static final String DB2XML_XSLT = "xml/xsl/BrowseJdbcTableExecute.xsl";

	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response)
		throws IOException, ServletException {

		// Initialize action
		initAction(request);
		if (null == config) {
			return (mapping.findForward("noconfig"));
		}

		// Was this transaction cancelled?
		// -------------------------------
		if (isCancelled(request)) {
			log.debug("browseJdbcTable was cancelled");
			removeFormBean(mapping, request);
			return (mapping.findForward("cancel"));
		}

		// Retrieve form content
		// ---------------------
		IniDynaActionForm browseJdbcTableForm = (IniDynaActionForm) form;
		String form_jmsRealm = (String) browseJdbcTableForm.get("jmsRealm");
		String form_tableName = (String) browseJdbcTableForm.get("tableName");
		String form_where = (String) browseJdbcTableForm.get("where");
		boolean form_numberOfRowsOnly = false;
		String form_order = (String) browseJdbcTableForm.get("order");
		if (browseJdbcTableForm.get("numberOfRowsOnly") != null)
			form_numberOfRowsOnly =
				((Boolean) browseJdbcTableForm.get("numberOfRowsOnly"))
					.booleanValue();
		int form_rownumMin = ((Integer) browseJdbcTableForm.get("rownumMin")).intValue();
		int form_rownumMax = ((Integer) browseJdbcTableForm.get("rownumMax")).intValue();

		if (!form_numberOfRowsOnly) {
			if (form_rownumMin < 0) {
				form_rownumMin = 0;
			}
			if (form_rownumMax < 0) {
				form_rownumMax = 0;
			}
			if (form_rownumMin == 0 && form_rownumMax == 0) {
				form_rownumMin = 1;
				form_rownumMax = 100;
			}
			if (errors.isEmpty()) {
				if (form_rownumMax < form_rownumMin) {
					error("errors.generic","Rownum max must be greater than or equal to Rownum min",null);
				}
			}
			if (errors.isEmpty()) {
				if (form_rownumMax - form_rownumMin >= 100) {
					error("errors.generic","Difference between Rownum max and Rownum min must be less than hundred",null);
				}
			}
		}

		if (errors.isEmpty()) {
			DirectQuerySender qs;
			String result = "";
			String query = null;
			try {
				qs = new DirectQuerySender();
				try {
					qs.setName("QuerySender");
					qs.setJmsRealm(form_jmsRealm);

					if (form_numberOfRowsOnly || qs.getDatabaseType() == JdbcFacade.DATABASE_ORACLE) {
						qs.setQueryType("select");
						qs.setBlobSmartGet(true);
						qs.setIncludeFieldDefinition(true);
						qs.configure();
						qs.open();
						query = "SELECT * FROM " + form_tableName + " WHERE ROWNUM=0";
						result = qs.sendMessage("dummy", query);
						String browseJdbcTableExecuteREQ =
							"<browseJdbcTableExecuteREQ>"
								+ "<tableName>"
								+ form_tableName
								+ "</tableName>"
								+ "<where>"
								+ XmlUtils.encodeChars(form_where)
								+ "</where>"
								+ "<numberOfRowsOnly>"
								+ form_numberOfRowsOnly
								+ "</numberOfRowsOnly>"
								+ "<order>"
								+ form_order
								+ "</order>"
								+ "<rownumMin>"<commit_msg>databasetype now defined in DbmsSupportFactory<commit_after>/*
 * $Log: BrowseJdbcTableExecute.java,v $
 * Revision 1.11  2011-03-16 16:38:39  L190409
 * databasetype now defined in DbmsSupportFactory
 *
 * Revision 1.10  2009/10/19 14:01:23  <NAME> <<EMAIL>>
 * includeFieldDefinition=true
 *
 * Revision 1.9  2009/09/25 12:28:49  <NAME> <<EMAIL>>
 * added WHERE facility
 *
 * Revision 1.8  2008/11/12 12:34:24  <NAME> <<EMAIL>>
 * fixed error in cookies
 *
 * Revision 1.7  2008/11/05 12:22:06  <NAME> <<EMAIL>>
 * impose limits to prevent memory exception
 *
 * Revision 1.6  2008/10/20 13:02:44  <NAME> <<EMAIL>>
 * also show not compressed blobs and not serialized blobs
 *
 * Revision 1.5  2008/06/18 09:17:42  <NAME> <<EMAIL>>
 * corrected rownums together with order by (MO)
 *
 * Revision 1.4  2008/05/22 07:34:00  <NAME> <<EMAIL>>
 * use inherited error() method
 *
 * Revision 1.3  2007/10/08 13:41:35  <NAME> <<EMAIL>>
 * changed ArrayList to List where possible
 *
 * Revision 1.2  2007/05/24 09:54:47  <NAME> <<EMAIL>>
 * made cloudscape compatible
 *
 * Revision 1.1  2007/05/21 12:24:57  <NAME> <<EMAIL>>
 * added browseJdbcTable functions
 *
 */
package nl.nn.adapterframework.webcontrol.action;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Transformer;

import nl.nn.adapterframework.jdbc.DirectQuerySender;
import nl.nn.adapterframework.jdbc.dbms.DbmsSupportFactory;
import nl.nn.adapterframework.jms.JmsRealmFactory;
import nl.nn.adapterframework.util.AppConstants;
import nl.nn.adapterframework.util.ClassUtils;
import nl.nn.adapterframework.util.StringTagger;
import nl.nn.adapterframework.util.XmlUtils;
import nl.nn.adapterframework.webcontrol.IniDynaActionForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class BrowseJdbcTableExecute extends ActionBase {
	public static final String version = "$RCSfile: BrowseJdbcTableExecute.java,v $ $Revision: 1.11 $ $Date: 2011-03-16 16:38:39 $";
	public static final String DB2XML_XSLT = "xml/xsl/BrowseJdbcTableExecute.xsl";

	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response)
		throws IOException, ServletException {

		// Initialize action
		initAction(request);
		if (null == config) {
			return (mapping.findForward("noconfig"));
		}

		// Was this transaction cancelled?
		// -------------------------------
		if (isCancelled(request)) {
			log.debug("browseJdbcTable was cancelled");
			removeFormBean(mapping, request);
			return (mapping.findForward("cancel"));
		}

		// Retrieve form content
		// ---------------------
		IniDynaActionForm browseJdbcTableForm = (IniDynaActionForm) form;
		String form_jmsRealm = (String) browseJdbcTableForm.get("jmsRealm");
		String form_tableName = (String) browseJdbcTableForm.get("tableName");
		String form_where = (String) browseJdbcTableForm.get("where");
		boolean form_numberOfRowsOnly = false;
		String form_order = (String) browseJdbcTableForm.get("order");
		if (browseJdbcTableForm.get("numberOfRowsOnly") != null)
			form_numberOfRowsOnly =
				((Boolean) browseJdbcTableForm.get("numberOfRowsOnly"))
					.booleanValue();
		int form_rownumMin = ((Integer) browseJdbcTableForm.get("rownumMin")).intValue();
		int form_rownumMax = ((Integer) browseJdbcTableForm.get("rownumMax")).intValue();

		if (!form_numberOfRowsOnly) {
			if (form_rownumMin < 0) {
				form_rownumMin = 0;
			}
			if (form_rownumMax < 0) {
				form_rownumMax = 0;
			}
			if (form_rownumMin == 0 && form_rownumMax == 0) {
				form_rownumMin = 1;
				form_rownumMax = 100;
			}
			if (errors.isEmpty()) {
				if (form_rownumMax < form_rownumMin) {
					error("errors.generic","Rownum max must be greater than or equal to Rownum min",null);
				}
			}
			if (errors.isEmpty()) {
				if (form_rownumMax - form_rownumMin >= 100) {
					error("errors.generic","Difference between Rownum max and Rownum min must be less than hundred",null);
				}
			}
		}

		if (errors.isEmpty()) {
			DirectQuerySender qs;
			String result = "";
			String query = null;
			try {
				qs = new DirectQuerySender();
				try {
					qs.setName("QuerySender");
					qs.setJmsRealm(form_jmsRealm);

					if (form_numberOfRowsOnly || qs.getDatabaseType() == DbmsSupportFactory.DBMS_ORACLE) {
						qs.setQueryType("select");
						qs.setBlobSmartGet(true);
						qs.setIncludeFieldDefinition(true);
						qs.configure();
						qs.open();
						query = "SELECT * FROM " + form_tableName + " WHERE ROWNUM=0";
						result = qs.sendMessage("dummy", query);
						String browseJdbcTableExecuteREQ =
							"<browseJdbcTableExecuteREQ>"
								+ "<tableName>"
								+ form_tableName
								+ "</tableName>"
								+ "<where>"
								+ XmlUtils.encodeChars(form_where)
								+ "</where>"
								+ "<numberOfRowsOnly>"
								+ form_numberOfRowsOnly
								+ "</numberOfRowsOnly>"
								+ "<order>"
								+ form_order
								+ "</order>"
								+ "<rownumMin>"
{'fraction_non_alphanumeric': 0.1352601156069364, 'fraction_numerical': 0.03998073217726397, 'mean_word_length': 3.6951605608322025, 'pattern_counts': {'":': 0, '<': 86, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1222410', 'n_tokens_mistral': 4236, 'n_tokens_neox': 3408, 'n_words': 720}