/**
 * (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.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

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.SolrDocument;
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 List<SolrGUIDocument> documents = new ArrayList<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());
		for (SolrDocument document:getAllDocuments()) {
			documents.add(new SolrGUIDocument(document.getFieldNames().toString()));
		}
	}
	
	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 List<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);
	}

}
@@ -1,149 +1,4 @@
-/**%0A * (c) Copyright Mirasol Op'nWorks Inc. 2002, 2003. %0A * http://www.opnworks.com%0A * Created on Apr 2, 2003 by lgauthier@opnworks.com%0A * %0A */%0A%0A
 pack
@@ -93,35 +93,8 @@
 et;%0A
-import java.util.Iterator;%0A
 impo
@@ -688,16 +688,44 @@
 rver %7B%0A%0A
+%09private SolrServer server;%0A
 %09private
@@ -886,36 +886,8 @@
 %3E();
-%0A%09private SolrServer server;
 %0A%0A%09s
@@ -961,126 +961,8 @@
 %7D;%0A%0A
-%09/**%0A%09 * Initialize the table data.%0A%09 * Create COUNT documents and add them them to the collection of documents.%0A%09 */%0A
 %09pub
@@ -1151,14 +1151,23 @@
 ield
-Names(
+Value(%22item_id%22
 ).to
@@ -2020,33 +2020,29 @@
 ocument);%0A%09%09
-Iterator%3C
+for (
 ISolrGUIServ
@@ -2045,37 +2045,32 @@
 ServerViewer
-%3E iterator = 
+ viewer:
 changeListen
@@ -2076,93 +2076,21 @@
 ners
-.iterator();%0A%09%09while (iterator.hasNext())%0A%09%09%09((ISolrGUIServerViewer) iterator.next())
+) %7B%0A%09%09%09viewer
 .add
@@ -2101,32 +2101,36 @@
 ment(document);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09/**%0A%09 * @pa
@@ -2235,25 +2235,21 @@
 ent);%0A%09%09
-Iterator%3C
+for (
 ISolrGUI
@@ -2256,37 +2256,32 @@
 ServerViewer
-%3E iterator = 
+ viewer:
 changeListen
@@ -2287,93 +2287,21 @@
 ners
-.iterator();%0A%09%09while (iterator.hasNext())%0A%09%09%09((ISolrGUIServerViewer) iterator.next())
+) %7B%0A%09%09%09viewer
 .rem
@@ -2315,32 +2315,36 @@
 ment(document);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09/**%0A%09 * @pa
@@ -2424,17 +2424,13 @@
 %7B%0A%09%09
-Iterator%3C
+for (
 ISol
@@ -2449,21 +2449,16 @@
 ewer
-%3E iterator = 
+ viewer:
 chan
@@ -2472,93 +2472,21 @@
 ners
-.iterator();%0A%09%09while (iterator.hasNext())%0A%09%09%09((ISolrGUIServerViewer) iterator.next())
+) %7B%0A%09%09%09viewer
 .upd
@@ -2508,16 +2508,20 @@
 ument);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09/**

Use implicit iterators.