/**
 * (c) Copyright Mirasol Op'nWorks Inc. 2002, 2003. 
 * http://www.opnworks.com
 * Created on Apr 2, 2003 by lgauthier@opnworks.com
 * 
 */

package tableviewer;
import java.net.URL;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;

/**
 * Class that plays the role of the domain model in the TableViewerExample
 * In real life, this class would access a persistent store of some kind.
 * 
 */
public class SolrGUIServer {

	private Vector<SolrGUIDocument> documents = new Vector<SolrGUIDocument>();
	private Set<ISolrGUIServerViewer> changeListeners = new HashSet<ISolrGUIServerViewer>();
	private SolrServer server;

	static final String[] OWNERS_ARRAY = { "?", "Nancy", "Larry", "Joe" };

	/**
	 * Initialize the table data.
	 * Create COUNT documents and add them them to the collection of documents.
	 */
	public SolrGUIServer(URL url) {
		server = new HttpSolrServer(url.toExternalForm());
		
		
			SolrGUIDocument document = new SolrGUIDocument("Document");
			document.setOwner(OWNERS_ARRAY[0]);
			documents.add(document);
		
	}
	
	private SolrDocumentList getAllDocuments() {
		SolrQuery query = new SolrQuery();
		query.set("q", "*:*");
		return getDocumentList(query);
	}
	
	private SolrDocumentList getDocumentList(SolrQuery query) {
		QueryResponse response;
		try {
			response = server.query(query);
		} catch (SolrServerException e) {
			// TODO log error
			return new SolrDocumentList();
		}
		return response.getResults();
	}

	/**
	 * Return the array of owners   
	 */
	public String[] getOwners() {
		return OWNERS_ARRAY;
	}
	
	/**
	 * Return the collection of documents
	 */
	public Vector<SolrGUIDocument> getDocuments() {
		return documents;
	}
	
	/**
	 * Add a new document to the collection of documents
	 */
	public void addDocument() {
		SolrGUIDocument document = new SolrGUIDocument("New document");
		documents.add(documents.size(), document);
		Iterator<ISolrGUIServerViewer> iterator = changeListeners.iterator();
		while (iterator.hasNext())
			((ISolrGUIServerViewer) iterator.next()).addDocument(document);
	}

	/**
	 * @param document
	 */
	public void removeDocument(SolrGUIDocument document) {
		documents.remove(document);
		Iterator<ISolrGUIServerViewer> iterator = changeListeners.iterator();
		while (iterator.hasNext())
			((ISolrGUIServerViewer) iterator.next()).removeDocument(document);
	}

	/**
	 * @param document
	 */
	public void documentChanged(SolrGUIDocument document) {
		Iterator<ISolrGUIServerViewer> iterator = changeListeners.iterator();
		while (iterator.hasNext())
			((ISolrGUIServerViewer) iterator.next()).updateDocument(document);
	}

	/**
	 * @param viewer
	 */
	public void removeChangeListener(ISolrGUIServerViewer viewer) {
		changeListeners.remove(viewer);
	}

	/**
	 * @param viewer
	 */
	public void addChangeListener(ISolrGUIServerViewer viewer) {
		changeListeners.add(viewer);
	}

}
@@ -180,16 +180,44 @@
 et.URL;%0A
+import java.util.ArrayList;%0A
 import j
@@ -278,18 +278,19 @@
 va.util.
-Se
+Lis
 t;%0Aimpor
@@ -301,22 +301,19 @@
 va.util.
-Vector
+Set
 ;%0A%0Aimpor
@@ -576,16 +576,60 @@
 sponse;%0A
+import org.apache.solr.common.SolrDocument;%0A
 import o
@@ -869,22 +869,20 @@
 private 
-Vector
+List
 %3CSolrGUI
@@ -907,22 +907,25 @@
 s = new 
-Vector
+ArrayList
 %3CSolrGUI
@@ -1339,22 +1339,17 @@
 ;%0A%09%09
-%0A%09%09%0A%09%09%09
+for (
 Solr
-GUI
 Docu
@@ -1365,99 +1365,66 @@
 ment
- = new SolrGUIDocument(%22Document%22);%0A%09%09%09document.setOwner(OWNERS_ARRAY%5B0%5D);%0A%09%09%09d
+:getAllDocuments()) %7B%0A%09%09%09documents.add(new SolrGUID
 ocument
-s.add
 (doc
@@ -1428,21 +1428,50 @@
 document
+.getFieldNames().toString())
 );%0A%09%09
+%7D
 %0A%09%7D%0A%09%0A%09p
@@ -2039,14 +2039,12 @@
 lic 
-Vector
+List
 %3CSol

Replace Vector by ArrayList.