package org.concord.otrunk.overlay;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import org.concord.framework.otrunk.OTID;
import org.concord.framework.otrunk.OTObject;
import org.concord.framework.otrunk.OTObjectService;
import org.concord.otrunk.OTObjectServiceImpl;
import org.concord.otrunk.user.OTUserObject;

public class OTUserOverlayManager
{
	HashMap overlayToObjectServiceMap = new HashMap();
	HashMap userToOverlayMap = new HashMap();
	Vector overlayDatabases = new Vector();
	
	public OTUserOverlayManager() {

	}
	
	public void add(OTOverlay otOverlay, OTObjectService objService, OTUserObject userObject) {
		overlayToObjectServiceMap.put(otOverlay, objService);
		userToOverlayMap.put(userObject, otOverlay);
		
		if (objService instanceof OTObjectServiceImpl) {
			overlayDatabases.add(((OTObjectServiceImpl) objService).getCreationDb());
		}
	}
	
	public OTObjectService getObjectService(OTOverlay overlay) {
		return (OTObjectService) overlayToObjectServiceMap.get(overlay);
	}
	
	public OTObjectService getObjectService(OTUserObject userObj) {
		return (OTObjectService) overlayToObjectServiceMap.get(userToOverlayMap.get(userObj));
	}
	
	public OTOverlay getOverlay(OTUserObject userObj) {
		return (OTOverlay) userToOverlayMap.get(userObj);
	}
	
	public OTOverlay getOverlay(OTObjectService objService) {
		if (overlayToObjectServiceMap.containsValue(objService)) {
    		for (Iterator overlayList = userToOverlayMap.keySet().iterator(); overlayList.hasNext();) {
    	        OTOverlay overlay = (OTOverlay) overlayList.next();
    	        if (userToOverlayMap.get(overlay) == objService) {
    	        	return overlay;
    	        }
            }
		}
		return null;
	}
	
	public OTUserObject getUserObject(OTOverlay overlay) {
		if (userToOverlayMap.containsValue(overlay)) {
    		for (Iterator userList = userToOverlayMap.keySet().iterator(); userList.hasNext();) {
    	        OTUserObject userObject = (OTUserObject) userList.next();
    	        if (userToOverlayMap.get(userObject) == overlay) {
    	        	return userObject;
    	        }
            }
		}
		return null;
	}
	
	public OTUserObject getUserObject(OTObjectService objService) {
		OTOverlay overlay = getOverlay(objService);
		return getUserObject(overlay);
	}
	
	public Vector getOverlayDatabases() {
		return this.overlayDatabases;
	}
	
	public OTObject getOTObject(OTUserObject userObject, OTID id) throws Exception {
		return getOTObject(getOverlay(userObject), id);
	}
	
	public OTObject getOTObject(OTOverlay overlay, OTID id) throws Exception {
		OTObjectService objService = getObjectService(overlay);
		return objService.getOTObject(id);
	}
}

@@ -2639,16 +2639,64 @@
 erlay);%0A
+%09%09if (objService == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A
 %09%09return

Handle a missing objectService gracefully when looking up otobjects.