private static string GetADOAttributeValue <TClass, TAttribute>(ADOEnum adoEnum) where TAttribute : Attribute { // Using reflection. //System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); // Reflection. IEnumerable <TAttribute> attrs = typeof(TClass).GetCustomAttributes <TAttribute>(); // Getting output. foreach (Attribute attr in attrs) { DatabaseMappingAttribute a = (DatabaseMappingAttribute)attr; switch (adoEnum) { case ADOEnum.Select: return(a.SelectSP); case ADOEnum.Insert: return(a.InsertSP); case ADOEnum.Update: return(a.UpdateSP); case ADOEnum.Delete: return(a.DeleteSP); default: return(string.Empty); } } return(string.Empty); }
private static void CacheClass(Type t) { if (!BusinessObject.IsClassCached.ContainsKey(t)) { bool isObjectToCache = false; // get the class attributes object[] obj = t.GetCustomAttributes(typeof(XmlSerializableAttribute), true); if (obj != null && obj.Length == 1) { XmlSerializableAttribute attr = (XmlSerializableAttribute)obj[0]; BusinessObject.ClassXmlSerializationCache.Add(t, new XmlSerializationCache() { Attribute = attr }); isObjectToCache = true; } obj = t.GetCustomAttributes(typeof(DatabaseMappingAttribute), true); if (obj != null && obj.Length > 0) { DatabaseMappingCache[] cache = new DatabaseMappingCache[obj.Length]; for (int i = 0; i < obj.Length; i++) { DatabaseMappingAttribute attr = (DatabaseMappingAttribute)obj[i]; cache[i] = new DatabaseMappingCache() { Attribute = attr }; } BusinessObject.ClassDatabaseMappingCache.Add(t, cache); isObjectToCache = true; } if (isObjectToCache) { //get properties XmlSerializableAttribiute LinkedList <XmlSerializationCache> xmlSerializableCacheList = new LinkedList <XmlSerializationCache>(); foreach (PropertyInfo propertyInfo in t.GetProperties()) { obj = propertyInfo.GetCustomAttributes(typeof(XmlSerializableAttribute), true); if (obj != null && obj.Length == 1) { XmlSerializableAttribute attr = (XmlSerializableAttribute)obj[0]; XmlSerializationCache cache = new XmlSerializationCache() { Attribute = attr, Property = propertyInfo }; if (attr.ProcessLast) { xmlSerializableCacheList.AddLast(cache); } else { xmlSerializableCacheList.AddFirst(cache); } } } XmlSerializationCache[] xmlCache = new XmlSerializationCache[xmlSerializableCacheList.Count]; int u = 0; foreach (XmlSerializationCache c in xmlSerializableCacheList) { xmlCache[u++] = c; } BusinessObject.PropertiesXmlSerializationCache.Add(t, xmlCache); //get properties ComparableAttribiute List <ComparableCache> comparableCacheList = new List <ComparableCache>(); foreach (PropertyInfo propertyInfo in t.GetProperties()) { obj = propertyInfo.GetCustomAttributes(typeof(ComparableAttribute), true); if (obj != null && obj.Length == 1) { ComparableAttribute attr = (ComparableAttribute)obj[0]; ComparableCache cache = new ComparableCache() { Attribute = attr, Property = propertyInfo }; comparableCacheList.Add(cache); } } BusinessObject.PropertiesComparableCache.Add(t, comparableCacheList.ToArray()); //get properties DatabaseMappingCache List <DatabaseMappingCache> databaseMappingCacheList = new List <DatabaseMappingCache>(); foreach (PropertyInfo propertyInfo in t.GetProperties()) { obj = propertyInfo.GetCustomAttributes(typeof(DatabaseMappingAttribute), true); if (obj != null && obj.Length > 0) { foreach (object objAttr in obj) { DatabaseMappingAttribute attr = (DatabaseMappingAttribute)objAttr; DatabaseMappingCache cache = new DatabaseMappingCache() { Attribute = attr, Property = propertyInfo }; databaseMappingCacheList.Add(cache); } } } BusinessObject.PropertiesDatabaseMappingCache.Add(t, databaseMappingCacheList.ToArray()); BusinessObject.IsClassCached.Add(t, true); } } }
/// <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())); } } }
/// <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())); } } }
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")); } } }
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); } } } }