KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > impl > DBPropGraph


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4 */

5
6 package com.hp.hpl.jena.db.impl;
7
8 import com.hp.hpl.jena.graph.*;
9 import com.hp.hpl.jena.util.iterator.*;
10 import com.hp.hpl.jena.vocabulary.DB;
11
12 import java.util.*;
13
14 /**
15  *
16  * A wrapper to assist in getting and setting DB information from
17  * a persistent store.
18  *
19  * This is written in the style of enhanced nodes - no state is
20  * stored in the DBStoreDesc, instead all state is in the
21  * underlying graph and this is just provided as a convenience.
22  *
23  * (We don't use enhanced nodes because, since we control everything
24  * in the persistent store system description, we can avoid any
25  * need to handle polymorhphism).
26  *
27  *
28  * @author csayers
29  * @version $Revision: 1.22 $
30  * @since Jena 2.0
31  */

32 public class DBPropGraph extends DBProp {
33
34     public static Node_URI graphName = (Node_URI)DB.graphName.getNode();
35     public static Node_URI graphType = (Node_URI)DB.graphType.getNode();
36     public static Node_URI graphLSet = (Node_URI)DB.graphLSet.getNode();
37     public static Node_URI graphPrefix = (Node_URI)DB.graphPrefix.getNode();
38     public static Node_URI graphId = (Node_URI)DB.graphId.getNode();
39     public static Node_URI stmtTable = (Node_URI)DB.stmtTable.getNode();
40     public static Node_URI reifTable = (Node_URI)DB.reifTable.getNode();
41
42
43     
44     public DBPropGraph( SpecializedGraph g, String JavaDoc symbolicName, String JavaDoc type) {
45         super(g);
46         
47         putPropString(graphName, symbolicName);
48         putPropString(graphType, type);
49     }
50     
51     public DBPropGraph( SpecializedGraph g, Node n) {
52         super(g,n);
53     }
54     
55     public DBPropGraph( SpecializedGraph g, String JavaDoc newSymbolicName, Graph oldProperties) {
56         super(g);
57         
58         putPropString(graphName, newSymbolicName);
59         // only copy user-configurable properties
60
Iterator it = oldProperties.find( Node.ANY, Node.ANY, Node.ANY);
61         while ( it.hasNext() ) {
62             Triple t = (Triple) it.next();
63             if ( t.getPredicate().equals(graphName) ||
64                 t.getPredicate().equals(graphId) ||
65                 t.getPredicate().equals(stmtTable) ||
66                 t.getPredicate().equals(reifTable) )
67                 continue;
68             putPropNode((Node_URI)t.getPredicate(),t.getObject());
69         }
70     }
71
72     
73     public void addLSet( DBPropLSet lset ) {
74         putPropNode( graphLSet, lset.getNode() );
75     }
76
77     public void addPrefix( DBPropPrefix prefix ) {
78         // First drop existing uses of prefix or URI
79
DBPropPrefix existing = getPrefix( prefix.getValue());
80         if( existing != null)
81             removePrefix( existing);
82         existing = getURI( prefix.getURI());
83         if( existing != null && !prefix.getValue().equals(""))
84             removePrefix( existing);
85         putPropNode( graphPrefix, prefix.getNode() );
86     }
87     
88     public void removePrefix( DBPropPrefix prefix ) {
89         SpecializedGraph.CompletionFlag complete = newComplete();
90         Iterator matches = graph.find( self, graphPrefix, prefix.getNode(), complete);
91         if( matches.hasNext() ) {
92             graph.delete( (Triple)(matches.next()), complete );
93             prefix.remove();
94         }
95     }
96     
97     public void addPrefix( String JavaDoc prefix, String JavaDoc uri ) {
98         addPrefix( new DBPropPrefix( graph, prefix, uri) );
99     }
100     
101     public void removePrefix( String JavaDoc prefix ) {
102         DBPropPrefix existing = getPrefix( prefix );
103         if (existing != null) removePrefix( existing );
104     }
105     
106     public void addGraphId( int id ) {
107         putPropString(graphId, Integer.toString(id));
108     }
109
110     public void addStmtTable( String JavaDoc table ) {
111         putPropString(stmtTable, table);
112     }
113     
114     public void addReifTable( String JavaDoc table ) {
115         putPropString(reifTable, table);
116     }
117
118
119     
120     public String JavaDoc getName() { return getPropString( graphName); }
121     public String JavaDoc getType() { return getPropString( graphType); }
122     public String JavaDoc getStmtTable() { return getPropString(stmtTable); }
123     public String JavaDoc getReifTable() { return getPropString(reifTable); }
124     public int getGraphId() {
125         String JavaDoc i = getPropString(graphId);
126         return i == null ? -1 : Integer.parseInt(i);
127     }
128     
129     public ExtendedIterator getAllLSets() {
130         return
131             graph.find( self, graphLSet, null, newComplete() )
132              .mapWith ( new MapToLSet() );
133     }
134     
135     public ExtendedIterator getAllPrefixes() {
136         return
137             graph.find( self, graphPrefix, null, newComplete() )
138             .mapWith ( new MapToPrefix() );
139     }
140     
141     public DBPropPrefix getPrefix( String JavaDoc value ) {
142         ExtendedIterator prefixes = getAllPrefixes();
143         while( prefixes.hasNext() ) {
144             DBPropPrefix prefix = (DBPropPrefix)prefixes.next();
145             if( prefix.getValue().compareTo(value)==0)
146                 return prefix;
147         }
148         return null;
149     }
150     
151     public DBPropPrefix getURI( String JavaDoc uri ) {
152         ExtendedIterator prefixes = getAllPrefixes();
153         while( prefixes.hasNext() ) {
154             DBPropPrefix prefix = (DBPropPrefix)prefixes.next();
155             if( prefix.getURI().compareTo(uri)==0)
156                 return prefix;
157         }
158         return null;
159     }
160     
161     public ExtendedIterator listTriples() {
162         // First get all the triples that directly desrcribe this graph
163
ExtendedIterator result = DBProp.listTriples( graph, self );
164         
165         // Now get all the triples that describe any lsets
166
ExtendedIterator lsets = getAllLSets();
167         while( lsets.hasNext()) {
168             result = result.andThen( ((DBPropLSet)lsets.next()).listTriples() );
169         }
170
171         // Now get all the triples that describe any prefixes
172
ExtendedIterator prefixes = getAllPrefixes();
173         while( prefixes.hasNext()) {
174             result = result.andThen( ((DBPropPrefix)prefixes.next()).listTriples() );
175         }
176         return result;
177     }
178     
179     
180     private class MapToLSet implements Map1 {
181         public Object JavaDoc map1( Object JavaDoc o) {
182             Triple t = (Triple) o;
183             return new DBPropLSet( graph, t.getObject() );
184         }
185     }
186     
187     private class MapToPrefix implements Map1 {
188         public Object JavaDoc map1( Object JavaDoc o) {
189             Triple t = (Triple) o;
190             return new DBPropPrefix( graph, t.getObject() );
191         }
192     }
193     
194     public static DBPropGraph findPropGraphByName( SpecializedGraph graph, String JavaDoc name ) {
195         Node myNode = Node.createLiteral( name );
196         ClosableIterator it = graph.find( null, graphName, myNode, newComplete() );
197         if( it.hasNext() )
198             try { return new DBPropGraph( graph, ((Triple)it.next()).getSubject()); }
199             finally { it.close(); }
200         else
201             return null;
202     }
203     
204     /*
205      * return true if the DBPropGraph has the required
206      * properties for the named, stored graph.
207      */

208     
209     public boolean isDBPropGraphOk ( String JavaDoc name ) {
210         String JavaDoc s = getName();
211         boolean res = (s == null) ? false : s.equals(name);
212         res = res & (getGraphId() != -1);
213         res = res & (getType() != null);
214         res = res & (getStmtTable() != null);
215         res = res & (getReifTable() != null);
216         return res;
217     }
218     
219     public void remove() {
220         Iterator it = getAllPrefixes();
221         while( it.hasNext()) {
222             ((DBPropPrefix)it.next()).remove();
223         }
224         it = getAllLSets();
225         while( it.hasNext()) {
226             ((DBPropLSet)it.next()).remove();
227         }
228         super.remove();
229     }
230
231 }
232
233 /*
234  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
235  * All rights reserved.
236  *
237  * Redistribution and use in source and binary forms, with or without
238  * modification, are permitted provided that the following conditions
239  * are met:
240  * 1. Redistributions of source code must retain the above copyright
241  * notice, this list of conditions and the following disclaimer.
242  * 2. Redistributions in binary form must reproduce the above copyright
243  * notice, this list of conditions and the following disclaimer in the
244  * documentation and/or other materials provided with the distribution.
245  * 3. The name of the author may not be used to endorse or promote products
246  * derived from this software without specific prior written permission.
247
248  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
249  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
250  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
251  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
252  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
253  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
254  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
255  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
257  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258  */
Popular Tags