Class Util
- java.lang.Object
-
- org.neotropic.kuwaiba.core.persistence.reference.neo4j.util.Util
-
public class Util extends Object
Utility class containing misc methods to perform common tasks- Author:
- Charles Edward Bedon Cortazar <charles.bedon@kuwaiba.org>
-
-
Constructor Summary
Constructors Constructor Description Util()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidchangeAttributeName(org.neo4j.graphdb.Node classNode, String oldAttributeName, String newAttributeName)static voidchangeAttributeProperty(org.neo4j.graphdb.GraphDatabaseService graphDb, org.neo4j.graphdb.Node classNode, String attributeName, String propertyName, Object propertyValue)static voidchangeAttributeTypeIfListType(org.neo4j.graphdb.Node classNode, String attributeName, String newAttributeType)static voidchangeAttributeTypeIfPrimitive(org.neo4j.graphdb.Node classNode, String attributeName, String newAttributeType)Transactions are not handled herestatic ObjectconvertIfPossible(Object oldValue, String convertTo)Tries to convert an attribute value to a new attribute type.static org.neo4j.graphdb.NodecreateActivityLogEntry(org.neo4j.graphdb.Node objectNode, String userName, int type, long timestamp, String affectedProperty, String oldValue, String newValue, String notes, org.neo4j.graphdb.GraphDatabaseService graphDb, TranslationService ts)Creates a new log entry upon an action performed by an user.Transactions are not managed herestatic voidcreateAttribute(org.neo4j.graphdb.Node classNode, AttributeMetadata attributeDefinition, boolean recursive)static AttributeMetadatacreateAttributeMetadataFromNode(org.neo4j.graphdb.Node attributeNode)Converts a attribute metadata node into a AttrributeMetadata objectstatic ClassMetadatacreateClassMetadataFromNode(org.neo4j.graphdb.Node classNode)Converts a class metadata node into a ClassMetadata objectstatic ClassMetadataLightcreateClassMetadataLightFromNode(org.neo4j.graphdb.Node classNode)Converts a class metadata node into a ClassMetadataLight objectstatic GenericObjectListcreateGenericObjectListFromNode(org.neo4j.graphdb.Node listTypeNode)Creates a generic object list (a list type) from a nodestatic GroupProfilecreateGroupProfileFromNode(org.neo4j.graphdb.Node groupNode)Converts a node representing a group into a GroupProfile objectstatic GroupProfileLightcreateGroupProfileLightFromNode(org.neo4j.graphdb.Node groupNode)Converts a node representing a group into a GroupProfileLight objectstatic InventoryObjectPoolcreatePoolFromNode(org.neo4j.graphdb.Node poolNode, TranslationService ts)static PrivilegecreatePrivilegeFromNode(org.neo4j.graphdb.Node privilegeNode)static ProcessInstancecreateProcessInstanceFromNode(org.neo4j.graphdb.Node processInstanceNode)static BusinessObjectLightcreateRemoteObjectLightFromPoolNode(org.neo4j.graphdb.Node instance)static InventoryObjectPoolcreateRemotePoolFromNode(org.neo4j.graphdb.Node instance)static TaskcreateTaskFromNode(org.neo4j.graphdb.Node taskNode)static UserProfileLightcreateUserProfileLightFromNode(org.neo4j.graphdb.Node userNode)Creates a UserProfileLight object (a user object without privileges) from a nodestatic UserProfilecreateUserProfileWithGroupPrivilegesFromNode(org.neo4j.graphdb.Node userNode)Converts a node representing a user into a UserProfile object.static UserProfilecreateUserProfileWithoutGroupPrivilegesFromNode(org.neo4j.graphdb.Node userNode)Converts a node representing a user into a UserProfile object.static voiddeleteAttributeIfListType(org.neo4j.graphdb.Node classNode, String attributeName)static voiddeleteAttributeIfPrimitive(org.neo4j.graphdb.Node classNode, String attributeName)static voiddeleteTemplateObject(org.neo4j.graphdb.Node instance)static voiddeleteUserNode(org.neo4j.graphdb.Node userNode)Releases all the relationships associated to a user, and deletes the node corresponding to such user.static org.neo4j.graphdb.NodefindNodeByLabelAndId(org.neo4j.graphdb.GraphDatabaseService graphDb, org.neo4j.graphdb.Label label, long id)Finds a node tagged with a label and with a particular idstatic org.neo4j.graphdb.NodefindNodeByLabelAndUuid(org.neo4j.graphdb.GraphDatabaseService graphDb, org.neo4j.graphdb.Label label, String id)static StringformatObjectList(List<BusinessObjectLight> objectList, boolean startFromTheLast, int howManyToShow)Outputs as a string a list of inventory objects (usually a list of parents in the containment hierarchy)static Iterable<org.neo4j.graphdb.Node>getAllSubclasses(org.neo4j.graphdb.Node classMetadata)Retrieves the subclasses of a given class metadata node within the class hierarchystatic StringgetAttributeFromNode(org.neo4j.graphdb.Node objectNode, String attribute)Retrieves a String with the property value of the attribute if exists as attribute of the node, if the property is a date it is formating into yyyy-MM-DD, if does not exists it return an empty string.static StringgetClassName(org.neo4j.graphdb.Node instance)Gets the class name of a given object given its respective nodestatic List<org.neo4j.graphdb.Node>getListTypeItemNodes(org.neo4j.graphdb.Node listTypeClassNode, List<String> listTypeIds)Gets the list type items nodes instances of the provided class and idsstatic StringgetObjectClassName(org.neo4j.graphdb.Node objectNode)Gets an object's class name given the node representing itstatic ObjectgetRealValue(String value, String type, TranslationService ts)Converts a String value to an object value based on a give mapping.static StringgetTypeOfAttribute(org.neo4j.graphdb.Node classNode, String attributeName)Gets the type(String, Integer, Float, Boolean, etc) of an attributestatic booleanisPossibleChild(String allegedParentClass, org.neo4j.graphdb.Node currentNode)Traverses the graph up into the class hierarchy trying to find out if a given class is the possible child of anotherstatic booleanisSubclassOf(String allegedParentClass, org.neo4j.graphdb.Node currentNode)Traverses the graph up into the class hierarchy trying to find out if a given class is the subclass of anotherstatic byte[]readBytesFromFile(String fileName)Read and returns the bytes of a given filestatic voidreleaseRelationships(org.neo4j.graphdb.Node instance, RelTypes relationshipType, org.neo4j.graphdb.Direction relationshipDirection, String propertyName, String propertyValue)Releases all relationships related to an object given its direction and a relationship's property valuestatic voidsaveFile(String directory, String fileName, byte[] content)Saves a file, receiving the file name and the contents as parameters.static ClassMetadatasetDefaultsForClassMetadatas(ClassMetadata classDefinition)Creates a ClassMetadata with default values
-
-
-
Method Detail
-
getRealValue
public static Object getRealValue(String value, String type, TranslationService ts) throws InvalidArgumentException
Converts a String value to an object value based on a give mapping. This method does not convert binary or relationship-like attributes- Parameters:
value- Value as Stringtype- The alleged type of the provided valuets- A reference to the translation service.- Returns:
- the converted value
- Throws:
InvalidArgumentException- If the type can't be converted
-
getListTypeItemNodes
public static List<org.neo4j.graphdb.Node> getListTypeItemNodes(org.neo4j.graphdb.Node listTypeClassNode, List<String> listTypeIds)
Gets the list type items nodes instances of the provided class and ids- Parameters:
listTypeIds- The ids of the list type items nodes to findlistTypeClassNode- Node the list items are supposed to be instance of- Returns:
- The corresponding list type item nodes. The ones not found will be ignored
-
releaseRelationships
public static void releaseRelationships(org.neo4j.graphdb.Node instance, RelTypes relationshipType, org.neo4j.graphdb.Direction relationshipDirection, String propertyName, String propertyValue)Releases all relationships related to an object given its direction and a relationship's property value- Parameters:
instance- Object from/to the relationships are connectedrelationshipType- Relationship typerelationshipDirection- Relationship DirectionpropertyName- Relationship's property to be used as filterpropertyValue- Relationship's property value to be used as filter
-
deleteTemplateObject
public static void deleteTemplateObject(org.neo4j.graphdb.Node instance)
-
readBytesFromFile
public static byte[] readBytesFromFile(String fileName) throws FileNotFoundException, IOException
Read and returns the bytes of a given file- Parameters:
fileName- file to be opened- Returns:
- bytes on that file
- Throws:
FileNotFoundException- If the file could not be foundIOException
-
saveFile
public static void saveFile(String directory, String fileName, byte[] content) throws FileNotFoundException, IOException
Saves a file, receiving the file name and the contents as parameters. If the directory structure doesn't exist, it's created- Parameters:
directory- path to the directoryfileName- the file namecontent- the file content- Throws:
FileNotFoundExceptionIOException
-
getObjectClassName
public static String getObjectClassName(org.neo4j.graphdb.Node objectNode) throws MetadataObjectNotFoundException, UnsupportedPropertyException
Gets an object's class name given the node representing it- Parameters:
objectNode- The node to e evaluated- Returns:
- The object's class name.
- Throws:
MetadataObjectNotFoundException- if no class node is associated to this node (this should not happen)UnsupportedPropertyException- if the class node is malformed
-
setDefaultsForClassMetadatas
public static ClassMetadata setDefaultsForClassMetadatas(ClassMetadata classDefinition) throws MetadataObjectNotFoundException
Creates a ClassMetadata with default values- Parameters:
classDefinition-- Returns:
- Throws:
MetadataObjectNotFoundException- If the class does not have a name
-
createClassMetadataLightFromNode
public static ClassMetadataLight createClassMetadataLightFromNode(org.neo4j.graphdb.Node classNode)
Converts a class metadata node into a ClassMetadataLight object- Parameters:
classNode- the class Node- Returns:
- a class metadata light object
-
createClassMetadataFromNode
public static ClassMetadata createClassMetadataFromNode(org.neo4j.graphdb.Node classNode)
Converts a class metadata node into a ClassMetadata object- Parameters:
classNode- a class metadata node- Returns:
- a class metadata object
-
createAttributeMetadataFromNode
public static AttributeMetadata createAttributeMetadataFromNode(org.neo4j.graphdb.Node attributeNode)
Converts a attribute metadata node into a AttrributeMetadata object- Parameters:
attributeNode-- Returns:
- the attribute as an instance of AttributeMetada class
-
createRemotePoolFromNode
public static InventoryObjectPool createRemotePoolFromNode(org.neo4j.graphdb.Node instance) throws InvalidArgumentException
- Throws:
InvalidArgumentException
-
createRemoteObjectLightFromPoolNode
public static BusinessObjectLight createRemoteObjectLightFromPoolNode(org.neo4j.graphdb.Node instance)
-
createPoolFromNode
public static InventoryObjectPool createPoolFromNode(org.neo4j.graphdb.Node poolNode, TranslationService ts) throws InvalidArgumentException
- Parameters:
poolNode-ts- A reference to the translation service.- Returns:
- Throws:
InvalidArgumentException
-
createTaskFromNode
public static Task createTaskFromNode(org.neo4j.graphdb.Node taskNode)
-
createProcessInstanceFromNode
public static ProcessInstance createProcessInstanceFromNode(org.neo4j.graphdb.Node processInstanceNode)
-
createUserProfileLightFromNode
public static UserProfileLight createUserProfileLightFromNode(org.neo4j.graphdb.Node userNode)
Creates a UserProfileLight object (a user object without privileges) from a node- Parameters:
userNode- The source user node- Returns:
- The UserProfileLight object built with the information of the source node
-
createUserProfileWithoutGroupPrivilegesFromNode
public static UserProfile createUserProfileWithoutGroupPrivilegesFromNode(org.neo4j.graphdb.Node userNode)
Converts a node representing a user into a UserProfile object. The privileges inherited from the group will not be taken into account. See alsocreateUserProfileFromNode.- Parameters:
userNode- The source user node- Returns:
- UserProfile The UserProfile object built with the information of the source node
-
createUserProfileWithGroupPrivilegesFromNode
public static UserProfile createUserProfileWithGroupPrivilegesFromNode(org.neo4j.graphdb.Node userNode)
Converts a node representing a user into a UserProfile object. The privileges inherited from the group will be computed. Note that the user privileges override the group privileges with the same feature token. See alsocreateUserProfileWithoutGroupPrivilegesFromNode.- Parameters:
userNode- The source user node- Returns:
- UserProfile The UserProfile object built with the information of the source node
-
deleteUserNode
public static void deleteUserNode(org.neo4j.graphdb.Node userNode) throws InvalidArgumentException, OperationNotPermittedExceptionReleases all the relationships associated to a user, and deletes the node corresponding to such user. The user "admin" can not be deleted. Historical entry associated to the user to be deleted are NOT deleted, so they can be edited later if necessary.- Parameters:
userNode- The user node.- Throws:
InvalidArgumentException- If you try to delete the default administratorOperationNotPermittedException
-
createGroupProfileFromNode
public static GroupProfile createGroupProfileFromNode(org.neo4j.graphdb.Node groupNode)
Converts a node representing a group into a GroupProfile object- Parameters:
groupNode- The source node- Returns:
- A GroupProfile object built from the source node information
-
createGroupProfileLightFromNode
public static GroupProfileLight createGroupProfileLightFromNode(org.neo4j.graphdb.Node groupNode)
Converts a node representing a group into a GroupProfileLight object- Parameters:
groupNode- The source node- Returns:
- A GroupProfileLight object built from the source node information
-
createPrivilegeFromNode
public static Privilege createPrivilegeFromNode(org.neo4j.graphdb.Node privilegeNode)
-
createGenericObjectListFromNode
public static GenericObjectList createGenericObjectListFromNode(org.neo4j.graphdb.Node listTypeNode) throws InvalidArgumentException
Creates a generic object list (a list type) from a node- Parameters:
listTypeNode- the list type node- Returns:
- a list type The specified list type node is malformed and lacks uuid property.
- Throws:
InvalidArgumentException
-
isSubclassOf
public static boolean isSubclassOf(String allegedParentClass, org.neo4j.graphdb.Node currentNode)
Traverses the graph up into the class hierarchy trying to find out if a given class is the subclass of another- Parameters:
allegedParentClass- The alleged parent class namecurrentNode-- Returns:
-
isPossibleChild
public static boolean isPossibleChild(String allegedParentClass, org.neo4j.graphdb.Node currentNode)
Traverses the graph up into the class hierarchy trying to find out if a given class is the possible child of another- Parameters:
allegedParentClass- The alleged parent class namecurrentNode-- Returns:
-
getAllSubclasses
public static Iterable<org.neo4j.graphdb.Node> getAllSubclasses(org.neo4j.graphdb.Node classMetadata)
Retrieves the subclasses of a given class metadata node within the class hierarchy- Parameters:
classMetadata- The parent class metadata- Returns:
- The root node of the list of class metadata nodes
-
getAttributeFromNode
public static String getAttributeFromNode(org.neo4j.graphdb.Node objectNode, String attribute)
Retrieves a String with the property value of the attribute if exists as attribute of the node, if the property is a date it is formating into yyyy-MM-DD, if does not exists it return an empty string.- Parameters:
objectNode- The object nodeattribute- The name of the attribute. This works only for primitive types- Returns:
- The string representation of the value of the given attribute
-
getTypeOfAttribute
public static String getTypeOfAttribute(org.neo4j.graphdb.Node classNode, String attributeName)
Gets the type(String, Integer, Float, Boolean, etc) of an attribute- Parameters:
classNode-attributeName-- Returns:
- A string with the type of the attribute
-
getClassName
public static String getClassName(org.neo4j.graphdb.Node instance)
Gets the class name of a given object given its respective node- Parameters:
instance- the node to be tested- Returns:
- The object class name. Null if none
-
createAttribute
public static void createAttribute(org.neo4j.graphdb.Node classNode, AttributeMetadata attributeDefinition, boolean recursive) throws InvalidArgumentException- Throws:
InvalidArgumentException
-
changeAttributeTypeIfPrimitive
public static void changeAttributeTypeIfPrimitive(org.neo4j.graphdb.Node classNode, String attributeName, String newAttributeType) throws InvalidArgumentExceptionTransactions are not handled here- Parameters:
classNode-attributeName-newAttributeType-- Throws:
InvalidArgumentException
-
changeAttributeTypeIfListType
public static void changeAttributeTypeIfListType(org.neo4j.graphdb.Node classNode, String attributeName, String newAttributeType) throws InvalidArgumentException- Throws:
InvalidArgumentException
-
changeAttributeProperty
public static void changeAttributeProperty(org.neo4j.graphdb.GraphDatabaseService graphDb, org.neo4j.graphdb.Node classNode, String attributeName, String propertyName, Object propertyValue)
-
changeAttributeName
public static void changeAttributeName(org.neo4j.graphdb.Node classNode, String oldAttributeName, String newAttributeName)- Parameters:
classNode-oldAttributeName-newAttributeName-
-
deleteAttributeIfPrimitive
public static void deleteAttributeIfPrimitive(org.neo4j.graphdb.Node classNode, String attributeName)
-
deleteAttributeIfListType
public static void deleteAttributeIfListType(org.neo4j.graphdb.Node classNode, String attributeName)
-
createActivityLogEntry
public static org.neo4j.graphdb.Node createActivityLogEntry(org.neo4j.graphdb.Node objectNode, String userName, int type, long timestamp, String affectedProperty, String oldValue, String newValue, String notes, org.neo4j.graphdb.GraphDatabaseService graphDb, TranslationService ts) throws ApplicationObjectNotFoundExceptionCreates a new log entry upon an action performed by an user.Transactions are not managed here- Parameters:
objectNode- The node corresponding to the object that was affected by the action. Null if it's a general activity log entry (that is, it's not related to any specific object)userName- User that performed the actiontype-timestamp-notes-oldValue-newValue-affectedProperty-graphDb- A reference to the connection handler.ts- A reference to the translation service.- Returns:
- Throws:
ApplicationObjectNotFoundException- If the user or the root of all log entries can't be found
-
convertIfPossible
public static Object convertIfPossible(Object oldValue, String convertTo) throws InvalidArgumentException
Tries to convert an attribute value to a new attribute type. It only works with primitive types String, Integer, Float, Boolean, Long, Date and Timestamp- Parameters:
oldValue- The old valueconvertTo- The type we want to convert the old value to- Returns:
- The converted value
- Throws:
InvalidArgumentException- If it's not possible to perform the conversion
-
formatObjectList
public static String formatObjectList(List<BusinessObjectLight> objectList, boolean startFromTheLast, int howManyToShow)
Outputs as a string a list of inventory objects (usually a list of parents in the containment hierarchy)- Parameters:
objectList- The list of objectsstartFromTheLast- The output string should start from the first or the last object?howManyToShow- How many elements should be displayed? used -1 to show all- Returns:
- A string with the names of the objects concatenated with a "/" as separator
-
findNodeByLabelAndId
public static org.neo4j.graphdb.Node findNodeByLabelAndId(org.neo4j.graphdb.GraphDatabaseService graphDb, org.neo4j.graphdb.Label label, long id)Finds a node tagged with a label and with a particular id- Parameters:
graphDb- The graphdb handler.label- The label used to tag the nodeid- The id of the node to find- Returns:
- The node or null if no node with with that label and id could be found
-
findNodeByLabelAndUuid
public static org.neo4j.graphdb.Node findNodeByLabelAndUuid(org.neo4j.graphdb.GraphDatabaseService graphDb, org.neo4j.graphdb.Label label, String id)
-
-