/// <summary> /// Creates communication xml for related versioned items of the specified <see cref="Item"/>. /// </summary> /// <param name="item"><see cref="Item"/> that may contains the related items.</param> /// <param name="localTransactionId">Local transaction ID.</param> /// <param name="deferredTransactionId">Deferred transaction ID.</param> private void CreateCommunicationXmlForRelatedVersionedItems(Item item, Guid localTransactionId, Guid deferredTransactionId) { foreach (ItemRelation relation in item.Relations.Children) { IVersionedBusinessObject related = relation.RelatedObject as IVersionedBusinessObject; if (related != null && (related.Status == BusinessObjectStatus.Modified || related.Status == BusinessObjectStatus.New || related.ForceSave == true)) { ItemRelationType relationType = DictionaryMapper.Instance.GetItemRelationType(relation.ItemRelationTypeId); string relatedObjectType = relationType.Metadata.Element("relatedObjectType").Value; switch (relatedObjectType) { case "Item": this.CreateCommunicationXmlForVersionedBusinessObject(related, localTransactionId, deferredTransactionId, StoredProcedure.communication_p_createItemPackage); break; case "Contractor": this.CreateCommunicationXmlForVersionedBusinessObject(related, localTransactionId, deferredTransactionId, StoredProcedure.communication_p_createContractorPackage); break; } } } }
/// <summary> /// Updates <see cref="IBusinessObject"/> dictionary index in the database. /// </summary> /// <param name="obj"><see cref="IBusinessObject"/> for which to update the index.</param> public override void UpdateDictionaryIndex(IBusinessObject obj) { Item item = (Item)obj; XDocument xml = XDocument.Parse("<root businessObjectId=\"\" mode=\"\" />"); //update main contractor if (item.Status == BusinessObjectStatus.Modified || item.Status == BusinessObjectStatus.New || item.ForceSave == true) { xml.Root.Attribute("businessObjectId").Value = item.Id.ToUpperString(); xml.Root.Attribute("mode").Value = item.Status == BusinessObjectStatus.New ? "insert" : "update"; this.ExecuteStoredProcedure(StoredProcedure.item_p_updateItemDictionary, false, xml , ConfigurationMapper.Instance.UpdateDictionaryIndexTimeout); } //update related items foreach (ItemRelation relation in item.Relations.Children) { IVersionedBusinessObject bo = relation.RelatedObject as IVersionedBusinessObject; if (bo != null && (bo.Status == BusinessObjectStatus.Modified || bo.Status == BusinessObjectStatus.New || bo.ForceSave == true)) { xml.Root.Attribute("businessObjectId").Value = bo.Id.ToUpperString(); xml.Root.Attribute("mode").Value = bo.Status == BusinessObjectStatus.New ? "insert" : "update"; ItemRelationType relationType = DictionaryMapper.Instance.GetItemRelationType(relation.Id.Value); string relatedObjectType = relationType.Metadata.Element("relatedObjectType").Value; switch (relatedObjectType) { case "Item": this.ExecuteStoredProcedure(StoredProcedure.item_p_updateItemDictionary, false, xml , ConfigurationMapper.Instance.UpdateDictionaryIndexTimeout); break; case "Contractor": this.ExecuteStoredProcedure(StoredProcedure.contractor_p_updateContractorDictionary, false, xml , ConfigurationMapper.Instance.UpdateDictionaryIndexTimeout); break; } } } }
/// <summary> /// Saves changes of specified <see cref="IBusinessObjectRelation"/> to the operations list. /// </summary> /// <param name="businessObject"><see cref="IBusinessObjectRelation"/> to save.</param> /// <param name="document">Xml document containing operation list to execute.</param> public static void SaveRelationChanges(IBusinessObjectRelation businessObject, XDocument document) { IVersionedBusinessObject mainObject; IVersionedBusinessObject relatedObject; //if we are processing a relation from alternate object if (businessObject.Status == BusinessObjectStatus.Deleted) { mainObject = (IVersionedBusinessObject)businessObject.Parent.AlternateVersion; //related object doesn't have to be independently versioned relatedObject = businessObject.RelatedObject as IVersionedBusinessObject; //reference to the OLD version //if we delete an object in new version we dont have its old version via the new main object reference //so we have to grab it from the old main object } else { mainObject = (IVersionedBusinessObject)businessObject.Parent; //related object doesn't have to be independently versioned relatedObject = businessObject.RelatedObject as IVersionedBusinessObject; } //mainObject and relatedObject now reference the NEW version of the alternates if (mainObject.Status == BusinessObjectStatus.Unchanged && mainObject.ForceSave == false && mainObject.NewVersion == null) { businessObject.UpgradeMainObjectVersion = true; mainObject.NewVersion = Guid.NewGuid(); } if (relatedObject != null && relatedObject.Status == BusinessObjectStatus.Unchanged && relatedObject.ForceSave == false && relatedObject.NewVersion == null) { businessObject.UpgradeRelatedObjectVersion = true; relatedObject.NewVersion = Guid.NewGuid(); } Type t = businessObject.GetType(); DatabaseMappingCache[] classCaches = BusinessObject.ClassDatabaseMappingCache[t]; foreach (DatabaseMappingCache classCache in classCaches) //foreach tableName { DatabaseMappingAttribute objAttribute = classCache.Attribute; //find or create table element XElement table = document.Root.Element(objAttribute.TableName); if (table == null) { table = new XElement(objAttribute.TableName); document.Root.Add(table); } //create new entry element XElement entry = new XElement("entry"); table.Add(entry); if (businessObject.Status != BusinessObjectStatus.Deleted) { BusinessObjectHelper.SaveBusinessObjectColumns(businessObject, t, objAttribute, entry, null); Guid newVersion; IVersionedBusinessObject versionedObject = businessObject as IVersionedBusinessObject; if (versionedObject != null) { versionedObject.NewVersion = Guid.NewGuid(); newVersion = versionedObject.NewVersion.Value; } else { newVersion = Guid.NewGuid(); } if (businessObject.Status == BusinessObjectStatus.New) { entry.Add(new XAttribute("action", "insert")); entry.Add(new XElement("version", newVersion.ToUpperString())); } else //BusinessObjectStatus.Modified or child elements changed { entry.Add(new XAttribute("action", "update")); entry.Add(new XElement("_version", newVersion.ToUpperString())); } } else { entry.Add(new XElement("id", businessObject.Id.ToUpperString())); entry.Add(new XElement("version", businessObject.Version.ToUpperString())); entry.Add(new XAttribute("action", "delete")); DatabaseMappingCache[] cache = BusinessObject.PropertiesDatabaseMappingCache[t]; for (int i = 0; i < cache.Length; i++) { DatabaseMappingCache c = cache[i]; if (c.Attribute.ForceSaveOnDelete) { entry.Add(BusinessObjectHelper.SerializeSingleValue(c.Property.PropertyType, c.Attribute.ColumnName, c.Property.GetValue(businessObject, null), c.Attribute.OnlyId, false, false, false)); } } } if (businessObject.UpgradeMainObjectVersion) { entry.Add(new XElement("_object1from", mainObject.Version.ToUpperString())); entry.Add(new XElement("_object1to", mainObject.NewVersion.ToUpperString())); } if (businessObject.UpgradeRelatedObjectVersion) { entry.Add(new XElement("_object2from", relatedObject.Version.ToUpperString())); entry.Add(new XElement("_object2to", relatedObject.NewVersion.ToUpperString())); } } }
/// <summary> /// Saves changes of specified <see cref="IBusinessObjectRelation"/> to the operations list. /// </summary> /// <param name="relation"><see cref="IBusinessObjectRelation"/> to save.</param> /// <param name="document">Xml document containing operation list to execute.</param> public static void SaveDictionaryRelationChanges(IBusinessObjectDictionaryRelation businessObject, XDocument document) { IVersionedBusinessObject mainObject; //if we are processing a relation from alternate object if (businessObject.Status == BusinessObjectStatus.Deleted) { mainObject = (IVersionedBusinessObject)businessObject.Parent.AlternateVersion; //if we delete an object in new version we dont have its old version via the new main object reference //so we have to grab it from the old main object } else { mainObject = (IVersionedBusinessObject)businessObject.Parent; } //mainObject now references the NEW version of the alternates if (mainObject.Status == BusinessObjectStatus.Unchanged && mainObject.ForceSave == false && mainObject.NewVersion == null) { businessObject.UpgradeMainObjectVersion = true; mainObject.NewVersion = Guid.NewGuid(); } Type t = businessObject.GetType(); DatabaseMappingCache[] classCaches = BusinessObject.ClassDatabaseMappingCache[t]; foreach (DatabaseMappingCache classCache in classCaches) //foreach tableName { DatabaseMappingAttribute objAttribute = classCache.Attribute; //find or create table element XElement table = document.Root.Element(objAttribute.TableName); if (table == null) { table = new XElement(objAttribute.TableName); document.Root.Add(table); } //create new entry element XElement entry = new XElement("entry"); table.Add(entry); if (businessObject.Status != BusinessObjectStatus.Deleted) { BusinessObjectHelper.SaveBusinessObjectColumns(businessObject, t, objAttribute, entry, null); Guid newVersion; IVersionedBusinessObject versionedObject = businessObject as IVersionedBusinessObject; if (versionedObject != null) { versionedObject.NewVersion = Guid.NewGuid(); newVersion = versionedObject.NewVersion.Value; } else { newVersion = Guid.NewGuid(); } if (businessObject.Status == BusinessObjectStatus.New) { entry.Add(new XAttribute("action", "insert")); entry.Add(new XElement("version", newVersion.ToUpperString())); } else //BusinessObjectStatus.Modified or child elements changed { entry.Add(new XAttribute("action", "update")); entry.Add(new XElement("_version", newVersion.ToUpperString())); } } else { entry.Add(new XElement("id", businessObject.Id.ToUpperString())); entry.Add(new XElement("version", businessObject.Version.ToUpperString())); entry.Add(new XAttribute("action", "delete")); } if (businessObject.UpgradeMainObjectVersion) { entry.Add(new XElement("_object1from", mainObject.Version.ToUpperString())); entry.Add(new XElement("_object1to", mainObject.NewVersion.ToUpperString())); } } }
public static void SaveBusinessObjectChanges(IBusinessObject businessObject, XDocument document, Dictionary <string, object> forcedElements, string dataType) { IVersionedBusinessObject versionedObject = businessObject as IVersionedBusinessObject; //force parent to save to change the main object version //these conditions are to secure a situation where both parent and its alternate version //are forced to save and therefore there is 1 additional update to the same record if (businessObject.Parent is IVersionedBusinessObject) { if (businessObject.Parent.AlternateVersion == null || ((IVersionedBusinessObject)businessObject.Parent.AlternateVersion).ForceSave == false) { ((IVersionedBusinessObject)businessObject.Parent).ForceSave = true; if (((IVersionedBusinessObject)businessObject.Parent).Status == BusinessObjectStatus.Unknown) { ((IVersionedBusinessObject)businessObject.Parent).Status = BusinessObjectStatus.Modified; } } } Type t = businessObject.GetType(); DatabaseMappingCache[] classCaches = BusinessObject.ClassDatabaseMappingCache[t]; foreach (DatabaseMappingCache classCache in classCaches) //foreach tableName { DatabaseMappingAttribute objAttribute = classCache.Attribute; //find or create table element XElement table = document.Root.Element(objAttribute.TableName); if (table == null) { table = new XElement(objAttribute.TableName); document.Root.Add(table); } //create new entry element XElement entry = new XElement("entry"); table.Add(entry); if (businessObject.Status != BusinessObjectStatus.Deleted) { BusinessObjectHelper.SaveBusinessObjectColumns(businessObject, t, objAttribute, entry, dataType); Guid newVersion; if (versionedObject != null) { versionedObject.NewVersion = Guid.NewGuid(); newVersion = versionedObject.NewVersion.Value; } else { newVersion = Guid.NewGuid(); } if (businessObject.Status == BusinessObjectStatus.New) { entry.Add(new XAttribute("action", "insert")); entry.Add(new XElement("version", newVersion.ToUpperString())); } else //BusinessObjectStatus.Modified or child elements changed { entry.Add(new XAttribute("action", "update")); entry.Add(new XElement("_version", newVersion.ToUpperString())); } if (forcedElements != null) { foreach (string key in forcedElements.Keys) { entry.Add(new XElement(key, forcedElements[key])); } } } else { entry.Add(new XElement("id", businessObject.Id.ToUpperString())); entry.Add(new XElement("version", businessObject.Version.ToUpperString())); entry.Add(new XAttribute("action", "delete")); } } }