All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.hub.HubDelegate Maven / Gradle / Ivy

There is a newer version: 3.7.10
Show newest version
/*  Copyright 1999 Vince Via [email protected]
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
package com.viaoa.hub;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import com.viaoa.object.*;
import com.viaoa.sync.OASync;
import com.viaoa.sync.OASyncDelegate;
import com.viaoa.util.*;

/**
 * Main delegate used for working with the Active Object for a Hub.
 * All methods that have an "_" prefix should not be called directly, as there is
 * a calling method that should be used, that performs additional functionality.
 * If a method does not have the "_" prefix and is accessible, then it is ok
 * to call it, but will most likely have a matching method name in the Hub class.
 * @author vincevia
 */
public class HubDelegate {

	public static final Boolean TRUE  = new Boolean(true);
	public static final Boolean FALSE = new Boolean(false);
	
	public static boolean getChanged(Hub thisHub, int iCascadeRule, OACascade cascade) {
	    if (cascade.wasCascaded(thisHub, true)) return false;
	
	    if (HubDataDelegate.getChanged(thisHub)) {
	        return true;
	    }
	    if (iCascadeRule == OAObject.CASCADE_NONE) {
	        return false;
	    }
	    
	    if (thisHub.isOAObject()) {
	        for (int i=0; ; i++) {
	            Object object = HubDataDelegate.getObjectAt(thisHub, i);
	            if (object == null) break;
	            if (object instanceof OAObject) {
	                OAObject obj = (OAObject) object;
	                if ( OAObjectDelegate.getChanged(obj, iCascadeRule, cascade) ) {
	                    return true;
	                }
	            }
	        }
	    }
	    return false;
	}
	
	

    
    /**
	    Verifies that the property in obj is unique for all objects in this Hub (not in all of system).
	    
Note: if OAObject and isLoading, then true is always returned.
Note: if property value is a null or a blank String, then it is not checked. @return true if object is valid, false if another object already uses same unique property value. */ public static boolean verifyUniqueProperty(Hub thisHub, Object object) { if (thisHub == null || object == null) return true; if (object instanceof OAObject) { if (OAThreadLocalDelegate.isLoading()) return true; } Object object2; Method m = null; try { m = thisHub.data.getUniquePropertyGetMethod(); if (m == null) { m = thisHub.datam.getUniquePropertyGetMethod(); if (m == null) return true; } object2 = m.invoke(object, null); if (object2 == null) return true; if (object2 instanceof String && ((String)object2).equals("")) return true; } catch (Exception e) { String s = m==null?"":m.getName(); throw new RuntimeException("Error invoking "+s, e); } for (int i=0; ;i++) { Object obj = thisHub.elementAt(i); if (obj == null) break; if (obj == object) continue; try { Object obj2 = m.invoke(obj, null); if (obj2 == null) continue; if (obj2 == object2 || obj2.equals(object2)) return false; } catch (Exception e) { String s = m==null?"":m.getName(); throw new RuntimeException("Error invoking "+s, e); } } return true; } protected static Object getRealObject(Hub hub, Object object) { if (object != null && !object.getClass().equals(hub.getObjectClass()) ) { Object objx = OAObjectCacheDelegate.get(hub.getObjectClass(), object); if (objx != null) return objx; object = HubDataDelegate.getObject(hub, object); // might not have loaded all data yet (fetchMore will be called) } return object; } protected static String getPropertyPathforClasses(Hub hub, Class[] classes) { if (classes == null) return null; Class c = hub.getObjectClass(); String path = null; int x = classes.length; for (int i=0; i Note: you can not reset the Object class if there are any objects in it. @param objClass Class for the object being stored */ public static void setObjectClass(Hub thisHub, Class objClass) { if (thisHub.data.objClass != null && !thisHub.data.objClass.equals(objClass) && !thisHub.data.objClass.equals(OAObject.class)) { if (HubDataDelegate.getCurrentSize(thisHub) > 0 || (thisHub.datau.getVecHubDetail() != null && thisHub.datau.getVecHubDetail().size() > 0) ) { throw new RuntimeException("cant change object class if objects are in hub"); } HubDataMaster dm = HubDetailDelegate.getDataMaster(thisHub); if (dm.getMasterHub() != null || thisHub.datam.getMasterObject() != null) { throw new RuntimeException("cant change object class if masterObject exists"); } if (thisHub.datau.getSharedHub() != null || HubShareDelegate.getSharedWeakHubSize(thisHub) > 0) { throw new RuntimeException("cant change object class since this is a shared hub."); } } // 20141111 removed since the select could be valid // HubSelectDelegate.cancelSelect(thisHub, true); thisHub.data.objClass = objClass; /* 20141111 not needed here if (objClass != null) { // find out if class is OAObject thisHub.data.setOAObjectFlag(OAObject.class.isAssignableFrom(objClass)); // thisHub.data.setObjectInfo(OAObjectInfoDelegate.getOAObjectInfo(objClass)); } else { thisHub.data.setObjectInfo(null); thisHub.data.setOAObjectFlag(false); } */ } /** If this hub has a master Hub or link Hub and that Hub does not have an active Object set, then this Hub is invalid. */ public static boolean isValid(final Hub hub) { HubDataMaster dm = HubDetailDelegate.getDataMaster(hub); if (dm.getMasterHub() != null && dm.getMasterObject() == null) { return false; } // 20181119 reworked to check other hubs for hubWithLink Hub h = HubLinkDelegate.getHubWithLink(hub, true); if (h != null) { h = h.datau.getLinkToHub(); if (h != null) { if (!HubDelegate.isValid(h)) { return false; } if (h.dataa.activeObject == null) { if (!h.datau.isAutoCreate()) return false; } } } if (hub.datau.getAddHub() != null) { return HubDelegate.isValid(hub.datau.getAddHub()); } return true; } /** created 20110904 * Find the hub that controls whether or not this hub is valid. * So that it can also be listened to, so that hub isValid can be recalculated * @return controlling hub, or param hub if there is no controlling hub */ public static Hub getControllingHub(Hub thisHub) { HubDataMaster dm = HubDetailDelegate.getDataMaster(thisHub); if (dm.getMasterHub() != null) { return dm.getMasterHub(); } // 20181119 find shared hub with link Hub hubWithLink = HubLinkDelegate.getHubWithLink(thisHub, true); if (hubWithLink != null && hubWithLink.datau.getLinkToHub() != null) { if (hubWithLink.datau.isAutoCreate()) { return getControllingHub(hubWithLink.datau.getLinkToHub()); } return hubWithLink.datau.getLinkToHub(); } if (thisHub.datau.getAddHub() != null) { return HubDelegate.getControllingHub(thisHub.datau.getAddHub()); } return thisHub; } /** Returns this hub, or one of its shared Hubs, that has an addHub established. */ public static Hub getAnyAddHub(final Hub hub) { if (hub.getAddHub() != null) return hub; // 20120716 OAFilter filter = new OAFilter() { @Override public boolean isUsed(Hub h) { return h.getAddHub() != null; } }; Hub[] hubs = HubShareDelegate.getAllSharedHubs(hub, filter); // was: Hub[] hubs = HubShareDelegate.getAllSharedHubs(hub); for (int i=0; i filter = new OAFilter() { @Override public boolean isUsed(Hub h) { return (h.dataa == thisHub.dataa && h.getLinkHub() != null); } }; Hub[] hubs = HubShareDelegate.getAllSharedHubs(thisHub, filter); //was: Hub[] hubs = HubShareDelegate.getAllSharedHubs(thisHub); for (int i=0; i 0) ) { bHasMethod = false; updateMany2ManyLinks(thisHub, dm); // update any link tables } } Object[] objs = HubDataDelegate.getRemovedObjects(thisHub); if (objs == null) return; for (int i=0; i= 0) { throw new IllegalArgumentException("Property "+propertyName+" can only be for a property in "+thisHub.getObjectClass().getName()); } thisHub.data.setUniquePropertyGetMethod(OAObjectInfoDelegate.getMethod(thisHub.getObjectClass(), "get"+propertyName)); if (thisHub.data.getUniquePropertyGetMethod() == null) { throw new IllegalArgumentException("Get Method for Property "+propertyName+" not found"); } if (thisHub.data.getUniquePropertyGetMethod().getParameterTypes().length > 0) { throw new IllegalArgumentException("Get Method for Property "+propertyName+" expects parameters"); } thisHub.data.setUniqueProperty(propertyName); } /** Used to update a property in each object to equal/store the position within this Hub. @param property is neme of property to update. @param startNumber is number to begin numbering at. Default is 0, which will match the Hub position. @param bKeepSeq, if false then seq numbers are not updated when an object is removed */ public static void setAutoSequence(Hub thisHub, String property, int startNumber, boolean bKeepSeq) { // 20091030 only set for server for detail hubs boolean bServerOnly = false; if (thisHub.getMasterObject() != null) { if (!HubCSDelegate.isServer(thisHub)) return; // only set up for server bServerOnly = true; } if (thisHub.data.getAutoSequence() != null) thisHub.data.getAutoSequence().close(); thisHub.cancelSort(); // 20090801 need to remove any sorters thisHub.data.setAutoSequence(new HubAutoSequence(thisHub, property, startNumber, bKeepSeq, bServerOnly)); } public static HubAutoSequence getAutoSequence(Hub thisHub) { return thisHub.data.getAutoSequence(); } public static void resequence(Hub thisHub) { if (thisHub.data.getAutoSequence() != null) { thisHub.data.getAutoSequence().resequence(); } } /** Makes sure that for each object in a hubMaster, there exists an object in this hub where property is equal to the hubMaster object. @param hubMaster hub with list of object @param property Property in this hubs objects that match object type in hubMaster */ public static void setAutoMatch(Hub thisHub, String property, Hub hubMaster, boolean bServerSideOnly) { if (thisHub.data.getAutoMatch() != null) thisHub.data.getAutoMatch().close(); if (hubMaster != null) { HubAutoMatch am = new HubAutoMatch(); thisHub.data.setAutoMatch(am); am.setServerSideOnly(bServerSideOnly); am.init(thisHub, property, hubMaster); } } public static HubAutoMatch getAutoMatch(Hub thisHub) { return thisHub.data.getAutoMatch(); } public static int getSize(Hub thisHub) { if (HubSelectDelegate.isMoreData(thisHub)) { if (!HubSelectDelegate.isCounted(thisHub)) { if (HubDataDelegate.getCurrentSize(thisHub) == 0) { HubSelectDelegate.fetchMore(thisHub); // see if this will load it, before calling count on the select if (!HubSelectDelegate.isMoreData(thisHub)) { return getSize(thisHub); } } } int x = HubSelectDelegate.getCount(thisHub); if (x > 0) return x; } return HubDataDelegate.getCurrentSize(thisHub); } protected static void setProperty(Hub thisHub, String name, Object obj) { if (name == null) return; name = name.toUpperCase(); if (thisHub.data.getHashProperty() == null) thisHub.data.setHashProperty(new Hashtable(7)); thisHub.data.getHashProperty().put(name, (obj==null)?OANullObject.instance:obj); } protected static Object getProperty(Hub thisHub, String name) { if (thisHub.data.getHashProperty() == null) return null; name = name.toUpperCase(); Object obj = thisHub.data.getHashProperty().get(name); if (obj instanceof OANullObject) obj = null; return obj; } protected static void removeProperty(Hub thisHub, String name) { if (thisHub.data.getHashProperty() != null) { name = name.toUpperCase(); thisHub.data.getHashProperty().remove(name); } } // 20141030 public static void setReferenceable(Hub hub, boolean bReferenceable) { if (hub == null) return; if (!OASyncDelegate.isServer(hub)) return; OAObjectInfo oi = OAObjectInfoDelegate.getOAObjectInfo(hub.getObjectClass()); if (!OAObjectInfoDelegate.isWeakReferenceable(oi)) return; boolean bSupportStorage = oi.getSupportsStorage(); Object master = HubDelegate.getMasterObject(hub); if (!(master instanceof OAObject)) return; OALinkInfo li = HubDetailDelegate.getLinkInfoFromDetailToMaster(hub); if (li == null) return; OALinkInfo liRev = li.getReverseLinkInfo(); if (liRev == null) return; if (liRev.getCacheSize() > 0) { if (bReferenceable || bSupportStorage) { boolean b = OAObjectPropertyDelegate.setPropertyWeakRef((OAObject) master, liRev.getName(), !bReferenceable, hub); if (!b) return; // already done, dont need to check/change parents } } if (bReferenceable) { // make parents referenceable OAObjectPropertyDelegate.setReferenceable((OAObject)master, bReferenceable); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy