Improving code

This commit is contained in:
Luca Frosini 2022-05-18 18:00:36 +02:00
parent d0981df329
commit d7af96d8a7
3 changed files with 31 additions and 15 deletions

View File

@ -113,8 +113,8 @@ public class DatabaseEnvironment {
private static final Key KEY; private static final Key KEY;
public static final String V = "V"; public static final String VERTEX_CLASS_NAME = OClass.VERTEX_CLASS_NAME;
public static final String E = "E"; public static final String EDGE_CLASS_NAME = OClass.EDGE_CLASS_NAME;
static { static {
Properties properties = new Properties(); Properties properties = new Properties();
@ -279,6 +279,19 @@ public class DatabaseEnvironment {
oDatabaseDocument.set(ATTRIBUTES.DATETIMEFORMAT, Element.DATETIME_PATTERN); oDatabaseDocument.set(ATTRIBUTES.DATETIMEFORMAT, Element.DATETIME_PATTERN);
} }
protected static void setRecordLevelSecurity(OMetadata oMetadata) {
logger.trace(
"Setting Record-level Security (see https://orientdb.org/docs/3.2.x/security/Database-Security.html#record-level-security-deprecated-in-v-31)");
OSchema oSchema = oMetadata.getSchema();
OClass oRestricted = oSchema.getClass(OSecurity.RESTRICTED_CLASSNAME);
OClass v = oSchema.getClass(VERTEX_CLASS_NAME);
v.addSuperClass(oRestricted);
OClass e = oSchema.getClass(EDGE_CLASS_NAME);
e.addSuperClass(oRestricted);
}
private static boolean initGraphDB() throws Exception { private static boolean initGraphDB() throws Exception {
OLogManager.instance().setWarnEnabled(false); OLogManager.instance().setWarnEnabled(false);
OLogManager.instance().setErrorEnabled(false); OLogManager.instance().setErrorEnabled(false);
@ -307,16 +320,7 @@ public class DatabaseEnvironment {
OUser newAdminUser = oSecurity.createUser(CHANGED_ADMIN_USERNAME, CHANGED_ADMIN_PASSWORD, adminRole); OUser newAdminUser = oSecurity.createUser(CHANGED_ADMIN_USERNAME, CHANGED_ADMIN_PASSWORD, adminRole);
newAdminUser.save(); newAdminUser.save();
logger.trace( setRecordLevelSecurity(oMetadata);
"Setting Record-level Security (see http://orientdb.com/docs/3.0.x/security/Database-Security.html#record-level-security)");
OSchema oSchema = oMetadata.getSchema();
OClass oRestricted = oSchema.getClass(OSecurity.RESTRICTED_CLASSNAME);
OClass v = oSchema.getClass(V);
v.addSuperClass(oRestricted);
OClass e = oSchema.getClass(E);
e.addSuperClass(oRestricted);
oDatabaseSession.commit(); oDatabaseSession.commit();
oDatabaseSession.close(); oDatabaseSession.close();

View File

@ -143,9 +143,16 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
OResultSet resultSet = oDatabaseDocument.command(select.toString(), new HashMap<>()); OResultSet resultSet = oDatabaseDocument.command(select.toString(), new HashMap<>());
if (resultSet != null && resultSet.hasNext()) { if (resultSet != null) {
try {
if(resultSet.hasNext()) {
throw new AlreadyPresentException(errorMessage.toString()); throw new AlreadyPresentException(errorMessage.toString());
} }
}finally {
resultSet.close();
}
}
} }
protected void tryTemplate() throws Exception { protected void tryTemplate() throws Exception {
@ -169,6 +176,9 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
OResultSet resultSet = oDatabaseDocument.query(select.toString(), new HashMap<>()); OResultSet resultSet = oDatabaseDocument.query(select.toString(), new HashMap<>());
if(resultSet == null || !resultSet.hasNext()) { if(resultSet == null || !resultSet.hasNext()) {
if(resultSet!=null) {
resultSet.close();
}
throw new NotFoundException("Error retrieving " + QueryTemplate.NAME + " with name " + getName()); throw new NotFoundException("Error retrieving " + QueryTemplate.NAME + " with name " + getName());
} }
@ -178,9 +188,11 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
logger.trace("{} representing vertex is {}", QueryTemplate.NAME, Utility.toJsonString(queryTemplate, true)); logger.trace("{} representing vertex is {}", QueryTemplate.NAME, Utility.toJsonString(queryTemplate, true));
if(resultSet.hasNext()) { if(resultSet.hasNext()) {
resultSet.close();
throw new NotFoundException("Found more than one " + QueryTemplate.NAME + " with name " + name + ". This should not occur, please contact the administrator"); throw new NotFoundException("Found more than one " + QueryTemplate.NAME + " with name " + name + ". This should not occur, please contact the administrator");
} }
resultSet.close();
return queryTemplate; return queryTemplate;
} catch(NotFoundException e) { } catch(NotFoundException e) {
throw getSpecificNotFoundException(e); throw getSpecificNotFoundException(e);

View File

@ -131,7 +131,7 @@ public class CachedType<T extends Type> {
String name = oSuperClass.getName(); String name = oSuperClass.getName();
CachedType<?> cachedType = typesCache.getCachedType(name); CachedType<?> cachedType = typesCache.getCachedType(name);
cachedType.setOClass(oSuperClass); cachedType.setOClass(oSuperClass);
if(name.compareTo(DatabaseEnvironment.V) == 0 || name.compareTo(DatabaseEnvironment.E) == 0 if(name.compareTo(DatabaseEnvironment.VERTEX_CLASS_NAME) == 0 || name.compareTo(DatabaseEnvironment.EDGE_CLASS_NAME) == 0
|| name.compareTo(OSecurity.RESTRICTED_CLASSNAME) == 0) { || name.compareTo(OSecurity.RESTRICTED_CLASSNAME) == 0) {
continue; continue;
} }