Skip to content

Instantly share code, notes, and snippets.

@salvatore-piccione
Created October 11, 2011 15:27
Show Gist options
  • Select an option

  • Save salvatore-piccione/1278398 to your computer and use it in GitHub Desktop.

Select an option

Save salvatore-piccione/1278398 to your computer and use it in GitHub Desktop.
The utility class that creates the Tinkerpop Graph Database to be used for testing
/*
* Copyright 2011 TXT e-solutions SpA
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This work was performed within the IoT_at_Work Project
* and partially funded by the European Commission's
* 7th Framework Programme under the research area ICT-2009.1.3
* Internet of Things and enterprise environments.
*
* Authors:
* Salvatore Piccione (TXT e-solutions SpA)
*
* Contributors:
* Domenico Rotondi (TXT e-solutions SpA)
*/
package com.orientechnologies.orient.jdbc;
import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.TransactionalGraph.Conclusion;
import com.tinkerpop.blueprints.pgm.impls.orientdb.OrientGraph;
public class OrientGraphJDBCCreationHelper {
static final String URL_DB = "remote:iot-at-work1/test-graph-orient-jdbc";
private static final String ADMINISTRATOR_NAME = "root";
private static final String ADMINISTRATOR_PWD =
"EB61C596EAA0239700B02171DF791F2F423CFABF6A890FB032218EB8F22565E9";
static final String USERNAME = "admin";
static final String PASSWORD = "admin";
static final String NAME = "name";
static final String DESCRIPTION = "description";
static final String CONTAINMENT_EDGE = "contains";
static final String COMPONENT_EDGE = "has";
public static void createGraphDatabase () {
//REGISTER THE ENGINE
//Orient.instance().registerEngine(new OEngineRemote());
OrientGraph graphDB = null;
try {
/**/
OServerAdmin adminTool = null;
//I have to use a OServerAdmin instance to check the existance of a server
//because the method ODatabaseXXX.exists() is not supported for remote database
//In order to use such class, you have to use the root account
adminTool = new OServerAdmin(URL_DB).connect(ADMINISTRATOR_NAME,
ADMINISTRATOR_PWD);
System.out.println("Successful connection to graphDB1.");
if (adminTool.existsDatabase()) {
System.out.println("The database already exists. It will be deleted and created again.");
adminTool.dropDatabase();
}
System.out.println("The database is going to be created.");
adminTool.createDatabase("local");
adminTool.close();
graphDB = new OrientGraph (URL_DB,USERNAME,PASSWORD);
graphDB.setMaxBufferSize(0);//MANUAL
graphDB.startTransaction();
Vertex root = graphDB.addVertex(null);
root.setProperty(NAME, "Plant");
root.setProperty(DESCRIPTION, "This is the Plant");
Vertex cell = graphDB.addVertex(null);
cell.setProperty(NAME, "Cell 1");
cell.setProperty(DESCRIPTION, "This is the Production Cell 1");
graphDB.addEdge(null, root, cell, CONTAINMENT_EDGE);
Vertex cellComponent = graphDB.addVertex(null);
cellComponent.setProperty(NAME, "Cell Element A1");
cellComponent.setProperty(DESCRIPTION, "This is an element of the production cell 1");
graphDB.addEdge(null, cell, cellComponent, COMPONENT_EDGE);
cell = graphDB.addVertex(null);
cell.setProperty(NAME, "Cell 2");
cell.setProperty(DESCRIPTION, "This is the Production Cell 2");
graphDB.addEdge(null, root, cell, CONTAINMENT_EDGE);
cellComponent = graphDB.addVertex(null);
cellComponent.setProperty(NAME, "Cell Element B1");
cellComponent.setProperty(DESCRIPTION, "This is an element of the production cell 2");
graphDB.addEdge(null, cell, cellComponent, COMPONENT_EDGE);
graphDB.stopTransaction(Conclusion.SUCCESS);
graphDB.shutdown();
} catch (Exception e) {
System.err.println("An error occured during the creation of the database " +
URL_DB + ": " + e.getMessage());
e.printStackTrace();
if (graphDB != null) {
graphDB.stopTransaction(Conclusion.FAILURE);
graphDB.shutdown();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment