public void SetValue(List <int> intValues) { PropertyType type = this.Type; if (type == PropertyType.Int32Vector) { List <long> longValues = new List <long>(); foreach (int intValue in intValues) { longValues.Add(intValue); } value.LongValues = longValues; } else { string errorMessage = String.Format("Failed to set value for property ({0}) because specified vector value is not compatible with property type ({1}).", this.id, type); CommonTrace.WriteTrace(CommonTrace.TraceError, errorMessage); throw new Exception(errorMessage); } }
public List <byte> AsBytes() { PropertyType type = this.Type; if (type == PropertyType.ByteVector) { List <byte> byteValues = new List <byte>(); foreach (long longValue in value.LongValues) { byteValues.Add((byte)longValue); } return(byteValues); } else { string errorMessage = String.Format("Failed to return value from property for specified type ({0}) because it is incompatible with actual property type ({1})", PropertyType.Int32Vector, type); CommonTrace.WriteTrace(CommonTrace.TraceError, errorMessage); throw new Exception(errorMessage); } }
public string AsString() { PropertyType type = this.Type; if (type == PropertyType.String) { if (value.StringValue != null) { return(value.StringValue); } else { return(string.Empty); } } else { string errorMessage = String.Format("Failed to return value from property for specified type ({0}) because it is incompatible with actual property type ({1})", PropertyType.String, type); CommonTrace.WriteTrace(CommonTrace.TraceError, errorMessage); throw new Exception(errorMessage); } }
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); }
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); }