KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > fetch > FopGetState


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.fetch;
13
14 import com.versant.core.metadata.FetchGroup;
15 import com.versant.core.metadata.ClassMetaData;
16 import com.versant.core.jdbc.sql.exp.SelectExp;
17 import com.versant.core.jdbc.sql.exp.SqlExp;
18 import com.versant.core.common.State;
19 import com.versant.core.common.OID;
20
21 import java.sql.SQLException JavaDoc;
22
23 /**
24  * A fetch of a complete State (Entity) for an existing OID.
25  */

26 public class FopGetState extends FetchOp {
27
28     private final FetchOpData src;
29     private final FetchGroup fg;
30     private final boolean includeSubClasses;
31     private final Data data;
32
33     private int firstColIndex; // the index of our first select list col
34

35     /**
36      * This gets our state from fetchData and delegates to our src for
37      * the OID and ResultSet.
38      */

39     public class Data extends FetchOpDataProxy {
40
41         public Data(FetchOpData src) {
42             super(src);
43         }
44
45         public void setState(FetchResultImp fetchResult, State state) {
46             fetchResult.setData(FopGetState.this, state);
47         }
48
49         public State getState(FetchResultImp fetchResult) {
50             return (State)fetchResult.getData(FopGetState.this);
51         }
52
53         public String JavaDoc getDescription() {
54             return " [" + getIndex() + "]";
55         }
56     }
57
58     /**
59      * Create a State and populate it according to fg. If includeSubclasses is
60      * true then fields for all possible subclasses are fetched. The src must
61      * provide the OID of the instance being fetched and the ResultSet.
62      */

63     public FopGetState(FetchSpec spec, FetchOpData src, FetchGroup fg,
64             boolean includeSubClasses) {
65         super(spec);
66         this.src = src;
67         this.fg = fg;
68         this.includeSubClasses = includeSubClasses
69                 && fg.classMetaData.pcSubclasses != null;
70         data = new Data(src);
71     }
72
73     public FetchOpData getOutputData() {
74         return data;
75     }
76
77     /**
78      * Add in whatever columns we need to have in the select list and return
79      * the last SqlExp we added.
80      *
81      * - may add new FetchOp's for superclass fields, subclass fields,
82      * prefetched references, collection fields and so on to the plan
83      */

84     public SqlExp init(SelectExp root, int firstColIndex) {
85         this.firstColIndex = firstColIndex;
86         ClassMetaData cmd = fg.classMetaData;
87         if (cmd.isInHeirachy()) {
88             // todo put in columns or create op to figure out the actual class
89
throw notImplemented();
90         }
91         // create ops to fetch all super fetch groups
92
for (FetchGroup g = fg; g != null; g = g.superFetchGroup) {
93             processFetchGroup(g);
94         }
95         // and recusively groups for all possible subclasses (if needed)
96
if (includeSubClasses) {
97             processSubFetchGroups(fg.subFetchGroups);
98         }
99         return null;
100     }
101
102     private void processFetchGroup(FetchGroup g) {
103         spec.addFetchOp(new FopGetFetchGroup(spec, data, g), false);
104     }
105
106     private void processSubFetchGroups(FetchGroup[] subs) {
107         if (subs != null) {
108             int n = subs.length;
109             for (int i = 0; i < n; i++) {
110                 processFetchGroup(subs[i]);
111             }
112             for (int i = 0; i < n; i++) {
113                 processSubFetchGroups(subs[i].subFetchGroups);
114             }
115         }
116     }
117
118     public void fetch(FetchResultImp fetchResult) throws SQLException JavaDoc {
119         OID oid = src.getOID(fetchResult);
120         if (oid == null) {
121             return; // nothing to fetch
122
}
123         ClassMetaData cmd = fg.classMetaData;
124         if (cmd.isInHeirachy()) {
125             // todo figure out correct cmd
126
throw notImplemented();
127         }
128         State state = cmd.createState();
129         oid.resolve(state);
130         data.setState(fetchResult, state);
131     }
132
133     public String JavaDoc getDescription() {
134         return fg.classMetaData.qname +
135                 (includeSubClasses ? " and subclasses" : "") +
136                 src.getDescription();
137     }
138
139     public int getResultType() {
140         return 0;
141     }
142
143     public Object JavaDoc getResult(FetchResultImp fetchResult) {
144         return data.getState(fetchResult);
145     }
146
147 }
148
149
Popular Tags