<commit_before>/*
 *  Copyright (C) 2004  The Concord Consortium, Inc.,
 *  10 Concord Crossing, Concord, MA 01742
 *
 *  Web Site: http://www.concord.org
 *  Email: <EMAIL>
 *
 *  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 */

/*
 * Created on Mar 21, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.concord.otrunk;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import org.concord.framework.otrunk.OTChangeEvent;
import org.concord.framework.otrunk.OTChangeListener;
import org.concord.framework.otrunk.OTID;
import org.concord.framework.otrunk.OTObject;
import org.concord.framework.otrunk.OTObjectList;
import org.concord.framework.otrunk.OTObjectMap;
import org.concord.framework.otrunk.OTObjectService;
import org.concord.framework.otrunk.OTResourceList;
import org.concord.framework.otrunk.OTResourceMap;
import org.concord.otrunk.datamodel.BlobResource;
import org.concord.otrunk.datamodel.OTDataList;
import org.concord.otrunk.datamodel.OTDataMap;
import org.concord.otrunk.datamodel.OTDataObject;
import org.concord.otrunk.view.OTViewerHelper;

/**
 * @author scott
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class OTResourceSchemaHandler extends OTInvocationHandler
{
	public final static boolean traceListeners =
		Boolean.getBoolean(OTViewerHelper.TRACE_LISTENERS_PROP);
	
	Class schemaInterface = null;
    OTrunkImpl db;
    OTObjectService objectService;
    
    Vector changeListeners = new Vector();

    /**
     * This is for debugging purposes it contains a mapping from
     * the weakreference object to the toString of the listener it
     * referenced.  This way when the listener is gc'd we can printout
     * its "label" (toString value).
     */
    Map changeListenerLabels;
    
    /**
     * This can be used by a user of an object to turn off the listening
     * 
     */
    protected boolean doNotifyListeners = true;
    
    /**
     * An internal variable to speed up skipping of the listener notification
     */
    protected boolean hasListeners = false;
    protected OTChangeEvent changeEvent;
    protected OTObject changeEventSource;
    
    /**
     * @param dataObject
     * @param db
     */
    public OTResourceSchemaHandler(OTDataObject dataObject, OTrunkImpl db, 
            OTObjectService objectService, Class schemaInterface)
    {
        super(dataObject);
        this.schemaInterface = schemaInterface; 
        this.db = db;
        this.objectService = objectService;
        // TODO Auto-generated constructor stub
    }

    public void setEventSource(OTObject src)
    {
    	changeEventSource = src;
    }
    
	public Object handleGet(String resourceName, Class returnType, Class proxyClass)
	throws Exception
	{
	    // Handle the globalId specially
	    if(resourceName.equals("globalId")) {
	        return dataObject.getGlobalId();
	    }
	    
        if(resourceName.equals("oTObjectService")) {
            return objectService;
        }
        
	    Object resourceValue = dataObject.getResource(resourceName);
	    
	    // we can't rely on the returnType here because it could be an
	    // interface that isn't in the ot package
	    if(resourceValue instanceof OTID){
	        OTObject object;
	        try {
	            if(resourceValue == null) {
	                return null;
	            }
	            OTID objId = (OTID)resourceValue;
	            
	            object = (OTObject)objectService.getOTObject(objId);
	            
	            return object;
	        } catch (Exception e)
	        {
	            e.printStackTrace();
	        }		
	        
	        return null;
	        
	    } else if(OTResourceMap.class.isAssignableFrom(returnType)) {
	        try {					
	            OTDataMap map = (OTDataMap)dataObject.getResourceCollection(
	                    resourceName, OTDataMap.class);
	            return new OTResourceMapImpl(resourceName, map, this);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        
	        return null;<commit_msg>added error checking for mis matched object references inside of of properties.<commit_after>/*
 *  Copyright (C) 2004  The Concord Consortium, Inc.,
 *  10 Concord Crossing, Concord, MA 01742
 *
 *  Web Site: http://www.concord.org
 *  Email: <EMAIL>
 *
 *  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 */

/*
 * Created on Mar 21, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.concord.otrunk;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import org.concord.framework.otrunk.OTChangeEvent;
import org.concord.framework.otrunk.OTChangeListener;
import org.concord.framework.otrunk.OTID;
import org.concord.framework.otrunk.OTObject;
import org.concord.framework.otrunk.OTObjectList;
import org.concord.framework.otrunk.OTObjectMap;
import org.concord.framework.otrunk.OTObjectService;
import org.concord.framework.otrunk.OTResourceList;
import org.concord.framework.otrunk.OTResourceMap;
import org.concord.otrunk.datamodel.BlobResource;
import org.concord.otrunk.datamodel.OTDataList;
import org.concord.otrunk.datamodel.OTDataMap;
import org.concord.otrunk.datamodel.OTDataObject;
import org.concord.otrunk.view.OTViewerHelper;

/**
 * @author scott
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class OTResourceSchemaHandler extends OTInvocationHandler
{
	public final static boolean traceListeners =
		Boolean.getBoolean(OTViewerHelper.TRACE_LISTENERS_PROP);
	
	Class schemaInterface = null;
    OTrunkImpl db;
    OTObjectService objectService;
    
    Vector changeListeners = new Vector();

    /**
     * This is for debugging purposes it contains a mapping from
     * the weakreference object to the toString of the listener it
     * referenced.  This way when the listener is gc'd we can printout
     * its "label" (toString value).
     */
    Map changeListenerLabels;
    
    /**
     * This can be used by a user of an object to turn off the listening
     * 
     */
    protected boolean doNotifyListeners = true;
    
    /**
     * An internal variable to speed up skipping of the listener notification
     */
    protected boolean hasListeners = false;
    protected OTChangeEvent changeEvent;
    protected OTObject changeEventSource;
    
    /**
     * @param dataObject
     * @param db
     */
    public OTResourceSchemaHandler(OTDataObject dataObject, OTrunkImpl db, 
            OTObjectService objectService, Class schemaInterface)
    {
        super(dataObject);
        this.schemaInterface = schemaInterface; 
        this.db = db;
        this.objectService = objectService;
        // TODO Auto-generated constructor stub
    }

    public void setEventSource(OTObject src)
    {
    	changeEventSource = src;
    }
    
	public Object handleGet(String resourceName, Class returnType, Class proxyClass)
	throws Exception
	{
	    // Handle the globalId specially
	    if(resourceName.equals("globalId")) {
	        return dataObject.getGlobalId();
	    }
	    
        if(resourceName.equals("oTObjectService")) {
            return objectService;
        }
        
	    Object resourceValue = dataObject.getResource(resourceName);
	    
	    // we can't rely on the returnType here because it could be an
	    // interface that isn't in the ot package
	    if(resourceValue instanceof OTID){
	        OTObject object;
	        try {
	            if(resourceValue == null) {
	                return null;
	            }
	            OTID objId = (OTID)resourceValue;
	            
	            object = (OTObject)objectService.getOTObject(objId);
	            
	            if(object != null){
	            	if(!returnType.isAssignableFrom(object.getClass())){
	            		System.err.println("Error: Type Mismatch");
	            		System.err.println("  value: " + object);
	            		System.err.println("  parentObject: " + 
	            				dataObject.getResource(OTrunkImpl.RES_CLASS_NAME));
	            		System.err.println("  resourceName: " + resourceName);
	        	        System.err.println("  expected type is: " + returnType);
	            		return null;
	            	}
	            }
	            
	            return object;
	        } catch (Exception e)
	        {
	            e.printStackTrace();
	        }		
	        
	        return null;
	        
	    } else if(OTResourceMap.class.isAssignableFrom(returnType)) {
	        try {					
	            OTDataMap map = (OTDataMap)dataObject.getResourceCollection(
	                    resourceName, OTDataMap.class);
	            return new OTResourceMapImpl(resourceName, map, this);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        
	        return null;
{'fraction_non_alphanumeric': 0.07802740747646127, 'fraction_numerical': 0.006152698797427053, 'mean_word_length': 2.6012084592145017, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4040591', 'n_tokens_mistral': 3123, 'n_tokens_neox': 2911, 'n_words': 908}