public void ProcessOp(NetworkOperation op, Dictionary<string, Node> _nodeElements, Dictionary<string, Section> _sectionElements) { switch (op.action) { case NetworkAction.Add: { if (typeof(Node) == op.newObj.GetType()) { if (!_nodeElements.ContainsKey(((Node)op.newObj).uid)) { _nodeElements.Add(((Node)op.newObj).uid, (Node)op.newObj); } } else if (typeof(Section) == op.newObj.GetType()) { Section newsect = ((Section)op.newObj); if(_nodeElements.ContainsValue(newsect.parentNode)) { Node pnd = newsect.parentNode; if (!pnd.childList.ContainsKey(newsect.uid)) { pnd.childList.Add(newsect.uid, newsect); } Node cnd = newsect.childNode; if (!cnd.parentList.ContainsKey(newsect.uid)) { cnd.parentList.Add(newsect.uid, newsect); } } _sectionElements.Add(((Section)op.newObj).uid, (Section)op.newObj); } break; } case NetworkAction.Delete: { if (typeof(Node) == op.oldObj.GetType()) { if (_nodeElements.ContainsKey(((Node)op.oldObj).uid)) { Node nddel = ((Node)op.oldObj); foreach (Section item in nddel.parentList.Values) { item.childNode = null; } foreach (Section item in nddel.childList.Values) { item.parentNode = null; } _nodeElements.Remove(((Node)op.oldObj).uid); } } else if (typeof(Section) == op.oldObj.GetType()) { Section oldsect = ((Section)op.oldObj); if (_nodeElements.ContainsValue(oldsect.parentNode)) { Node pnd = oldsect.parentNode; if (pnd.childList.ContainsKey(oldsect.uid)) { pnd.childList.Remove(oldsect.uid); } Node cnd = oldsect.childNode; if (cnd.parentList.ContainsKey(oldsect.uid)) { cnd.parentList.Remove(oldsect.uid); } } if (_sectionElements.ContainsKey(((Section)op.oldObj).uid)) _sectionElements.Remove(((Section)op.oldObj).uid); } break; } case NetworkAction.Update: { if (typeof(Node) == op.oldObj.GetType()) { if (_nodeElements.ContainsKey(((Node)op.oldObj).uid)) { Node newNode = (Node)(op.newObj); _nodeElements[(((Node)op.oldObj).uid)] = (Node)(op.newObj); Node ndOld = ((Node)op.oldObj); foreach (Section item in ndOld.parentList.Values) { item.childNode = newNode; } foreach (Section item in ndOld.childList.Values) { item.parentNode = newNode; } } } else if (typeof(Section) == op.oldObj.GetType()) { Section oldsect = ((Section)op.oldObj); Section newSect = (Section)(op.newObj); if (_nodeElements.ContainsValue(oldsect.parentNode)) { Node pnd = oldsect.parentNode; if (pnd.childList.ContainsKey(oldsect.uid)) { pnd.childList[oldsect.uid] = newSect; } Node cnd = oldsect.childNode; if (cnd.parentList.ContainsKey(oldsect.uid)) { cnd.parentList[oldsect.uid] = newSect; } } if (_sectionElements.ContainsKey(((Section)op.oldObj).uid)) _sectionElements[((Section)op.oldObj).uid] = (Section)(op.newObj); } break; } } }
public NetworkOperation ReverseOp(NetworkOperation op) { NetworkOperation newOp = new NetworkOperation(); switch (op.action) { case NetworkAction.Add: { newOp.action = NetworkAction.Delete; newOp.oldObj = op.newObj; newOp.newObj = null; break; } case NetworkAction.Delete: { newOp.action = NetworkAction.Add; newOp.newObj = op.oldObj; newOp.oldObj = null; break; } case NetworkAction.Update: { newOp.action = NetworkAction.Update; newOp.newObj = op.oldObj; newOp.oldObj = op.newObj; break; } case NetworkAction.None: newOp.action = NetworkAction.None; break; } return newOp; }