Ejemplo n.º 1
0
        public List <GraphQlAfElementTemplate> GetAfElementTemplates(ResolveFieldContext context, string aAfDatabasePath, string[] nameFilter = null)
        {
            aAfDatabasePath = cleanupPath(aAfDatabasePath);
            if (attemptLogin(context, getPiSystemName(aAfDatabasePath)))
            {
                var aAfDb = AFDatabase.FindObject(aAfDatabasePath) as AFDatabase;
                if (aAfDb == null)
                {
                    context.Errors.Add(new ExecutionError($"AFDatabase not found."));
                    return(null);
                }

                var nameFilterStrings = nameFilter != null?nameFilter.ToList() : new List <string>();

                var afElementsField = GraphQlHelpers.GetFieldFromContext(context, "afElements");

                var returnObject = new ConcurrentBag <GraphQlAfElementTemplate>();

                var aAfElementTemplateList = aAfDb.ElementTemplates;
                Parallel.ForEach(aAfElementTemplateList, aAfElementTemplate =>
                {
                    if (nameFilterStrings.Count == 0 || nameFilterStrings.Contains(aAfElementTemplate.Name))
                    {
                        var graphQlAfElementTemplate = new GraphQlAfElementTemplate(aAfElementTemplate, afElementsField);
                        returnObject.Add(graphQlAfElementTemplate);
                    }
                });
                return(returnObject.OrderBy(x => x.name).ToList());
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
 public GraphQlPiSystem GetPiSystem(ResolveFieldContext context, string name)
 {
     if (name == null)
     {
         string aDedicatedPiSystemName = ConfigurationManager.AppSettings["dedicatedPiSystem"];
         if (aDedicatedPiSystemName == "")
         {
             context.Errors.Add(new ExecutionError($"No PISystem specified."));
             return(null);
         }
         else
         {
             name = aDedicatedPiSystemName;
         }
     }
     if (attemptLogin(context, name))
     {
         var afDbsField = GraphQlHelpers.GetFieldFromContext(context, "afDbs");
         return(new GraphQlPiSystem(aPiSystem, afDbsField));
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 3
0
 public GraphQlAfElement GetAfElementByPath(ResolveFieldContext context, string aAfElementPath)
 {
     aAfElementPath = cleanupPath(aAfElementPath);
     if (attemptLogin(context, getPiSystemName(aAfElementPath)))
     {
         var aAfElementSearch = AFElement.FindElementsByPath(new string[] { aAfElementPath }, null);
         if (aAfElementSearch.Count == 0)
         {
             context.Errors.Add(new ExecutionError($"AFElement path '{aAfElementPath}' not correct."));
             return(null);
         }
         else
         {
             var aAfElement        = aAfElementSearch.First() as AFElement;
             var afElementsField   = GraphQlHelpers.GetFieldFromContext(context, "afElements");
             var afAttributesField = GraphQlHelpers.GetFieldFromContext(context, "afAttributes");
             var graphQlAfElement  = new GraphQlAfElement(aAfElement, afElementsField, afAttributesField);
             return(graphQlAfElement);
         }
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 4
0
        public QLFamilyInstanceResolve(FamilyInstance _familyInstance, Field queryFieldForParameters)
        {
            id   = _familyInstance.Id.ToString();
            name = _familyInstance.Name;

            if (queryFieldForParameters != null)
            {
                var returnElementsObject = new ConcurrentBag <QLParameter>();

                var nameFiltersContained = GraphQlHelpers.GetArgumentStrings(queryFieldForParameters, "nameFilter");

                var          _doc       = ResolverEntry.Doc;
                ParameterSet objectList = _familyInstance.Parameters;

                //Parallel.ForEach(objectList, x =>
                foreach (Parameter x in objectList)
                {
                    if (nameFiltersContained.Count == 0 || nameFiltersContained.Contains(x.Definition.Name))
                    {
                        returnElementsObject.Add(new QLParameter()
                        {
                            id             = x.Id.ToString(),
                            name           = x.Definition.Name,
                            value          = x.AsValueString() == null ? x.AsString() : x.AsValueString(),
                            userModifiable = x.UserModifiable,
                            isReadOnly     = x.IsReadOnly
                        });
                    }
                }

                qlParameters = returnElementsObject.OrderBy(x => x.name).ToList();
            }
        }
Ejemplo n.º 5
0
        public GraphQlAfElementTemplate(AFElementTemplate aAfElementTemplate, Field afElementsField)
        {
            name = aAfElementTemplate.Name;
            ThisAfElementTemplate = aAfElementTemplate;
            if (afElementsField != null)
            {
                var afElementsNameFilterStrings           = GraphQlHelpers.GetArgumentStrings(afElementsField, "nameFilter");
                var afElementsAttributeValueFilterStrings = GraphQlHelpers.GetArgumentStrings(afElementsField, "attributeValueFilter");

                var afElementsChildField   = GraphQlHelpers.GetFieldFromSelectionSet(afElementsField, "afElements");
                var afAttributesChildField = GraphQlHelpers.GetFieldFromSelectionSet(afElementsField, "afAttributes");

                var returnObject = new ConcurrentBag <GraphQlAfElement>();

                List <AFElement> afElementList = aAfElementTemplate.FindInstantiatedElements(true, OSIsoft.AF.AFSortField.Name, OSIsoft.AF.AFSortOrder.Ascending, 10000).Select(x => x as AFElement).Where(x => x != null).ToList();
                Parallel.ForEach(afElementList, aAfChildElement =>
                {
                    if (GraphQlHelpers.JudgeElementOnFilters(aAfChildElement, afElementsNameFilterStrings, afElementsAttributeValueFilterStrings))
                    {
                        returnObject.Add(new GraphQlAfElement(aAfChildElement, afElementsChildField, afAttributesChildField));
                    }
                });

                afElements = returnObject.OrderBy(x => x.name).ToList();
            }
        }
Ejemplo n.º 6
0
        //public QLFamilyCategoryResolve(Category aCategory, Field queryFieldForFamilies, Field qlFamilyInstancesField)
        //{
        //    id = aCategory.Id.ToString();
        //    name = aCategory.Name;

        //    if (queryFieldForFamilies != null)
        //    {

        //        var returnElementsObject = new ConcurrentBag<QLFamily>();

        //        var nameFiltersContained = GraphQlHelpers.GetArgumentStrings(queryFieldForFamilies, "nameFilter");
        //        //var queryFieldForFamilySymbols = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForFamilies, "qlFamilySymbols");
        //        //var queryFieldForFamily2Instances = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForFamilies, "qlFamilyInstances");

        //        var _doc = ResolverEntry.Doc;
        //        List<Family> objectList = new FilteredElementCollector(_doc).OfClass(typeof(Family)).Select(x => (x as Family)).Where(x => x.FamilyCategory.Id.ToString() == id).ToList();

        //        //Parallel.ForEach(objectList, x =>
        //        foreach(var x in objectList)
        //        {
        //            if (nameFiltersContained.Count == 0 || nameFiltersContained.Contains(x.Name))
        //            {
        //                //returnElementsObject.Add(new QLFamilyResolve(x, queryFieldForFamilySymbols, queryFieldForFamily2Instances));
        //                returnElementsObject.Add(new QLFamilyResolve(x, queryFieldForFamilies));
        //            }
        //        }



        //        qlFamilies = returnElementsObject.OrderBy(x => x.name).ToList();

        //    }

        //    if (qlFamilyInstancesField != null)
        //    {

        //        var returnElementsObject = new ConcurrentBag<QLFamilyInstance>();

        //        var nameFiltersContained = GraphQlHelpers.GetArgumentStrings(qlFamilyInstancesField, "nameFilter");
        //        //var queryFieldForParameters = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForFamilies, "qlParameters");

        //        var _doc = ResolverEntry.Doc;
        //        List<FamilyInstance> objectList = new FilteredElementCollector(_doc).OfClass(typeof(FamilyInstance))
        //            .Select(x => (x as FamilyInstance)).Where(x => x.Symbol.Family.FamilyCategory.Id.ToString() == id).ToList();

        //        //Parallel.ForEach(objectList, x =>
        //        foreach (var x in objectList)
        //        {
        //            if (nameFiltersContained.Count == 0 || nameFiltersContained.Contains(x.Name))
        //            {
        //                returnElementsObject.Add(new QLFamilyInstanceResolve(x, queryFieldForFamilies));
        //            }
        //        }

        //        qlFamilyInstances = returnElementsObject.OrderBy(x => x.name).ToList();
        //    }

        //}
        public QLFamilyCategoryResolve(Category aCategory, object aFieldOrContext)
        {
            id   = aCategory.Id.ToString();
            name = aCategory.Name;

            var queryFieldForFamilies = GraphQlHelpers.GetFieldFromFieldOrContext(aFieldOrContext, "qlFamilies");

            if (queryFieldForFamilies != null)
            {
                var returnElementsObject = new ConcurrentBag <QLFamily>();

                var nameFiltersContained = GraphQlHelpers.GetArgumentStrings(queryFieldForFamilies, "nameFilter");
                //var queryFieldForFamilySymbols = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForFamilies, "qlFamilySymbols");
                //var queryFieldForFamily2Instances = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForFamilies, "qlFamilyInstances");

                var           _doc       = ResolverEntry.Doc;
                List <Family> objectList = new FilteredElementCollector(_doc).OfClass(typeof(Family)).Select(x => (x as Family)).Where(x => x.FamilyCategory.Id.ToString() == id).ToList();

                //Parallel.ForEach(objectList, x =>
                foreach (var x in objectList)
                {
                    if (nameFiltersContained.Count == 0 || nameFiltersContained.Contains(x.Name))
                    {
                        //returnElementsObject.Add(new QLFamilyResolve(x, queryFieldForFamilySymbols, queryFieldForFamily2Instances));
                        returnElementsObject.Add(new QLFamilyResolve(x, queryFieldForFamilies));
                    }
                }



                qlFamilies = returnElementsObject.OrderBy(x => x.name).ToList();
            }

            var qlFamilyInstancesField = GraphQlHelpers.GetFieldFromFieldOrContext(aFieldOrContext, "qlFamilyInstances");

            if (qlFamilyInstancesField != null)
            {
                var returnElementsObject = new ConcurrentBag <QLFamilyInstance>();

                var nameFiltersContained = GraphQlHelpers.GetArgumentStrings(qlFamilyInstancesField, "nameFilter");
                //var queryFieldForParameters = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForFamilies, "qlParameters");

                var _doc = ResolverEntry.Doc;
                List <FamilyInstance> objectList = new FilteredElementCollector(_doc).OfClass(typeof(FamilyInstance))
                                                   .Select(x => (x as FamilyInstance)).Where(x => x.Symbol.Family.FamilyCategory.Id.ToString() == id).ToList();

                //Parallel.ForEach(objectList, x =>
                foreach (var x in objectList)
                {
                    if (nameFiltersContained.Count == 0 || nameFiltersContained.Contains(x.Name))
                    {
                        returnElementsObject.Add(new QLFamilyInstanceResolve(x, queryFieldForFamilies));
                    }
                }

                qlFamilyInstances = returnElementsObject.OrderBy(x => x.name).ToList();
            }
        }
        //public QLViewScheduleResolve(ViewSchedule aViewSchedule, Field queryFieldForViewScheduleData)
        //{
        //    id = aViewSchedule.Id.ToString();
        //    name = aViewSchedule.Name;

        //    if (queryFieldForViewScheduleData != null)
        //    {
        //        try
        //        {
        //            qlViewScheduleData = ResolverEntry.aRevitTask.Run<QLViewScheduleData>(app =>
        //            {
        //                ////https://thebuildingcoder.typepad.com/blog/2012/05/the-schedule-api-and-access-to-schedule-data.html
        //                TableData table = aViewSchedule.GetTableData();
        //                var rowCount = aViewSchedule.GetTableData().GetSectionData(SectionType.Body).NumberOfRows;
        //                var colCount = aViewSchedule.GetTableData().GetSectionData(SectionType.Body).NumberOfColumns;

        //                QLViewScheduleData data = new QLViewScheduleData();
        //                data.headers = new List<string>();
        //                data.rows = new List<QLViewScheduleRow>();

        //                for (int i = 0; i < rowCount; i++)
        //                {
        //                    QLViewScheduleRow newRow = new QLViewScheduleRow();
        //                    newRow.id = i.ToString();
        //                    newRow.cells = new List<string>();
        //                    for (int j = 0; j < colCount; j++)
        //                    {
        //                        var cellString = aViewSchedule.GetCellText(SectionType.Body, i, j);
        //                        if (i == 0)
        //                        {
        //                            data.headers.Add(cellString);
        //                        }
        //                        else
        //                        {
        //                            newRow.cells.Add(cellString);
        //                        }
        //                    }
        //                    if (i == 0)
        //                    {
        //                        i++; // skip 2nd row - it's always blank
        //                    }
        //                    else
        //                    {
        //                        data.rows.Add(newRow);
        //                    }
        //                }
        //                return data;
        //            }).Result;
        //        }
        //        catch (Exception ex)
        //        {
        //            var m = ex.Message;
        //        }
        //    }
        //}
        public QLViewScheduleResolve(ViewSchedule aViewSchedule, object aFieldOrContext)
        {
            id   = aViewSchedule.Id.ToString();
            name = aViewSchedule.Name;

            var queryFieldForViewScheduleData = GraphQlHelpers.GetFieldFromFieldOrContext(aFieldOrContext, "qlViewScheduleData");

            if (queryFieldForViewScheduleData != null)
            {
                try
                {
                    qlViewScheduleData = ResolverEntry.aRevitTask.Run <QLViewScheduleData>(app =>
                    {
                        ////https://thebuildingcoder.typepad.com/blog/2012/05/the-schedule-api-and-access-to-schedule-data.html
                        TableData table = aViewSchedule.GetTableData();
                        var rowCount    = aViewSchedule.GetTableData().GetSectionData(SectionType.Body).NumberOfRows;
                        var colCount    = aViewSchedule.GetTableData().GetSectionData(SectionType.Body).NumberOfColumns;

                        QLViewScheduleData data = new QLViewScheduleData();
                        data.headers            = new List <string>();
                        data.rows = new List <QLViewScheduleRow>();

                        for (int i = 0; i < rowCount; i++)
                        {
                            QLViewScheduleRow newRow = new QLViewScheduleRow();
                            newRow.id    = i.ToString();
                            newRow.cells = new List <string>();
                            for (int j = 0; j < colCount; j++)
                            {
                                var cellString = aViewSchedule.GetCellText(SectionType.Body, i, j);
                                if (i == 0)
                                {
                                    data.headers.Add(cellString);
                                }
                                else
                                {
                                    newRow.cells.Add(cellString);
                                }
                            }
                            if (i == 0)
                            {
                                i++; // skip 2nd row - it's always blank
                            }
                            else
                            {
                                data.rows.Add(newRow);
                            }
                        }
                        return(data);
                    }).Result;
                }
                catch (Exception ex)
                {
                    var m = ex.Message;
                }
            }
        }
Ejemplo n.º 8
0
        public GraphQlAfAttribute(AFAttribute aAfAttribute, Field afAttributesField = null, Field tsPlotValuesField = null)
        {
            AFValue aAfValue = aAfAttribute.GetValue();

            name            = aAfAttribute.Name;
            ThisAfAttribute = aAfAttribute;
            value           = aAfValue.Value?.ToString();
            uom             = aAfAttribute.DisplayUOM?.Abbreviation;

            if (aAfAttribute.DataReference?.Name == "PI Point")
            {
                timeStamp = aAfValue.Timestamp.UtcTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
            }

            if (afAttributesField != null)
            {
                var afAttributesNameFilterStrings = GraphQlHelpers.GetArgumentStrings(afAttributesField, "nameFilter");
                var afAttributesChildField        = GraphQlHelpers.GetFieldFromSelectionSet(afAttributesField, "afAttributes");

                var returnAttributesObject = new ConcurrentBag <GraphQlAfAttribute>();
                var afAttributeList        = aAfAttribute.Attributes.ToList <AFAttribute>();
                Parallel.ForEach(afAttributeList, aAfChildAttribute =>
                {
                    if (afAttributesNameFilterStrings.Count == 0 || afAttributesNameFilterStrings.Contains(aAfChildAttribute.Name))
                    {
                        returnAttributesObject.Add(new GraphQlAfAttribute(aAfAttribute, afAttributesChildField));
                    }
                });
                afAttributes = returnAttributesObject.OrderBy(x => x.name).ToList();
            }

            if (tsPlotValuesField != null)
            {
                if (aAfAttribute.DataReference?.Name == "PI Point")
                {
                    var plotDensity   = GraphQlHelpers.GetArgumentDouble(tsPlotValuesField, "plotDensity");
                    var startDateTime = GraphQlHelpers.GetArgumentDateTime(tsPlotValuesField, "startDateTime");
                    var endDateTime   = GraphQlHelpers.GetArgumentDateTime(tsPlotValuesField, "endDateTime");

                    var timeRange = new AFTimeRange(startDateTime, endDateTime);

                    AFValues asdf = ThisAfAttribute.GetValues(timeRange, (int)plotDensity, null);

                    var returnObject = new ConcurrentBag <GraphQlTsValue>();
                    foreach (AFValue aAfTsValue in asdf)
                    {
                        returnObject.Add(new GraphQlTsValue()
                        {
                            timeStamp = aAfTsValue.Timestamp.UtcTime.ToString("yyyy-MM-ddTHH:mm:ssZ"),
                            value     = aAfTsValue.Value.ToString()
                        });
                    }
                    tsPlotValues = returnObject.OrderBy(x => x.timeStamp).ToList();
                }
            }
        }
Ejemplo n.º 9
0
        //public QLElementCollectionResolve(ICollection<ElementId> _ids, Field queryFieldForFamilyInstances, Field queryFieldForFabricationParts)
        //{
        //    elementIds = _ids.Select(x=>x.ToString()).ToList();

        //    if (queryFieldForFamilyInstances != null || queryFieldForFabricationParts != null)
        //    {

        //        var _doc = ResolverEntry.Doc;

        //        var returnFamilyInstancesObject = new ConcurrentBag<QLFamilyInstance>();
        //        var returnFabricationPartsObject = new ConcurrentBag<QLFabricationPart>();


        //        foreach (var aId in _ids)
        //        {
        //            var aElement = _doc.GetElement(aId);
        //            if (aElement == null)
        //            {

        //            }
        //            else
        //            {
        //                if (aElement is FamilyInstance && queryFieldForFamilyInstances != null)
        //                {
        //                    var nameFiltersContained = GraphQlHelpers.GetArgumentStrings(queryFieldForFamilyInstances, "nameFilter");
        //                    var queryFieldForParameters = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForFamilyInstances, "qlParameters");
        //                    if (nameFiltersContained.Count == 0 || nameFiltersContained.Contains(aElement.Name))
        //                    {
        //                        returnFamilyInstancesObject.Add(new QLFamilyInstanceResolve(aElement as FamilyInstance, queryFieldForParameters));
        //                    }
        //                }
        //                if (aElement is FabricationPart && queryFieldForFabricationParts != null)
        //                {
        //                    var nameFiltersContained = GraphQlHelpers.GetArgumentStrings(queryFieldForFabricationParts, "nameFilter");
        //                    var queryFieldForParameters = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForFabricationParts, "qlParameters");
        //                    if (nameFiltersContained.Count == 0 || nameFiltersContained.Contains(aElement.Name))
        //                    {
        //                        returnFabricationPartsObject.Add(new QLFabricationPartResolve(aElement as FabricationPart, queryFieldForParameters));
        //                    }
        //                }
        //            }
        //        }

        //        qlFamilyInstances = returnFamilyInstancesObject.OrderBy(x => x.name).ToList();
        //        qlFabricationParts = returnFabricationPartsObject.OrderBy(x => x.name).ToList();
        //    }

        //}

        public QLElementCollectionResolve(ICollection <ElementId> _ids, object aFieldOrContext)
        {
            elementIds = _ids.Select(x => x.ToString()).ToList();

            var queryFieldForFamilyInstances  = GraphQlHelpers.GetFieldFromFieldOrContext(aFieldOrContext, "qlFamilyInstances");
            var queryFieldForFabricationParts = GraphQlHelpers.GetFieldFromFieldOrContext(aFieldOrContext, "qlFabricationParts");


            if (queryFieldForFamilyInstances != null || queryFieldForFabricationParts != null)
            {
                var _doc = ResolverEntry.Doc;

                var returnFamilyInstancesObject  = new ConcurrentBag <QLFamilyInstance>();
                var returnFabricationPartsObject = new ConcurrentBag <QLFabricationPart>();


                foreach (var aId in _ids)
                {
                    var aElement = _doc.GetElement(aId);
                    if (aElement == null)
                    {
                    }
                    else
                    {
                        if (aElement is FamilyInstance && queryFieldForFamilyInstances != null)
                        {
                            var nameFiltersContained = GraphQlHelpers.GetArgumentStrings(queryFieldForFamilyInstances, "nameFilter");
                            //var queryFieldForParameters = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForFamilyInstances, "qlParameters");
                            if (nameFiltersContained.Count == 0 || nameFiltersContained.Contains(aElement.Name))
                            {
                                //returnFamilyInstancesObject.Add(new QLFamilyInstanceResolve(aElement as FamilyInstance, queryFieldForParameters));
                                returnFamilyInstancesObject.Add(new QLFamilyInstanceResolve(aElement as FamilyInstance, queryFieldForFamilyInstances));
                            }
                        }
                        if (aElement is FabricationPart && queryFieldForFabricationParts != null)
                        {
                            var nameFiltersContained = GraphQlHelpers.GetArgumentStrings(queryFieldForFabricationParts, "nameFilter");
                            //var queryFieldForParameters = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForFabricationParts, "qlParameters");
                            if (nameFiltersContained.Count == 0 || nameFiltersContained.Contains(aElement.Name))
                            {
                                //returnFabricationPartsObject.Add(new QLFabricationPartResolve(aElement as FabricationPart, queryFieldForParameters));
                                returnFabricationPartsObject.Add(new QLFabricationPartResolve(aElement as FabricationPart, queryFieldForFabricationParts));
                            }
                        }
                    }
                }

                qlFamilyInstances  = returnFamilyInstancesObject.OrderBy(x => x.name).ToList();
                qlFabricationParts = returnFabricationPartsObject.OrderBy(x => x.name).ToList();
            }
        }
Ejemplo n.º 10
0
        public QLAssemblyResolve(AssemblyInstance _assembly, Field qlFieldViews, List <View> viewListing, Field queryFieldForEllementCollection)
        {
            id   = _assembly.Id.ToString();
            name = _assembly.Name;

            if (qlFieldViews != null && viewListing != null)
            {
                hasViews = viewListing.Any(v => v.AssociatedAssemblyInstanceId.IntegerValue == _assembly.Id.IntegerValue);
            }

            if (queryFieldForEllementCollection != null)
            {
                var qlFamilyInstancesField         = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForEllementCollection, "qlFamilyInstances");
                var qlFabricationPartsField        = GraphQlHelpers.GetFieldFromSelectionSet(queryFieldForEllementCollection, "qlFabricationParts");
                ICollection <ElementId> elementIds = _assembly.GetMemberIds();
                qlElementCollection = new QLElementCollectionResolve(elementIds, qlFamilyInstancesField, qlFabricationPartsField);
            }
        }
Ejemplo n.º 11
0
        public GraphQlAfDatabase GetAfDatabase(ResolveFieldContext context, string aAfDatabasePath)
        {
            aAfDatabasePath = cleanupPath(aAfDatabasePath);
            if (attemptLogin(context, getPiSystemName(aAfDatabasePath)))
            {
                var aAfDb = AFDatabase.FindObject(aAfDatabasePath) as AFDatabase;

                var afElementsField = GraphQlHelpers.GetFieldFromContext(context, "afElements");

                var aGraphQlAfDatabase = new GraphQlAfDatabase(aAfDb, afElementsField);

                return(aGraphQlAfDatabase);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 12
0
        public GraphQlAfElement(AFElement aAfElement, Field afElementsField, Field afAttributesField)
        {
            name          = aAfElement.Name;
            template      = aAfElement.Template?.Name;
            path          = aAfElement.GetPath();
            ThisAfElement = aAfElement;

            if (afElementsField != null)
            {
                var afElementsNameFilterStrings           = GraphQlHelpers.GetArgumentStrings(afElementsField, "nameFilter");
                var afElementsAttributeValueFilterStrings = GraphQlHelpers.GetArgumentStrings(afElementsField, "attributeValueFilter");
                var afElementsChildField   = GraphQlHelpers.GetFieldFromSelectionSet(afElementsField, "afElements");
                var afAttributesChildField = GraphQlHelpers.GetFieldFromSelectionSet(afElementsField, "afAttributes");

                var returnElementsObject = new ConcurrentBag <GraphQlAfElement>();
                var afElementList        = aAfElement.Elements;
                Parallel.ForEach(afElementList, aAfChildElement =>
                {
                    if (GraphQlHelpers.JudgeElementOnFilters(aAfChildElement, afElementsNameFilterStrings, afElementsAttributeValueFilterStrings))
                    {
                        returnElementsObject.Add(new GraphQlAfElement(aAfChildElement, afElementsChildField, afAttributesChildField));
                    }
                });
                afElements = returnElementsObject.OrderBy(x => x.name).ToList();
            }

            if (afAttributesField != null)
            {
                var afAttributesNameFilterStrings = GraphQlHelpers.GetArgumentStrings(afAttributesField, "nameFilter");
                var afAttributesChildField        = GraphQlHelpers.GetFieldFromSelectionSet(afAttributesField, "afAttributes");
                var tsValuesField = GraphQlHelpers.GetFieldFromSelectionSet(afAttributesField, "tsPlotValues");

                var returnAttributesObject = new ConcurrentBag <GraphQlAfAttribute>();
                var afAttributeList        = aAfElement.Attributes.ToList <AFAttribute>();
                Parallel.ForEach(afAttributeList, aAfAttribute =>
                {
                    if (afAttributesNameFilterStrings.Count == 0 || afAttributesNameFilterStrings.Contains(aAfAttribute.Name))
                    {
                        returnAttributesObject.Add(new GraphQlAfAttribute(aAfAttribute, afAttributesChildField, tsValuesField));
                    }
                });
                afAttributes = returnAttributesObject.OrderBy(x => x.name).ToList();
            }
        }
        public QLPiSystemResolve(PISystem aPiSystem, Field afDbsField)
        {
            name = aPiSystem.Name;
            if (afDbsField != null)
            {
                var nameFilterStrings = GraphQlHelpers.GetArgumentStrings(afDbsField, "nameFilter");
                var afElementsField   = GraphQlHelpers.GetFieldFromFieldOrContext(afDbsField, "afElements");

                var returnElementsObject = new ConcurrentBag <QLAfDatabase>();
                var afDbsList            = aPiSystem.Databases;
                Parallel.ForEach(afDbsList, aAfDb =>
                {
                    if (nameFilterStrings.Count == 0 || nameFilterStrings.Contains(aAfDb.Name))
                    {
                        returnElementsObject.Add(new QLAfDatabaseResolve(aAfDb, afElementsField));
                    }
                });
                afDbs = returnElementsObject.OrderBy(x => x.name).ToList();
            }
        }
        public DynamicSubscriptionType(IGraphQlTypePool graphTypePool, IEnumerable <Type> types)
        {
            Name        = "Subscriptions";
            Description = "Contains the subscriptions of the graphql api";

            foreach (var type in types)
            {
                if (!type.IsClass || type.IsInterface)
                {
                    throw new ArgumentException("Invalid subscription type");
                }

                // Generate fields -----------------------------------------------
                // start with the properties
                var properties = type
                                 // Get all properties with getters
                                 .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.GetProperty)
                                 // ignore the ones that have the ignore attribute
                                 .Where(x => x.GetAttribute <IgnoreAttribute>() == null);

                foreach (var property in properties)
                {
                    var observableType = property.PropertyType.GetInterfacesIncludingType()
                                         .FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition().Equals(typeof(IObservable <>)));

                    if (observableType == null)
                    {
                        throw new InvalidOperationException("Subscription property can only return or have type IObservable<>, derive from it or have the IgnoreAttribute");
                    }

                    var graphType       = graphTypePool.GetGraphType(observableType.GetGenericArguments()[0]);
                    var descriptionAttr = property.GetAttribute <DescriptionAttribute>();
                    var fieldNameAttr   = property.GetAttribute <NameAttribute>();
                    var isNonNull       = property.GetAttribute <NonNullAttribute>() != null;

                    var field = new EventStreamFieldType()
                    {
                        Name         = fieldNameAttr == null ? property.Name : fieldNameAttr.Name,
                        Description  = descriptionAttr?.Description ?? DocXmlHelper.DocReader.GetMemberComments(property).Summary,
                        ResolvedType = isNonNull ? new NonNullGraphType(graphType) : graphType,
                        Resolver     = new FuncFieldResolver <object>(ResolveObject),
                        Subscriber   = new EventStreamResolver <object>(c => Subscribe(c, property, type))
                    };

                    // add the .net type of this field in the metadata
                    field.Metadata["type"] = property;

                    var metadatas = Attribute.GetCustomAttributes(type, typeof(MetadataAttribute));
                    foreach (MetadataAttribute metadata in metadatas)
                    {
                        Metadata[metadata.Key] = metadata.Value;
                    }

                    AddField(field);
                }

                // work with the methods
                var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
                              .Where(x => x.GetAttribute <IgnoreAttribute>() == null);

                foreach (var method in methods)
                {
                    if (method.IsSpecialName)
                    {
                        continue;
                    }

                    var descriptionAttr = method.GetAttribute <DescriptionAttribute>();
                    var fieldNameAttr   = method.GetAttribute <NameAttribute>();

                    IGraphType                graphType;
                    IEventStreamResolver      subscriber      = null;
                    IAsyncEventStreamResolver subscriberAsync = null;
                    if (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task <>))
                    {
                        var observable = method.ReturnType.GetGenericArguments()[0];

                        var observableInterface = observable.GetInterfacesIncludingType()
                                                  .FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition().Equals(typeof(IObservable <>)));

                        if (observableInterface == null)
                        {
                            throw new InvalidOperationException("Subscription property can only return or have type IObservable<>, derive from it or have the IgnoreAttribute");
                        }

                        var observableType = observableInterface.GetGenericArguments()[0];

                        graphType       = graphTypePool.GetGraphType(observableType);
                        subscriberAsync = new AsyncEventStreamResolver <object>(async c =>
                        {
                            var task = GraphQlHelpers.ExecuteResolverFunction(method, c, type, true);
                            await((Task)task);

                            var resultProp = task.GetType().GetProperty(nameof(Task <object> .Result));
                            var result     = resultProp.GetValue(task);

                            return(result as IObservable <object>);
                        });
                    }
                    else
                    {
                        var observable = method.ReturnType;

                        var observableInterface = observable.GetInterfacesIncludingType()
                                                  .FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition().Equals(typeof(IObservable <>)));

                        var observableType = observableInterface.GetGenericArguments()[0];

                        graphType  = graphTypePool.GetGraphType(observableType);
                        subscriber = new EventStreamResolver <object>(c => GraphQlHelpers.ExecuteResolverFunction(method, c, type, true) as IObservable <object>);
                    }

                    var isNonNull = method.GetAttribute <NonNullAttribute>() != null;

                    // create field
                    var field = new EventStreamFieldType()
                    {
                        Arguments       = GraphQlHelpers.GetArguments(graphTypePool, method),
                        Name            = fieldNameAttr == null ? method.Name : fieldNameAttr.Name,
                        Description     = descriptionAttr?.Description ?? DocXmlHelper.DocReader.GetMemberComments(method).Summary,
                        ResolvedType    = isNonNull ? new NonNullGraphType(graphType) : graphType,
                        Resolver        = new FuncFieldResolver <object>(ResolveObject),
                        Subscriber      = subscriber,
                        AsyncSubscriber = subscriberAsync
                    };

                    // add the .net type of this field in the metadata
                    field.Metadata["type"] = method;

                    var metadatas = Attribute.GetCustomAttributes(type, typeof(MetadataAttribute));
                    foreach (MetadataAttribute metadata in metadatas)
                    {
                        Metadata[metadata.Key] = metadata.Value;
                    }

                    AddField(field);
                }
            }
        }
        // new FuncFieldResolver<object>(c => GraphQlHelpers.GetFinalValue(property.GetValue(GraphQlHelpers.GetSourceInstance(c, type, false))))

        private static IObservable <object> Subscribe(ResolveEventStreamContext c, PropertyInfo property, Type type)
        {
            var observable = property.GetValue(GraphQlHelpers.GetSourceInstance(c, type, true));

            return(observable as IObservable <object>);
        }
        public DynamicMutationType(IGraphQlTypePool graphTypePool, IEnumerable <Type> types)
        {
            Name        = "Mutations";
            Description = "Contains the mutation of the graphql api";

            foreach (var type in types)
            {
                if (!type.IsClass || type.IsInterface)
                {
                    throw new ArgumentException("Invalid subscription type");
                }

                // work with the methods
                var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
                              .Where(x => x.GetAttribute <IgnoreAttribute>() == null);

                foreach (var method in methods)
                {
                    if (method.Name == nameof(GraphNodeType <object> .OnCreateAsync) ||
                        method.IsSpecialName)
                    {
                        continue;
                    }

                    var descriptionAttr = method.GetAttribute <DescriptionAttribute>();
                    var fieldNameAttr   = method.GetAttribute <NameAttribute>();

                    IGraphType     graphType;
                    IFieldResolver resolver;
                    if (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task <>))
                    {
                        var awaitableReturnType = method.ReturnType.GetGenericArguments()[0];
                        graphType = graphTypePool.GetGraphType(awaitableReturnType);
                        resolver  = new AsyncFieldResolver <object>(async c =>
                        {
                            var task = GraphQlHelpers.ExecuteResolverFunction(method, c, type, true);
                            await((Task)task);

                            var resultProp = task.GetType().GetProperty(nameof(Task <object> .Result));
                            var result     = resultProp.GetValue(task);

                            return(result);
                        });
                    }
                    else
                    {
                        graphType = graphTypePool.GetGraphType(method.ReturnType);
                        resolver  = new FuncFieldResolver <object>(c => GraphQlHelpers.ExecuteResolverFunction(method, c, type, true));
                    }

                    var isNonNull = method.GetAttribute <NonNullAttribute>() != null;

                    // create field
                    var field = new FieldType()
                    {
                        Arguments    = GraphQlHelpers.GetArguments(graphTypePool, method),
                        Name         = fieldNameAttr == null ? method.Name : fieldNameAttr.Name,
                        Description  = descriptionAttr?.Description ?? DocXmlHelper.DocReader.GetMemberComments(method).Summary,
                        ResolvedType = isNonNull ? new NonNullGraphType(graphType) : graphType,
                        Resolver     = resolver
                    };

                    // add the .net type of this field in the metadata
                    field.Metadata["type"] = method;

                    var metadatas = Attribute.GetCustomAttributes(type, typeof(MetadataAttribute));
                    foreach (MetadataAttribute metadata in metadatas)
                    {
                        Metadata[metadata.Key] = metadata.Value;
                    }

                    AddField(field);
                }
            }
        }
Ejemplo n.º 17
0
        public DynamicGraphType(IGraphQlTypePool graphTypePool, Type type, bool isRoot = false)
        {
            if (!type.IsClass || type.IsInterface)
            {
                throw new ArgumentException("Invalid object type");
            }

            // get the name
            var nameAttr = type.GetAttribute <NameAttribute>();
            var descAttr = type.GetAttribute <DescriptionAttribute>();

            // set type name and description
            Name        = nameAttr?.Name ?? type.Name;
            Description = descAttr?.Description ?? DocXmlHelper.DocReader.GetTypeComments(type).Summary;

            // set the type metadata
            Metadata["type"] = type;

            {
                // sets the custom metadatas
                var metadatas = Attribute.GetCustomAttributes(type, typeof(MetadataAttribute));
                foreach (MetadataAttribute metadata in metadatas)
                {
                    Metadata[metadata.Key] = metadata.Value;
                }
            }

            // Check for interfaces
            var interfaces = type.GetNotDerivedInterfaces();

            // Add all the interface that this implement
            foreach (var intrfce in interfaces)
            {
                AddResolvedInterface(graphTypePool.GetGraphType(intrfce) as IInterfaceGraphType);
            }

            // Implementing isTypeOf in the case this type implement an interface
            IsTypeOf = obj => obj.GetType() == type;

            // Generate fields -----------------------------------------------
            // start with the properties
            var properties = type
                             // Get all properties with getters
                             .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.GetProperty)
                             // ignore the ones that have the ignore attribute
                             .Where(x => x.GetAttribute <IgnoreAttribute>() == null);

            foreach (var property in properties)
            {
                var graphType       = graphTypePool.GetGraphType(property.PropertyType);
                var descriptionAttr = property.GetAttribute <DescriptionAttribute>();
                var fieldNameAttr   = property.GetAttribute <NameAttribute>();
                var isNonNull       = property.GetAttribute <NonNullAttribute>() != null;

                // create field
                var field = new FieldType()
                {
                    Name         = fieldNameAttr == null ? property.Name : fieldNameAttr.Name,
                    Description  = descriptionAttr?.Description ?? DocXmlHelper.DocReader.GetMemberComments(property).Summary,
                    ResolvedType = isNonNull ? new NonNullGraphType(graphType) : graphType,
                    Resolver     = new FuncFieldResolver <object>(c => GraphQlHelpers.GetFinalValue(property.GetValue(GraphQlHelpers.GetSourceInstance(c, type, isRoot)))),
                };

                // add the .net type of this field in the metadata
                field.Metadata["type"] = property;

                var metadatas = Attribute.GetCustomAttributes(type, typeof(MetadataAttribute));
                foreach (MetadataAttribute metadata in metadatas)
                {
                    Metadata[metadata.Key] = metadata.Value;
                }

                AddField(field);
            }

            // work with the methods
            var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
                          .Where(x => x.GetAttribute <IgnoreAttribute>() == null);

            // for each method public
            foreach (var method in methods)
            {
                if (method.Name == nameof(GraphNodeType <object> .OnCreateAsync) ||
                    method.IsSpecialName)
                {
                    continue;
                }

                var descriptionAttr = method.GetAttribute <DescriptionAttribute>();
                var fieldNameAttr   = method.GetAttribute <NameAttribute>();

                IGraphType     graphType;
                IFieldResolver resolver;
                if (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task <>))
                {
                    var awaitableReturnType = method.ReturnType.GetGenericArguments()[0];
                    graphType = graphTypePool.GetGraphType(awaitableReturnType);
                    resolver  = new AsyncFieldResolver <object>(async c =>
                    {
                        var task = GraphQlHelpers.ExecuteResolverFunction(method, c, type, isRoot);
                        await((Task)task);

                        var resultProp = task.GetType().GetProperty(nameof(Task <object> .Result));
                        var result     = resultProp.GetValue(task);

                        return(result);
                    });
                }
                else
                {
                    graphType = graphTypePool.GetGraphType(method.ReturnType);
                    resolver  = new FuncFieldResolver <object>(c => GraphQlHelpers.ExecuteResolverFunction(method, c, type, isRoot));
                }

                var isNonNull = method.GetAttribute <NonNullAttribute>() != null;

                // create field
                var field = new FieldType()
                {
                    Arguments    = GraphQlHelpers.GetArguments(graphTypePool, method),
                    Name         = fieldNameAttr == null ? method.Name : fieldNameAttr.Name,
                    Description  = descriptionAttr?.Description ?? DocXmlHelper.DocReader.GetMemberComments(method).Summary,
                    ResolvedType = isNonNull ? new NonNullGraphType(graphType) : graphType,
                    Resolver     = resolver
                };

                // add the .net type of this field in the metadata
                field.Metadata["type"] = method;

                var metadatas = Attribute.GetCustomAttributes(type, typeof(MetadataAttribute));
                foreach (MetadataAttribute metadata in metadatas)
                {
                    Metadata[metadata.Key] = metadata.Value;
                }

                AddField(field);
            }
        }