package net.kanngard.domino.util;

import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import lotus.domino.Document;
import lotus.domino.Item;
import lotus.domino.NotesException;
import lotus.domino.View;
import lotus.domino.ViewEntry;
import lotus.domino.ViewEntryCollection;


/**
 * Please read the Gnu General Public License before use:
 * http://www.gnu.org/licenses/gpl.txt
 *
 * @author Johan Känngård, johan@kanngard.net
 */
public class DominoUtils {

	/**
	 * Loads properties from a Domino View.
	 *
	 * The supplied view must contain at least two columns.
	 *
	 * @param view the view to load the parameters from.
	 * @param keyColumn the number of the column to treat as the key column, starting with 0.
	 * @param valueColumn the number of the column to treat as the value column, starting with 0.
	 * @return a Properties object containing keys from the specified column
	 * in the View, and values from the specified column in the View.
	 * @throws NotesException if any Notes-related error occurs.
	 */
	public static Properties getProperties(View view, int keyColumn, int valueColumn) throws NotesException {

		if(view == null) {
			return null;
		}
		
		if(view.getColumnCount() < 2) {
			return null;
		}

		ViewEntryCollection col = view.getAllEntries();
		Properties p = new Properties();

		if(col==null) {
			return p;
		}
		
		if(col.getCount() <= 0) {
			return p;
		}
		
		Vector colValues = null;
		ViewEntry entry = col.getFirstEntry();
		
		while (entry!=null) {
			colValues = entry.getColumnValues();
			p.put((String)colValues.elementAt(keyColumn), (String)colValues.elementAt(valueColumn));
			entry = col.getNextEntry(entry);
		}

		col.recycle();
		col = null;

		return p;
	}
	
	/**
	 * Loads properties from a Domino View.
	 *
	 * The supplied view must contain at least two columns. The first column is
	 * treated as the "key" column, and the second as the "value" column.
	 * All values in the column are converted to Strings.
	 *
	 * @param view the view to load the parameters from.
	 * @return a Properties object containing keys from the first column
	 * in the View, and values from the second column in the View.
	 * @throws NotesException if any Notes-related error occurs.
	 */
	public static Properties getProperties(View view) throws NotesException {
		return DominoUtils.getProperties(view, 0, 1);
	}

	/**
	 * Creates a property object with ALL the fields in the specified document.
	 * All values are treated as Strings.
	 *
	 * @param doc the Notes document to use as "property sheet".
	 * @return a Properties object with field names as keys and field
	 * values as values.
	 */
	public static Properties getProperties(Document doc) throws NotesException {
		if(doc == null) {
			return null;
		}
		Properties p = new Properties();
		Vector items = doc.getItems();
		Item item = null;
		Enumeration enum = items.elements();
		while(enum.hasMoreElements()) {
			item = (Item)enum.nextElement();
			p.put(item.getName(), item.getValueString());
		}
		return p;
	}
}