Changed strategy to set the DateTimeFormat and added test for that

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@177238 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2019-02-22 13:35:56 +00:00
parent e7e7cc588e
commit 6a16d77b70
2 changed files with 36 additions and 54 deletions

View File

@ -23,6 +23,7 @@ import org.slf4j.LoggerFactory;
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.client.remote.OStorageRemote.CONNECTION_STRATEGY;
import com.orientechnologies.orient.core.db.ODatabase.ATTRIBUTES;
import com.orientechnologies.orient.core.db.ODatabasePool;
import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.ODatabaseType;
@ -86,21 +87,11 @@ public class DatabaseEnvironment {
private static final String SERVER_URI;
public static final String DB_URI;
/*
private static final String DATABASE_TYPE = "graph";
private static final String STORAGE_MODE = "plocal";
*/
public static final String O_RESTRICTED_CLASS = "ORestricted";
public static final CONNECTION_STRATEGY CONNECTION_STRATEGY_PARAMETER = CONNECTION_STRATEGY.ROUND_ROBIN_CONNECT;
//private static final String ALTER_DATETIME_FORMAT_QUERY_TEMPLATE = "ALTER DATABASE DATETIMEFORMAT \"%s\"";
private static final String DATE_FORMAT_NAME = "dateformat";
private static final String ALTER_DATETIME_FORMAT_QUERY_TEMPLATE = "ALTER DATABASE DATETIMEFORMAT :" + DATE_FORMAT_NAME;
// Used to indicate virtual admin security context
private static final String ADMIN_SECURITY_CONTEXT;
public static final UUID ADMIN_SECURITY_CONTEXT_UUID;
@ -113,7 +104,6 @@ public class DatabaseEnvironment {
private static final String CONTEXT_SECURITY_CONTEXT;
public static final UUID CONTEXT_SECURITY_CONTEXT_UUID;
protected static final String DB_KEY_FILENAME_VARNAME = "DB_KEY_FILENAME";
protected static final String DB_KEY_ALGORITHM_VARNAME = "DB_KEY_ALGORITHM";
@ -216,14 +206,19 @@ public class DatabaseEnvironment {
ISMDiscovery.manageISM(schemaAction);
}
logger.info("Database Connection has been properly initialized");
} catch(Throwable e) {
logger.error("Error initializing database connection", e);
throw new RuntimeException("Error initializing database connection", e);
}
logger.info("Database Connection has been properly initialized");
KEY = initDbKey(properties);
}
protected static Key initDbKey(Properties properties) {
try {
logger.trace("Going to get properties required to load DB key");
String keyFileName = properties.getProperty(DB_KEY_FILENAME_VARNAME);
@ -232,15 +227,20 @@ public class DatabaseEnvironment {
URL keyFileURL = DatabaseEnvironment.class.getClassLoader().getResource(keyFileName);
File keyFile = new File(keyFileURL.toURI());
logger.debug("Trying to load DB key from file {} created for algorithm {}", keyFile.getAbsolutePath(), keyAlgorithm);
KEY = SymmetricKey.loadKeyFromFile(keyFile, keyAlgorithm);
Key key = SymmetricKey.loadKeyFromFile(keyFile, keyAlgorithm);
logger.info("DB Key has been properly initialized");
return key;
} catch(Throwable e) {
logger.error("Error loading DB Key", e);
throw new RuntimeException("Error loading DB Key. Unable to continue", e);
}
logger.info("DB Key has been properly initialized");
}
protected static void setDateTimeFormat(ODatabaseSession oDatabaseSession) {
oDatabaseSession.set(ATTRIBUTES.DATETIMEFORMAT, ISConstants.DATETIME_PATTERN);
}
private static boolean initGraphDB() throws Exception {
OLogManager.instance().setWarnEnabled(false);
@ -251,45 +251,21 @@ public class DatabaseEnvironment {
logger.info("Connecting as {} to {}", ROOT_USERNAME, DB_URI);
OrientDB orientDB = new OrientDB(SERVER_URI, ROOT_USERNAME, ROOT_PASSWORD, OrientDBConfig.defaultConfig());
/*
OServerAdmin serverAdmin = new OServerAdmin(SERVER_URI).connect(ROOT_USERNAME, ROOT_PASSWORD);
if(!serverAdmin.existsDatabase(DB, STORAGE_MODE)) {
*/
if(!orientDB.exists(DB)) {
logger.info("The database {} does not exist. Going to create it.", DB_URI);
//serverAdmin.createDatabase(DB, DATABASE_TYPE, STORAGE_MODE);
orientDB.create(DB, ODatabaseType.PLOCAL);
logger.trace("Connecting to newly created database {} as {} with default password", DB_URI,
DEFAULT_ADMIN_USERNAME);
/*
OrientGraphFactory factory = new OrientGraphFactory(DB_URI, DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD)
.setupPool(1, 10);
OrientGraphNoTx orientGraphNoTx = factory.getNoTx();
*/
ODatabasePool pool = new ODatabasePool(orientDB,DB, DEFAULT_ADMIN_USERNAME,DEFAULT_ADMIN_PASSWORD);
ODatabaseSession oDatabaseSession = pool.acquire();
/* Updating DateTimeFormat to be aligned with IS model definition */
/*
String query = String.format(ALTER_DATETIME_FORMAT_QUERY_TEMPLATE, ISConstants.DATETIME_PATTERN);
OCommandSQL preparedQuery = new OCommandSQL(query);
orientGraphNoTx.getRawGraph().command(preparedQuery).execute();
*/
Map<String, Object> map = new HashMap<>();
map.put(DATE_FORMAT_NAME, ISConstants.DATETIME_PATTERN);
oDatabaseSession.command(ALTER_DATETIME_FORMAT_QUERY_TEMPLATE, map);
DatabaseEnvironment.setDateTimeFormat(oDatabaseSession);
//OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata();
OMetadata oMetadata = oDatabaseSession.getMetadata();
OSecurity oSecurity = oMetadata.getSecurity();
@ -315,31 +291,20 @@ public class DatabaseEnvironment {
OSchema oSchema = oMetadata.getSchema();
OClass oRestricted = oSchema.getClass(O_RESTRICTED_CLASS);
/*
OrientVertexType v = orientGraphNoTx.getVertexBaseType();
v.addSuperClass(oRestricted);
*/
OClass v = oSchema.getClass("V");
v.addSuperClass(oRestricted);
/*
OrientEdgeType e = orientGraphNoTx.getEdgeBaseType();
e.addSuperClass(oRestricted);
*/
OClass e = oSchema.getClass("E");
e.addSuperClass(oRestricted);
// orientGraphNoTx.commit();
// orientGraphNoTx.shutdown();
oDatabaseSession.commit();
oDatabaseSession.close();
// factory.close();
pool.close();
return true;
}
// serverAdmin.close();
orientDB.close();
return false;

View File

@ -1,19 +1,36 @@
package org.gcube.informationsystem.resourceregistry.dbinitialization;
import org.gcube.informationsystem.model.reference.ISConstants;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.security.SecurityContext.PermissionMode;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.db.ODatabase.ATTRIBUTES;
import com.orientechnologies.orient.core.db.ODatabaseSession;
public class DatabaseEnvironmentTest {
private static Logger logger = LoggerFactory.getLogger(DatabaseEnvironmentTest.class);
@Test
// @Test
public void createDB() throws Exception{
String db = DatabaseEnvironment.DB_URI;
logger.trace("Created DB is {}", db);
}
@Test
public void testAlterDateTimeFormat() throws ResourceRegistryException {
ODatabaseSession oDatabaseSession = ContextUtility.getAdminSecurityContext().getDatabaseSession(PermissionMode.WRITER);
DatabaseEnvironment.setDateTimeFormat(oDatabaseSession);
String dateTime = oDatabaseSession.get(ATTRIBUTES.DATETIMEFORMAT).toString();
Assert.assertTrue(dateTime.compareTo(ISConstants.DATETIME_PATTERN)==0);
}
/*
@Test
public void generateDBKey() throws Exception {