Created
August 7, 2013 08:04
-
-
Save ttiurani/6172154 to your computer and use it in GitHub Desktop.
Logging for UUIDTransactionEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.neo4j.extension.uuid; | |
import com.fasterxml.uuid.Generators; | |
import com.fasterxml.uuid.impl.TimeBasedGenerator; | |
import org.neo4j.graphdb.PropertyContainer; | |
import org.neo4j.graphdb.event.PropertyEntry; | |
import org.neo4j.graphdb.event.TransactionData; | |
import org.neo4j.graphdb.event.TransactionEventHandler; | |
import java.util.UUID; | |
/** | |
* a {@see TransactionEventHandler} that | |
* <ul> | |
* <li>generates UUID properties for each new node and relationship</li> | |
* <li>rejects any modification to pre-existing uuids</li> | |
* </ul> | |
*/ | |
public class UUIDTransactionEventHandler<T> implements TransactionEventHandler<T> { | |
public static final String UUID_PROPERTY_NAME = "uuid"; | |
private final TimeBasedGenerator uuidGenerator = Generators.timeBasedGenerator(); | |
public UUIDTransactionEventHandler(){ | |
System.out.println("UUIDTransactionEventHandler.UUIDTransactionEventHandler"); | |
} | |
@Override | |
public T beforeCommit(TransactionData data) throws Exception { | |
System.out.println("UUIDTransactionEventHandler.beforeCommit start"); | |
checkForUuidChanges(data.removedNodeProperties(), "remove"); | |
checkForUuidChanges(data.assignedNodeProperties(), "assign"); | |
checkForUuidChanges(data.removedRelationshipProperties(), "remove"); | |
checkForUuidChanges(data.assignedRelationshipProperties(), "assign"); | |
populateUuidsFor(data.createdNodes()); | |
populateUuidsFor(data.createdRelationships()); | |
System.out.println("UUIDTransactionEventHandler.beforeCommit end"); | |
return null; | |
} | |
@Override | |
public void afterCommit(TransactionData data, java.lang.Object state) { | |
} | |
@Override | |
public void afterRollback(TransactionData data, java.lang.Object state) { | |
} | |
/** | |
* @param propertyContainers set UUID property for a iterable on nodes or relationships | |
*/ | |
private void populateUuidsFor(Iterable<? extends PropertyContainer> propertyContainers) { | |
System.out.println("UUIDTransactionEventHandler.populateUuidsFor"); | |
for (PropertyContainer propertyContainer : propertyContainers) { | |
if (!propertyContainer.hasProperty(UUID_PROPERTY_NAME)) { | |
final UUID uuid = uuidGenerator.generate(); | |
final StringBuilder sb = new StringBuilder(); | |
sb.append(Long.toHexString(uuid.getMostSignificantBits())).append(Long.toHexString(uuid.getLeastSignificantBits())); | |
propertyContainer.setProperty(UUID_PROPERTY_NAME, sb.toString()); | |
} | |
} | |
} | |
private void checkForUuidChanges(Iterable<? extends PropertyEntry<? extends PropertyContainer>> changeList, String action) { | |
System.out.println("UUIDTransactionEventHandler.checkForUuidChanges"); | |
for (PropertyEntry<? extends PropertyContainer> removedProperty : changeList) { | |
if (removedProperty.key().equals(UUID_PROPERTY_NAME)) { | |
throw new IllegalStateException("you are not allowed to " + action + " " + UUID_PROPERTY_NAME + " properties"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment