001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: RDBModelAssembler.java,v 1.9 2008/01/03 15:19:05 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.assemblers;
008:
009: import com.hp.hpl.jena.assembler.*;
010: import com.hp.hpl.jena.db.*;
011: import com.hp.hpl.jena.graph.Graph;
012: import com.hp.hpl.jena.rdf.model.*;
013: import com.hp.hpl.jena.shared.*;
014:
015: public class RDBModelAssembler extends NamedModelAssembler implements
016: Assembler {
017: protected Model openEmptyModel(Assembler a, Resource root, Mode mode) {
018: return openModel(a, root, Content.empty, mode);
019: }
020:
021: protected Model openModel(Assembler a, Resource root,
022: Content initial, Mode mode) {
023: checkType(root, JA.RDBModel);
024: String name = getModelName(root);
025: ReificationStyle style = getReificationStyle(root);
026: ConnectionDescription c = getConnection(a, root);
027: Model m = openModel(root, c, name, style, initial, mode);
028: // if (!initial.isEmpty()) addContent( root, m, initial );
029: return m;
030: }
031:
032: protected ConnectionDescription getConnection(Assembler a,
033: Resource root) {
034: Resource C = getRequiredResource(root, JA.connection);
035: return (ConnectionDescription) a.open(C);
036: }
037:
038: protected Model openModel(Resource root, ConnectionDescription c,
039: String name, ReificationStyle style, Content initial,
040: Mode mode) {
041: IDBConnection ic = c.getConnection();
042: return isDefaultName(name) ? ic.containsDefaultModel() ? ModelRDB
043: .open(ic)
044: : ModelRDB.createModel(ic)
045: : openByMode(root, initial, name, mode, style, ic);
046: }
047:
048: private Model openByMode(Resource root, Content initial,
049: String name, Mode mode, ReificationStyle style,
050: IDBConnection ic) {
051: if (ic.containsModel(name)) {
052: if (mode.permitUseExisting(root, name))
053: return consModel(ic, name, style, false);
054: throw new AlreadyExistsException(name);
055: } else {
056: if (mode.permitCreateNew(root, name))
057: return initial.fill(consModel(ic, name, style, true));
058: throw new NotFoundException(name);
059: }
060: }
061:
062: private static final String nameForDefault = "DEFAULT";
063:
064: private boolean isDefaultName(String name) {
065: return name.equals(nameForDefault) || name.equals("");
066: }
067:
068: protected Model consModel(IDBConnection c, String name,
069: ReificationStyle style, boolean fresh) {
070: return new ModelRDB(consGraph(c, name, style, fresh));
071: }
072:
073: protected GraphRDB consGraph(IDBConnection c, String name,
074: ReificationStyle style, boolean fresh) {
075: Graph p = c.getDefaultModelProperties().getGraph();
076: int reificationStyle = GraphRDB.styleRDB(style);
077: return new GraphRDB(c, name, (fresh ? p : null),
078: reificationStyle, fresh);
079: }
080: }
081:
082: /*
083: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
084: * All rights reserved.
085: *
086: * Redistribution and use in source and binary forms, with or without
087: * modification, are permitted provided that the following conditions
088: * are met:
089: * 1. Redistributions of source code must retain the above copyright
090: * notice, this list of conditions and the following disclaimer.
091: * 2. Redistributions in binary form must reproduce the above copyright
092: * notice, this list of conditions and the following disclaimer in the
093: * documentation and/or other materials provided with the distribution.
094: * 3. The name of the author may not be used to endorse or promote products
095: * derived from this software without specific prior written permission.
096: *
097: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
098: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
099: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
100: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
101: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
102: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
103: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
104: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
105: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
106: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
107: */
|