private async Task <bool> ReloadSecondaryMetadata()
        {
            var methods             = _conn.ApplyAsync(new Command(@"<AML>
  <Item type='Method' action='get' select='config_id,core,name,method_code,comments,execution_allowed_to,method_type'>
    <method_type condition='ne'>JavaScript</method_type>
  </Item>
  <Item type='Method' action='get' select='config_id,core,name,comments,method_type'>
    <method_type>JavaScript</method_type>
  </Item>
</AML>").WithAction(CommandAction.ApplyAML), true, false).ToTask();
            var sysIdents           = _conn.ApplyAsync(@"<Item type='Identity' action='get' select='id,name'>
  <name condition='in'>'World', 'Creator', 'Owner', 'Manager', 'Innovator Admin', 'Super User'</name>
</Item>", true, true).ToTask();
            var sqls                = _conn.ApplyAsync("<Item type='SQL' action='get' select='id,name,type'></Item>", true, false).ToTask();
            var customProps         = _conn.ApplyAsync(@"<Item type='Property' action='get' select='name,source_id(id,name)'>
  <created_by_id condition='ne'>AD30A6D8D3B642F5A2AFED1A4B02BEFA</created_by_id>
  <source_id>
    <Item type='ItemType' action='get'>
      <core>1</core>
      <created_by_id>AD30A6D8D3B642F5A2AFED1A4B02BEFA</created_by_id>
    </Item>
  </source_id>
</Item>", true, false).ToTask();
            var polyLists           = _conn.ApplyAsync(@"<Item type='Property' action='get' select='data_source(id)'>
  <name>itemtype</name>
  <data_type>list</data_type>
  <source_id>
    <Item type='ItemType' action='get'>
      <implementation_type>polymorphic</implementation_type>
    </Item>
  </source_id>
</Item>", true, false).ToTask();
            var sequences           = _conn.ApplyAsync(@"<Item type='Sequence' action='get' select='name'></Item>", true, false).ToTask();
            var elementTypes        = _conn.ApplyAsync(@"<Item action='get' type='cmf_ElementType' select='generated_type'>
  <Relationships>
    <Item action='get' type='cmf_PropertyType' select='generated_type'>
    </Item>
  </Relationships>
</Item>", true, false).ToTask();
            var contentTypes        = _conn.ApplyAsync(@"<Item action='get' type='cmf_ContentType' select='linked_item_type'>
  <linked_item_type>
    <Item type='ItemType' action='get'>
    </Item>
  </linked_item_type>
</Item>", true, false).ToTask();
            var presentationConfigs = _conn.ApplyAsync(@"<Item type='ITPresentationConfiguration' action='get' select='id,related_id'>
  <related_id>
    <Item type='PresentationConfiguration' action='get' select='id'>
      <name condition='like'>*_TOC_Configuration</name>
      <color condition='is null'></color>
      <Relationships>
        <Item action='get' type='cui_PresentConfigWinSection' select='id' />
        <Item action='get' type='PresentationCommandBarSection' select='id,related_id(id)' />
      </Relationships>
    </Item>
  </related_id>
</Item>", true, false).ToTask();
            var serverEvents        = _conn.ApplyAsync(@"<Item type='Server Event' action='get' related_expand='0' select='source_id,server_event,related_id,sort_order'>
</Item>", true, false).ToTask();
            var morphae             = _conn.ApplyAsync(@"<Item type='Morphae' action='get' related_expand='0' select='source_id,related_id'>
</Item>", true, false).ToTask();

            Methods = (await methods).Items().Select(i => new Method(i)).ToList();

            _systemIdentities = (await sysIdents).Items()
                                .Select(i =>
            {
                var itemRef       = ItemReference.FromFullItem(i, false);
                itemRef.KeyedName = i.Property("name").AsString("");
                return(itemRef);
            }).ToDictionary(i => i.Unique);


            _sql = (await sqls).Items()
                   .Select(i =>
            {
                var itemRef       = Sql.FromFullItem(i, false);
                itemRef.KeyedName = i.Property("name").AsString("");
                itemRef.Type      = i.Property("type").AsString("");
                return(itemRef);
            }).ToDictionary(i => i.KeyedName.ToLowerInvariant(), StringComparer.OrdinalIgnoreCase);

            var r = (await customProps);

            foreach (var customProp in r.Items())
            {
                var itemType = customProp.SourceItem();
                _customProps[new ItemProperty()
                             {
                                 ItemType = itemType.Property("name").Value,
                                 ItemTypeId = itemType.Id(),
                                 Property = customProp.Property("name").Value,
                                 PropertyId = customProp.Id()
                             }] = new ItemReference("Property", customProp.Id())
                {
                    KeyedName = customProp.Property("name").Value
                };
            }

            PolyItemLists = (await polyLists).Items()
                            .OfType <Innovator.Client.Model.Property>()
                            .Select(i => new ItemReference("List", i.DataSource().Value)
            {
                KeyedName = i.DataSource().KeyedName().Value
            }).ToArray();

            Sequences = (await sequences).Items().Select(i => ItemReference.FromFullItem(i, true)).ToArray();

            try
            {
                CmfGeneratedTypes = new HashSet <string>((await elementTypes).Items().SelectMany(x =>
                {
                    var relations = x.Relationships().Select(y => y.Property("generated_type").Value).ToList();
                    relations.Add(x.Property("generated_type").Value);
                    return(relations);
                }));

                CmfLinkedTypes = (await contentTypes).Items().ToDictionary(x => x.Property("linked_item_type").Value, y => ItemReference.FromFullItem(y, true));
            }
            catch (ServerException)
            {
                //TODO: Do something when cmf types don't exist
            }

            try
            {
                TocPresentationConfigs.Clear();
                TocPresentationConfigs.UnionWith((await presentationConfigs)
                                                 .Items()
                                                 .Select(i => i.RelatedItem())
                                                 .Where(i => i.Relationships().Count() == 1 &&
                                                        i.Relationships().Single().RelatedId().KeyedName().AsString("").EndsWith("_TOC_Content"))
                                                 .Select(i => i.Id()));
            }
            catch (ServerException)
            {
                //TODO: Do something
            }

            foreach (var serverEvent in (await serverEvents).Items())
            {
                var ev = new ServerEvent(serverEvent);
                if (_itemTypesById.TryGetValue(serverEvent.SourceId().Value, out var itemType))
                {
                    itemType.ServerEvents.Add(ev);
                }
                var method = Methods.FirstOrDefault(m => m.Unique == ev.Method.Unique);
                if (method != null)
                {
                    method.IsServerEvent = true;
                }
            }

            foreach (var poly in (await morphae).Items())
            {
                if (_itemTypesById.TryGetValue(poly.SourceId().Value, out var itemType))
                {
                    itemType.Morphae.Add(poly.RelatedId().Attribute("name").Value ?? poly.RelatedId().KeyedName().Value);
                }
            }

            return(true);
        }
        private async Task <bool> ReloadSecondaryMetadata()
        {
            var methods             = _conn.ApplyAsync("<Item type='Method' action='get' select='config_id,core,name'></Item>", true, false).ToTask();
            var sysIdents           = _conn.ApplyAsync(@"<Item type='Identity' action='get' select='id,name'>
                                      <name condition='in'>'World', 'Creator', 'Owner', 'Manager', 'Innovator Admin', 'Super User'</name>
                                    </Item>", true, true).ToTask();
            var sqls                = _conn.ApplyAsync("<Item type='SQL' action='get' select='id,name,type'></Item>", true, false).ToTask();
            var customProps         = _conn.ApplyAsync(@"<Item type='Property' action='get' select='name,source_id(id,name)'>
                                          <created_by_id condition='ne'>AD30A6D8D3B642F5A2AFED1A4B02BEFA</created_by_id>
                                          <source_id>
                                            <Item type='ItemType' action='get'>
                                              <core>1</core>
                                              <created_by_id>AD30A6D8D3B642F5A2AFED1A4B02BEFA</created_by_id>
                                            </Item>
                                          </source_id>
                                        </Item>", true, false).ToTask();
            var polyLists           = _conn.ApplyAsync(@"<Item type='Property' action='get' select='data_source(id)'>
                                          <name>itemtype</name>
                                          <data_type>list</data_type>
                                          <source_id>
                                            <Item type='ItemType' action='get'>
                                              <implementation_type>polymorphic</implementation_type>
                                            </Item>
                                          </source_id>
                                        </Item>", true, false).ToTask();
            var sequences           = _conn.ApplyAsync(@"<Item type='Sequence' action='get' select='name'></Item>", true, false).ToTask();
            var elementTypes        = _conn.ApplyAsync(@"<Item action='get' type='cmf_ElementType' select='generated_type'>
                                                <Relationships>
                                                  <Item action='get' type='cmf_PropertyType' select='generated_type'>
                                                  </Item>
                                                </Relationships>
                                              </Item>", true, false).ToTask();
            var contentTypes        = _conn.ApplyAsync(@"<Item action='get' type='cmf_ContentType' select='linked_item_type'>
                                              <linked_item_type>
                                                <Item type='ItemType' action='get'>
                                                </Item>
                                              </linked_item_type>
                                            </Item>", true, false).ToTask();
            var presentationConfigs = _conn.ApplyAsync(@"<Item type='ITPresentationConfiguration' action='get' select='id,related_id'>
  <related_id>
    <Item type='PresentationConfiguration' action='get' select='id'>
      <name condition='like'>*_TOC_Configuration</name>
      <color condition='is null'></color>
      <Relationships>
        <Item action='get' type='cui_PresentConfigWinSection' select='id' />
        <Item action='get' type='PresentationCommandBarSection' select='id,related_id(id)' />
      </Relationships>
    </Item>
  </related_id>
</Item>", true, false).ToTask();

            _methods = (await methods).Items().Select(i =>
            {
                var method       = Method.FromFullItem(i, false);
                method.KeyedName = i.Property("name").AsString("");
                method.IsCore    = i.Property("core").AsBoolean(false);
                return(method);
            }).ToArray();


            _systemIdentities = (await sysIdents).Items()
                                .Select(i =>
            {
                var itemRef       = ItemReference.FromFullItem(i, false);
                itemRef.KeyedName = i.Property("name").AsString("");
                return(itemRef);
            }).ToDictionary(i => i.Unique);


            _sql = (await sqls).Items()
                   .Select(i =>
            {
                var itemRef       = Sql.FromFullItem(i, false);
                itemRef.KeyedName = i.Property("name").AsString("");
                itemRef.Type      = i.Property("type").AsString("");
                return(itemRef);
            }).ToDictionary(i => i.KeyedName.ToLowerInvariant(), StringComparer.OrdinalIgnoreCase);

            var           r = (await customProps);
            IReadOnlyItem itemType;

            foreach (var customProp in r.Items())
            {
                itemType = customProp.SourceItem();
                _customProps[new ItemProperty()
                             {
                                 ItemType = itemType.Property("name").Value,
                                 ItemTypeId = itemType.Id(),
                                 Property = customProp.Property("name").Value,
                                 PropertyId = customProp.Id()
                             }] = new ItemReference("Property", customProp.Id())
                {
                    KeyedName = customProp.Property("name").Value
                };
            }

            PolyItemLists = (await polyLists).Items()
                            .OfType <Innovator.Client.Model.Property>()
                            .Select(i => new ItemReference("List", i.DataSource().Value)
            {
                KeyedName = i.DataSource().KeyedName().Value
            }).ToArray();

            Sequences1 = (await sequences).Items().Select(i => ItemReference.FromFullItem(i, true)).ToArray();

            try
            {
                CmfGeneratedTypes = new HashSet <string>((await elementTypes).Items().SelectMany(x =>
                {
                    var relations = x.Relationships().Select(y => y.Property("generated_type").Value).ToList();
                    relations.Add(x.Property("generated_type").Value);
                    return(relations);
                }));

                CmfLinkedTypes = (await contentTypes).Items().ToDictionary(x => x.Property("linked_item_type").Value, y => ItemReference.FromFullItem(y, true));
            }
            catch (ServerException)
            {
                //TODO: Do something when cmf types don't exist
            }

            try
            {
                TocPresentationConfigs.Clear();
                TocPresentationConfigs.UnionWith((await presentationConfigs)
                                                 .Items()
                                                 .Select(i => i.RelatedItem())
                                                 .Where(i => i.Relationships().Count() == 1 &&
                                                        i.Relationships().Single().RelatedId().KeyedName().AsString("").EndsWith("_TOC_Content"))
                                                 .Select(i => i.Id()));
            }
            catch (ServerException)
            {
                //TODO: Do something
            }

            return(true);
        }