<commit_before>/*******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2002-2012 by Pentaho : http://www.pentaho.com
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.di.trans.steps.ldapinput;

import java.util.HashSet;

import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.row.RowDataUtil;
import org.pentaho.di.core.row.RowMeta;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStep;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.pentaho.di.core.encryption.Encr;



/**
 * Read LDAP Host, convert them to rows and writes these to one or more output streams.
 * 
 * @author Samatar
 * @since 21-09-2007
 */
public class LDAPInput extends BaseStep implements StepInterface
{
	private static Class<?> PKG = LDAPInputMeta.class; // for i18n purposes, needed by Translator2!!   $NON-NLS-1$

	private LDAPInputMeta meta;
	private LDAPInputData data;

	
	public LDAPInput(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta, Trans trans)
	{
		super(stepMeta, stepDataInterface, copyNr, transMeta, trans);
	}
	
	public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
       
		if(!data.dynamic) {
			if (first) {
	   
	            first = false;
	            
	            // Create the output row meta-data
	            data.outputRowMeta = new RowMeta();
	            meta.getFields(data.outputRowMeta, getStepname(), null, null, this); // get the metadata populated
	            
	            // Create convert meta-data objects that will contain Date & Number formatters
	            //
	
				data.convertRowMeta = data.outputRowMeta.clone();
				for (int i=0;i<data.convertRowMeta.size();i++) {
						data.convertRowMeta.getValueMeta(i).setType(ValueMetaInterface.TYPE_STRING);           
				}
	
			    // Search records once
				search(data.staticSearchBase, data.staticFilter);
	        }
		}
		
        Object[] outputRowData=null;

		try {	
			outputRowData =getOneRow();
			
			if(outputRowData==null) {
				setOutputDone();
				return false;
			}
		
			putRow(data.outputRowMeta, outputRowData);  // copy row to output rowset(s);
		    
			if(log.isRowLevel()) logRowlevel(BaseMessages.getString(PKG, "LDAPInput.log.ReadRow"), data.outputRowMeta.getString(outputRowData));
			
		    if (checkFeedback(getLinesInput())) {
		    	if(log.isDetailed()) logDetailed(BaseMessages.getString(PKG, "LDAPInput.log.LineRow") + getLinesInput());
		    }
		    
		    return true; 
		    
		}  catch(Exception e){
	        boolean sendToErrorRow=false;
			String errorMessage = null;
			
			if (getStepMeta().isDoingErrorHandling()){
		         sendToErrorRow = true;
		         errorMessage = e.toString();
			} else {
				logError(BaseMessages.getString(PKG, "LDAPInput.log.Exception", e.getMessage()));
				setErrors(1);
				logError(Const.getStackTracker(e));
				stopAll();
				setOutputDone();  // signal end to receiver(s)
				return false;				
			}
			if (sendToErrorRow) {
			   // Simply add this row to the error row
			   putError(getInputRowMeta(), outputRowData, 1, errorMessage, null, "LDAPINPUT001");
			}
		} 
		return true;
	}	
	
	private boolean dynamicSearch() throws KettleException {
	
		data.readRow=getRow();  
		// Get row from input rowset & set row busy!
		if (data.readRow==null) {
			if (log.isDetailed()) logDetailed(BaseMessages.getString(PKG, "LDAPInput.Log.FinishedProcessing"));
			setOutputDone();
			return false;
	    }
		if(first) {
            first = false;

        	if(meta.isDynamicSearch()) {
	        	if(Const.isEmpty(meta.getDynamicSearchFieldName())) {
	        		throw new KettleException(BaseMessages.getString(PKG, "LDAPInput.Error.DynamicSearchFieldMissing"));
	        	}
        	}
        	if(meta.isDynamicFilter()) {
        		if(Const.isEmpty(meta.getDynamicFilterFieldName())) {
            		throw new KettleException(BaseMessages.getString(PKG, "LDAPInput.Error.DynamicFilterFieldMissing"));
            	}
        	}
        	
            // Create the output row meta-data
            data.nrIncomingFields= getInputRowMeta().size();
            data.outputRowMeta = getInputRowMeta().clone();
            meta.getFields(data.outputRowMeta, getStepname(), null, null, this); // get the metadata populated
            
            // Create convert meta-data objects that will contain Date & Number formatters
            //

			data.convertRowMeta = data.outputRowMeta.clone();
			for (int i=0;i<data.convertRowMeta.size();i++) {
				data.convertRowMeta.getValueMeta(i).setType(ValueMetaInterface.TYPE_STRING);           
			}
			if(meta.isDynamicSearch()) {
				data.indexOfSearchBaseField =getInputRowMeta().indexOfValue(meta.getDynamicSearchFieldName());
				if (data.indexOfSearchBaseField<0) {
					// The field is unreachable !
					throw new KettleException(BaseMessages.getString(PKG, "LDAPInput.Exception.CouldnotFindField",meta.getDynamicSearchFieldName())); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
			if(meta.isDynamicFilter()) {
				data.indexOfFilterField =getInputRowMeta().indexOfValue(meta.getDynamicFilterFieldName());
				if (data.indexOfFilterField<0) {
					// The field is unreachable !
					throw new KettleException(BaseMessages.getString(PKG, "LDAPInput.Exception.CouldnotFindField",meta.getDynamicFilterFieldName())); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
		}// end if
		
		String searchBase=data.staticSearchBase;
		if(data.indexOfSearchBaseField>0) {
			// retrieve dynamic search base value
			searchBase=getInputRowMeta().getString(data.readRow, data.indexOfSearchBaseField);
		}
		String filter=data.staticFilter;
		if(data.indexOfFilterField>0) {<commit_msg>PDI-7000:  LDAPInput error when using dynamic filter. Modified the logic used to determine the dynamic filter's field position.<commit_after>/*******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2002-2012 by Pentaho : http://www.pentaho.com
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.di.trans.steps.ldapinput;

import java.util.HashSet;

import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.row.RowDataUtil;
import org.pentaho.di.core.row.RowMeta;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStep;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.pentaho.di.core.encryption.Encr;



/**
 * Read LDAP Host, convert them to rows and writes these to one or more output streams.
 * 
 * @author Samatar
 * @since 21-09-2007
 */
public class LDAPInput extends BaseStep implements StepInterface
{
	private static Class<?> PKG = LDAPInputMeta.class; // for i18n purposes, needed by Translator2!!   $NON-NLS-1$

	private LDAPInputMeta meta;
	private LDAPInputData data;

	
	public LDAPInput(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta, Trans trans)
	{
		super(stepMeta, stepDataInterface, copyNr, transMeta, trans);
	}
	
	public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
       
		if(!data.dynamic) {
			if (first) {
	   
	            first = false;
	            
	            // Create the output row meta-data
	            data.outputRowMeta = new RowMeta();
	            meta.getFields(data.outputRowMeta, getStepname(), null, null, this); // get the metadata populated
	            
	            // Create convert meta-data objects that will contain Date & Number formatters
	            //
	
				data.convertRowMeta = data.outputRowMeta.clone();
				for (int i=0;i<data.convertRowMeta.size();i++) {
						data.convertRowMeta.getValueMeta(i).setType(ValueMetaInterface.TYPE_STRING);           
				}
	
			    // Search records once
				search(data.staticSearchBase, data.staticFilter);
	        }
		}
		
        Object[] outputRowData=null;

		try {	
			outputRowData =getOneRow();
			
			if(outputRowData==null) {
				setOutputDone();
				return false;
			}
		
			putRow(data.outputRowMeta, outputRowData);  // copy row to output rowset(s);
		    
			if(log.isRowLevel()) logRowlevel(BaseMessages.getString(PKG, "LDAPInput.log.ReadRow"), data.outputRowMeta.getString(outputRowData));
			
		    if (checkFeedback(getLinesInput())) {
		    	if(log.isDetailed()) logDetailed(BaseMessages.getString(PKG, "LDAPInput.log.LineRow") + getLinesInput());
		    }
		    
		    return true; 
		    
		}  catch(Exception e){
	        boolean sendToErrorRow=false;
			String errorMessage = null;
			
			if (getStepMeta().isDoingErrorHandling()){
		         sendToErrorRow = true;
		         errorMessage = e.toString();
			} else {
				logError(BaseMessages.getString(PKG, "LDAPInput.log.Exception", e.getMessage()));
				setErrors(1);
				logError(Const.getStackTracker(e));
				stopAll();
				setOutputDone();  // signal end to receiver(s)
				return false;				
			}
			if (sendToErrorRow) {
			   // Simply add this row to the error row
			   putError(getInputRowMeta(), outputRowData, 1, errorMessage, null, "LDAPINPUT001");
			}
		} 
		return true;
	}	
	
	private boolean dynamicSearch() throws KettleException {
	
		data.readRow=getRow();  
		// Get row from input rowset & set row busy!
		if (data.readRow==null) {
			if (log.isDetailed()) logDetailed(BaseMessages.getString(PKG, "LDAPInput.Log.FinishedProcessing"));
			setOutputDone();
			return false;
	    }
		if(first) {
            first = false;

        	if(meta.isDynamicSearch()) {
	        	if(Const.isEmpty(meta.getDynamicSearchFieldName())) {
	        		throw new KettleException(BaseMessages.getString(PKG, "LDAPInput.Error.DynamicSearchFieldMissing"));
	        	}
        	}
        	if(meta.isDynamicFilter()) {
        		if(Const.isEmpty(meta.getDynamicFilterFieldName())) {
            		throw new KettleException(BaseMessages.getString(PKG, "LDAPInput.Error.DynamicFilterFieldMissing"));
            	}
        	}
        	
            // Create the output row meta-data
            data.nrIncomingFields= getInputRowMeta().size();
            data.outputRowMeta = getInputRowMeta().clone();
            meta.getFields(data.outputRowMeta, getStepname(), null, null, this); // get the metadata populated
            
            // Create convert meta-data objects that will contain Date & Number formatters
            //

			data.convertRowMeta = data.outputRowMeta.clone();
			for (int i=0;i<data.convertRowMeta.size();i++) {
				data.convertRowMeta.getValueMeta(i).setType(ValueMetaInterface.TYPE_STRING);           
			}
			if(meta.isDynamicSearch()) {
				data.indexOfSearchBaseField =getInputRowMeta().indexOfValue(meta.getDynamicSearchFieldName());
				if (data.indexOfSearchBaseField<0) {
					// The field is unreachable !
					throw new KettleException(BaseMessages.getString(PKG, "LDAPInput.Exception.CouldnotFindField",meta.getDynamicSearchFieldName())); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
			if(meta.isDynamicFilter()) {
				data.indexOfFilterField =getInputRowMeta().indexOfValue(meta.getDynamicFilterFieldName());
				if (data.indexOfFilterField<0) {
					// The field is unreachable !
					throw new KettleException(BaseMessages.getString(PKG, "LDAPInput.Exception.CouldnotFindField",meta.getDynamicFilterFieldName())); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
		}// end if
		
		String searchBase=data.staticSearchBase;
		if(data.indexOfSearchBaseField>0) {
			// retrieve dynamic search base value
			searchBase=getInputRowMeta().getString(data.readRow, data.indexOfSearchBaseField);
		}
		String filter=data.staticFilter;
		if(data.indexOfFilterField>=0) {
{'fraction_non_alphanumeric': 0.14284687791909176, 'fraction_numerical': 0.006179492706761515, 'mean_word_length': 3.4044303797468354, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '527598', 'n_tokens_mistral': 4552, 'n_tokens_neox': 4027, 'n_words': 873}