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 void
changeAttributeName(org.neo4j.graphdb.Node classNode, String oldAttributeName, String newAttributeName)
static void
changeAttributeProperty(org.neo4j.graphdb.Node classNode, String attributeName, String propertyName, Object propertyValue)
static void
changeAttributeTypeIfListType(org.neo4j.graphdb.Node classNode, String attributeName, String newAttributeType)
static void
changeAttributeTypeIfPrimitive(org.neo4j.graphdb.Node classNode, String attributeName, String newAttributeType)
Transactions are not handled herestatic Object
convertIfPossible(Object oldValue, String convertTo)
Tries to convert an attribute value to a new attribute type.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)
Creates a new log entry upon an action performed by an user.Transactions are not managed herestatic void
createAttribute(org.neo4j.graphdb.Node classNode, AttributeMetadata attributeDefinition, boolean recursive)
static AttributeMetadata
createAttributeMetadataFromNode(org.neo4j.graphdb.Node attributeNode)
Converts a attribute metadata node into a AttrributeMetadata objectstatic ClassMetadata
createClassMetadataFromNode(org.neo4j.graphdb.Node classNode)
Converts a class metadata node into a ClassMetadata objectstatic ClassMetadataLight
createClassMetadataLightFromNode(org.neo4j.graphdb.Node classNode)
Converts a class metadata node into a ClassMetadataLight objectstatic GenericObjectList
createGenericObjectListFromNode(org.neo4j.graphdb.Node listTypeNode)
Creates a generic object list (a list type) from a nodestatic GroupProfile
createGroupProfileFromNode(org.neo4j.graphdb.Node groupNode)
Converts a node representing a group into a GroupProfile objectstatic GroupProfileLight
createGroupProfileLightFromNode(org.neo4j.graphdb.Node groupNode)
Converts a node representing a group into a GroupProfileLight objectstatic InventoryObjectPool
createPoolFromNode(org.neo4j.graphdb.Node poolNode)
static Privilege
createPrivilegeFromNode(org.neo4j.graphdb.Node privilegeNode)
static ProcessInstance
createProcessInstanceFromNode(org.neo4j.graphdb.Node processInstanceNode)
static BusinessObjectLight
createRemoteObjectLightFromPoolNode(org.neo4j.graphdb.Node instance)
static InventoryObjectPool
createRemotePoolFromNode(org.neo4j.graphdb.Node instance)
static Task
createTaskFromNode(org.neo4j.graphdb.Node taskNode)
static UserProfileLight
createUserProfileLightFromNode(org.neo4j.graphdb.Node userNode)
Creates a UserProfileLight object (a user object without privileges) from a nodestatic UserProfile
createUserProfileWithGroupPrivilegesFromNode(org.neo4j.graphdb.Node userNode)
Converts a node representing a user into a UserProfile object.static UserProfile
createUserProfileWithoutGroupPrivilegesFromNode(org.neo4j.graphdb.Node userNode)
Converts a node representing a user into a UserProfile object.static void
deleteAttributeIfListType(org.neo4j.graphdb.Node classNode, String attributeName)
static void
deleteAttributeIfPrimitive(org.neo4j.graphdb.Node classNode, String attributeName)
static void
deleteTemplateObject(org.neo4j.graphdb.Node instance)
static void
deleteUserNode(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.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 idstatic 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)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 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.static String
getClassName(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 String
getObjectClassName(org.neo4j.graphdb.Node objectNode)
Gets an object's class name given the node representing itstatic Object
getRealValue(String value, String type)
Converts a String value to an object value based on a give mapping.static String
getTypeOfAttribute(org.neo4j.graphdb.Node classNode, String attributeName)
Gets the type(String, Integer, Float, Boolean, etc) of an attributestatic 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 anotherstatic 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 anotherstatic byte[]
readBytesFromFile(String fileName)
Read and returns the bytes of a given filestatic 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 valuestatic void
saveFile(String directory, String fileName, byte[] content)
Saves a file, receiving the file name and the contents as parameters.static ClassMetadata
setDefaultsForClassMetadatas(ClassMetadata classDefinition)
Creates a ClassMetadata with default values
-
-
-
Method Detail
-
getRealValue
public static Object getRealValue(String value, String type) 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 value- 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:
FileNotFoundException
IOException
-
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) throws InvalidArgumentException
- Parameters:
poolNode
-- 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
Releases 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 administrator
-
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 InvalidArgumentException
Transactions 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.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) throws ApplicationObjectNotFoundException
Creates 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.- 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
-
-