Exemplo n.º 1
0
        /// <summary>
        /// Gets model type code for specified entity id.
        /// </summary>
        /// <param name="id">entity id</param>
        /// <returns>Model type code</returns>
        public ModelCode GetModelCodeFromId(long id)
        {
            DMSType type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(id);

            ModelCode modelCode;

            if (this.type2modelCode.TryGetValue(type, out modelCode))
            {
                return(modelCode);
            }
            else
            {
                string message = string.Format("Failed to get model type code for DMS type: {0}. Invalid DMS type. ID = 0x{1:x16}", type, id);
                CommonTrace.WriteTrace(CommonTrace.TraceError, message);
                throw new Exception(message);
            }
        }
Exemplo n.º 2
0
        public void SortOperations()
        {
            string message = String.Format("Sorting delta operations for delta with ID = {0}.", GetCompositeId(id));

            CommonTrace.WriteTrace(CommonTrace.TraceVerbose, message);

            List <ResourceDescription> insertOpsOrdered = new List <ResourceDescription>();
            List <ResourceDescription> deleteOpsOrdered = new List <ResourceDescription>();
            int insertOpsOrderedNo = 0;
            int deleteOpsOrderedNo = 0;
            int indexOp            = 0;

            // pass through all given types
            for (int indexType = 0; indexType < Delta.ResourceDescs.TypeIdsInInsertOrder.Count; indexType++)
            {
                DMSType type = ModelResourcesDesc.GetTypeFromModelCode(Delta.ResourceDescs.TypeIdsInInsertOrder[indexType]);

                // pass through all insert operations
                // move operations with current type to list of ordered insert operations
                indexOp = 0;
                for (indexOp = 0; indexOp < insertOps.Count; indexOp++)
                {
                    if (insertOps[indexOp] != null && type == (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(insertOps[indexOp].Id))
                    {
                        // add at the end of list of ordered insert operations
                        insertOpsOrdered.Add(insertOps[indexOp]);
                        insertOps[indexOp] = null;
                        insertOpsOrderedNo++;

                        // remove from the old non sorted list - ATTENTION: this turns to be VERY SLOW operation in case delta is BIG
                        ////insertOps.RemoveAt(indexOp);
                    }
                }

                // pass through all delete operations
                // move operations with current type to list of ordered delete operations
                indexOp = 0;
                for (indexOp = 0; indexOp < deleteOps.Count; indexOp++)
                {
                    if (deleteOps[indexOp] != null && type == (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(deleteOps[indexOp].Id))
                    {
                        // add at the end of list of ordered delete operations
                        deleteOpsOrdered.Add(deleteOps[indexOp]);
                        deleteOps[indexOp] = null;
                        deleteOpsOrderedNo++;

                        // remove from the old non sorted list - ATTENTION: this turns to be VERY SLOW operation in case delta is BIG
                        ////deleteOps.RemoveAt(indexOp);
                    }
                }
            }

            // check if there are insert operations not covered by given data model types
            if (insertOps.Count != insertOpsOrderedNo)
            {
                // find type that is not specified in given list of types
                short typeNotDefined = 0;
                for (indexOp = 0; indexOp < insertOps.Count; indexOp++)
                {
                    if (insertOps[indexOp] != null)
                    {
                        typeNotDefined = (short)ModelCodeHelper.ExtractTypeFromGlobalId(insertOps[indexOp].Id);
                    }
                }

                message = String.Format("Failed to sort delta operations because there are some insert operations (count = {0}) whose type (e.g {1}) is not specified in the given list of types.", insertOps.Count - insertOpsOrderedNo, typeNotDefined);
                CommonTrace.WriteTrace(CommonTrace.TraceError, message);

                string exceptionMessage = String.Format("Invalid delta. Some insert operations (count = {0}) whose type (e.g {1}) is not correct.", insertOps.Count - insertOpsOrderedNo, typeNotDefined);
                throw new ModelException(ErrorCode.InvalidDelta, exceptionMessage);
            }

            // remember ordered insert operations
            insertOps = insertOpsOrdered;

            // check if there are delete operations not covered by given data model types
            if (deleteOps.Count != deleteOpsOrderedNo)
            {
                // find type that is not specified in given list of types
                short typeNotDefined = 0;
                for (indexOp = 0; indexOp < deleteOps.Count; indexOp++)
                {
                    if (deleteOps[indexOp] != null)
                    {
                        typeNotDefined = ModelCodeHelper.ExtractTypeFromGlobalId(deleteOps[indexOp].Id);
                    }
                }
                message = String.Format("Failed to sort delta operations because there are some delete operations (count = {0}) which type (e.g. {1}) is not specified in given list of types.", deleteOps.Count, typeNotDefined);
                CommonTrace.WriteTrace(CommonTrace.TraceError, message);

                string exceptionMessage = String.Format("Invalid delta. Some delete operations (count = {0}) which type (e.g. {1}) is not correct.", deleteOps.Count, typeNotDefined);
                throw new ModelException(ErrorCode.InvalidDelta, exceptionMessage);
            }

            // remember ordered delete operations
            deleteOpsOrdered.Reverse();
            deleteOps = deleteOpsOrdered;

            message = String.Format("Sorting delta operations for delta with ID = {0} completed successfully.", GetCompositeId(id));
            CommonTrace.WriteTrace(CommonTrace.TraceVerbose, message);
        }
Exemplo n.º 3
0
        public void FixNegativeToPositiveIds(ref Dictionary <short, int> typesCounters, ref Dictionary <long, long> globaldChanges)
        {
            string message = String.Format("Fixing negative to positive IDs for delta with ID = {0} started.", GetCompositeId(id));

            CommonTrace.WriteTrace(CommonTrace.TraceVerbose, message);

            if (globaldChanges == null)
            {
                globaldChanges = new Dictionary <long, long>();
            }

            // fix ids in insert operations - generate positive ids
            foreach (ResourceDescription rd in insertOps)
            {
                long gidOld = rd.Id;
                int  idOld  = ModelCodeHelper.ExtractEntityIdFromGlobalId(rd.Id);
                if (idOld < 0)
                {
                    if (!globaldChanges.ContainsKey(gidOld))
                    {
                        short type = ModelCodeHelper.ExtractTypeFromGlobalId(rd.Id);

                        if (type <= 0)
                        {
                            throw new ModelException(ErrorCode.InvalidDelta, gidOld, 0, string.Format("Invalid DMS type found in insert delta operation ID = 0x{0:x16}.", rd.Id));
                        }

                        int idNew = typesCounters[type] + 1;
                        typesCounters[type] = idNew;

                        long gidNew = ChangeEntityIdInGlobalId(gidOld, idNew);
                        gidNew = IncorporateSystemIdToValue(gidNew, 0);

                        globaldChanges[gidOld] = gidNew;
                        rd.Id = gidNew;
                    }
                    else
                    {
                        message = String.Format("Failed to fix negative to positive IDs in insert delta operations for delta with ID = {0} because negative resource ID = 0x{1:x16} already exists in previous insert delta operations.", GetCompositeId(id), gidOld);
                        CommonTrace.WriteTrace(CommonTrace.TraceError, message);
                        string exceptionMessage = String.Format("Invalid delta. Negative resource ID = 0x{0:x16} already exists in previous insert delta operations.", gidOld);
                        throw new Exception(exceptionMessage);
                    }
                }
                else if (!this.positiveIdsAllowed)
                {
                    message = String.Format("Failed to fix negative to positive IDs in insert delta operations for delta with ID = {0} because resource ID = 0x{1:x16} must not be positive.", GetCompositeId(id), gidOld);
                    CommonTrace.WriteTrace(CommonTrace.TraceError, message);

                    string exceptionMessage = String.Format("Invalid insert delta operation. Resource ID = 0x{0:x16} must not be positive.", gidOld);
                    throw new Exception(exceptionMessage);
                }
            }

            // change reference ids in insert operations
            foreach (ResourceDescription rd in insertOps)
            {
                foreach (Property p in rd.Properties)
                {
                    if (p.Type == PropertyType.Reference)
                    {
                        long gidOld = p.AsReference();
                        int  idOld  = ModelCodeHelper.ExtractEntityIdFromGlobalId(gidOld);
                        if (idOld < 0)
                        {
                            if (globaldChanges.ContainsKey(gidOld))
                            {
                                p.SetValue(globaldChanges[gidOld]);
                            }
                            else
                            {
                                message = String.Format("Failed to fix negative to positive IDs in insert delta operations for delta with ID = {0} because negative reference (property code: {1}, value: 0x{2:x16}) does not exist in insert delta operations. Resource ID = 0x{3:x16}. ", GetCompositeId(id), p.Id, gidOld, rd.Id);
                                CommonTrace.WriteTrace(CommonTrace.TraceError, message);

                                string exceptionMessage = String.Format("Invalid insert delta operation. Negative reference (property code: {0}, value: 0x{1:x16}) does not exist in insert delta operations. Resource ID = 0x{2:x16}. ", p.Id, gidOld, rd.Id);
                                throw new Exception(exceptionMessage);
                            }
                        }
                    }
                    else if (p.Type == PropertyType.ReferenceVector)
                    {
                        bool changed = false;

                        List <long> gidsRef = p.AsReferences();
                        for (int i = 0; i < gidsRef.Count; i++)
                        {
                            long gidOldRef = gidsRef[i];
                            int  idOldRef  = ModelCodeHelper.ExtractEntityIdFromGlobalId(gidOldRef);
                            if (idOldRef < 0)
                            {
                                if (globaldChanges.ContainsKey(gidOldRef))
                                {
                                    gidsRef[i] = globaldChanges[gidOldRef];
                                    changed    = true;
                                }
                                else
                                {
                                    message = String.Format("Failed to fix negative to positive IDs in insert delta operations for delta with ID = {0} because negative reference (property code: {1}, value: 0x{2:x16}) does not exist in insert delta operations. Resource ID = 0x{3:x16}. ", GetCompositeId(id), p.Id, gidOldRef, rd.Id);
                                    CommonTrace.WriteTrace(CommonTrace.TraceError, message);

                                    string exceptionMessage = String.Format("Invalid insert delta operation. Negative reference (property code: {0}, value: 0x{1:x16}) does not exist in insert delta operations. Resource ID = 0x{2:x16}. ", p.Id, gidOldRef, rd.Id);
                                    throw new Exception(exceptionMessage);
                                }
                            }
                        }

                        if (changed)
                        {
                            p.SetValue(gidsRef);
                        }
                    }
                }
            }

            // change ids and reference ids in update operations
            foreach (ResourceDescription rd in updateOps)
            {
                long gidOld = rd.Id;
                int  idOld  = ModelCodeHelper.ExtractEntityIdFromGlobalId(rd.Id);
                if (idOld < 0)
                {
                    if (globaldChanges.ContainsKey(gidOld))
                    {
                        rd.Id = globaldChanges[gidOld];
                    }
                    else
                    {
                        message = String.Format("Failed to fix negative to positive IDs in update delta operations for delta with ID = {0} because negative resource ID = 0x{1:x16} does not exists in insert delta operations.", GetCompositeId(id), gidOld);
                        CommonTrace.WriteTrace(CommonTrace.TraceError, message);

                        string exceptionMessage = String.Format("Invalid update delta operation. Negative resource ID = 0x{0:x16} does not exists in insert delta operations.", gidOld);
                        throw new Exception(exceptionMessage);
                    }
                }

                foreach (Property p in rd.Properties)
                {
                    if (p.Type == PropertyType.Reference)
                    {
                        long gidOldRef = p.AsReference();
                        int  idOldRef  = ModelCodeHelper.ExtractEntityIdFromGlobalId(gidOldRef);
                        if (idOldRef < 0)
                        {
                            if (globaldChanges.ContainsKey(gidOldRef))
                            {
                                p.SetValue(globaldChanges[gidOldRef]);
                            }
                            else
                            {
                                message = String.Format("Failed to fix negative to positive IDs in update delta operations for delta with ID = {0} because negative reference (property code: {1}, value: 0x{2:x16}) does not exist in insert delta operations. Resource ID = 0x{3:x16}. ", GetCompositeId(id), p.Id, gidOldRef, rd.Id);
                                CommonTrace.WriteTrace(CommonTrace.TraceError, message);

                                string exceptionMessage = String.Format("Invalid update delta operation. Negative reference (property code: {0}, value: 0x{1:x16}) does not exist in insert delta operations. Resource ID = 0x{2:x16}. ", p.Id, gidOldRef, rd.Id);
                                throw new Exception(exceptionMessage);
                            }
                        }
                    }
                    else if (p.Type == PropertyType.ReferenceVector)
                    {
                        bool changed = false;

                        List <long> gidsRef = p.AsReferences();
                        for (int i = 0; i < gidsRef.Count; i++)
                        {
                            long gidOldRef = gidsRef[i];
                            int  idOldRef  = ModelCodeHelper.ExtractEntityIdFromGlobalId(gidOldRef);
                            if (idOldRef < 0)
                            {
                                if (globaldChanges.ContainsKey(gidOldRef))
                                {
                                    gidsRef[i] = globaldChanges[gidOldRef];
                                    changed    = true;
                                }
                                else
                                {
                                    message = String.Format("Failed to fix negative to positive IDs in update delta operations for delta with ID = {0} because negative reference (property code: {1}, value: 0x{2:x16}) does not exist in insert delta operations. Resource ID = 0x{3:x16}. ", GetCompositeId(id), p.Id, gidOldRef, rd.Id);
                                    CommonTrace.WriteTrace(CommonTrace.TraceError, message);

                                    string exceptionMessage = String.Format("Invalid update delta operation. Negative reference (property code: {0}, value: 0x{1:x16}) does not exist in insert delta operations. Resource ID = 0x{2:x16}. ", p.Id, gidOldRef, rd.Id);
                                    throw new Exception(exceptionMessage);
                                }
                            }
                        }

                        if (changed)
                        {
                            p.SetValue(gidsRef);
                        }
                    }
                }
            }

            // change ids in delete operations
            foreach (ResourceDescription rd in deleteOps)
            {
                long gidOld = rd.Id;
                int  idOld  = ModelCodeHelper.ExtractEntityIdFromGlobalId(rd.Id);
                if (idOld < 0)
                {
                    if (globaldChanges.ContainsKey(gidOld))
                    {
                        rd.Id = globaldChanges[gidOld];
                    }
                    else
                    {
                        message = String.Format("Failed to fix negative to positive IDs in delete delta operations for delta with ID = {0} because negative resource ID = 0x{1:x16} does not exists in insert delta operations.", GetCompositeId(id), gidOld);
                        CommonTrace.WriteTrace(CommonTrace.TraceError, message);

                        string exceptionMessage = String.Format("Invalid delete delta operation. Negative resource ID = 0x{0:x16} does not exists in insert delta operations.", gidOld);
                        throw new Exception(message);
                    }
                }
            }

            message = String.Format("Fixing negative to positive IDs for delta with ID = {0} completed successfully.", GetCompositeId(id));
            CommonTrace.WriteTrace(CommonTrace.TraceVerbose, message);
        }
Exemplo n.º 4
0
        public void ExportToXml(XmlTextWriter xmlWriter)
        {
            StringBuilder sb;

            xmlWriter.WriteStartElement("ResourceDescription");
            xmlWriter.WriteStartElement("id");
            xmlWriter.WriteStartAttribute("gid");
            xmlWriter.WriteValue(String.Format("0x{0:x16}", this.Id));
            xmlWriter.WriteStartAttribute("type");
            xmlWriter.WriteValue(((DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(this.Id)).ToString());
            xmlWriter.WriteEndElement();             // id
            xmlWriter.WriteStartElement("Properties");
            for (int i = 0; i < this.Properties.Count; i++)
            {
                xmlWriter.WriteStartElement("Property");
                xmlWriter.WriteStartAttribute("id");
                xmlWriter.WriteValue(this.Properties[i].Id.ToString());
                xmlWriter.WriteStartAttribute("value");
                switch (Properties[i].Type)
                {
                case PropertyType.Float:
                    xmlWriter.WriteValue(this.Properties[i].AsFloat());
                    break;

                case PropertyType.Bool:
                case PropertyType.Byte:
                case PropertyType.Int32:
                case PropertyType.Int64:
                case PropertyType.TimeSpan:
                case PropertyType.DateTime:
                    if (this.Properties[i].Id == ModelCode.IDOBJ_GID)
                    {
                        xmlWriter.WriteValue(String.Format("0x{0:x16}", this.Properties[i].AsLong()));
                    }
                    else
                    {
                        xmlWriter.WriteValue(this.Properties[i].AsLong());
                    }

                    break;

                case PropertyType.Enum:
                    try
                    {
                        EnumDescs enumDescs = new EnumDescs();
                        xmlWriter.WriteValue(enumDescs.GetStringFromEnum(this.Properties[i].Id, this.Properties[i].AsEnum()));
                    }
                    catch (Exception)
                    {
                        xmlWriter.WriteValue(this.Properties[i].AsEnum());
                    }

                    break;

                case PropertyType.Reference:
                    xmlWriter.WriteValue(String.Format("0x{0:x16}", this.Properties[i].AsReference()));
                    break;

                case PropertyType.String:
                    if (this.Properties[i].PropertyValue.StringValue == null)
                    {
                        this.Properties[i].PropertyValue.StringValue = String.Empty;
                    }
                    xmlWriter.WriteValue(this.Properties[i].AsString());
                    break;

                case PropertyType.Int64Vector:
                case PropertyType.ReferenceVector:
                    if (this.Properties[i].AsLongs().Count > 0)
                    {
                        sb = new StringBuilder(100);
                        for (int j = 0; j < this.Properties[i].AsLongs().Count; j++)
                        {
                            sb.Append(String.Format("0x{0:x16}", this.Properties[i].AsLongs()[j])).Append(", ");
                        }

                        xmlWriter.WriteValue(sb.ToString(0, sb.Length - 2));
                    }
                    else
                    {
                        xmlWriter.WriteValue("empty long/reference vector");
                    }

                    break;

                case PropertyType.TimeSpanVector:
                    if (this.Properties[i].AsLongs().Count > 0)
                    {
                        sb = new StringBuilder(100);
                        for (int j = 0; j < this.Properties[i].AsLongs().Count; j++)
                        {
                            sb.Append(String.Format("0x{0:x16}", this.Properties[i].AsTimeSpans()[j])).Append(", ");
                        }

                        xmlWriter.WriteValue(sb.ToString(0, sb.Length - 2));
                    }
                    else
                    {
                        xmlWriter.WriteValue("empty long/reference vector");
                    }

                    break;

                case PropertyType.Int32Vector:
                    if (this.Properties[i].AsInts().Count > 0)
                    {
                        sb = new StringBuilder(100);
                        for (int j = 0; j < this.Properties[i].AsInts().Count; j++)
                        {
                            sb.Append(String.Format("{0}", this.Properties[i].AsInts()[j])).Append(", ");
                        }

                        xmlWriter.WriteValue(sb.ToString(0, sb.Length - 2));
                    }
                    else
                    {
                        xmlWriter.WriteValue("empty int vector");
                    }

                    break;

                case PropertyType.DateTimeVector:
                    if (this.Properties[i].AsDateTimes().Count > 0)
                    {
                        sb = new StringBuilder(100);
                        for (int j = 0; j < this.Properties[i].AsDateTimes().Count; j++)
                        {
                            sb.Append(String.Format("{0}", this.Properties[i].AsDateTimes()[j])).Append(", ");
                        }

                        xmlWriter.WriteValue(sb.ToString(0, sb.Length - 2));
                    }
                    else
                    {
                        xmlWriter.WriteValue("empty DateTime vector");
                    }

                    break;

                case PropertyType.BoolVector:
                    if (this.Properties[i].AsBools().Count > 0)
                    {
                        sb = new StringBuilder(100);
                        for (int j = 0; j < this.Properties[i].AsBools().Count; j++)
                        {
                            sb.Append(String.Format("{0}", this.Properties[i].AsBools()[j])).Append(", ");
                        }

                        xmlWriter.WriteValue(sb.ToString(0, sb.Length - 2));
                    }
                    else
                    {
                        xmlWriter.WriteValue("empty int vector");
                    }

                    break;

                case PropertyType.FloatVector:
                    if (this.Properties[i].AsFloats().Count > 0)
                    {
                        sb = new StringBuilder(100);
                        for (int j = 0; j < this.Properties[i].AsFloats().Count; j++)
                        {
                            sb.Append(this.Properties[i].AsFloats()[j]).Append(", ");
                        }

                        xmlWriter.WriteValue(sb.ToString(0, sb.Length - 2));
                    }
                    else
                    {
                        xmlWriter.WriteValue("empty float vector");
                    }

                    break;

                case PropertyType.StringVector:
                    if (this.Properties[i].AsStrings().Count > 0)
                    {
                        sb = new StringBuilder(100);
                        for (int j = 0; j < this.Properties[i].AsStrings().Count; j++)
                        {
                            sb.Append(this.Properties[i].AsStrings()[j]).Append(", ");
                        }

                        xmlWriter.WriteValue(sb.ToString(0, sb.Length - 2));
                    }
                    else
                    {
                        xmlWriter.WriteValue("empty string vector");
                    }

                    break;

                case PropertyType.EnumVector:
                    if (this.Properties[i].AsEnums().Count > 0)
                    {
                        sb = new StringBuilder(100);
                        EnumDescs enumDescs = new EnumDescs();

                        for (int j = 0; j < this.Properties[i].AsEnums().Count; j++)
                        {
                            try
                            {
                                sb.Append(String.Format("{0}", enumDescs.GetStringFromEnum(this.Properties[i].Id, this.Properties[i].AsEnums()[j]))).Append(", ");
                            }
                            catch (Exception)
                            {
                                sb.Append(String.Format("{0}", this.Properties[i].AsEnums()[j])).Append(", ");
                            }
                        }

                        xmlWriter.WriteValue(sb.ToString(0, sb.Length - 2));
                    }
                    else
                    {
                        xmlWriter.WriteValue("empty enum vector");
                    }

                    break;

                default:
                    throw new Exception("Failed to export Resource Description as XML. Invalid property type.");
                }

                xmlWriter.WriteEndElement();                 // Property
            }

            xmlWriter.WriteEndElement();             // Properties
            xmlWriter.WriteEndElement();             // ResourceDescription
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets property ids that corresponds to all settable attributes of the entity.
        /// </summary>
        /// <param name="globalId">GlobalId</param>
        /// <returns>List of property ids that corresponds to settable entity attributes.</returns>
        public List <ModelCode> GetAllSettablePropertyIdsForEntityId(long globalId)
        {
            DMSType type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId);

            return(GetAllSettablePropertyIds(type));
        }