// Path: src/java/com/idega/block/importer/data/ImportFile.java
// public interface ImportFile{
// 
//   public Collection getRecords();
//   public Object getNextRecord(); 
//   public ArrayList getValuesFromRecordString(String recordString);
//   public String getValueAtIndexFromRecordString(int index, String recordString);
//   public void setFile(File file);
//   public String getEmptyValueString();
//   public void close();
// /**
//  * Method setEmptyValueString. This will be the value returned if a column you want is empty in the import file.
//  * @param emptyValueString
//  */
//   public void setEmptyValueString(String emptyValueString);
//   public File getFile();
//   public String getEncoding();
//   //public Object getRecordAtIndex(int index);
//  // public boolean parse();
// }


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import org.sadun.util.polling.BasePollManager;
import org.sadun.util.polling.FileFoundEvent;
import org.sadun.util.polling.FileSetFoundEvent;
import com.idega.block.importer.data.ImportFile;
import com.idega.business.IBOLookup;
import com.idega.business.IBOLookupException;
import com.idega.business.IBOService;
import com.idega.idegaweb.IWMainApplication;
import com.idega.repository.data.RefactorClassRegistry;
package com.idega.block.importer.business;




/**
 * AutoImportPollManager handles the action when file(s) are found in folders for automatic imports
 * One AutoImportPollManager is created for each folder to be polled from.
 * It basically calls the specified importer class with the files found in the folder.
 * 
 * Copyright:    Copyright (c) 2004
 * Company:      idega software
 * @author Joakim@idega.is
 */
public class AutoImportPollManager extends BasePollManager {
	private String fileClass;
	private ImportFileHandler handler;

	public AutoImportPollManager(String importerClass, String fc) throws IBOLookupException, ClassNotFoundException {
		this.fileClass = fc;
		this.handler = getImportFileHandler(importerClass);
	}
	
	/**
	 * Implemented interface (callback)
	 * @see org.sadun.util.polling.BasePollManager
	 */
	public void fileFound(FileFoundEvent evt){
		File file = evt.getFile();
		processFile(file);
		
	}
	
	/**
	 * Implemented interface (callback)
	 * @see org.sadun.util.polling.BasePollManager
	 */
	public void fileSetFound(FileSetFoundEvent evt){
		File[] files = evt.getFiles();
		for(int i=0;i<files.length;i++){
			processFile(files[i]);
		}
	}

	/**
	 * Calls the import and creates a report file if needed for the 
	 * @param filePath
	 */
	private void processFile(File filePath) {
		try {
// Path: src/java/com/idega/block/importer/data/ImportFile.java
// public interface ImportFile{
// 
//   public Collection getRecords();
//   public Object getNextRecord(); 
//   public ArrayList getValuesFromRecordString(String recordString);
//   public String getValueAtIndexFromRecordString(int index, String recordString);
//   public void setFile(File file);
//   public String getEmptyValueString();
//   public void close();
// /**
//  * Method setEmptyValueString. This will be the value returned if a column you want is empty in the import file.
//  * @param emptyValueString
//  */
//   public void setEmptyValueString(String emptyValueString);
//   public File getFile();
//   public String getEncoding();
//   //public Object getRecordAtIndex(int index);
//  // public boolean parse();
// }


// Path: src/java/com/idega/block/importer/business/AutoImportPollManager.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import org.sadun.util.polling.BasePollManager;
import org.sadun.util.polling.FileFoundEvent;
import org.sadun.util.polling.FileSetFoundEvent;
import com.idega.block.importer.data.ImportFile;
import com.idega.business.IBOLookup;
import com.idega.business.IBOLookupException;
import com.idega.business.IBOService;
import com.idega.idegaweb.IWMainApplication;
import com.idega.repository.data.RefactorClassRegistry;

package com.idega.block.importer.business;




/**
 * AutoImportPollManager handles the action when file(s) are found in folders for automatic imports
 * One AutoImportPollManager is created for each folder to be polled from.
 * It basically calls the specified importer class with the files found in the folder.
 * 
 * Copyright:    Copyright (c) 2004
 * Company:      idega software
 * @author Joakim@idega.is
 */
public class AutoImportPollManager extends BasePollManager {
	private String fileClass;
	private ImportFileHandler handler;

	public AutoImportPollManager(String importerClass, String fc) throws IBOLookupException, ClassNotFoundException {
		this.fileClass = fc;
		this.handler = getImportFileHandler(importerClass);
	}
	
	/**
	 * Implemented interface (callback)
	 * @see org.sadun.util.polling.BasePollManager
	 */
	public void fileFound(FileFoundEvent evt){
		File file = evt.getFile();
		processFile(file);
		
	}
	
	/**
	 * Implemented interface (callback)
	 * @see org.sadun.util.polling.BasePollManager
	 */
	public void fileSetFound(FileSetFoundEvent evt){
		File[] files = evt.getFiles();
		for(int i=0;i<files.length;i++){
			processFile(files[i]);
		}
	}

	/**
	 * Calls the import and creates a report file if needed for the 
	 * @param filePath
	 */
	private void processFile(File filePath) {
		try {
			ImportFile file = (ImportFile) RefactorClassRegistry.forName(this.fileClass).newInstance();