001: /**********************************************************************
002: Copyright (c) 2004 Erik Bengtson 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:
016: Contributors:
017: 2004 Andy Jefferson - changed to extend MetaData
018: 2004 Andy Jefferson - added toString()
019: ...
020: **********************************************************************/package org.jpox.metadata;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: /**
026: * A fetch group defines a particular loaded state for an object graph.
027: * It specifies fields/properties to be loaded for all of the instances in the graph when
028: * this fetch group is active.
029: *
030: * @since 1.1
031: * @version $Revision: 1.12 $
032: */
033: public class FetchGroupMetaData extends MetaData {
034: /**
035: * The post-load attribute on the fetch-group element indicates whether the
036: * jdoPost-Load callback will be made when the fetch group is loaded. It
037: * defaults to false, for all fetch groups except the default fetch group,
038: * on which it defaults to true.
039: */
040: Boolean postLoad;
041:
042: /**
043: * The name attribute on a field element contained within a fetch-group
044: * element is the name of field in the enclosing class or a dot-separated
045: * expression identifying a field reachable from the class by navigating a
046: * reference, collection or map. For maps of persistencecapable classes
047: * "#key" or "#value" may be appended to the name of the map field to
048: * navigate the key or value respectively (e.g. to include a field of the
049: * key class or value class in the fetch group).
050: *
051: * For collection and arrays of persistence-capable classes, "#element" may
052: * be appended to the name of the field to navigate the element. This is
053: * optional; if omitted for collections and arrays, #element is assumed.
054: */
055: final String name;
056:
057: /**
058: * A contained fetch-group element indicates that the named group is to be
059: * included in the group being defined. Nested fetch group elements are
060: * limited to only the name attribute.
061: */
062: protected FetchGroupMetaData[] fetchGroupMetaData;
063:
064: /** members declared to be in this fetch group. */
065: protected AbstractMemberMetaData[] memberMetaData;
066:
067: // -------------------------------------------------------------------------
068: // Fields below here are used in the parsing process, up to the call to
069: // initialise(). Thereafter they are typically cleared out and shouldn't be used.
070:
071: /**
072: * A contained fetch-group element indicates that the named group is to be
073: * included in the group being defined. Nested fetch group elements are
074: * limited to only the name attribute.
075: */
076: protected List fetchGroups = new ArrayList();
077:
078: /** members (fields/properties) declared to be in this fetch group. */
079: protected List members = new ArrayList();
080:
081: /**
082: * Constructor.
083: * @param parent The parent MetaData
084: * @param postLoad Whether to use at post load
085: * @param name Name of fetch group
086: */
087: public FetchGroupMetaData(MetaData parent, String postLoad,
088: String name) {
089: super (parent);
090:
091: // "postLoad"
092: if (postLoad != null) {
093: if (postLoad.equalsIgnoreCase("true")) {
094: this .postLoad = Boolean.TRUE;
095: } else if (postLoad.equalsIgnoreCase("false")) {
096: this .postLoad = Boolean.FALSE;
097: }
098: }
099: if (this .postLoad == null) {
100: this .postLoad = Boolean.FALSE;
101: }
102:
103: // "name"
104: this .name = name;
105: }
106:
107: /**
108: * Initialisation method. This should be called AFTER using the populate
109: * method if you are going to use populate. It creates the internal
110: * convenience arrays etc needed for normal operation.
111: */
112: public void initialise() {
113: if (members.isEmpty()) {
114: memberMetaData = new AbstractMemberMetaData[0];
115: } else {
116: memberMetaData = new AbstractMemberMetaData[members.size()];
117: for (int i = 0; i < memberMetaData.length; i++) {
118: memberMetaData[i] = (AbstractMemberMetaData) members
119: .get(i);
120: }
121: }
122:
123: if (fetchGroups.isEmpty()) {
124: fetchGroupMetaData = new FetchGroupMetaData[0];
125: } else {
126: fetchGroupMetaData = new FetchGroupMetaData[fetchGroups
127: .size()];
128: for (int i = 0; i < fetchGroupMetaData.length; i++) {
129: fetchGroupMetaData[i] = (FetchGroupMetaData) fetchGroups
130: .get(i);
131: }
132: }
133: for (int i = 0; i < fetchGroupMetaData.length; i++) {
134: fetchGroupMetaData[i].initialise();
135: }
136:
137: super .initialise();
138:
139: // Clear out parsing data
140: fetchGroups.clear();
141: fetchGroups = null;
142: members.clear();
143: members = null;
144: }
145:
146: /**
147: * Accessor for name
148: * @return Returns the name.
149: */
150: public final String getName() {
151: return name;
152: }
153:
154: /**
155: * Accessor for postLoad
156: * @return Returns the postLoad.
157: */
158: public final Boolean getPostLoad() {
159: return postLoad;
160: }
161:
162: /**
163: * Accessor for fetchGroupMetaData
164: * @return Returns the fetchGroupMetaData.
165: */
166: public final FetchGroupMetaData[] getFetchGroupMetaData() {
167: return fetchGroupMetaData;
168: }
169:
170: /**
171: * Accessor for metadata for the members of this group.
172: * @return Returns the metadata for members
173: */
174: public final AbstractMemberMetaData[] getMemberMetaData() {
175: return memberMetaData;
176: }
177:
178: // ------------------------------ Mutators ---------------------------------
179:
180: /**
181: * Add a new FetchGroupMetaData
182: * @param fgmd the fetch group
183: */
184: public void addFetchGroup(FetchGroupMetaData fgmd) {
185: fetchGroups.add(fgmd);
186: }
187:
188: /**
189: * Add a new field/property.
190: * @param mmd the field/property metadata
191: */
192: public void addMember(AbstractMemberMetaData mmd) {
193: members.add(mmd);
194: }
195:
196: // ----------------------------- Utilities ------------------------------------
197:
198: /**
199: * Returns a string representation of the object.
200: * This can be used as part of a facility to output a MetaData file.
201: * @param prefix prefix string
202: * @param indent indent string
203: * @return a string representation of the object.
204: */
205: public String toString(String prefix, String indent) {
206: StringBuffer sb = new StringBuffer();
207: sb.append(prefix)
208: .append("<fetch-group name=\"" + name + "\"\n");
209:
210: // Add fetch-groups
211: if (fetchGroupMetaData != null) {
212: for (int i = 0; i < fetchGroupMetaData.length; i++) {
213: sb.append(fetchGroupMetaData[i].toString(prefix
214: + indent, indent));
215: }
216: }
217:
218: // Add fields
219: if (memberMetaData != null) {
220: for (int i = 0; i < memberMetaData.length; i++) {
221: sb.append(memberMetaData[i].toString(prefix + indent,
222: indent));
223: }
224: }
225:
226: sb.append(prefix + "</fetch-group>\n");
227: return sb.toString();
228: }
229: }
|