Package org.jpox

Source Code of org.jpox.FetchGroupImpl

/**********************************************************************
Copyright (c) 2007 Andy Jefferson and others. All rights reserved.
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.

Contributors:
    ...
**********************************************************************/
package org.jpox;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

import org.jpox.exceptions.JPOXUserException;
import org.jpox.util.ClassUtils;
import org.jpox.util.Localiser;

/**
* Group of fields for fetching, to be part of a FetchPlan.
* @version $Revision: 1.2 $
*/
public class FetchGroupImpl implements FetchGroup, Serializable
{
    /** Localisation utility for output messages */
    protected static final Localiser LOCALISER = Localiser.getInstance("org.jpox.Localisation",
        ObjectManagerFactoryImpl.class.getClassLoader());

    /** Name of the group. */
    private String name;

    /** The class that this group is for. */
    private Class cls;

    /** Whether the postLoad callback is to be called when this group is loaded. */
    private boolean postLoad = false;

    /** Names of the fields of the class that are part of this group. */
    private Collection fieldNames = new HashSet();

    /** FetchPlans listening to this group for changes. */
    private Collection planListeners = new HashSet();

    /**
     * Constructor.
     * @param name Name of the group
     * @param cls The class
     */
    public FetchGroupImpl(String name, Class cls)
    {
        this.name = name;
        this.cls = cls;
    }

    /**
     * Accessor for the group name.
     * @return Name of the group
     */
    public String getName()
    {
        return name;
    }

    /**
     * Accessor for the class that this group is for.
     * @return the class
     */
    public Class getFetchGroupClass()
    {
        return cls;
    }

    /**
     * Accessor for the class for this fetch group.
     * @return Name of the class for this group
     */
    public String getClassName()
    {
        return cls.getName();
    }

    /**
     * Mutator for whether the postLoad callback should be called on loading this fetch group.
     * @param postLoad Whether the postLoad callback should be called.
     */
    public void setPostLoad(boolean postLoad)
    {
        this.postLoad  = postLoad;
    }

    /**
     * Accessor for whether to call postLoad when this group is loaded.
     * @return Whether to call postLoad
     */
    public boolean getPostLoad()
    {
        return postLoad;
    }

    /**
     * Accessor for whether a particular field is in this group.
     * @param fieldName Name of the field
     * @return Whether it is present
     */
    public boolean hasField(String fieldName)
    {
        return fieldNames.contains(fieldName);
    }

    /**
     * Accessor for the names of the fields in this fetch group.
     * @return Names of the fields in the group
     */
    public String[] getFieldNames()
    {
        return (String[])fieldNames.toArray(new String[fieldNames.size()]);
    }

    /**
     * Method to add a field of the class to the fetch group.
     * @param fieldName Name of the field
     * @return This FetchGroup
     * @throws JPOXUserException if the field doesnt exist for this class
     */
    public FetchGroup add(String fieldName)
    {
        Field fld = ClassUtils.getFieldForClass(cls, fieldName);
        if (fld == null)
        {
            throw new JPOXUserException(LOCALISER.msg("006004", fieldName, cls.getName()));
        }
        this.fieldNames.add(fieldName);
        notifyListeners();
        return this;
    }

    /**
     * Method to remove a field of the class from the fetch group.
     * @param fieldName Name of the field
     * @return This FetchGroup
     */
    public FetchGroup remove(String fieldName)
    {
        this.fieldNames.remove(fieldName);
        notifyListeners();
        return this;
    }

    /**
     * Method to notify all FetchPlan listeners that this group has changed.
     */
    private void notifyListeners()
    {
        Iterator iter = planListeners.iterator();
        while (iter.hasNext())
        {
            ((FetchPlan)iter.next()).notifyFetchGroupChange(this);
        }
    }

    /**
     * Method to register a listener for changes to this FetchGroup.
     * @param plan The FetchPlan that is listening
     */
    public void registerListener(FetchPlan plan)
    {
        planListeners.add(plan);
    }

    /**
     * Method to deregister a listener for changes to this FetchGroup.
     * @param plan The FetchPlan that is no longer listening
     */
    public void deregisterListener(FetchPlan plan)
    {
        planListeners.remove(plan);
    }

    /**
     * Method to disconnect this fetch group from all listeners since the group is removed from use.
     */
    public void disconnectFromListeners()
    {
        Iterator iter = planListeners.iterator();
        while (iter.hasNext())
        {
            ((FetchPlan)iter.next()).notifyFetchGroupRemove(this);
        }
        planListeners.clear();
    }
}
TOP

Related Classes of org.jpox.FetchGroupImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.