public override Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>> Update(DBContext myDBContext, DBObjectStream myDBObjectStream, GraphDBType myGraphDBType)
        {
            Dictionary<String, Tuple<TypeAttribute, IObject>> attrsForResult = new Dictionary<String, Tuple<TypeAttribute, IObject>>();

            #region AttributeUpdateList

            #region data

            Exceptional validateResult = AttributeIDChain.Validate(myDBContext, false);
            if (validateResult.Failed())
            {
                return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(validateResult);
            }

            TypeAttribute attrDef = AttributeIDChain.LastAttribute;
            IEdgeType elementsToBeAdded;
            EdgeTypeListOfBaseObjects undefAttrList;

            #endregion

            #region undefined attributes

            if (AttributeIDChain.IsUndefinedAttribute)
            {

                if (Assign)
                {
                    undefAttrList = new EdgeTypeListOfBaseObjects();
                }
                else
                {

                    var loadExcept = LoadUndefAttributes(AttributeIDChain.UndefinedAttribute, myDBContext, myDBObjectStream);

                    if (loadExcept.Failed())
                        return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(loadExcept);

                    if (!loadExcept.Value.ContainsKey(AttributeIDChain.UndefinedAttribute))
                    {

                        undefAttrList = new EdgeTypeListOfBaseObjects();

                        var addExcept = myDBContext.DBObjectManager.AddUndefinedAttribute(AttributeIDChain.UndefinedAttribute, undefAttrList, myDBObjectStream);

                        if (addExcept.Failed())
                            return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(addExcept);
                    }

                    else if (!(loadExcept.Value[AttributeIDChain.UndefinedAttribute] is EdgeTypeListOfBaseObjects))
                    {
                        return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(new Error_InvalidAttributeKind());
                    }

                    else
                    {
                        undefAttrList = (EdgeTypeListOfBaseObjects) loadExcept.Value[AttributeIDChain.UndefinedAttribute];
                    }

                }

                var elementsToBeAddedEdge = (IBaseEdge)undefAttrList.GetNewInstance();

                foreach (var tuple in CollectionDefinition.TupleDefinition)
                {
                    if (tuple.Value is ValueDefinition)
                    {
                        var elemToAdd = (tuple.Value as ValueDefinition).Value;
                        ((EdgeTypeListOfBaseObjects)elementsToBeAddedEdge).Add(elemToAdd);
                        undefAttrList.Add(elemToAdd);
                    }
                    else
                    {
                        return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(new Error_NotImplemented(new System.Diagnostics.StackTrace(true)));
                    }
                }

                if (CollectionDefinition.CollectionType == CollectionType.Set)
                    undefAttrList.UnionWith(undefAttrList);

                var addUndefExcept = myDBContext.DBObjectManager.AddUndefinedAttribute(AttributeIDChain.UndefinedAttribute, undefAttrList, myDBObjectStream);

                if (addUndefExcept.Failed())
                    return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(addUndefExcept);

                attrsForResult.Add(AttributeIDChain.UndefinedAttribute, new Tuple<TypeAttribute, IObject>(AttributeIDChain.LastAttribute, elementsToBeAddedEdge));
                return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(attrsForResult);
            }
            #endregion

            #region Validation that this is really a list

            if (attrDef.KindOfType == KindsOfType.SetOfReferences && CollectionDefinition.CollectionType == CollectionType.List)
            {
                return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(new Error_InvalidAssignOfSet(attrDef.Name));
            }

            #endregion

            if (attrDef.KindOfType == KindsOfType.ListOfNoneReferences || attrDef.KindOfType == KindsOfType.SetOfNoneReferences)
            {
                #region ListOfNoneReferences or SetOfNoneReferences - the edge is in this case IBaseEdge

                elementsToBeAdded = ((IEdgeType)AttributeIDChain.LastAttribute.EdgeType.GetNewInstance());
                foreach (var tupleElem in CollectionDefinition.TupleDefinition)
                {
                    (elementsToBeAdded as IBaseEdge).Add(GraphDBTypeMapper.GetGraphObjectFromTypeName(attrDef.GetDBType(myDBContext.DBTypeManager).Name, (tupleElem.Value as ValueDefinition).Value.Value), tupleElem.Parameters.ToArray());
                }

                #endregion
            }
            else
            {
                #region References

                if (CollectionDefinition.CollectionType == CollectionType.SetOfUUIDs)
                {
                    var result = CollectionDefinition.TupleDefinition.GetAsUUIDEdge(myDBContext, attrDef);
                    if (result.Failed())
                    {
                        return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(result);
                    }
                    elementsToBeAdded = (IEdgeType)result.Value;
                }
                else
                {
                    if (AttributeIDChain.LastAttribute.EdgeType == null)
                    {
                        return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(new Error_InvalidEdgeType(AttributeIDChain.LastAttribute.GetType()));
                    }

                    var result = CollectionDefinition.TupleDefinition.GetCorrespondigDBObjectUUIDAsList(myGraphDBType, myDBContext, AttributeIDChain.LastAttribute.EdgeType.GetNewInstance(), AttributeIDChain.LastAttribute.GetDBType(myDBContext.DBTypeManager));
                    if (result.Failed())
                    {
                        return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(result);
                    }
                    elementsToBeAdded = (IEdgeType)result.Value;
                }

                #endregion

            }

            #region add elements

            if (elementsToBeAdded == null)
            {
                return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(new Error_UpdateAttributeNoElements(AttributeIDChain.LastAttribute));
            }

            if (myDBObjectStream.HasAttribute(AttributeIDChain.LastAttribute.UUID, attrDef.GetRelatedType(myDBContext.DBTypeManager)))
            {
                if (Assign)
                {

                    if (elementsToBeAdded is IReferenceEdge)
                    {
                        var oldEdge = ((IListOrSetEdgeType)myDBObjectStream.GetAttribute(AttributeIDChain.LastAttribute.UUID));
                        var removeRefExcept = RemoveBackwardEdgesOnReferences(this, (IReferenceEdge)oldEdge, myDBObjectStream, myDBContext);

                        if (!removeRefExcept.Success())
                        {
                            return new Exceptional<Dictionary<string,Tuple<TypeAttribute,IObject>>>(removeRefExcept.IErrors.First());
                        }
                    }

                    var alterResult = myDBObjectStream.AlterAttribute(AttributeIDChain.LastAttribute.UUID, elementsToBeAdded);
                    if (alterResult.Failed())
                    {
                        return new Exceptional<Dictionary<string, Tuple<TypeAttribute, IObject>>>(alterResult);
                    }
                }
                else
                {
                    ((IListOrSetEdgeType)myDBObjectStream.GetAttribute(AttributeIDChain.LastAttribute.UUID)).UnionWith((IListOrSetEdgeType)elementsToBeAdded);
                }
            }
            else
            {
                myDBObjectStream.AddAttribute(AttributeIDChain.LastAttribute.UUID, elementsToBeAdded);
            }

            #region add backward edges

            if (AttributeIDChain.LastAttribute.GetDBType(myDBContext.DBTypeManager).IsUserDefined)
            {
                Dictionary<AttributeUUID, IObject> userdefinedAttributes = new Dictionary<AttributeUUID, IObject>();
                userdefinedAttributes.Add(AttributeIDChain.LastAttribute.UUID, elementsToBeAdded);

                var omm = new ObjectManipulationManager();
                var setBackEdgesExcept = omm.SetBackwardEdges(myGraphDBType, userdefinedAttributes, myDBObjectStream.ObjectUUID, myDBContext);

                if (setBackEdgesExcept.Failed())
                    return new Exceptional<Dictionary<String, Tuple<TypeAttribute, IObject>>>(setBackEdgesExcept);
            }

            #endregion

            attrsForResult.Add(AttributeIDChain.LastAttribute.Name, new Tuple<TypeAttribute, IObject>(AttributeIDChain.LastAttribute, elementsToBeAdded));
            return new Exceptional<Dictionary<string, Tuple<TypeAttribute, IObject>>>(attrsForResult);

            #endregion

            #endregion
        }
Example #2
0
        internal Exceptional<Tuple<String, TypeAttribute, IObject>> ApplyAssignAttribute(AAttributeAssignOrUpdate myAAttributeAssign, DBContext myDBContext, DBObjectStream myDBObject, GraphDBType myGraphDBType)
        {
            System.Diagnostics.Debug.Assert(myAAttributeAssign != null);

            //get value for assignement
            var aValue = myAAttributeAssign.GetValueForAttribute(myDBObject, myDBContext, myGraphDBType);
            if (aValue.Failed())
            {
                return new Exceptional<Tuple<String, TypeAttribute, IObject>>(aValue);
            }

            object oldValue = null;
            IObject newValue = aValue.Value;

            if (myDBObject.HasAttribute(myAAttributeAssign.AttributeIDChain.LastAttribute.UUID, myGraphDBType))
            {

                #region Update the value because it already exists

                oldValue = myDBObject.GetAttribute(myAAttributeAssign.AttributeIDChain.LastAttribute, myGraphDBType, myDBContext).Value;

                switch (myAAttributeAssign.AttributeIDChain.LastAttribute.KindOfType)
                {
                    case KindsOfType.SetOfReferences:
                        var typeOfCollection = ((AttributeAssignOrUpdateList)myAAttributeAssign).CollectionDefinition.CollectionType;

                        if (typeOfCollection == CollectionType.List)
                            return new Exceptional<Tuple<String, TypeAttribute, IObject>>(new Error_InvalidAssignOfSet(myAAttributeAssign.AttributeIDChain.LastAttribute.Name));

                        var removeRefExcept = RemoveBackwardEdgesOnReferences(myAAttributeAssign, (IReferenceEdge)oldValue, myDBObject, myDBContext);

                        if (!removeRefExcept.Success())
                            return new Exceptional<Tuple<String, TypeAttribute, IObject>>(removeRefExcept.IErrors.First());

                        newValue = (ASetOfReferencesEdgeType)newValue;
                        break;

                    case KindsOfType.SetOfNoneReferences:
                    case KindsOfType.ListOfNoneReferences:
                        newValue = (IBaseEdge)newValue;
                        break;

                    case KindsOfType.SingleNoneReference:
                        if (!(oldValue as ADBBaseObject).IsValidValue((newValue as ADBBaseObject).Value))
                        {
                            return new Exceptional<Tuple<string, TypeAttribute, IObject>>(new Error_DataTypeDoesNotMatch((oldValue as ADBBaseObject).ObjectName, (newValue as ADBBaseObject).ObjectName));
                        }
                        newValue = (oldValue as ADBBaseObject).Clone((newValue as ADBBaseObject).Value);
                        break;

                    case KindsOfType.SingleReference:
                        if (newValue is ASingleReferenceEdgeType)
                        {
                            removeRefExcept = RemoveBackwardEdgesOnReferences(myAAttributeAssign, (IReferenceEdge)oldValue, myDBObject, myDBContext);

                            if (!removeRefExcept.Success())
                                return new Exceptional<Tuple<String, TypeAttribute, IObject>>(removeRefExcept.IErrors.First());

                            ((ASingleReferenceEdgeType)oldValue).Merge((ASingleReferenceEdgeType)newValue);
                            newValue = (ASingleReferenceEdgeType)oldValue;
                        }
                        break;

                    case KindsOfType.SpecialAttribute: // Special attributes can't be updated currently

                        if ((newValue as DBString) != null && (newValue as DBString).CompareTo(oldValue) == 0)
                        {
                            return new Exceptional<Tuple<string, GraphDB.TypeManagement.TypeAttribute, GraphDB.TypeManagement.IObject>>(new Tuple<string, GraphDB.TypeManagement.TypeAttribute, GraphDB.TypeManagement.IObject>(myAAttributeAssign.AttributeIDChain.LastAttribute.Name, myAAttributeAssign.AttributeIDChain.LastAttribute, newValue as IObject));
                        }
                        else
                        {
                            return new Exceptional<Tuple<String, TypeAttribute, IObject>>(new Error_NotImplemented(new System.Diagnostics.StackTrace(true)));
                        }

                        break;

                    default:
                        return new Exceptional<Tuple<String, TypeAttribute, IObject>>(new Error_NotImplemented(new System.Diagnostics.StackTrace(true)));
                }

                #endregion

            }

            var alterExcept = myDBObject.AlterAttribute(myAAttributeAssign.AttributeIDChain.LastAttribute.UUID, newValue);

            if (alterExcept.Failed())
                return new Exceptional<Tuple<string, TypeAttribute, IObject>>(alterExcept);

            if (!alterExcept.Value)
            {
                myDBObject.AddAttribute(myAAttributeAssign.AttributeIDChain.LastAttribute.UUID, newValue);
            }

            #region add backward edges

            if (myAAttributeAssign.AttributeIDChain.LastAttribute.GetDBType(myDBContext.DBTypeManager).IsUserDefined)
            {
                Dictionary<AttributeUUID, IObject> userdefinedAttributes = new Dictionary<AttributeUUID, IObject>();
                userdefinedAttributes.Add(myAAttributeAssign.AttributeIDChain.LastAttribute.UUID, newValue);

                var omm = new ObjectManipulationManager(myDBContext, myGraphDBType);
                var setBackEdges = omm.SetBackwardEdges(userdefinedAttributes, myDBObject.ObjectUUID);

                if (setBackEdges.Failed())
                    return new Exceptional<Tuple<string, TypeAttribute, IObject>>(setBackEdges);
            }

            #endregion

            return new Exceptional<Tuple<String, TypeAttribute, IObject>>(new Tuple<String, TypeAttribute, IObject>(myAAttributeAssign.AttributeIDChain.LastAttribute.Name, myAAttributeAssign.AttributeIDChain.LastAttribute, newValue));
        }