001: /**********************************************************************
002: Copyright (c) 2007 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox;
018:
019: import java.io.Serializable;
020: import java.lang.reflect.Field;
021: import java.util.Collection;
022: import java.util.HashSet;
023: import java.util.Iterator;
024:
025: import org.jpox.exceptions.JPOXUserException;
026: import org.jpox.util.ClassUtils;
027: import org.jpox.util.Localiser;
028:
029: /**
030: * Group of fields for fetching, to be part of a FetchPlan.
031: * @version $Revision: 1.2 $
032: */
033: public class FetchGroupImpl implements FetchGroup, Serializable {
034: /** Localisation utility for output messages */
035: protected static final Localiser LOCALISER = Localiser
036: .getInstance("org.jpox.Localisation");
037:
038: /** Name of the group. */
039: private String name;
040:
041: /** The class that this group is for. */
042: private Class cls;
043:
044: /** Whether the postLoad callback is to be called when this group is loaded. */
045: private boolean postLoad = false;
046:
047: /** Names of the fields of the class that are part of this group. */
048: private Collection fieldNames = new HashSet();
049:
050: /** FetchPlans listening to this group for changes. */
051: private Collection planListeners = new HashSet();
052:
053: /**
054: * Constructor.
055: * @param name Name of the group
056: * @param cls The class
057: */
058: public FetchGroupImpl(String name, Class cls) {
059: this .name = name;
060: this .cls = cls;
061: }
062:
063: /**
064: * Accessor for the group name.
065: * @return Name of the group
066: */
067: public String getName() {
068: return name;
069: }
070:
071: /**
072: * Accessor for the class that this group is for.
073: * @return the class
074: */
075: public Class getFetchGroupClass() {
076: return cls;
077: }
078:
079: /**
080: * Accessor for the class for this fetch group.
081: * @return Name of the class for this group
082: */
083: public String getClassName() {
084: return cls.getName();
085: }
086:
087: /**
088: * Mutator for whether the postLoad callback should be called on loading this fetch group.
089: * @param postLoad Whether the postLoad callback should be called.
090: */
091: public void setPostLoad(boolean postLoad) {
092: this .postLoad = postLoad;
093: }
094:
095: /**
096: * Accessor for whether to call postLoad when this group is loaded.
097: * @return Whether to call postLoad
098: */
099: public boolean getPostLoad() {
100: return postLoad;
101: }
102:
103: /**
104: * Accessor for whether a particular field is in this group.
105: * @param fieldName Name of the field
106: * @return Whether it is present
107: */
108: public boolean hasField(String fieldName) {
109: return fieldNames.contains(fieldName);
110: }
111:
112: /**
113: * Accessor for the names of the fields in this fetch group.
114: * @return Names of the fields in the group
115: */
116: public String[] getFieldNames() {
117: return (String[]) fieldNames.toArray(new String[fieldNames
118: .size()]);
119: }
120:
121: /**
122: * Method to add a field of the class to the fetch group.
123: * @param fieldName Name of the field
124: * @return This FetchGroup
125: * @throws JPOXUserException if the field doesnt exist for this class
126: */
127: public FetchGroup add(String fieldName) {
128: Field fld = ClassUtils.getFieldForClass(cls, fieldName);
129: if (fld == null) {
130: throw new JPOXUserException(LOCALISER.msg("006004",
131: fieldName, cls.getName()));
132: }
133: this .fieldNames.add(fieldName);
134: notifyListeners();
135: return this ;
136: }
137:
138: /**
139: * Method to remove a field of the class from the fetch group.
140: * @param fieldName Name of the field
141: * @return This FetchGroup
142: */
143: public FetchGroup remove(String fieldName) {
144: this .fieldNames.remove(fieldName);
145: notifyListeners();
146: return this ;
147: }
148:
149: /**
150: * Method to notify all FetchPlan listeners that this group has changed.
151: */
152: private void notifyListeners() {
153: Iterator iter = planListeners.iterator();
154: while (iter.hasNext()) {
155: ((FetchPlan) iter.next()).notifyFetchGroupChange(this );
156: }
157: }
158:
159: /**
160: * Method to register a listener for changes to this FetchGroup.
161: * @param plan The FetchPlan that is listening
162: */
163: public void registerListener(FetchPlan plan) {
164: planListeners.add(plan);
165: }
166:
167: /**
168: * Method to deregister a listener for changes to this FetchGroup.
169: * @param plan The FetchPlan that is no longer listening
170: */
171: public void deregisterListener(FetchPlan plan) {
172: planListeners.remove(plan);
173: }
174:
175: /**
176: * Method to disconnect this fetch group from all listeners since the group is removed from use.
177: */
178: public void disconnectFromListeners() {
179: Iterator iter = planListeners.iterator();
180: while (iter.hasNext()) {
181: ((FetchPlan) iter.next()).notifyFetchGroupRemove(this);
182: }
183: planListeners.clear();
184: }
185: }
|