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

com.viaoa.hub.HubAutoMatch 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 com.viaoa.*;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.lang.reflect.*;

import com.viaoa.object.*;
import com.viaoa.remote.multiplexer.OARemoteThreadDelegate;
import com.viaoa.util.*;

/** 
    Makes sure that for each object in a master hub, there exists an object with
    a reference to it in a second hub.
    @see Hub#setAutoMatch
*/
public class HubAutoMatch extends HubListenerAdapter implements java.io.Serializable {
    static final long serialVersionUID = 1L;

    protected Hub hub, hubMaster;
    protected String property;
    protected boolean bManuallyCalled;
    private boolean bServerSideOnly;

    protected transient Method getMethod, setMethod;


    /**
       Create new HubAutoMatch that will automatically create objects in a Hub with references that 
       match the objects in a master hub.
       ex: new HubAutoMatch(hub, itemMain.getItemOptionTypes(), "itemOptionType")
       @param hubMaster hub that has all objects to use
       @param property property in hub that has same type as objects in hubMaster.
       @param bManuallyCalled set to true if the update method will be manually called.  This is used in cases where the hubMaster 
       could be generating events that should not affect the matching.  For example, if the hubMaster is controlled by a HubMerger and
       objects are added/removed.  
    */
    public HubAutoMatch(Hub hub, String property, Hub hubMaster, boolean bManuallyCalled) {
        if (hub == null) throw new IllegalArgumentException("hub can not be null");
        if (hubMaster == null) throw new IllegalArgumentException("hubMaster can not be null");

        this.hub = hub;
        this.hubMaster = hubMaster;
        this.bManuallyCalled = bManuallyCalled;
        if (!bManuallyCalled) {
            hubMaster.addHubListener(this);
        }
        setProperty(property);
    }

    public HubAutoMatch(Hub hub, String property, Hub hubMaster) {
        this(hub, property, hubMaster, false);
    }

    public HubAutoMatch() {
    }
    
    public void init(Hub hub, String property, Hub hubMaster) {
        if (hub == null) throw new IllegalArgumentException("hub can not be null");
        if (hubMaster == null) throw new IllegalArgumentException("hubMaster can not be null");

        this.hub = hub;
        this.hubMaster = hubMaster;
        hubMaster.addHubListener(this);
        setProperty(property);
    }
    
    
    
    /**
     * This needs to be set to true if it is only created on the server, but client applications will be
     * using the same Hub that is filtered. This is so that changes on the hub will be published to the
     * clients, even if initiated on an OAClientThread.
     */
    public void setServerSideOnly(boolean b) {
        bServerSideOnly = b;
    }
    
    /**
        Closes HubAutoMatch.
    */
    public void close() {
        hubMaster.removeHubListener(this);
    }
    protected void finalize() throws Throwable {
        close();
        super.finalize();
    }
    protected void setProperty(String property) {
        this.property = property;
        Class c = null;
        if (property == null || property.length() == 0) {
            c = hub.getObjectClass();
            if ( !hubMaster.getObjectClass().equals(c) ) {
                // find property to use
                OAObjectInfo oi = OAObjectInfoDelegate.getOAObjectInfo(c);
                List al = oi.getLinkInfos();
                for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy