/*
 *  Copyright (C) 2008  The Concord Consortium, Inc.,
 *  25 Love Lane, Concord, MA 01742
 *
 *  Web Site: http://www.concord.org
 *  Email: info@concord.org
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * END LICENSE */


package org.concord.otrunk.view;


import java.net.URL;
import java.util.HashMap;

import org.concord.framework.otrunk.OTObject;
import org.concord.framework.otrunk.OTObjectList;
import org.concord.framework.otrunk.OTrunk;
import org.concord.framework.otrunk.view.AbstractOTView;
import org.concord.framework.otrunk.view.OTXHTMLView;
import org.concord.otrunk.OTrunkImpl;
import org.concord.otrunk.xml.XMLDatabase;


public class OTMultiUserRootView extends AbstractOTView implements OTXHTMLView 
{
	private HashMap dbMap = new HashMap(); // key: url, value: database
	
	
	public String getXHTMLText(OTObject otObject) {
		System.out.println("OTMultiUserRootView.getXHTMLText()");
		
		OTrunk otrunk = (OTrunk) getViewService(OTrunk.class);
		OTrunkImpl otrunkImpl = (OTrunkImpl) otrunk;
		
	    OTMultiUserRoot root = (OTMultiUserRoot) otObject;
	    OTUserList userList = (OTUserList) root.getUserList();
	    OTObjectList userDatabases = userList.getUserDatabases();
	    
	    for (int i = 0; i < userDatabases.size(); i++) {
	    	OTUserDatabaseRef ref = (OTUserDatabaseRef) userDatabases.get(i);
	    	URL url = ref.getUrl();
	    	
	    	if (dbMap.containsKey(url)) {
	    		continue;
	    	}
	    	
	    	try {
	    		XMLDatabase db = new XMLDatabase(url);
	    		db.loadObjects();
	    		otrunkImpl.registerUserDataDatabase(db, null);
	    		dbMap.put(url, db);
	    	}
	    	catch (Exception e) {
	    		e.printStackTrace();
	    	}
	    }
	    OTObject reportTemplate = (OTObject) root.getReportTemplate();
	    return "<object refid=\"" + reportTemplate.otExternalId() + "\"/>";
    }

}

@@ -1203,24 +1203,84 @@
 ractOTView;%0A
+import org.concord.framework.otrunk.view.OTUserListService;%0A
 import org.c
@@ -1499,67 +1499,32 @@
 ate 
-HashMap dbMap = new HashMap(); // key: url, value: database
+boolean firstRun = true;
 %0A%09%0A%09
@@ -1595,16 +1595,23 @@
 rintln(%22
+ENTER: 
 OTMultiU
@@ -1641,17 +1641,370 @@
 t()%22);%0A%09
-%09
+    OTMultiUserRoot root = (OTMultiUserRoot) otObject;%0A%09%09if (firstRun) %7B //why is this method called twice?%0A%09%09%09loadUserDatabases(root);%0A%09%09%09firstRun = false;%0A%09%09%7D%0A%09    OTObject reportTemplate = root.getReportTemplate();%0A%09    return %22%3Cobject refid=%5C%22%22 + reportTemplate.otExternalId() + %22%5C%22/%3E%22;%0A    %7D%0A%09%0A%09private void loadUserDatabases(OTMultiUserRoot root) %7B
 %0A%09%09OTrun
@@ -2107,64 +2107,8 @@
 %0A%09%09%0A
-%09    OTMultiUserRoot root = (OTMultiUserRoot) otObject;%0A
 %09   
@@ -2283,11 +2283,11 @@
 (); 
-i
 ++
+i
 ) %7B%0A
@@ -2399,76 +2399,8 @@
   %09%0A
-%09    %09if (dbMap.containsKey(url)) %7B%0A%09    %09%09continue;%0A%09    %09%7D%0A%09    %09%0A
 %09   
@@ -2536,35 +2536,8 @@
 l);%0A
-%09    %09%09dbMap.put(url, db);%0A
 %09   
@@ -2616,153 +2616,8 @@
  %7D%0A%09
-    OTObject reportTemplate = (OTObject) root.getReportTemplate();%0A%09    return %22%3Cobject refid=%5C%22%22 + reportTemplate.otExternalId() + %22%5C%22/%3E%22;%0A    %7D%0A
+%7D
 %0A%7D%0A

Separated out loadUserDatabases() method.