Package org.apache.marmotta.kiwi.persistence

Examples of org.apache.marmotta.kiwi.persistence.KiWiConnection


        this.defaultContext = defaultContext;
    }

    protected KiWiConnection aqcuireConnection() {
        try {
            KiWiConnection connection = store.getPersistence().getConnection();
            connection.setAutoCommit(true);
            return connection;
        } catch(SQLException ex) {
            log.error("could not acquire database connection", ex);
            throw new RuntimeException(ex);
        }
View Full Code Here


     * @param uri A string-representation of a URI.
     * @return An object representing the URI.
     */
    @Override
    public URI createURI(String uri) {
        KiWiConnection connection = aqcuireConnection();
        try {
            // first look in the registry for newly created resources if the resource has already been created and
            // is still volatile
            KiWiUriResource result = connection.loadUriResource(uri);

            if(result == null) {
                result = new KiWiUriResource(uri);

                connection.storeNode(result);

            }
            if(result.getId() < 0) {
                log.error("node ID is null!");
            }
View Full Code Here

     * @param nodeID The blank node identifier.
     * @return An object representing the blank node.
     */
    @Override
    public BNode createBNode(String nodeID) {
        KiWiConnection connection = aqcuireConnection();
        try {
            // first look in the registry for newly created resources if the resource has already been created and
            // is still volatile
            KiWiAnonResource result = connection.loadAnonResource(nodeID);

            if(result == null) {
                result = new KiWiAnonResource(nodeID);

                connection.storeNode(result);
            }
            if(result.getId() < 0) {
                log.error("node ID is null!");
            }

View Full Code Here

        KiWiLiteral result;

        final KiWiUriResource rtype = type==null ? null: (KiWiUriResource)createURI(type);

        final KiWiConnection connection = aqcuireConnection();
        try {

            try {
                // differentiate between the different types of the value
                if (type == null) {
                    // FIXME: MARMOTTA-39 (this is to avoid a NullPointerException in the following if-clauses)
                    result = connection.loadLiteral(value.toString(), lang, rtype);

                    if(result == null) {
                        result = new KiWiStringLiteral(value.toString(), locale, rtype);
                    }
                } else if(value instanceof Date || type.equals(Namespaces.NS_XSD+"dateTime")) {
                    // parse if necessary
                    final Date dvalue;
                    if(value instanceof Date) {
                        dvalue = (Date)value;
                    } else {
                        dvalue = DateUtils.parseDate(value.toString());
                    }

                    result = connection.loadLiteral(dvalue);

                    if(result == null) {
                        result= new KiWiDateLiteral(dvalue, rtype);
                    }
                } else if(Integer.class.equals(value.getClass()) || int.class.equals(value.getClass())  ||
                        Long.class.equals(value.getClass())    || long.class.equals(value.getClass()) ||
                        type.equals(Namespaces.NS_XSD+"integer") || type.equals(Namespaces.NS_XSD+"long")) {
                    long ivalue = 0;
                    if(Integer.class.equals(value.getClass()) || int.class.equals(value.getClass())) {
                        ivalue = (Integer)value;
                    } else if(Long.class.equals(value.getClass()) || long.class.equals(value.getClass())) {
                        ivalue = (Long)value;
                    } else {
                        ivalue = Long.parseLong(value.toString());
                    }


                    result = connection.loadLiteral(ivalue);

                    if(result == null) {
                        result= new KiWiIntLiteral(ivalue, rtype);
                    }
                } else if(Double.class.equals(value.getClass())   || double.class.equals(value.getClass())  ||
                        Float.class.equals(value.getClass())    || float.class.equals(value.getClass()) ||
                        type.equals(Namespaces.NS_XSD+"double") || type.equals(Namespaces.NS_XSD+"float")) {
                    double dvalue = 0.0;
                    if(Float.class.equals(value.getClass()) || float.class.equals(value.getClass())) {
                        dvalue = (Float)value;
                    } else if(Double.class.equals(value.getClass()) || double.class.equals(value.getClass())) {
                        dvalue = (Double)value;
                    } else {
                        dvalue = Double.parseDouble(value.toString());
                    }


                    result = connection.loadLiteral(dvalue);

                    if(result == null) {
                        result= new KiWiDoubleLiteral(dvalue, rtype);
                    }
                } else if(Boolean.class.equals(value.getClass())   || boolean.class.equals(value.getClass())  ||
                        type.equals(Namespaces.NS_XSD+"boolean")) {
                    boolean bvalue = false;
                    if(Boolean.class.equals(value.getClass())   || boolean.class.equals(value.getClass())) {
                        bvalue = (Boolean)value;
                    } else {
                        bvalue = Boolean.parseBoolean(value.toString());
                    }


                    result = connection.loadLiteral(bvalue);

                    if(result == null) {
                        result= new KiWiBooleanLiteral(bvalue, rtype);
                    }
                } else {
                    result = connection.loadLiteral(value.toString(), lang, rtype);

                    if(result == null) {
                        result = new KiWiStringLiteral(value.toString(), locale, rtype);
                    }
                }
            } catch(IllegalArgumentException ex) {
                // malformed number or date
                log.warn("malformed argument for typed literal of type {}: {}", rtype.stringValue(), value);
                KiWiUriResource mytype = (KiWiUriResource)createURI(Namespaces.NS_XSD+"string");

                result = connection.loadLiteral(value.toString(), lang, mytype);

                if(result == null) {
                    result = new KiWiStringLiteral(value.toString(), locale, mytype);
                }

            }

            if(result.getId() < 0) {
                connection.storeNode(result);
            }

            return result;

View Full Code Here

        this.defaultContext = defaultContext;
    }

    protected KiWiConnection aqcuireConnection() {
        try {
            KiWiConnection connection = store.getPersistence().getConnection();
            return connection;
        } catch(SQLException ex) {
            log.error("could not acquire database connection",ex);
            throw new RuntimeException(ex);
        }
View Full Code Here

     */
    @Override
    public URI createURI(String uri) {

        ReentrantLock lock = acquireResourceLock(uri);
        KiWiConnection connection = aqcuireConnection();
        try {
            // first look in the registry for newly created resources if the resource has already been created and
            // is still volatile
            KiWiUriResource result = connection.loadUriResource(uri);

            if(result == null) {
                result = new KiWiUriResource(uri);
                connection.storeNode(result);
            }

            return result;
        } catch (SQLException e) {
            log.error("database error, could not load URI resource",e);
View Full Code Here

     * @return An object representing the blank node.
     */
    @Override
    public BNode createBNode(String nodeID) {
        nodeLock.lock();
        KiWiConnection connection = aqcuireConnection();
        try {
            // first look in the registry for newly created resources if the resource has already been created and
            // is still volatile
            KiWiAnonResource result = connection.loadAnonResource(nodeID);

            if(result == null) {
                result = new KiWiAnonResource(nodeID);
                connection.storeNode(result);
            }

            return result;
        } catch (SQLException e) {
            log.error("database error, could not load anonymous resource",e);
View Full Code Here

            locale = LocaleUtils.toLocale(lang);
        } else
            locale  = null;

        ReentrantLock lock = acquireLiteralLock(value);
        KiWiConnection connection = aqcuireConnection();
        try {


            // differentiate between the different types of the value
            if(value instanceof Date || type.equals(Namespaces.NS_XSD+"dateTime")) {
                // parse if necessary
                final Date dvalue;
                if(value instanceof Date) {
                    dvalue = (Date)value;
                } else {
                    dvalue = DateUtils.parseDate(value.toString());
                }

                result = connection.loadLiteral(dvalue);

                if(result == null) {
                    result= new KiWiDateLiteral(dvalue, rtype);
                }
            } else if(Integer.class.equals(value.getClass()) || int.class.equals(value.getClass())  ||
                    Long.class.equals(value.getClass())    || long.class.equals(value.getClass()) ||
                    type.equals(Namespaces.NS_XSD+"integer") || type.equals(Namespaces.NS_XSD+"long")) {
                long ivalue = 0;
                if(Integer.class.equals(value.getClass()) || int.class.equals(value.getClass())) {
                    ivalue = (Integer)value;
                } else if(Long.class.equals(value.getClass()) || long.class.equals(value.getClass())) {
                    ivalue = (Long)value;
                } else {
                    ivalue = Long.parseLong(value.toString());
                }


                result = connection.loadLiteral(ivalue);

                if(result == null) {
                    result= new KiWiIntLiteral(ivalue, rtype);
                }
            } else if(Double.class.equals(value.getClass())   || double.class.equals(value.getClass())  ||
                    Float.class.equals(value.getClass())    || float.class.equals(value.getClass()) ||
                    type.equals(Namespaces.NS_XSD+"double") || type.equals(Namespaces.NS_XSD+"float")) {
                double dvalue = 0.0;
                if(Float.class.equals(value.getClass()) || float.class.equals(value.getClass())) {
                    dvalue = (Float)value;
                } else if(Double.class.equals(value.getClass()) || double.class.equals(value.getClass())) {
                    dvalue = (Double)value;
                } else {
                    dvalue = Double.parseDouble(value.toString());
                }


                result = connection.loadLiteral(dvalue);

                if(result == null) {
                    result= new KiWiDoubleLiteral(dvalue, rtype);
                }
            } else if(Boolean.class.equals(value.getClass())   || boolean.class.equals(value.getClass())  ||
                    type.equals(Namespaces.NS_XSD+"boolean")) {
                boolean bvalue = false;
                if(Boolean.class.equals(value.getClass())   || boolean.class.equals(value.getClass())) {
                    bvalue = (Boolean)value;
                } else {
                    bvalue = Boolean.parseBoolean(value.toString());
                }


                result = connection.loadLiteral(bvalue);

                if(result == null) {
                    result= new KiWiBooleanLiteral(bvalue, rtype);
                }
            } else {
                result = connection.loadLiteral(value.toString(), lang, rtype);

                if(result == null) {
                    result = new KiWiStringLiteral(value.toString(), locale, rtype);
                }
            }

            if(result.getId() == null) {
                connection.storeNode(result);
            }

            return result;

        } catch (SQLException e) {
View Full Code Here

     *
     * @throws SQLException
     */
    @Test
    public void testStoreStringLiteralLanguage() throws SQLException {
        KiWiConnection connection = persistence.getConnection();
        try {
            KiWiUriResource   stype   = new KiWiUriResource(getRDFLangStringType());
            connection.storeNode(stype);

            // add a new URI to the triple store and check if it exists afterwards, before and after commit
            KiWiStringLiteral literal = new KiWiStringLiteral(RandomStringUtils.randomAlphanumeric(8), Locale.ENGLISH, stype);
            connection.storeNode(literal);

            // check if it then has a database ID
            Assert.assertNotNull(literal.getId());

            KiWiNode testLiteral1 = connection.loadNodeById(literal.getId());

            // needs to be equal, and should also be the identical object!
            Assert.assertEquals(literal,testLiteral1);
            Assert.assertTrue(literal == testLiteral1);

            connection.commit();

            KiWiNode testLiteral2 = connection.loadNodeById(literal.getId());

            // needs to be equal, and should also be the identical object!
            Assert.assertEquals(literal,testLiteral2);
            Assert.assertTrue(literal == testLiteral2);

            KiWiNode testLiteral3 = connection.loadLiteral(literal.stringValue(),Locale.ENGLISH.getLanguage(),null);

            // needs to be equal, and should also be the identical object!
            Assert.assertEquals(literal,testLiteral3);
            Assert.assertTrue(literal == testLiteral3);

            connection.commit();


            // clear cache and test again
            persistence.clearCache();
            KiWiNode testLiteral4 = connection.loadNodeById(literal.getId());

            // needs to be equal, but now it should not be the same object!
            Assert.assertEquals(literal,testLiteral4);
            Assert.assertTrue(literal != testLiteral4);

            KiWiNode testLiteral5 = connection.loadLiteral(literal.stringValue(),Locale.ENGLISH.getLanguage(),null);

            // needs to be equal, but now it should not be the same object!
            Assert.assertEquals(literal,testLiteral5);
            Assert.assertTrue(literal != testLiteral5);

            connection.commit();

            // finally do a test on the nodes table, it should contain exactly one entry
            PreparedStatement checkNodeStmt = connection.getJDBCConnection().prepareStatement("SELECT * FROM nodes");
            ResultSet result = checkNodeStmt.executeQuery();

            Assert.assertTrue(result.next());
            Assert.assertTrue(result.next());
            Assert.assertEquals((long) literal.getId(), result.getLong("id"));
            Assert.assertEquals(literal.stringValue(), result.getString("svalue"));
            Assert.assertEquals("string",result.getString("ntype"));
            Assert.assertEquals(Locale.ENGLISH.getLanguage(),result.getString("lang"));
            Assert.assertNotNull(result.getObject("ltype"));

            result.close();
            connection.commit();
        } finally {
            connection.close();
        }

    }
View Full Code Here

     *
     * @throws SQLException
     */
    @Test
    public void testStoreStringLiteralType() throws SQLException {
        KiWiConnection connection = persistence.getConnection();
        try {
            KiWiUriResource uri = new KiWiUriResource("http://localhost/"+ RandomStringUtils.randomAlphanumeric(8));

            // add a new URI to the triple store and check if it exists afterwards, before and after commit
            KiWiStringLiteral literal = new KiWiStringLiteral(RandomStringUtils.randomAlphanumeric(8), null, uri);
            connection.storeNode(literal);

            // check if it then has a database ID
            Assert.assertNotNull(literal.getId());

            KiWiNode testLiteral1 = connection.loadNodeById(literal.getId());

            // needs to be equal, and should also be the identical object!
            Assert.assertEquals(literal,testLiteral1);
            Assert.assertEquals(uri,((KiWiLiteral)testLiteral1).getType());
            Assert.assertTrue(literal == testLiteral1);

            connection.commit();

            KiWiNode testLiteral2 = connection.loadNodeById(literal.getId());

            // needs to be equal, and should also be the identical object!
            Assert.assertEquals(literal,testLiteral2);
            Assert.assertEquals(uri,((KiWiLiteral)testLiteral2).getType());
            Assert.assertTrue(literal == testLiteral2);

            KiWiNode testLiteral3 = connection.loadLiteral(literal.stringValue(),null,uri);

            // needs to be equal, and should also be the identical object!
            Assert.assertEquals(literal,testLiteral3);
            Assert.assertEquals(uri,((KiWiLiteral)testLiteral3).getType());
            Assert.assertTrue(literal == testLiteral3);

            connection.commit();


            // clear cache and test again
            persistence.clearCache();
            KiWiNode testLiteral4 = connection.loadNodeById(literal.getId());

            // needs to be equal, but now it should not be the same object!
            Assert.assertEquals(literal,testLiteral4);
            Assert.assertEquals(uri,((KiWiLiteral)testLiteral4).getType());
            Assert.assertTrue(literal != testLiteral4);

            KiWiNode testLiteral5 = connection.loadLiteral(literal.stringValue(),null,uri);

            // needs to be equal, but now it should not be the same object!
            Assert.assertEquals(literal,testLiteral5);
            Assert.assertEquals(uri,((KiWiLiteral)testLiteral5).getType());
            Assert.assertTrue(literal != testLiteral5);

            connection.commit();

            // finally do a test on the nodes table, it should contain exactly one entry
            PreparedStatement checkNodeStmt = connection.getJDBCConnection().prepareStatement("SELECT * FROM nodes WHERE ntype='string'");
            ResultSet result = checkNodeStmt.executeQuery();

            Assert.assertTrue(result.next());
            Assert.assertEquals((long)literal.getId(),result.getLong("id"));
            Assert.assertEquals(literal.stringValue(),result.getString("svalue"));
            Assert.assertEquals("string",result.getString("ntype"));
            Assert.assertNull(result.getString("lang"));
            Assert.assertEquals((long) uri.getId(), result.getLong("ltype"));

            result.close();
            connection.commit();
        } finally {
            connection.close();
        }

    }
View Full Code Here

TOP

Related Classes of org.apache.marmotta.kiwi.persistence.KiWiConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.