KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: GraphRDBMaker.java,v 1.21 2005/04/10 12:45:46 chris-dollin Exp $
5 */

6
7 package com.hp.hpl.jena.db.impl;
8
9 import com.hp.hpl.jena.db.GraphRDB;
10 import com.hp.hpl.jena.db.IDBConnection;
11 import com.hp.hpl.jena.graph.*;
12 import com.hp.hpl.jena.graph.impl.*;
13 import com.hp.hpl.jena.shared.*;
14 import com.hp.hpl.jena.util.CollectionFactory;
15 import com.hp.hpl.jena.util.iterator.*;
16 import com.hp.hpl.jena.vocabulary.*;
17
18 import java.util.*;
19
20 /**
21     A GraphFactory that produces Graphs from database connections.
22     The connection is supplied when the factory is constructed. All the
23     created graphs are tracked so that we can supply a removeAll call
24     to dispose of them.
25
26     @author kers
27 */

28
29 public class GraphRDBMaker extends BaseGraphMaker
30     {
31     private IDBConnection c;
32     private int counter = 0;
33     private Set created = CollectionFactory.createHashedSet();
34     int reificationStyle;
35     
36     /**
37         Construct a new GraphRDB factory based on the supplied DB connection.
38         @param c the database connection
39     */

40     public GraphRDBMaker( IDBConnection c, ReificationStyle style )
41         {
42         super( style );
43         this.c = c;
44         this.reificationStyle = GraphRDB.styleRDB( style );
45         }
46
47     /**
48         Answer the RDFS class of this RDB GraphMaker
49         @return JenaModelSpec.RDBMakerClass [as node]
50     */

51     public Node getMakerClass()
52         { return JenaModelSpec.RDBMakerSpec.asNode(); }
53         
54     /**
55         Augment the maker description of this maker with RDB-specific properties.
56         TODO do this
57     */

58     protected void augmentDescription( Graph g, Node self )
59         {}
60         
61     /**
62         Answer the default graph of this Maker; make it if necessary.
63     */

64     public Graph getGraph()
65         { if (defaultGraph == null)
66             defaultGraph = consGraph( null, !c.containsDefaultModel() );
67          return defaultGraph; }
68         
69     /**
70         The default graph for this maker, or null if there isn't one.
71     */

72     protected Graph defaultGraph = null;
73     
74     /**
75         Answer an "anonymous", freshly-created graph. We fake this by creating a graph
76         with the name "anon_<digit>+". This may lead to problems later; such a graph
77         may need to be deleted when the connection is closed.
78         
79         TODO resolve this issue.
80     */

81     public Graph createGraph()
82         { return createGraph( "anon_" + counter++ + "", false ); }
83     
84     /**
85         Create an RDB graph and remember its name.
86      */

87     public Graph createGraph( String JavaDoc name, boolean strict )
88         {
89         created.add( name );
90         boolean fresh = strict || !hasGraph( name );
91         return consGraph( name, fresh );
92         }
93     
94     /**
95         Open an existing graph; if there's no such graph, but failIfAbsent is
96         false, create a new one. In any case, return that graph.
97     */

98     public Graph openGraph( String JavaDoc name, boolean strict )
99         {
100         boolean fresh = hasGraph( name ) == false && strict == false;
101         if (fresh) created.add( name );
102         return consGraph( name, fresh );
103         }
104         
105     protected Graph consGraph( String JavaDoc name, boolean fresh )
106         {
107         Graph p = c.getDefaultModelProperties().getGraph();
108         return new GraphRDB( c, name, (fresh ? p : null), reificationStyle, fresh );
109         }
110         
111     /**
112         Remove a graph from the database - at present, this has to be done by
113         opening it first.
114         
115     */

116     public void removeGraph( String JavaDoc name )
117         {
118         GraphRDB toDelete = (GraphRDB) openGraph( name, true );
119         toDelete.remove();
120         toDelete.close();
121         created.remove( name );
122         }
123         
124     /**
125         Return true iff there's a graph with the given name.
126     */

127     public boolean hasGraph( String JavaDoc name )
128         { return c.containsModel( name ); }
129         
130     /**
131         Remove all the graphs that have been created by this factory.
132     */

133     public void removeAll()
134         {
135         Iterator it = CollectionFactory.createHashedSet( created ).iterator();
136         while (it.hasNext()) removeGraph( (String JavaDoc) it.next() );
137         }
138         
139     public void close()
140         { /* should consider - do we close the connection or not? */ }
141         
142     public ExtendedIterator listGraphs()
143         { return c.getAllModelNames() .filterDrop ( filterDEFAULT ); }
144         
145     private Filter filterDEFAULT = new Filter()
146         { public boolean accept( Object JavaDoc x ) { return "DEFAULT".equals( x ); } };
147     }
148
149 /*
150     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
151     All rights reserved.
152
153     Redistribution and use in source and binary forms, with or without
154     modification, are permitted provided that the following conditions
155     are met:
156
157     1. Redistributions of source code must retain the above copyright
158        notice, this list of conditions and the following disclaimer.
159
160     2. Redistributions in binary form must reproduce the above copyright
161        notice, this list of conditions and the following disclaimer in the
162        documentation and/or other materials provided with the distribution.
163
164     3. The name of the author may not be used to endorse or promote products
165        derived from this software without specific prior written permission.
166
167     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
168     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
169     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
170     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
171     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
172     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
173     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
174     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
175     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
176     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
177 */
Popular Tags