private static void removeUnnecessaryLinksAttributes(savedqueryLayoutxmlGrid viewFeo, ViewExcelSheetsInfo excelData)
        {
            FetchEntityType   currentEt = excelData.fetchObj.Items.Where(x => x is FetchEntityType).Select(x => (FetchEntityType)x).FirstOrDefault();
            ViewsRelationsObj obj       = excelData.relationsList.Where(x => x.relationAlias == null).FirstOrDefault();

            if (currentEt != null && obj != null)
            {
                List <object> array = currentEt.Items.ToList();
                for (int i = array.Count - 1; i > -1; i--)
                {
                    object currentItem = array[i];
                    if (currentItem is FetchLinkEntityType)
                    {
                        FetchLinkEntityType link = currentItem as FetchLinkEntityType;
                        removeUnnecessaryAttributesInner(link, viewFeo);
                        if (link == null || link.visible == false)
                        {
                            if (link == null || link.Items.Count() == 0)
                            {
                                array.RemoveAt(i);
                            }
                        }
                    }
                    else if (currentItem is FetchAttributeType)
                    {
                        FetchAttributeType attr = currentItem as FetchAttributeType;
                        if (!(viewFeo.row.cell.Where(x => x.name == attr.name).Count() > 0) && attr.name != obj.entityMetadata.PrimaryIdAttribute)
                        {
                            array.RemoveAt(i);
                        }
                    }
                }
                currentEt.Items = array.ToArray();
            }
        }
 private static void removeUnnecessaryAttributesInner(FetchLinkEntityType link, savedqueryLayoutxmlGrid viewFeo)
 {
     if (link.Items != null)
     {
         List <object> array = link.Items.ToList();
         for (int i = array.Count - 1; i > -1; i--)
         {
             object currentItem = array[i];
             if (currentItem is FetchAttributeType)
             {
                 FetchAttributeType attr  = currentItem as FetchAttributeType;
                 string             alias = string.Format("{0}.{1}", link.alias, attr.name);
                 if (!(viewFeo.row.cell.Where(x => x.name == attr.name).Count() > 0))
                 {
                     array.RemoveAt(i);
                 }
             }
         }
         link.Items = array.ToArray();
     }
 }
Example #3
0
 private static IEnumerable <FetchAttributeInfo> GetAllAttributes(string entityLogicalName, FetchAttributeType attribute)
 {
     yield return(new FetchAttributeInfo(entityLogicalName, attribute));
 }
 public FetchAttributeInfo(string entityLogicalName, FetchAttributeType attribute)
 {
     Attribute         = attribute;
     EntityLogicalName = entityLogicalName;
 }
Example #5
0
        private static List <T> PerformCountColumnAggregation <T>(FetchType fe, List <T> entities, FetchAttributeType aggregate, List <FetchAttributeInfo> attributes) where T : Entity
        {
            var aggregateEntities = new Dictionary <string, T>();
            var distinctValues    = new HashSet <string>();

            foreach (var entity in entities.Where(e => e.HasAliasedAttribute(aggregate.alias) &&
                                                  e.GetAliasedValue <object>(aggregate.alias) != null))
            {
                T   temp;
                var key = new StringBuilder();
                foreach (var attribute in attributes.Where(a => a.Attribute != aggregate))
                {
                    key.AppendFormat("{0}.{1},{2}|", attribute.EntityLogicalName, attribute.Attribute.alias,
                                     entity.GetAliasedValue <object>(attribute.Attribute.alias));
                }

                if (fe.distinctSpecified && fe.distinct)
                {
                    var value = key + "~" + entity.GetAliasedValue <object>(aggregate.alias);
                    if (distinctValues.Contains(value))
                    {
                        continue;
                    }

                    distinctValues.Add(value);
                }

                if (aggregateEntities.TryGetValue(key.ToString(), out temp))
                {
                    temp[aggregate.alias] = temp.GetAliasedValue <int>(aggregate.alias) + 1;
                }
                else
                {
                    entity[aggregate.alias] = new AliasedValue(null, aggregate.alias, 1);
                    aggregateEntities.Add(key.ToString(), entity);
                }
            }
            return(aggregateEntities.Values.ToList());
        }
Example #6
0
 // ReSharper disable once UnusedParameter.Local
 private static void ProcessFetchXmlItem(LocalCrmDatabaseOrganizationService service, LinkEntity entityLink, FetchAttributeType attribute)
 {
     if (!entityLink.Columns.Columns.Contains(attribute.name))
     {
         entityLink.Columns.AddColumn(attribute.name);
     }
 }
Example #7
0
        internal FetchAttributeType AddAttribute(string colName, Func <FetchAttributeType, bool> predicate, IAttributeMetadataCache metadata, out bool added, out FetchLinkEntityType linkEntity)
        {
            var parts = colName.Split('.');

            if (parts.Length == 1)
            {
                added = false;
                return(Entity.FindAliasedAttribute(colName, predicate, out linkEntity));
            }

            var entityName = parts[0];
            var attr       = new FetchAttributeType {
                name = parts[1].ToLowerInvariant()
            };

            if (Alias == entityName)
            {
                linkEntity = null;

                var meta = metadata[Entity.name].Attributes.SingleOrDefault(a => a.LogicalName == attr.name && a.AttributeOf == null);
                if (meta == null && (attr.name.EndsWith("name") || attr.name.EndsWith("type")))
                {
                    var logicalName = attr.name.Substring(0, attr.name.Length - 4);
                    meta = metadata[Entity.name].Attributes.SingleOrDefault(a => a.LogicalName == logicalName && a.AttributeOf == null);

                    if (meta != null)
                    {
                        attr.name = logicalName;
                    }
                }

                if (Entity.Items != null)
                {
                    var existing = Entity.Items.OfType <FetchAttributeType>().FirstOrDefault(a => a.name == attr.name || a.alias == attr.name);
                    if (existing != null && (predicate == null || predicate(existing)))
                    {
                        added = false;
                        return(existing);
                    }
                }

                Entity.AddItem(attr);
            }
            else
            {
                linkEntity = Entity.FindLinkEntity(entityName);

                var meta = metadata[linkEntity.name].Attributes.SingleOrDefault(a => a.LogicalName == attr.name && a.AttributeOf == null);
                if (meta == null && (attr.name.EndsWith("name") || attr.name.EndsWith("type")))
                {
                    var logicalName = attr.name.Substring(0, attr.name.Length - 4);
                    meta = metadata[linkEntity.name].Attributes.SingleOrDefault(a => a.LogicalName == logicalName && a.AttributeOf == null);

                    if (meta != null)
                    {
                        attr.name = logicalName;
                    }
                }

                if (linkEntity.Items != null)
                {
                    var existing = linkEntity.Items.OfType <FetchAttributeType>().FirstOrDefault(a => a.name == attr.name || a.alias == attr.name);
                    if (existing != null && (predicate == null || predicate(existing)))
                    {
                        added = false;
                        return(existing);
                    }
                }

                linkEntity.AddItem(attr);
            }

            added = true;
            return(attr);
        }
Example #8
0
        private bool ContainsSortOnLookupAttribute(IAttributeMetadataCache metadata, string logicalName, object[] items, out FetchAttributeType lookupAttr)
        {
            if (items == null)
            {
                lookupAttr = null;
                return(false);
            }

            foreach (var order in items.OfType <FetchOrderType>())
            {
                if (!String.IsNullOrEmpty(order.alias))
                {
                    lookupAttr = items.OfType <FetchAttributeType>().FirstOrDefault(attr => attr.alias.Equals(order.alias, StringComparison.OrdinalIgnoreCase));
                }
                else
                {
                    lookupAttr = items.OfType <FetchAttributeType>().FirstOrDefault(attr => attr.name.Equals(order.attribute, StringComparison.OrdinalIgnoreCase));
                }

                if (lookupAttr == null)
                {
                    continue;
                }

                var meta         = metadata[logicalName];
                var attrName     = lookupAttr.name;
                var attrMetadata = meta.Attributes.SingleOrDefault(a => a.LogicalName.Equals(attrName, StringComparison.OrdinalIgnoreCase));

                if (attrMetadata is LookupAttributeMetadata)
                {
                    return(true);
                }
            }

            foreach (var linkEntity in items.OfType <FetchLinkEntityType>())
            {
                if (ContainsSortOnLookupAttribute(metadata, linkEntity.name, linkEntity.Items, out lookupAttr))
                {
                    return(true);
                }
            }

            lookupAttr = null;
            return(false);
        }
Example #9
0
        private static List <T> PerformSumColumnAggregation <T>(FetchType fe, List <T> entities, FetchAttributeType aggregate, List <FetchAttributeInfo> attributes) where T : Entity
        {
            var aggregateEntities    = new Dictionary <string, T>();
            var distinctValues       = new HashSet <string>();
            var resultAttributeNames = new HashSet <string>(attributes.Where(a => a.Attribute.aggregateSpecified).Select(a => a.Attribute.alias));

            foreach (var entity in entities.Where(e => e.HasAliasedAttribute(aggregate.alias) &&
                                                  e.GetAliasedValue <object>(aggregate.alias) != null))
            {
                var key = new StringBuilder();
                foreach (var attribute in attributes.Where(a => a.Attribute != aggregate))
                {
                    key.AppendFormat("{0}.{1},{2}|", attribute.EntityLogicalName, attribute.Attribute.alias,
                                     entity.GetAliasedValue <object>(attribute.Attribute.alias));
                }

                if (fe.distinctSpecified && fe.distinct)
                {
                    var value = key + "~" + entity.GetAliasedValue <object>(aggregate.alias);
                    if (distinctValues.Contains(value))
                    {
                        continue;
                    }

                    distinctValues.Add(value);
                }

                var aliasValue = entity.GetAliasedValue <Money>(aggregate.alias).GetValueOrDefault();
                if (aggregateEntities.TryGetValue(key.ToString(), out T temp))
                {
                    var current = (Money)temp.GetAttributeValue <AliasedValue>(aggregate.alias).Value;
                    current.Value += aliasValue;
                }
                else
                {
                    entity[aggregate.alias] = new AliasedValue(null, aggregate.alias, new Money(aliasValue));
                    foreach (var att in entity.Attributes.Keys.ToList().Where(k => !resultAttributeNames.Contains(k)))
                    {
                        entity.Attributes.Remove(att);
                    }
                    aggregateEntities.Add(key.ToString(), entity);
                }
            }
            return(aggregateEntities.Values.ToList());
        }
 private static void addNewAttributeToFech(FetchType fecthObj, string attributeName, ViewsRelationsObj currentEntity)
 {
     if (string.IsNullOrEmpty(currentEntity.relationAlias))
     {
         FetchEntityType currentEt = fecthObj.Items.Where(x => x is FetchEntityType && ((FetchEntityType)x).name.Equals(currentEntity.entity, StringComparison.InvariantCultureIgnoreCase)).Select(x => (FetchEntityType)x).FirstOrDefault();
         if (currentEt != null)
         {
             List <object>      items     = currentEt.Items.ToList();
             FetchAttributeType attribute = items.Where(x => x is FetchAttributeType && ((FetchAttributeType)x).name.Equals(attributeName)).Select(x => (FetchAttributeType)x).FirstOrDefault();
             if (attribute == null)
             {
                 List <object> objectList = currentEt.Items.ToList();
                 objectList.Add(new FetchAttributeType()
                 {
                     name = attributeName
                 });
                 currentEt.Items = objectList.ToArray();
             }
         }
     }
     else
     {
         FetchEntityType currentEt = fecthObj.Items.Where(x => x is FetchEntityType).Select(x => (FetchEntityType)x).FirstOrDefault();
         if (currentEt != null)
         {
             FetchLinkEntityType currentLink = currentEt.Items.Where(x => x is FetchLinkEntityType &&
                                                                     ((FetchLinkEntityType)x).alias.Equals(currentEntity.relationAlias, StringComparison.InvariantCultureIgnoreCase))
                                               .Select(x => (FetchLinkEntityType)x).FirstOrDefault();
             if (currentLink != null)
             {
                 List <object>      items     = currentLink.Items.ToList();
                 FetchAttributeType attribute = items.Where(x => x is FetchAttributeType && ((FetchAttributeType)x).name.Equals(attributeName)).Select(x => (FetchAttributeType)x).FirstOrDefault();
                 if (attribute == null)
                 {
                     List <object> objectList = currentLink.Items.ToList();
                     objectList.Add(new FetchAttributeType()
                     {
                         name = attributeName
                     });
                     currentLink.Items = objectList.ToArray();
                 }
             }
             else
             {
                 List <object> objectList = currentEt.Items.ToList();
                 objectList.Add(new FetchLinkEntityType()
                 {
                     Items              = new object[] { (new FetchAttributeType()
                         {
                             name = attributeName
                         }) },
                     name               = currentEntity.entity,
                     alias              = currentEntity.relationAlias,
                     linktype           = "outer",
                     from               = currentEntity.relationFrom,
                     to                 = currentEntity.relationTo,
                     intersect          = false,
                     intersectSpecified = false,
                     visible            = false,
                     visibleSpecified   = false,
                 });
                 currentEt.Items = objectList.ToArray();
             }
         }
     }
 }