Esempio n. 1
0
        private void ApplyCriterionForField(
            SortCriterionContext context, SortCriterionConfiguration criterion)
        {
            // This uses the logic from ContentFieldsSortCriterion
            var partDefinition = _contentDefinitionManager.GetPartDefinition(criterion.PartName);

            if (partDefinition == null)
            {
                Logger.Error(T("Impossible to find a part definition with name {0}.",
                               criterion.PartName).Text);
            }
            else
            {
                var fieldDefinition = partDefinition.Fields
                                      .FirstOrDefault(fd => fd.Name.Equals(criterion.FieldName));
                if (fieldDefinition == null)
                {
                    Logger.Error(T("Impossible to find a field definition with name {0} within the part {1}.",
                                   criterion.FieldName, criterion.PartName).Text);
                }
                else
                {
                    var propertyName = string.Join(".",
                                                   // part
                                                   criterion.PartName,
                                                   // field
                                                   criterion.FieldName,
                                                   // field's property (e.g. LinkField.Text)
                                                   criterion.PropertyName ?? "");

                    var fieldTypeEditors = GetFieldEditors(fieldDefinition.FieldDefinition.Name, criterion.PropertyName);
                    if (fieldTypeEditors.Any())
                    {
                        // I think there should be only one
                        foreach (var fieldTypeEditor in fieldTypeEditors)
                        {
                            // use an alias with the join so that two filters on the same Field Type wont collide
                            var relationship = fieldTypeEditor.GetFilterRelationship(propertyName.ToSafeName());
                            // generate the predicate based on the editor which has been used
                            Action <IHqlExpressionFactory> predicate = y => y.Eq("PropertyName", propertyName);
                            // apply a filter for the specific property
                            context.Query = context.Query.Where(relationship, predicate);
                            // apply sort
                            context.Query = criterion.Ascending
                                ? context.Query.OrderBy(relationship, x => x.Asc(context.GetSortColumnName()))
                                : context.Query.OrderBy(relationship, x => x.Desc(context.GetSortColumnName()));
                        }
                    }
                    else
                    {
                        Logger.Error(T("Impossible to identify the IFieldTypeEditor to sort by {0}.", propertyName).Text);
                    }
                }
            }
        }
Esempio n. 2
0
 private void CriterionApplication(
     SortCriterionContext context, SortCriterionConfiguration criterionToUse)
 {
     if (criterionToUse.IsForField())
     {
         // order based on a field
         ApplyCriterionForField(context, criterionToUse);
     }
     else if (criterionToUse.IsForPart())
     {
         // order based on a part's property
         ApplyCriterionForPart(context, criterionToUse);
     }
     else if (criterionToUse.IsForProvider())
     {
         // we can invoke another provider to do its thing
         InvokeOtherProvider(context, criterionToUse);
     }
 }
Esempio n. 3
0
 private void InvokeOtherProvider(
     SortCriterionContext context, SortCriterionConfiguration criterion)
 {
     if (allSortProviders.Any())
     {
         // pretend we are the ProjectionManager here
         // look for the specific filter component
         var descriptor = AllDescriptors()
                          .FirstOrDefault(x =>
                                          x.Category == criterion.SortCriterionProviderCategory &&
                                          x.Type == criterion.SortCriterionProviderType);
         if (descriptor == null)
         {
             Logger.Error(
                 T("It was impossible to find a descriptor for sort provider with category {0} and type {1}.",
                   criterion.SortCriterionProviderCategory, criterion.SortCriterionProviderType).Text);
         }
         else
         {
             var criterionState = FormParametersHelper.ToDynamic(criterion.SortCriterionProviderState ?? "");
             criterionState.Sort = criterion.Ascending;
             var sortCriterionContext = new SortCriterionContext {
                 Query           = context.Query,
                 State           = criterionState,
                 QueryPartRecord = context.QueryPartRecord,
                 Tokens          = context.Tokens
             };
             descriptor.Sort(sortCriterionContext);
             context.Query = sortCriterionContext.Query;
         }
     }
     if (criterion.Children.Any())
     {
         foreach (var childCriterion in criterion.Children)
         {
             CriterionApplication(context, childCriterion);
         }
     }
 }
Esempio n. 4
0
        private void ApplyCriterionForPart(
            SortCriterionContext context, SortCriterionConfiguration criterion)
        {
            var groupedMembers = GetMemberGroups();

            var typeKey = groupedMembers.Keys
                          // the type for the ContentPartRecord we asked
                          .FirstOrDefault(k => k.FullName
                                          .Equals(criterion.PartRecordTypeName, StringComparison.OrdinalIgnoreCase));

            if (typeKey == null)
            {
                // quality of life: allow to input only the class name rather than
                // the full name. e.g. TitlePartRecord rather than
                // Orchard.Core.Title.Models.TitlePartRecord
                var types = groupedMembers.Keys
                            .Where(k => k.Name
                                   .Equals(criterion.PartRecordTypeName, StringComparison.OrdinalIgnoreCase));
                if (types.Count() == 1)
                {
                    typeKey = types.First();
                }
                else if (types.Count() > 1)
                {
                    var eMsg = T("The type asked for in the sort criterion does not have a unique name. Use its FullName.").Text
                               + Environment.NewLine
                               + T("The configuration is {0}.", criterion.PartRecordTypeName).Text
                               + Environment.NewLine
                               + T("Possible types found are: {0}", string.Join(Environment.NewLine, types.Select(t => t.FullName))).Text;
                    Logger.Error(eMsg);
                }
            }
            if (typeKey != null)
            {
                var membersGroup = groupedMembers[typeKey];
                var member       = membersGroup
                                   // the property we asked
                                   .FirstOrDefault(m => m.Property.Name
                                                   .Equals(criterion.PropertyName, StringComparison.OrdinalIgnoreCase));
                if (member != null)
                {
                    context.Query = criterion.Ascending
                        ? context.Query
                                    .OrderBy(alias =>
                                             alias.ContentPartRecord(typeKey),
                                             x => x.Asc(criterion.PropertyName))
                        : context.Query
                                    .OrderBy(alias =>
                                             alias.ContentPartRecord(typeKey),
                                             x => x.Desc(criterion.PropertyName));
                }
                else
                {
                    Logger.Error(
                        T("It was impossible to uniquely identify a property named {0} for type {1}. Perhaps it lacks a MemberBinding configuration?",
                          criterion.PropertyName, criterion.PartRecordTypeName).Text);
                }
            }
            else
            {
                Logger.Error(
                    T("It was impossible to uniquely identify a type for {0}. Perhaps it lacks a MemberBinding configuration?",
                      criterion.PartRecordTypeName).Text);
            }
            if (criterion.Children.Any())
            {
                foreach (var childCriterion in criterion.Children)
                {
                    CriterionApplication(context, childCriterion);
                }
            }
        }