/// <summary> /// Converts an Object's IDs, Guids, or CodeNames to the Objects CodeNames /// </summary> /// <param name="classObjSummary">The Class Object Summary</param> /// <param name="ObjectIdentifications">List of Object IDs, Guids, or CodeNames</param> /// <returns>A list of the Object's Code Names</returns> private static IEnumerable <string> ObjectIdentitiesToCodeNames(ClassObjSummary classObjSummary, IEnumerable <object> ObjectIdentifications) { switch (classObjSummary.ClassName.ToLower()) { case "cms.tree": case "cms.document": return(new DocumentQuery().Where(ObjectIdentitiesWhere(classObjSummary, ObjectIdentifications)).Columns(classObjSummary.CodeNameColumn).Select(x => (string)x.GetValue(classObjSummary.CodeNameColumn)).ToArray()); default: if (classObjSummary.ClassIsDocumentType) { return(new DocumentQuery(classObjSummary.ClassName).Where(ObjectIdentitiesWhere(classObjSummary, ObjectIdentifications)).Columns(classObjSummary.CodeNameColumn).Select(x => (string)x.GetValue(classObjSummary.CodeNameColumn)).ToArray()); } else if (classObjSummary.ClassIsCustomTable) { return(CustomTableItemProvider.GetItems(classObjSummary.ClassName).Where(ObjectIdentitiesWhere(classObjSummary, ObjectIdentifications)).Columns(classObjSummary.CodeNameColumn).Select(x => (string)x.GetValue(classObjSummary.CodeNameColumn)).ToArray()); } else if (classObjSummary.ClassIsForm) { return(BizFormItemProvider.GetItems(classObjSummary.ClassName).Where(ObjectIdentitiesWhere(classObjSummary, ObjectIdentifications)).Columns(classObjSummary.CodeNameColumn).Select(x => (string)x.GetValue(classObjSummary.CodeNameColumn)).ToArray()); } else { return(new ObjectQuery(classObjSummary.ClassName).Where(ObjectIdentitiesWhere(classObjSummary, ObjectIdentifications)).Columns(classObjSummary.CodeNameColumn).Select(x => (string)x.GetValue(classObjSummary.CodeNameColumn)).ToArray()); } } }
/// <summary> /// Returns a full where condition (for Binding Tables that bind on any object) to be used in filtering (ex repeaters). For property exampples, we will assume Demo.Foo, Demo.Bar, and Demo.FooBar /// </summary> /// <param name="BindingClass">The Binding Class Code Name. Ex: Demo.FooBar</param> /// <param name="ObjectClass">The Object Class Code Name (the thing that is bound to the current object through the binding table). Ex: Demo.Bar</param> /// <param name="ObjectIDFieldName">The Field Name of this object that matches the binding table's Left Field value. Ex: FooID (from Demo.Foo)</param> /// <param name="LeftFieldName">The Field Name of the binding class that contains this Object IDs value. Ex: FooID (from Demo.FooBar)</param> /// <param name="RightFieldName">The Field Name of the binding class that contains the related objects's identy value. Ex: BarID (from Demo.FooBar)</param> /// <param name="Values">list of object values (int IDs, GUIDs, or string CodeNames)</param> /// <param name="Identity">RelEnums.IdentityType of what value is stored in the binding table for the category, default is ID</param> /// <param name="Condition">RelEnums.ConditionType of what type of condition to generate.</param> /// <param name="ObjectIDTableName">The Table Name/Alias where the ObjectIDFieldName belongs. Only needed for the 'All' Condition and if the ObjectIDField and LeftFieldName are the same. Ex: Demo_FooBar</param> /// <returns>The Where Condition, If no object values provided or none found, returns 1=1</returns> public static string GetBindingWhere(string BindingClass, string ObjectClass, string ObjectIDFieldName, string LeftFieldName, string RightFieldName, IEnumerable <string> Values, IdentityType Identity = IdentityType.ID, ConditionType Condition = ConditionType.Any, string ObjectIDTableName = null) { LeftFieldName = GetBracketedColumnName(LeftFieldName); RightFieldName = GetBracketedColumnName(RightFieldName); ObjectIDFieldName = GetBracketedColumnName(ObjectIDFieldName); return(CacheHelper.Cache <string>(cs => { // Find class table name DataClassInfo ClassObj = DataClassInfoProvider.GetDataClassInfo(BindingClass); ClassObjSummary classObjSummary = GetClassObjSummary(ObjectClass); string WhereInValue = ""; string TableName = ClassObj.ClassTableName; int Count = 0; switch (Identity) { case IdentityType.ID: IEnumerable <int> ObjectIDs = ObjectIdentitiesToIDs(classObjSummary, Values); WhereInValue = (ObjectIDs.Count() > 0 ? string.Join(",", ObjectIDs) : "''"); Count = ObjectIDs.Count(); break; case IdentityType.Guid: IEnumerable <Guid> ObjectGUIDs = ObjectIdentitiesToGUIDs(classObjSummary, Values); WhereInValue = "'" + string.Join("','", ObjectGUIDs) + "'"; Count = ObjectGUIDs.Count(); break; case IdentityType.CodeName: IEnumerable <string> ObjectCodeNames = ObjectIdentitiesToCodeNames(classObjSummary, Values); WhereInValue = "'" + string.Join("','", ObjectCodeNames) + "'"; Count = ObjectCodeNames.Count(); break; } // If no related object IDs found, then completely ignore. if (Count == 0) { return "(1=1)"; } switch (Condition) { case ConditionType.Any: default: return string.Format("({0} in (Select {1} from {2} where {3} in ({4})))", ObjectIDFieldName, LeftFieldName, TableName, RightFieldName, WhereInValue); case ConditionType.All: return string.Format("(Select Count(*) from {0} where {0}.{1} = {2}{3} and {4} in ({5})) = {6}", TableName, LeftFieldName, (!string.IsNullOrWhiteSpace(ObjectIDTableName) ? ObjectIDTableName + "." : ""), ObjectIDFieldName, RightFieldName, WhereInValue, Count); case ConditionType.None: return string.Format("({0} not in (Select {1} from {2} where {3} in ({4})))", ObjectIDFieldName, LeftFieldName, TableName, RightFieldName, WhereInValue); } }, new CacheSettings(CacheMinutes, "GetBindingWhere", BindingClass, ObjectClass, ObjectIDFieldName, LeftFieldName, RightFieldName, string.Join("|", Values), Identity, Condition, ObjectIDTableName))); }
/// <summary> /// Gets the Object WHERE condition based on the given identities /// </summary> /// <param name="classObjSummary">The Class Object Summary</param> /// <param name="ObjectIdentifications">List of IDs, Guids, or CodeNames</param> /// <returns>The WHERE condition to select the objects (ex MyObjectID in (1,2,3) )</returns> private static string ObjectIdentitiesWhere(ClassObjSummary classObjSummary, IEnumerable <object> ObjectIdentifications) { List <Guid> Guids = new List <Guid>(); List <int> Ints = new List <int>(); List <string> Strings = new List <string>(); foreach (object ObjectIdentification in ObjectIdentifications) { Guid GuidVal = ValidationHelper.GetGuid(ObjectIdentification, Guid.Empty); int IntVal = ValidationHelper.GetInteger(ObjectIdentification, -1); string StringVal = ValidationHelper.GetString(ObjectIdentification, ""); if (GuidVal != Guid.Empty) { Guids.Add(GuidVal); } else if (IntVal > 0) { Ints.Add(IntVal); } else if (!string.IsNullOrWhiteSpace(StringVal)) { Strings.Add(SqlHelper.EscapeQuotes(StringVal)); } } string WhereCondition = ""; if (Guids.Count > 0 && !string.IsNullOrWhiteSpace(classObjSummary.GUIDColumn)) { WhereCondition = SqlHelper.AddWhereCondition(WhereCondition, string.Format("({0} in ('{1}'))", classObjSummary.GUIDColumn, string.Join("','", Guids.Select(x => x.ToString()).ToArray())), "OR"); } if (Ints.Count > 0 && !string.IsNullOrWhiteSpace(classObjSummary.IDColumn)) { WhereCondition = SqlHelper.AddWhereCondition(WhereCondition, string.Format("({0} in ({1}))", classObjSummary.IDColumn, string.Join(",", Ints.Select(x => x.ToString()).ToArray())), "OR"); } if (Strings.Count > 0 && !string.IsNullOrWhiteSpace(classObjSummary.CodeNameColumn)) { WhereCondition = SqlHelper.AddWhereCondition(WhereCondition, string.Format("({0} in ('{1}'))", classObjSummary.CodeNameColumn, string.Join("','", Strings.ToArray()), CurrentSiteID), "OR"); } return(!string.IsNullOrWhiteSpace(WhereCondition) ? WhereCondition : "(1=0)"); }
/// <summary> /// Gets a Class Object Summary based on the class name. /// </summary> /// <param name="ClassName">The Class Name</param> /// <returns>The Class Object Summary</returns> private static ClassObjSummary GetClassObjSummary(string ClassName) { return(CacheHelper.Cache <ClassObjSummary>(cs => { ClassObjSummary summaryObj = new ClassObjSummary(ClassName); DataClassInfo ClassObj = DataClassInfoProvider.GetDataClassInfo(ClassName); if (ClassObj != null) { summaryObj.ClassIsCustomTable = ClassObj.ClassIsCustomTable; summaryObj.ClassIsDocumentType = ClassObj.ClassIsDocumentType; summaryObj.ClassIsForm = ClassObj.ClassIsForm; } else { summaryObj.ClassIsCustomTable = false; summaryObj.ClassIsDocumentType = false; summaryObj.ClassIsForm = false; } // now get GUID and Code Name if possible. var ObjectClassFactoryObj = new InfoObjectFactory(ClassName); if (ObjectClassFactoryObj != null && ObjectClassFactoryObj.Singleton != null) { ObjectTypeInfo typeInfoObj = ((BaseInfo)ObjectClassFactoryObj.Singleton).TypeInfo; summaryObj.IDColumn = ValidationHelper.GetString(typeInfoObj.IDColumn, "").Replace(ObjectTypeInfo.COLUMN_NAME_UNKNOWN, ""); summaryObj.GUIDColumn = ValidationHelper.GetString(typeInfoObj.GUIDColumn, "").Replace(ObjectTypeInfo.COLUMN_NAME_UNKNOWN, ""); summaryObj.CodeNameColumn = ValidationHelper.GetString(typeInfoObj.CodeNameColumn, "").Replace(ObjectTypeInfo.COLUMN_NAME_UNKNOWN, ""); } else { // handle unique cases switch (ClassName.ToLower()) { case "cms.tree": case "cms.node": case "cms.root": summaryObj.IDColumn = "NodeID"; summaryObj.CodeNameColumn = "NodeAliasPath"; summaryObj.GUIDColumn = "NodeGUID"; break; case "cms.document": summaryObj.IDColumn = "DocumentID"; summaryObj.GUIDColumn = "DocumentGUID"; break; case "om.contactgroupmember": summaryObj.IDColumn = "ContactGroupMemberID"; break; case "om.membership": summaryObj.IDColumn = "MembershipID"; summaryObj.GUIDColumn = "MembershipGUID"; break; } // if still missing fields, try parsing XML if (string.IsNullOrWhiteSpace(summaryObj.CodeNameColumn) || string.IsNullOrWhiteSpace(summaryObj.GUIDColumn) || string.IsNullOrWhiteSpace(summaryObj.IDColumn)) { XmlDocument classXML = new XmlDocument(); classXML.LoadXml(ClassObj.ClassFormDefinition); if (string.IsNullOrWhiteSpace(summaryObj.IDColumn)) { try { summaryObj.IDColumn = classXML.SelectNodes("/form/field[@columntype='integer' and @isPK='true']").Item(0).Attributes["column"].Value; } catch (Exception) { // can't figure out that code name } } if (string.IsNullOrWhiteSpace(summaryObj.CodeNameColumn)) { try { summaryObj.CodeNameColumn = classXML.SelectNodes("/form/field[@columntype='text' and contains(@column, 'CodeName')]").Item(0).Attributes["column"].Value; } catch (Exception) { // can't figure out that code name } } if (string.IsNullOrWhiteSpace(summaryObj.GUIDColumn)) { try { summaryObj.GUIDColumn = classXML.SelectNodes("/form/field[@publicfield='false' and @columntype='guid' and system='true']").Item(0).Attributes["column"].Value; } catch (Exception) { // Can't figure out GUID } } } } if (cs.Cached) { cs.CacheDependency = CacheHelper.GetCacheDependency("cms.class|byname|" + ClassName); } return summaryObj; }, new CacheSettings(CacheMinutes, "GetClassObjSummary", ClassName))); }