public virtual XElement Serialize(bool selfOnly) { XElement returnVal = null; Type t = this.GetType(); if (BusinessObject.ClassXmlSerializationCache.ContainsKey(t)) { XmlSerializationCache classCache = BusinessObject.ClassXmlSerializationCache[t]; returnVal = new XElement(classCache.Attribute.XmlField); if (this.BOType != BusinessObjectType.Other) { returnVal.Add(new XAttribute("type", this.BOType.ToString())); } XmlSerializationCache[] cache = BusinessObject.PropertiesXmlSerializationCache[t]; for (int i = 0; i < cache.Length; i++) { XmlSerializationCache c = cache[i]; object propertyValue = c.Property.GetValue(this, null); if (propertyValue != null) { XObject element = BusinessObjectHelper.SerializeSingleValue(c.Property.PropertyType, c.Attribute.XmlField, propertyValue, false, c.Attribute.UseAttribute, c.Attribute.SelfOnlySerialization, selfOnly); if (String.IsNullOrEmpty(c.Attribute.EncapsulatingXmlField)) { returnVal.Add(element); } else { if (returnVal.Element(c.Attribute.EncapsulatingXmlField) != null) { returnVal.Element(c.Attribute.EncapsulatingXmlField).Add(element); } else { returnVal.Add(new XElement(c.Attribute.EncapsulatingXmlField, element)); } } } } } return(returnVal); }
/// <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())); } } }
private static void SaveBusinessObjectColumns(IBusinessObject businessObject, Type boType, DatabaseMappingAttribute objAttribute, XElement entry, string dataType) { DatabaseMappingCache[] propCaches = BusinessObject.PropertiesDatabaseMappingCache[boType]; foreach (DatabaseMappingCache propCache in propCaches) { DatabaseMappingAttribute propertyAttr = propCache.Attribute; if (propertyAttr.LoadOnly) { continue; } PropertyInfo propertyInfo = propCache.Property; if (propertyAttr.TableName == null || propertyAttr.TableName == objAttribute.TableName) { object value = propertyInfo.GetValue(businessObject, null); //proper attribute for the currently processing table if (value != null && !propertyAttr.VariableColumnName) { XElement existingColumn = entry.Element(propertyAttr.ColumnName); if (existingColumn == null) { entry.Add(BusinessObjectHelper.SerializeSingleValue(propertyInfo.PropertyType, propertyAttr.ColumnName, propertyInfo.GetValue(businessObject, null), propertyAttr.OnlyId, false, false, false)); } else if (existingColumn != null && propertyAttr.TableName == objAttribute.TableName) //eg. if there version from contractor but we're saving employee table { existingColumn.Remove(); entry.Add(BusinessObjectHelper.SerializeSingleValue(propertyInfo.PropertyType, propertyAttr.ColumnName, propertyInfo.GetValue(businessObject, null), propertyAttr.OnlyId, false, false, false)); } } else if (value != null && propertyAttr.VariableColumnName) { //change attribute type XElement fieldValue = (XElement)BusinessObjectHelper.SerializeSingleValue(propertyInfo.PropertyType, propertyAttr.ColumnName, propertyInfo.GetValue(businessObject, null), propertyAttr.OnlyId, false, false, false); switch (dataType) { case "select": case "multiselect": case "string": fieldValue.Name = XName.Get(VariableColumnName.TextValue, String.Empty); break; case "float": case "money": case "boolean": case "integer": case "decimal": fieldValue.Name = XName.Get(VariableColumnName.DecimalValue, String.Empty); break; case "xml": fieldValue.Name = XName.Get(VariableColumnName.XmlValue, String.Empty); break; case "datetime": case "booldate": //Data wyświetlana w GUI jako checkbox fieldValue.Name = XName.Get(VariableColumnName.DateValue, String.Empty); break; case "guid": fieldValue.Name = XName.Get(VariableColumnName.GuidValue, String.Empty); break; case "link": fieldValue.Name = XName.Get(VariableColumnName.TextValue, String.Empty); break; case "auto": if (fieldValue.FirstNode is XElement) { fieldValue.Name = XName.Get(VariableColumnName.XmlValue, String.Empty); } else { fieldValue.Name = XName.Get(VariableColumnName.TextValue, String.Empty); } break; } entry.Add(fieldValue); } } } }