KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > jdbcimpl > JdbcMultivaluedIndex


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.jdbcimpl;
20
21 import org.netbeans.mdr.persistence.*;
22 import org.netbeans.mdr.util.*;
23
24 import java.util.*;
25 import java.io.*;
26 import java.text.*;
27 import java.sql.*;
28
29 /**
30  * JdbcMultivaluedIndex implements the MDR MultivaluedIndex interface using
31  * JDBC.
32  *
33  * @author John V. Sichi
34  * @version $Id: JdbcMultivaluedIndex.java,v 1.6 2006/06/30 20:56:10 jtulach Exp $
35  */

36 class JdbcMultivaluedIndex
37     extends JdbcIndex implements MultivaluedIndex
38 {
39     protected boolean queryDuplicates;
40
41     protected LazyPreparedStatement sqlDeleteWithValue;
42     protected LazyPreparedStatement sqlDeleteWithSurrogate;
43     protected LazyPreparedStatement sqlFindCount;
44     protected LazyPreparedStatement sqlFindCountWithValue;
45     protected LazyPreparedStatement sqlFindSurrogate;
46     
47     protected void defineSql()
48     {
49         super.defineSql();
50         
51         sqlDeleteWithValue = new LazyPreparedStatement(
52             "delete from " + tableName
53             + " where " + keyColName + " = ?"
54             + " and " + valColName + " = ?");
55         
56         sqlDeleteWithSurrogate = new LazyPreparedStatement(
57             "delete from " + tableName
58             + " where " + keyColName + " = ?"
59             + " and " + JdbcStorage.SURROGATE_COL_NAME + " = ?");
60         
61         sqlFindCount = new LazyPreparedStatement(
62             "select count(*) from " + tableName
63             + " where " + keyColName + " = ?");
64
65         sqlFindCountWithValue = new LazyPreparedStatement(
66             "select count(*) from " + tableName
67             + " where " + keyColName + " = ?"
68             + " and " + valColName + " = ?");
69
70         sqlFindSurrogate = new LazyPreparedStatement(
71             "select " + JdbcStorage.SURROGATE_COL_NAME
72             + " from " + tableName
73             + " where " + keyColName + " = ?"
74             + " and " + valColName + " = ?");
75     }
76     
77     // implement MultivaluedIndex
78
public Collection getItems(Object JavaDoc key) throws StorageException
79     {
80         return new ItemCollection(key,null);
81     }
82     
83     // implement MultivaluedIndex
84
public Collection getObjects(
85         Object JavaDoc key, SinglevaluedIndex repos) throws StorageException
86     {
87         if (keyType == Storage.EntryType.MOFID) {
88             return new ItemCollection(key,repos);
89         } else {
90             return getItems(key);
91         }
92     }
93     
94     // implement MultivaluedIndex
95
public boolean isUnique() throws StorageException
96     {
97         return !needSurrogate;
98     }
99
100     // override JdbcIndex
101
public void add(Object JavaDoc key, Object JavaDoc value) throws StorageException
102     {
103         if (queryDuplicates) {
104             int n = storage.getSingletonInt(
105                 sqlFindCountWithValue,
106                 new Object JavaDoc [] { key, value } );
107             if (n > 0) {
108                 throw new StorageBadRequestException("duplicate detected");
109             }
110         }
111         addImpl(key, value);
112     }
113
114     // implement MultivaluedIndex
115
public boolean remove(Object JavaDoc key, Object JavaDoc value) throws StorageException
116     {
117         Object JavaDoc [] pair = new Object JavaDoc[]{key,value};
118
119         int rowCount;
120         if (isUnique()) {
121             // since this index is unique, don't need to worry about
122
// duplicate handling
123
rowCount = storage.executeUpdate(sqlDeleteWithValue,pair);
124         } else {
125             // there may be duplicates, and we're only supposed to delete
126
// one of them
127
int surrogateKey = storage.getSingletonInt(sqlFindSurrogate,pair);
128             if (surrogateKey == -1) {
129                 return false;
130             }
131             rowCount = storage.executeUpdate(
132                 sqlDeleteWithSurrogate,
133                 new Object JavaDoc[]{key,new Integer JavaDoc(surrogateKey)});
134         }
135         return rowCount > 0;
136     }
137     
138     // implement MultivaluedIndex
139
public Collection queryByKeyPrefix(
140         Object JavaDoc prefix, SinglevaluedIndex repos) throws StorageException
141     {
142         // TODO:
143
throw new RuntimeException JavaDoc("oops, not yet implemented");
144     }
145
146     private class ItemCollection extends AbstractCollection
147     {
148         private Object JavaDoc key;
149         private SinglevaluedIndex repos;
150
151         ItemCollection(Object JavaDoc key,SinglevaluedIndex repos)
152         {
153             this.key = key;
154             this.repos = repos;
155         }
156         
157         // implement AbstractCollection
158
public Iterator iterator()
159         {
160             try {
161                 return new ItemCollectionIter(
162                     storage.getResultSetIterator(
163                         sqlFind,
164                         new Object JavaDoc[]{key},
165                         getValueType()),
166                     repos,
167                     key);
168             } catch (StorageException ex) {
169                 throw new RuntimeStorageException(ex);
170             }
171         }
172
173         public int size()
174         {
175             try {
176                 return storage.getSingletonInt(sqlFindCount,new Object JavaDoc[]{key});
177             } catch (StorageException ex) {
178                 throw new RuntimeStorageException(ex);
179             }
180         }
181
182         // implement AbstractCollection
183
public boolean add(Object JavaDoc value)
184         {
185             try {
186                 JdbcMultivaluedIndex.this.add(key,value);
187                 return true;
188             } catch (StorageException ex) {
189                 throw new RuntimeStorageException(ex);
190             }
191         }
192     }
193
194     private class ItemCollectionIter implements Iterator
195     {
196         private Iterator iter;
197         private SinglevaluedIndex repos;
198         private Object JavaDoc key;
199         private Object JavaDoc value;
200
201         ItemCollectionIter(Iterator iter,SinglevaluedIndex repos,Object JavaDoc key)
202         {
203             this.iter = iter;
204             this.repos = repos;
205             this.key = key;
206         }
207
208         // implement Iterator
209
public boolean hasNext()
210         {
211             return iter.hasNext();
212         }
213
214         // implement Iterator
215
public Object JavaDoc next()
216         {
217             value = iter.next();
218             if (repos != null) {
219                 try {
220                     return repos.get(value);
221                 } catch (StorageException ex) {
222                     throw new RuntimeStorageException(ex);
223                 }
224             }
225             return value;
226         }
227
228         // implement Iterator
229
public void remove()
230         {
231             try {
232                 // NOTE: in the presence of duplicates, this won't necessarily
233
// delete the one we're on, but it doesn't really matter
234
// since the duplicates are externally indistinguishable
235
// (they only differ in their surrogate keys, which are
236
// totally internal)
237
JdbcMultivaluedIndex.this.remove(key,value);
238             } catch (StorageException ex) {
239                 throw new RuntimeStorageException(ex);
240             }
241         }
242     }
243 }
244
245 // End JdbcMultivaluedIndex.java
246
Popular Tags