Removed dependency over information-system-model-orientdb-binding

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-orientdb-hooks@133726 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-10-28 13:47:22 +00:00
parent 6e86673041
commit 520bbb2a51
2 changed files with 66 additions and 58 deletions

View File

@ -34,11 +34,6 @@
<groupId>org.gcube.information-system</groupId>
<artifactId>information-system-model</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>information-system-model-orientdb-binding</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
</dependencies>

View File

@ -3,16 +3,15 @@
*/
package org.gcube.informationsystem.orientdb.hooks;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.orientdb.impl.embedded.Header;
import org.gcube.informationsystem.model.relation.Relation;
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.db.ODatabaseInternal;
import com.orientechnologies.orient.core.db.ODatabaseLifecycleListener;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
@ -29,20 +28,20 @@ import com.orientechnologies.orient.core.record.impl.ODocument;
public class HeaderHook<OrientGraphNoTx> extends ODocumentHookAbstract
implements ODatabaseLifecycleListener {
protected void init(){
protected void init() {
setIncludeClasses(Entity.NAME, Relation.NAME);
}
@SuppressWarnings("deprecation")
public HeaderHook() {
super();
//System.out.println("HeaderHook()");
// System.out.println("HeaderHook()");
init();
}
public HeaderHook(ODatabaseDocument database) {
super(database);
//System.out.println("HeaderHook(ODatabaseDocument database)");
// System.out.println("HeaderHook(ODatabaseDocument database)");
init();
}
@ -53,67 +52,75 @@ public class HeaderHook<OrientGraphNoTx> extends ODocumentHookAbstract
@Override
public RESULT onRecordBeforeCreate(final ODocument iDocument) {
System.out.println("\n\n--------------\n" + iDocument);
OLogManager.instance().debug(this, "Checking %s on %s",
Header.NAME, iDocument.toJSON());
ODocument oDocument = iDocument.field(Entity.HEADER_PROPERTY);
if(oDocument==null){
System.out.println("Header not present. Going to create it");
Header header = new Header();
if (oDocument == null) {
OLogManager.instance().info(this,
"%s not present. Going to create it on %s",
Header.NAME, iDocument.toJSON());
oDocument = new ODocument(Header.NAME);
UUID uuid = UUID.randomUUID();
System.out.println("Setting generated UUID : " + uuid.toString());
header.setUUID(uuid);
System.out.println("Creator is unknown setting as : " + org.gcube.informationsystem.model.embedded.Header.UNKNOWN_USER);
header.setCreator(org.gcube.informationsystem.model.embedded.Header.UNKNOWN_USER);
Date date = Calendar.getInstance().getTime();
SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Setting Last Update and Creation Time to " + ft.format(date));
header.setCreationTime(date);
header.setLastUpdateTime(date);
System.out.println("Setting newly create header " + header);
iDocument.field(Entity.HEADER_PROPERTY, header);
oDocument.field(Header.UUID_PROPERTY, uuid.toString());
OLogManager.instance().debug(this,
"Creator is unknown setting as %s", Header.UNKNOWN_USER);
oDocument.field(Header.CREATOR_PROPERTY, Header.UNKNOWN_USER);
long timestamp = Calendar.getInstance().getTimeInMillis();
oDocument.field(Header.CREATION_TIME_PROPERTY, timestamp);
oDocument.field(Header.LAST_UPDATE_TIME_PROPERTY, timestamp);
iDocument.field(Entity.HEADER_PROPERTY, oDocument);
OLogManager.instance().info(this,
"%s has now an %s", iDocument.toJSON(), Header.NAME);
return RESULT.RECORD_CHANGED;
}else{
System.out.println("Header already present : " + oDocument);
} else {
OLogManager.instance().info(this, "%s already present on %s",
Header.NAME, iDocument.toJSON());
return RESULT.RECORD_NOT_CHANGED;
}
}
@Override
public RESULT onRecordBeforeUpdate(final ODocument iDocument) {
System.out.println("\n\n--------------\nUpdating Last Update Time");
OLogManager.instance().debug(this, "Updating Last Update Time on %s of %s",
Header.NAME, iDocument.toJSON());
ODocument oDocument = iDocument.field(Entity.HEADER_PROPERTY);
long timestamp = System.currentTimeMillis();
System.out.println("Updating Last Update Time in header of " + iDocument + " to " + timestamp);
oDocument.field(org.gcube.informationsystem.model.embedded.Header.LAST_UPDATE_TIME_PROPERTY, timestamp);
System.out.println("Updated Document is " + iDocument);
Calendar calendar = Calendar.getInstance();
long timestamp = calendar.getTimeInMillis();
oDocument.field(Header.LAST_UPDATE_TIME_PROPERTY, timestamp);
OLogManager.instance().info(this, "Updated Document is %s", iDocument);
String entityType = iDocument.getClassName();
System.out.println("Document Type is " + entityType);
OMetadata oMetadata = database.getMetadata();
OSchema oSchema = oMetadata.getSchema();
OClass oClass = oSchema.getClass(entityType);
if(oClass.isSubClassOf(Facet.NAME)){
System.out.println("Updating a Facet. Also Attached Resources Last Update Time MUST be updated to " + timestamp);
// TODO
if (oClass.isSubClassOf(Facet.NAME)) {
OLogManager.instance().warn(this,
"TODO : Also Last Update Time of Resources pointing to this Facet MUST be updated to %T",
calendar);
// TODO
// Get all Resources attached to this Facet and set last update time
}
return RESULT.RECORD_CHANGED;
}
@Override
public PRIORITY getPriority() { return PRIORITY.REGULAR; }
@Override
public void onCreate(@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase) {
public PRIORITY getPriority() {
return PRIORITY.REGULAR;
}
@Override
public void onCreate(
@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase) {
// REGISTER THE HOOK
iDatabase.registerHook(this);
}
@ -125,25 +132,31 @@ public class HeaderHook<OrientGraphNoTx> extends ODocumentHookAbstract
}
@Override
public void onClose(@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase) {
public void onClose(
@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase) {
// REGISTER THE HOOK
iDatabase.unregisterHook(this);
iDatabase.unregisterHook(this);
}
@Override
public void onDrop(@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase) {
iDatabase.unregisterHook(this);
}
@Override
public void onCreateClass(@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase, OClass iClass) {
public void onCreateClass(
@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase,
OClass iClass) {
}
@Override
public void onDropClass(@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase, OClass iClass) {
public void onDropClass(
@SuppressWarnings("rawtypes") ODatabaseInternal iDatabase,
OClass iClass) {
}
@Override
public void onLocalNodeConfigurationRequest(ODocument iConfiguration) {
}
}