001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdbc;
012:
013: import com.versant.core.metadata.FetchGroupField;
014: import com.versant.core.metadata.MDStatics;
015: import com.versant.core.metadata.FetchGroup;
016:
017: import java.util.List;
018: import java.util.ArrayList;
019:
020: /**
021: * This is a dataStructure that is used to remember join conditions.
022: * This is a composite structure where by a child knows its parent, but not
023: * the other way round.
024: */
025: public class JoinStructure implements Comparable {
026: public FetchGroupField fgField;
027: public JoinStructure parent;
028: public boolean refField;
029: public int level = 1;
030:
031: public FetchGroupField[] fgFields;
032: /**
033: * If this is the root of the joinStructTree
034: */
035: public boolean rootJoinStructure;
036: /**
037: * This is a collection of all the JoinStructure end points that is a
038: * collection.
039: */
040: public List colJoinStructs;
041: public FetchGroup fetchGroup;
042: private boolean finished;
043:
044: public JoinStructure(FetchGroup fg) {
045: rootJoinStructure = true;
046: colJoinStructs = new ArrayList();
047: this .fetchGroup = fg;
048: }
049:
050: public JoinStructure(JoinStructure parent, FetchGroupField field) {
051: this .fgField = field;
052: this .setParent(parent);
053: this .refField = (field.fmd.category == MDStatics.CATEGORY_REF);
054: if (!refField) {
055: addColJoinStruct(this );
056: }
057: }
058:
059: public boolean isRefField() {
060: return refField;
061: }
062:
063: /**
064: * Called to finalize the structure.
065: */
066: public void finish() {
067: if (finished)
068: return;
069: finished = true;
070: List structs = colJoinStructs;
071: if (structs == null)
072: return;
073: for (int i = 0; i < structs.size(); i++) {
074: JoinStructure js = (JoinStructure) structs.get(i);
075: js.finishImp();
076: }
077: }
078:
079: private void finishImp() {
080: fgFields = new FetchGroupField[level - 1];
081: appendTo(fgFields);
082: }
083:
084: private void appendTo(FetchGroupField[] fgFArray) {
085: if (rootJoinStructure)
086: return;
087: fgFArray[level - 2] = fgField;
088: parent.appendTo(fgFArray);
089: }
090:
091: private void addColJoinStruct(JoinStructure js) {
092: if (rootJoinStructure) {
093: colJoinStructs.add(js);
094: } else {
095: parent.addColJoinStruct(js);
096: }
097: }
098:
099: private void setParent(JoinStructure joinStructure) {
100: if (joinStructure != null) {
101: parent = joinStructure;
102: level = parent.level + 1;
103: }
104: }
105:
106: public int compareTo(Object o) {
107: JoinStructure other = (JoinStructure) o;
108: return level - other.level;
109: }
110:
111: public String toString() {
112: if (rootJoinStructure) {
113: return "JoinStructure2@" + System.identityHashCode(this )
114: + " for ROOT";
115: } else {
116: return "JoinStructure2@" + System.identityHashCode(this )
117: + " for " + fgField.fmd.name + " ref "
118: + isRefField();
119: }
120: }
121:
122: public void dump(String indent) {
123: System.out.println(indent + "this = " + this + " finished "
124: + finished);
125: if (colJoinStructs == null)
126: return;
127: for (int i = 0; i < colJoinStructs.size(); i++) {
128: JoinStructure joinStructure = (JoinStructure) colJoinStructs
129: .get(i);
130: joinStructure.dump(indent + " ");
131: }
132: }
133: }
|