use of com.orientechnologies.orient.core.hook.ORecordHook in project wicket-orientdb by OrienteerBAP.
the class OrientDbWebApplication method init.
@Override
protected void init() {
super.init();
Orient.instance().registerThreadDatabaseFactory(new DefaultODatabaseThreadLocalFactory(this));
Orient.instance().addDbLifecycleListener(new ODatabaseLifecycleListener() {
private ORecordHook createHook(Class<? extends ORecordHook> clazz, ODatabaseInternal iDatabase) {
if (!(iDatabase instanceof ODatabaseDocument))
return null;
try {
return (ORecordHook) clazz.getConstructor(ODatabaseDocument.class).newInstance(iDatabase);
} catch (Exception e) {
try {
return (ORecordHook) clazz.newInstance();
} catch (Exception e1) {
throw new IllegalStateException("Can't initialize hook " + clazz.getName(), e);
}
}
}
@Override
public void onOpen(ODatabaseInternal iDatabase) {
registerHooks(iDatabase);
}
@Override
public void onCreate(ODatabaseInternal iDatabase) {
registerHooks(iDatabase);
// Fix for "feature" appeared in OrientDB 2.1.1
// Issue: https://github.com/orientechnologies/orientdb/issues/4906
fixOrientDBRights(iDatabase);
}
public void registerHooks(ODatabaseInternal iDatabase) {
Set<ORecordHook> hooks = iDatabase.getHooks().keySet();
List<Class<? extends ORecordHook>> hooksToRegister = new ArrayList<Class<? extends ORecordHook>>(getOrientDbSettings().getORecordHooks());
for (ORecordHook hook : hooks) {
if (hooksToRegister.contains(hook.getClass()))
hooksToRegister.remove(hook.getClass());
}
for (Class<? extends ORecordHook> oRecordHookClass : hooksToRegister) {
ORecordHook hook = createHook(oRecordHookClass, iDatabase);
if (hook != null) {
if (hook instanceof IHookPosition) {
iDatabase.registerHook(hook, ((IHookPosition) hook).getPosition());
} else {
iDatabase.registerHook(hook);
}
}
}
}
@Override
public void onClose(ODatabaseInternal iDatabase) {
/*NOP*/
}
@Override
public void onDrop(ODatabaseInternal iDatabase) {
/*NOP*/
}
public PRIORITY getPriority() {
return PRIORITY.REGULAR;
}
@Override
public void onCreateClass(ODatabaseInternal iDatabase, OClass iClass) {
/*NOP*/
}
@Override
public void onDropClass(ODatabaseInternal iDatabase, OClass iClass) {
/*NOP*/
}
@Override
public void onLocalNodeConfigurationRequest(ODocument arg0) {
/*NOP*/
}
});
getRequestCycleListeners().add(newTransactionRequestCycleListener());
getRequestCycleListeners().add(new OrientDefaultExceptionsHandlingListener());
getSecuritySettings().setAuthorizationStrategy(new WicketOrientDbAuthorizationStrategy(this));
getApplicationListeners().add(new IApplicationListener() {
@Override
public void onAfterInitialized(Application application) {
Orient.instance().startup();
Orient.instance().removeShutdownHook();
}
@Override
public void onBeforeDestroyed(Application application) {
Orient.instance().shutdown();
}
});
getAjaxRequestTargetListeners().add(new FixFormEncTypeListener());
// workaround to support changing system users passwords in web interface
getOrientDbSettings().getORecordHooks().add(OUserCatchPasswordHook.class);
PropertyResolver.setLocator(this, new ODocumentPropertyLocator(new PropertyResolver.CachingPropertyLocator(new PropertyResolver.DefaultPropertyLocator())));
}
use of com.orientechnologies.orient.core.hook.ORecordHook in project orientdb by orientechnologies.
the class HookReadTest method testSelectChangedInHook.
@Test
public void testSelectChangedInHook() {
database.registerHook(new ORecordHook() {
@Override
public void onUnregister() {
}
@Override
public RESULT onTrigger(TYPE iType, ORecord iRecord) {
if (iType == TYPE.AFTER_READ)
((ODocument) iRecord).field("read", "test");
return RESULT.RECORD_CHANGED;
}
@Override
public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
return null;
}
});
database.save(new ODocument("TestClass"));
List<ODocument> res = database.query(new OSQLSynchQuery<Object>("select from TestClass"));
assertEquals(res.get(0).field("read"), "test");
}
use of com.orientechnologies.orient.core.hook.ORecordHook in project orientdb by orientechnologies.
the class HookSaveTest method testCreatedLinkedInHook.
@Test
public void testCreatedLinkedInHook() {
database.registerHook(new ORecordHook() {
@Override
public void onUnregister() {
}
@Override
public RESULT onTrigger(TYPE iType, ORecord iRecord) {
if (iType != TYPE.BEFORE_CREATE)
return RESULT.RECORD_NOT_CHANGED;
ODocument doc = (ODocument) iRecord;
if (doc.containsField("test"))
return RESULT.RECORD_NOT_CHANGED;
ODocument doc1 = new ODocument();
doc1.field("test", "value");
doc.field("testNewLinkedRecord", doc1);
return RESULT.RECORD_CHANGED;
}
@Override
public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
return null;
}
});
ODocument doc = database.save(new ODocument("test"));
ODocument newRef = doc.field("testNewLinkedRecord");
assertNotNull(newRef);
assertNotNull(newRef.getIdentity().isPersistent());
}
use of com.orientechnologies.orient.core.hook.ORecordHook in project orientdb by orientechnologies.
the class HookSaveTest method testCreatedBackLinkedInHook.
@Test
public void testCreatedBackLinkedInHook() {
database.registerHook(new ORecordHook() {
@Override
public void onUnregister() {
}
@Override
public RESULT onTrigger(TYPE iType, ORecord iRecord) {
if (iType != TYPE.BEFORE_CREATE)
return RESULT.RECORD_NOT_CHANGED;
ODocument doc = (ODocument) iRecord;
if (doc.containsField("test"))
return RESULT.RECORD_NOT_CHANGED;
ODocument doc1 = new ODocument();
doc1.field("test", "value");
doc.field("testNewLinkedRecord", doc1);
doc1.field("backLink", doc);
return RESULT.RECORD_CHANGED;
}
@Override
public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
return null;
}
});
ODocument doc = database.save(new ODocument("test"));
ODocument newRef = doc.field("testNewLinkedRecord");
assertNotNull(newRef);
assertNotNull(newRef.getIdentity().isPersistent());
}
use of com.orientechnologies.orient.core.hook.ORecordHook in project orientdb by orientechnologies.
the class DbImportExportTest method testDbImport.
@Test(dependsOnMethods = "testDbExport")
public void testDbImport() throws IOException {
final File importDir = new File(testPath + "/" + NEW_DB_PATH);
if (importDir.exists())
for (File f : importDir.listFiles()) f.delete();
else
importDir.mkdir();
ODatabaseDocumentTx database = new ODatabaseDocumentTx(getStorageType() + ":" + testPath + "/" + NEW_DB_URL);
database.create();
ODatabaseImport dbImport = new ODatabaseImport(database, testPath + "/" + exportFilePath, this);
// UNREGISTER ALL THE HOOKS
for (ORecordHook hook : new ArrayList<ORecordHook>(database.getHooks().keySet())) {
database.unregisterHook(hook);
}
dbImport.setPreserveRids(true);
dbImport.setDeleteRIDMapping(false);
dbImport.importDatabase();
dbImport.close();
database.close();
}
Aggregations