Ejemplo n.º 1
0
        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
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates IndexKeys from a DBObject.
        /// </summary>
        /// <param name="myDBObject">The DBObject reference for the resulting IndexKeys</param>
        /// <param name="myTypeOfDBObject">The Type of the DBObject</param>
        /// <param name="myToken">The SessionInfos</param>
        /// <returns>A HashSet of IndexKeys</returns>
        private HashSet<IndexKey> GetIndexkeysFromDBObject(DBObjectStream myDBObject, GraphDBType myTypeOfDBObject, DBContext dbContext)
        {
            HashSet<IndexKey> result = new HashSet<IndexKey>();
            TypeAttribute currentAttribute;

            foreach (var aIndexAttributeUUID in IndexKeyDefinition.IndexKeyAttributeUUIDs)
            {
                currentAttribute = myTypeOfDBObject.GetTypeAttributeByUUID(aIndexAttributeUUID);

                if (!currentAttribute.GetDBType(dbContext.DBTypeManager).IsUserDefined)
                {
                    #region base attribute

                    if (myDBObject.HasAttribute(aIndexAttributeUUID, myTypeOfDBObject))
                    {
                        ADBBaseObject newIndexKeyItem = null;

                        switch (currentAttribute.KindOfType)
                        {
                            #region List/Set

                            case KindsOfType.ListOfNoneReferences:
                            case KindsOfType.SetOfNoneReferences:

                                var helperSet = new List<ADBBaseObject>();

                                foreach (var aBaseObject in ((IBaseEdge)myDBObject.GetAttribute(aIndexAttributeUUID, myTypeOfDBObject, dbContext)).GetBaseObjects())
                                {
                                    helperSet.Add((ADBBaseObject)aBaseObject);
                                }

                                if (result.Count != 0)
                                {
                                    #region update

                                    HashSet<IndexKey> helperResultSet = new HashSet<IndexKey>();

                                    foreach (var aNewItem in helperSet)
                                    {
                                        foreach (var aReturnVal in result)
                                        {
                                            helperResultSet.Add(new IndexKey(aReturnVal, aIndexAttributeUUID, aNewItem, this.IndexKeyDefinition));
                                        }
                                    }

                                    result = helperResultSet;

                                    #endregion
                                }
                                else
                                {
                                    #region create new

                                    foreach (var aNewItem in helperSet)
                                    {
                                        result.Add(new IndexKey(aIndexAttributeUUID, aNewItem, this.IndexKeyDefinition));
                                    }

                                    #endregion
                                }

                                break;

                            #endregion

                            #region single/special

                            case KindsOfType.SingleReference:
                            case KindsOfType.SingleNoneReference:
                            case KindsOfType.SpecialAttribute:

                                newIndexKeyItem = (ADBBaseObject)myDBObject.GetAttribute(aIndexAttributeUUID, myTypeOfDBObject, dbContext);

                                if (result.Count != 0)
                                {
                                    #region update

                                    foreach (var aResultItem in result)
                                    {
                                        aResultItem.AddAADBBAseObject(aIndexAttributeUUID, newIndexKeyItem);
                                    }

                                    #endregion
                                }
                                else
                                {
                                    #region create new

                                    result.Add(new IndexKey(aIndexAttributeUUID, newIndexKeyItem, this.IndexKeyDefinition));

                                    #endregion
                                }

                                break;

                            #endregion

                            #region not implemented

                            case KindsOfType.SetOfReferences:
                            default:

                                throw new GraphDBException(new Error_NotImplemented(new System.Diagnostics.StackTrace(true), "Currently its not implemented to insert anything else than a List/Set/Single of base types"));

                            #endregion
                        }
                    }
                    else
                    {
                        //add default value

                        var defaultADBBAseObject = GraphDBTypeMapper.GetADBBaseObjectFromUUID(currentAttribute.DBTypeUUID);
                        defaultADBBAseObject.SetValue(DBObjectInitializeType.Default);

                        if (result.Count != 0)
                        {
                            #region update

                            foreach (var aResultItem in result)
                            {
                                aResultItem.AddAADBBAseObject(aIndexAttributeUUID, defaultADBBAseObject);
                            }

                            #endregion
                        }
                        else
                        {
                            #region create new

                            result.Add(new IndexKey(aIndexAttributeUUID, defaultADBBAseObject, this.IndexKeyDefinition));

                            #endregion
                        }

                    }
                    #endregion
                }
                else
                {
                    #region reference attribute

                    throw new GraphDBException(new Error_NotImplemented(new System.Diagnostics.StackTrace(true)));

                    #endregion
                }
            }

            return result;
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
0
        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 AttributeRemoveList

            #region data

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

            ASetOfReferencesEdgeType _elementsToBeRemoved;
            EdgeTypeListOfBaseObjects undefAttrList;

            #endregion

            #region undefined attributes

            if (AttributeIDChain.IsUndefinedAttribute)
            {
                var loadExcept = LoadUndefAttributes(AttributeName, myDBContext, myDBObjectStream);

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

                if (!loadExcept.Value.ContainsKey(AttributeName))
                {
                    attrsForResult.Add(AttributeName, new Tuple<TypeAttribute, IObject>(AttributeIDChain.LastAttribute, null));
                    return new Exceptional<Dictionary<string, Tuple<TypeAttribute, IObject>>>(attrsForResult);
                }

                if (!(loadExcept.Value[AttributeName] is EdgeTypeListOfBaseObjects))
                    return new Exceptional<Dictionary<string, Tuple<TypeAttribute, IObject>>>(new Error_InvalidAttributeKind());

                undefAttrList = (EdgeTypeListOfBaseObjects)loadExcept.Value[AttributeName];

                foreach (var tuple in TupleDefinition)
                {
                    undefAttrList.Remove((tuple.Value as ValueDefinition).Value);
                }

                myDBContext.DBObjectManager.AddUndefinedAttribute(AttributeName, undefAttrList, myDBObjectStream);

                attrsForResult.Add(AttributeName, new Tuple<TypeAttribute, IObject>(AttributeIDChain.LastAttribute, null));
                return new Exceptional<Dictionary<string, Tuple<TypeAttribute, IObject>>>(attrsForResult);
            }

            #endregion

            #region get elements

            if (AttributeIDChain.LastAttribute.EdgeType == null)
            {
                return new Exceptional<Dictionary<string, Tuple<TypeAttribute, IObject>>>(new Error_InvalidEdgeType(AttributeIDChain.LastAttribute.GetType()));
            }

            var elements = TupleDefinition.GetCorrespondigDBObjectUUIDAsList(myGraphDBType, myDBContext, AttributeIDChain.LastAttribute.EdgeType.GetNewInstance(), AttributeIDChain.LastAttribute.GetDBType(myDBContext.DBTypeManager));

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

            _elementsToBeRemoved = (ASetOfReferencesEdgeType)elements.Value;

            #endregion

            #region remove elements from list

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

            if (myDBObjectStream.HasAttribute(AttributeIDChain.LastAttribute.UUID, AttributeIDChain.LastAttribute.GetRelatedType(myDBContext.DBTypeManager)))
            {
                ASetOfReferencesEdgeType edge = (ASetOfReferencesEdgeType)myDBObjectStream.GetAttribute(AttributeIDChain.LastAttribute.UUID);

                foreach (var aUUID in (_elementsToBeRemoved as ASetOfReferencesEdgeType).GetAllReferenceIDs())
                {
                    edge.RemoveUUID(aUUID);
                }

                #region remove backward edges

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

                    RemoveBackwardEdges(myGraphDBType.UUID, userdefinedAttributes, myDBObjectStream.ObjectUUID, myDBContext);
                }

                #endregion

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

            #endregion

            #endregion

            return new Exceptional<Dictionary<string, Tuple<TypeAttribute, IObject>>>(attrsForResult);
        }
Ejemplo n.º 5
0
 private bool IsValidDBObjectStreamForBinExpr(DBObjectStream DBObjectStream, TypeAttribute myTypeAttribute, DBTypeManager myDBTypeManager)
 {
     if (DBObjectStream.HasAttribute(myTypeAttribute.UUID, myTypeAttribute.GetRelatedType(myDBTypeManager)) || myTypeAttribute.IsBackwardEdge)
     {
         return true;
     }
     else
     {
         return false;
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Extracts the attribute from <paramref name="myDBObject"/>.
        /// </summary>
        /// <param name="myType"></param>
        /// <param name="myTypeAttribute"></param>
        /// <param name="myDBObject"></param>
        /// <param name="myLevelKey"></param>
        /// <returns></returns>
        private Object GetAttributeValue(GraphDBType myType, TypeAttribute myTypeAttribute, DBObjectStream myDBObject, EdgeList myLevelKey)
        {
            if (myTypeAttribute.TypeCharacteristics.IsBackwardEdge)
            {

                #region IsBackwardEdge

                EdgeKey edgeKey = myTypeAttribute.BackwardEdgeDefinition;
                var contBackwardExcept = myDBObject.ContainsBackwardEdge(edgeKey, _DBContext, _DBContext.DBObjectCache, myType);

                if (contBackwardExcept.Failed())
                    throw new GraphDBException(contBackwardExcept.IErrors);

                if (contBackwardExcept.Value)
                {
                    var getBackwardExcept = myDBObject.GetBackwardEdges(edgeKey, _DBContext, _DBContext.DBObjectCache, myTypeAttribute.GetDBType(_DBContext.DBTypeManager));

                    if (getBackwardExcept.Failed())
                        throw new GraphDBException(getBackwardExcept.IErrors);

                    return getBackwardExcept.Value;
                }

                #endregion

            }
            else if (myDBObject.HasAttribute(myTypeAttribute.UUID, myType))
            {

                #region ELSE (!IsBackwardEdge)

                return myDBObject.GetAttribute(myTypeAttribute.UUID, myType, _DBContext);

                #endregion

            }

            return null;
        }
Ejemplo n.º 7
0
        /// <summary>   Gets an attribute value - references will be resolved. </summary>
        ///
        /// <remarks>   Stefan, 16.04.2010. </remarks>
        ///
        /// <param name="myType">           Type. </param>
        /// <param name="myTypeAttribute">  my type attribute. </param>
        /// <param name="myDBObject">       my database object. </param>
        /// <param name="myDepth">          Depth of my. </param>
        /// <param name="myLevelKey">       my level key. </param>
        /// <param name="reference">        The reference. </param>
        /// <param name="myUsingGraph">     true to my using graph. </param>
        /// <param name="attributeValue">   [out] The attribute value. </param>
        ///
        /// <returns>   true if it succeeds, false if the DBO does not have the attribute. </returns>
        private Boolean GetAttributeValueAndResolve(GraphDBType myType, SelectionElement mySelectionelement, DBObjectStream myDBObject, Int64 myDepth, EdgeList myLevelKey, String reference, Boolean myUsingGraph, out Object attributeValue, String myUndefAttrName = null)
        {

            var typeAttribute = mySelectionelement.Element;

            if (typeAttribute.TypeCharacteristics.IsBackwardEdge)
            {

                #region IsBackwardEdge

                EdgeKey edgeKey = typeAttribute.BackwardEdgeDefinition;
                var contBackwardExcept = myDBObject.ContainsBackwardEdge(edgeKey, _DBContext, _DBContext.DBObjectCache, myType);

                if (contBackwardExcept.Failed())
                    throw new GraphDBException(contBackwardExcept.IErrors);

                if (contBackwardExcept.Value)
                {
                    if (myDepth > 0)
                    {
                        var dbos = myDBObject.GetBackwardEdges(edgeKey, _DBContext, _DBContext.DBObjectCache, typeAttribute.GetDBType(_DBContext.DBTypeManager));

                        if (dbos.Failed())
                            throw new GraphDBException(dbos.IErrors);

                        if (dbos.Value != null)
                        {
                            attributeValue = ResolveAttributeValue(typeAttribute, dbos.Value, myDepth, myLevelKey, myDBObject, reference, myUsingGraph);
                            return true;
                        }
                    }
                    else
                    {
                        attributeValue = GetNotResolvedBackwardEdgeReferenceAttributeValue(myDBObject, typeAttribute, edgeKey, myLevelKey, myUsingGraph, _DBContext);
                        return true;

                    }
                }

                #endregion

            }
            else if (myDBObject.HasAttribute(typeAttribute, _DBContext))
            {

                #region SelectValueAssignment - kind of static assignment of selected attribute

                if (mySelectionelement.SelectValueAssignment != null && mySelectionelement.SelectValueAssignment.ValueAssignmentType == SelectValueAssignment.ValueAssignmentTypes.Always)
                {
                    // Currently the prior add SelectionElement verifies that TermDefinition is always a ValueDefinition - if others will become valid this must be changed!
                    attributeValue = (mySelectionelement.SelectValueAssignment.TermDefinition as ValueDefinition).Value.Value;
                    return true;
                }

                #endregion

                #region ELSE (!IsBackwardEdge)

                #region not a reference attribute value

                if (!typeAttribute.IsUserDefinedType(_DBContext.DBTypeManager))
                {
                    var attrVal = myDBObject.GetAttribute(typeAttribute, myType, _DBContext);

                    if (attrVal.Failed())
                    {
                        throw new GraphDBException(attrVal.IErrors);
                    }

                    // currently, we do not want to return a ADBBaseObject but the real value
                    if (attrVal.Value is ADBBaseObject)
                        attributeValue = (attrVal.Value as ADBBaseObject).GetReadoutValue();
                    else if (attrVal.Value is IBaseEdge)
                        attributeValue = (attrVal.Value as IBaseEdge).GetReadoutValues();
                    else
                        attributeValue = attrVal.Value;

                    return true;
                }

                #endregion

                #region ELSE Reference attribute value

                else
                {
                    if (myDepth > 0)
                    {
                        var attrValue = myDBObject.GetAttribute(typeAttribute.UUID, myType, _DBContext);
                        attributeValue = ResolveAttributeValue(typeAttribute, attrValue, myDepth, myLevelKey, myDBObject, reference, myUsingGraph);
                        return true;
                    }
                    else
                    {
                        attributeValue = GetNotResolvedReferenceAttributeValue(myDBObject, typeAttribute, myType, myLevelKey, myUsingGraph, _DBContext);
                        return true;
                    }
                }

                #endregion

                #endregion

            }
            else if (mySelectionelement.SelectValueAssignment != null)
            {
                // Currently the prior add SelectionElement verifies that TermDefinition is always a ValueDefinition - if others will become valid this must be changed!
                attributeValue = (mySelectionelement.SelectValueAssignment.TermDefinition as ValueDefinition).Value.Value;
                return true;
            }

            attributeValue = null;
            return false;

        }
Ejemplo n.º 8
0
        /// <summary>
        /// This method adds a DBOBjectStream to a Level if it is valid for a LevelKey.
        /// </summary>
        /// <param name="aDBObject">The Object that is going to be added</param>
        /// <param name="myLevelKey">The LevelKey which is needed for validation.</param>
        /// <param name="currentBackwardResolution">The current backward resolution (initially 0)</param>
        /// <param name="source">The ObjectUUID of the </param>
        /// <returns>True if it was valid or false otherwise.</returns>
        private bool AddNodeIfValid(DBObjectStream aDBObject, LevelKey myLevelKey, int currentBackwardResolution, ObjectUUID source, int backwardResolutiondepth)
        {
            #region data

            Exceptional<BackwardEdgeStream> beStream            = null;
            TypeAttribute                   tempTypeAttribute   = null;

            IEnumerable<ObjectUUID>         referenceUUIDs      = null;
            GraphDBType                     referenceType       = null;

            #endregion

            if ((myLevelKey.Level - currentBackwardResolution) > 0)
            {
                #region level > 0

                int desiredBackwardEdgeLevel = myLevelKey.Level - currentBackwardResolution - 1;

                tempTypeAttribute = _DBContext.DBTypeManager.GetTypeByUUID(myLevelKey.Edges[desiredBackwardEdgeLevel].TypeUUID).GetTypeAttributeByUUID(myLevelKey.Edges[desiredBackwardEdgeLevel].AttrUUID);

                #region get reference UUIDs

                if (tempTypeAttribute.IsBackwardEdge)
                {
                    #region backward edge handling

                    referenceType = _DBContext.DBTypeManager.GetTypeByUUID(tempTypeAttribute.BackwardEdgeDefinition.TypeUUID).GetTypeAttributeByUUID(tempTypeAttribute.BackwardEdgeDefinition.AttrUUID).GetDBType(_DBContext.DBTypeManager);

                    if (aDBObject.HasAttribute(tempTypeAttribute.BackwardEdgeDefinition.AttrUUID, _DBContext.DBTypeManager.GetTypeByUUID(tempTypeAttribute.BackwardEdgeDefinition.TypeUUID)))
                    {
                        referenceUUIDs = GetUUIDsForAttribute(aDBObject, tempTypeAttribute.BackwardEdgeDefinition.GetTypeAndAttributeInformation(_DBContext.DBTypeManager).Item2, _DBContext.DBTypeManager.GetTypeByUUID(aDBObject.TypeUUID));
                    }

                    #endregion
                }
                else
                {
                    #region forward edge handling

                    beStream = _DBObjectCache.LoadDBBackwardEdgeStream(tempTypeAttribute.GetDBType(_DBContext.DBTypeManager), aDBObject.ObjectUUID);

                    if (beStream.Failed())
                    {
                        throw new GraphDBException(new Error_CouldNotLoadBackwardEdge(aDBObject, tempTypeAttribute, beStream.IErrors));
                    }

                    var tempEdgeKey = GetBackwardEdgeKey(myLevelKey, desiredBackwardEdgeLevel, _DBContext);

                    if (!beStream.Value.ContainsBackwardEdge(tempEdgeKey))
                    {
                        return false;
                    }

                    referenceUUIDs = beStream.Value.GetBackwardEdgeUUIDs(tempEdgeKey);
                    referenceType = tempTypeAttribute.GetRelatedType(_DBContext.DBTypeManager);

                    #endregion
                }

                #endregion

                if (referenceUUIDs != null)
                {
                    #region references

                    Exceptional<DBObjectStream> tempDbo = null;
                    Dictionary<ObjectUUID, ADBBaseObject> validUUIDs = new Dictionary<ObjectUUID, ADBBaseObject>();
                    SettingInvalidReferenceHandling invalidReferenceSetting = null;

                    #region process references recursivly

                    foreach (var aReferenceUUID in referenceUUIDs)
                    {
                        tempDbo = _DBObjectCache.LoadDBObjectStream(referenceType, aReferenceUUID);

                        if (!tempDbo.Success())
                        {
                            #region error

                            if (invalidReferenceSetting == null)
                            {
                                invalidReferenceSetting = (SettingInvalidReferenceHandling)_DBContext.DBSettingsManager.GetSetting(SettingInvalidReferenceHandling.UUID, _DBContext, TypesSettingScope.ATTRIBUTE, referenceType, tempTypeAttribute).Value;
                            }

                            switch (invalidReferenceSetting.Behaviour)
                            {
                                case BehaviourOnInvalidReference.ignore:
                                    #region ignore

                                    //insert if the next lower level is 0

                                    if ((myLevelKey.Level - currentBackwardResolution) == 0)
                                    {
                                        if (currentBackwardResolution <= backwardResolutiondepth)
                                        {
                                            #region fill graph

                                            LevelKey newLevelKey = new LevelKey(myLevelKey.Edges.First().TypeUUID, _DBContext.DBTypeManager);

                                            if (currentBackwardResolution > 0)
                                            {
                                                //we have to add forwardEdges and (if not already there) add nodes
                                                AddEmptyLevel(newLevelKey);
                                                Levels[0].AddNode(newLevelKey, new ExpressionNode(aReferenceUUID, null));
                                                Levels[0].AddForwardEdgeToNode(newLevelKey, aReferenceUUID, new EdgeKey(myLevelKey.Edges[0].TypeUUID, myLevelKey.Edges[0].AttrUUID), aDBObject.ObjectUUID, null);

                                            }
                                            else
                                            {
                                                //we are in the lowest level and are the first time in this method... so there's no need for adding forward-edges to nodes
                                                AddEmptyLevel(newLevelKey);
                                                Levels[0].AddNode(newLevelKey, new ExpressionNode(aReferenceUUID, null));
                                            }

                                            #endregion
                                        }

                                        //add to valid uuids be
                                        validUUIDs.Add(tempDbo.Value.ObjectUUID, null);
                                    }

                                    #endregion
                                    break;
                                case BehaviourOnInvalidReference.log:

                                    AddWarning(new Warning_EdgeToNonExistingNode(aDBObject, tempTypeAttribute.GetDBType(_DBContext.DBTypeManager), tempTypeAttribute, tempDbo.IErrors));

                                    break;

                                default:

                                    throw new GraphDBException(new Error_NotImplemented(new System.Diagnostics.StackTrace()));
                            }

                            #endregion
                        }
                        else
                        {
                            if (AddNodeIfValid(tempDbo.Value, myLevelKey, currentBackwardResolution + 1, aDBObject.ObjectUUID, backwardResolutiondepth))
                            {
                                validUUIDs.Add(tempDbo.Value.ObjectUUID, null);
                            }
                        }
                    }

                    #endregion

                    if (validUUIDs.Count > 0)
                    {
                        //some valid uuids
                        if (currentBackwardResolution <= backwardResolutiondepth)
                        {
                            #region fill graph

                            FillGraph(aDBObject, myLevelKey, currentBackwardResolution, source, myLevelKey.Edges[desiredBackwardEdgeLevel], validUUIDs);

                            #endregion
                        }

                        return true;
                    }
                    else
                    {
                        //not valid
                        return false;
                    }

                    #endregion
                }
                else
                {
                    #region no references

                    return false;

                    #endregion
                }
                #endregion
            }
            else
            {
                #region Level 0

                if (currentBackwardResolution <= backwardResolutiondepth)
                {
                    #region fill graph

                    LevelKey newLevelKey = new LevelKey(myLevelKey.Edges.First().TypeUUID, _DBContext.DBTypeManager);

                    if (currentBackwardResolution > 0)
                    {
                        //we have to add forwardEdges and (if not already there) add nodes
                        AddEmptyLevel(newLevelKey);
                        Levels[0].AddNode(newLevelKey, new ExpressionNode(aDBObject, null));
                        Levels[0].AddForwardEdgeToNode(newLevelKey, aDBObject.ObjectUUID, new EdgeKey(myLevelKey.Edges[0].TypeUUID, myLevelKey.Edges[0].AttrUUID), source, null);

                    }
                    else
                    {
                        //we are in the lowest level and are the first time in this method... so there's no need for adding forward-edges to nodes
                        AddEmptyLevel(newLevelKey);
                        Levels[0].AddNode(newLevelKey, new ExpressionNode(aDBObject, null));
                    }

                    #endregion
                }

                return true;

                #endregion
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Execute the remove of attributes
        /// </summary>
        /// <param name="myAttrsToRemove">The list of attributes to remove</param>
        /// <param name="myDBContext">The db context</param>
        /// <param name="myDBDBObject">The db object from which the attributes should be deleted</param>
        /// <param name="myGraphDBType">The type of the db object</param>
        /// <returns>The list of removed attributes</returns>
        private Exceptional<List<TypeAttribute>> ApplyRemoveAttribute(List<string> myAttrsToRemove, DBContext myDBContext, DBObjectStream myDBDBObject, GraphDBType myGraphDBType)
        {
            #region data

            List<TypeAttribute> removedAttributes = new List<TypeAttribute>();

            #endregion

            var MandatoryTypeAttrib = myGraphDBType.GetMandatoryAttributesUUIDs(myDBContext.DBTypeManager);
            foreach (String aAttribute in myAttrsToRemove)
            {
                TypeAttribute typeAttribute = myGraphDBType.GetTypeAttributeByName(aAttribute);

                if (myDBDBObject.HasAttribute(typeAttribute.UUID, myGraphDBType))
                {
                    if (!MandatoryTypeAttrib.Contains(typeAttribute.UUID))
                    {
                        #region remove backward edges

                        if (typeAttribute.GetDBType(myDBContext.DBTypeManager).IsUserDefined)
                        {
                            var userdefinedAttributes = new Dictionary<AttributeUUID, object>();
                            userdefinedAttributes.Add(typeAttribute.UUID, myDBDBObject.GetAttribute(typeAttribute.UUID));

                            RemoveBackwardEdges(myGraphDBType.UUID, userdefinedAttributes, myDBDBObject.ObjectUUID, myDBContext);
                        }

                        #endregion

                        myDBDBObject.RemoveAttribute(typeAttribute.UUID);
                        removedAttributes.Add(typeAttribute);
                    }
                    else
                    {
                        return new Exceptional<List<TypeAttribute>>(new Error_MandatoryConstraintViolation("Error in update statement. The attribute \"" + typeAttribute.Name + "\" is mandatory and can not be removed."));
                    }
                }
            }

            return new Exceptional<List<TypeAttribute>>(removedAttributes);
        }