/// <summary> /// This method is used for transforming Node into NodeWrapper. /// </summary> /// <param name="node"> Node that need to be tranformed into NodeWrapper</param> /// <returns>NodeWrapper that is being result of transformation of Node</returns> public static NodeWrapper TransformeNode(Node<Guid, object, EdgeData> node) { if (node == null) { return null; } NodeWrapper newNode = new NodeWrapper(); newNode.Commited = node.Commited; newNode.NodeType = node.NodeType; newNode.Previous = node.Previous; ObjectWrapper newData = null; if (node.NodeType == NodeType.Type) { //getting name of type of node data. newData = ObjectWrapper.CreateObjectWrapper(node.Data); //this is needed because data contains string that is contain data that //identifies type in .NET. Because of that we need to change type of objectWrapper newData.TypeName = TypeSingleton.ExtractPropertyType(Type.GetType((String)node.Data)); newNode.Data = newData; } else { //getting type name of data, in case node.Data is null type will be null newData = ObjectWrapper.CreateObjectWrapper(node.Data); newNode.Data = newData; } foreach (KeyValuePair<Guid, object> item in node.Values) { //making ObjectWrapper from object ObjectWrapper ow = ObjectWrapper.CreateObjectWrapper(item.Value); //adding new Guid-ObjectWrapper pair into Values newNode.Values.Add(item.Key.ToString(), ow); } foreach (KeyValuePair<EdgeData, Edge<Guid, EdgeData>> item in node.Edges) { //getting type of key and value Type typeOfObject = item.Key.GetType(); Type edgeDataType = typeof(EdgeData); if (typeOfObject.Equals(edgeDataType)) { EdgeDataWrapper edgeDataKey = EdgeDataWrapper.TransformeEdgeData(item.Key); EdgeWrapper edgeWraper = EdgeWrapper.TransformeEdge(item.Value); newNode.Edges.Add(new KeyValueWrapper(edgeDataKey, edgeWraper)); } else { throw new Exception("Unexpected type in node wraper!"); } } return newNode; }
/// <summary> /// Method is used for parsing Nodes in change set. /// </summary> /// <param name="dictForParsing">Dictionary from ChangeSet which should contain Nodes</param> /// <returns></returns> private static DirectNodeProviderSafe <Guid, object, EdgeData> ParseNodes(Dictionary <String, Object> dictForParsing) { DirectNodeProviderSafe <Guid, object, EdgeData> nodes = new DirectNodeProviderSafe <Guid, object, EdgeData>(new MemoryStorageSafe <Guid, object>(), false); Dictionary <String, Object> dictFromObject = dictForParsing; //parsing nodes from objectForParsing if (dictFromObject.ContainsKey(NODES)) { Object oNodes = dictFromObject[NODES]; if (oNodes is Dictionary <String, Object> ) { Dictionary <String, Object> dNodes = oNodes as Dictionary <String, Object>; if (dNodes.ContainsKey(STORAGE)) { Object oStorage = dNodes[STORAGE]; if (oStorage is Dictionary <String, Object> ) { Dictionary <String, Object> dStorage = oStorage as Dictionary <String, Object>; Object oDictionary = dStorage[DICTIONARY]; if (oDictionary is Dictionary <String, Object> ) { Dictionary <String, Object> dDictionary = oDictionary as Dictionary <String, Object>; Object oArray = dDictionary[ARRAY]; if (oArray is Object[]) { Object[] dArray = oArray as Object[]; foreach (Object o in dArray) { if (o is Dictionary <String, Object> ) { Dictionary <String, Object> dNode = o as Dictionary <String, Object>; if (dNode.ContainsKey(KEY) && dNode.ContainsKey(VALUE)) { Guid key = Guid.Empty; if (dNode[KEY] is Dictionary <String, Object> ) { Dictionary <String, Object> dKey = (dNode[KEY] as Dictionary <String, Object>); if (dKey.ContainsKey(VALUE) && dKey[VALUE] is String) { key = new Guid((String)dKey[VALUE]); } else { //case when there is not right properties in Dictionary continue; } } else { //case when dNode[KEY] is not Dictionary continue; } Node <Guid, object, EdgeData> node = NodeWrapper.ParseNode(dNode[VALUE]); nodes.SetNode(key, node); } } else { continue; } } } } } } } } else { throw new Exception("Node object is not valid!"); } return(nodes); }
/// <summary> /// ParseNode is method that is trying to transform an object into Node object /// </summary> /// <param name="objectToParse"> object that needs to be transformed</param> /// <returns>Node object or is throwing an Exception</returns> public static Node <Guid, Object, EdgeData> ParseNode(Object objectToParse) { if (objectToParse is Dictionary <String, Object> ) { Dictionary <String, Object> nodeRepresentation = objectToParse as Dictionary <String, Object>; //checking if there is everything in objectToParse that should be there if (nodeRepresentation.ContainsKey(PREVIOUS) && nodeRepresentation[PREVIOUS] is Dictionary <string, object> && nodeRepresentation.ContainsKey(COMMITED) && nodeRepresentation[COMMITED] is Boolean && nodeRepresentation.ContainsKey(NODE_TYPE) && nodeRepresentation[NODE_TYPE] is int && nodeRepresentation.ContainsKey(DATA) && nodeRepresentation.ContainsKey(VALUES) && nodeRepresentation[VALUES] is Dictionary <String, Object> && nodeRepresentation.ContainsKey(EDGES)) { //getting Guid of previous Node Dictionary <string, object> previousDict = nodeRepresentation[PREVIOUS] as Dictionary <string, object>; if (previousDict.ContainsKey(VALUE) && previousDict[VALUE] is String) { Guid previous = new Guid(previousDict[VALUE] as String); } else { throw new Exception("Object is not formated in right way! Previous Guid is missing!"); } Boolean commited = (Boolean)nodeRepresentation[COMMITED]; NodeType nodeType = (NodeType)((int)nodeRepresentation[NODE_TYPE]); Object data = ObjectWrapper.ParseObjectWrapper(nodeRepresentation[DATA]); Dictionary <Guid, Object> values = new Dictionary <Guid, object>(); //getting Dictionary that is contining Values dictionary Dictionary <String, Object> valuesDict = (Dictionary <String, Object>)nodeRepresentation[VALUES]; if (valuesDict.ContainsKey(ARRAY) && valuesDict[ARRAY] is Object[]) { Object[] tempList = (Object[])valuesDict[ARRAY]; foreach (Object o in tempList) { if (o is Dictionary <String, object> ) { Dictionary <String, object> tempDict = o as Dictionary <String, object>; if (tempDict.ContainsKey(KEY) && tempDict.ContainsKey(VALUE)) { Guid key = (Guid)ObjectWrapper.ParseObjectWrapper(tempDict[KEY]); Object value = ObjectWrapper.ParseObjectWrapper(tempDict[VALUE]); values.Add(key, value); } } else { throw new Exception("Object is not formated in right way!"); } } } else { throw new Exception("Object is not formated in right way!"); } Object edges = nodeRepresentation[EDGES]; SortedList <EdgeData, Edge <Guid, EdgeData> > edgesSortedList = NodeWrapper.ParseEdges(edges); Node <Guid, Object, EdgeData> serverNode = new Node <Guid, object, EdgeData>(nodeType, data, edgesSortedList, values); return(serverNode); } else { throw new Exception("Object is not formated in right way!"); } } else { throw new Exception("Object is not formated in right way!"); } }