Exemplo n.º 1
0
        public static void GenerateController(IBase baseObject, string controllersFolderPath, string controllerName, IGeneratorConfiguration generatorConfiguration, List <RelatedEntity> relatedEntities, List <Generators.EntityProperty> entityProperties)
        {
            var host             = new TemplateEngineHost();
            var pass             = generatorConfiguration.CurrentPass;
            var exports          = new List <ESModule>();
            var declarations     = new List <IDeclarable>();
            var isIdentityEntity = generatorConfiguration.IsIdentityEntity(baseObject);
            var element          = (IElement)baseObject;
            var keyAttribute     = element.GetKey();
            var keyType          = keyAttribute.GetShortType();
            Dictionary <string, object> sessionVariables;
            FileInfo fileInfo;
            string   fileLocation;
            string   filePath;
            string   output;

            try
            {
                // WebAPI controller class

                sessionVariables = new Dictionary <string, object>();

                sessionVariables.Add("ControllerName", controllerName);
                sessionVariables.Add("EntityName", baseObject.Name);
                sessionVariables.Add("RelatedEntities", relatedEntities);
                sessionVariables.Add("EntityProperties", entityProperties);
                sessionVariables.Add("Container", baseObject.GetContainer());
                sessionVariables.Add("ContainerSet", baseObject.GetContainerSet().Name);
                sessionVariables.Add("RootNamespace", generatorConfiguration.AppName);
                sessionVariables.Add("KeyName", keyAttribute.Name);
                sessionVariables.Add("KeyType", keyType);

                if (generatorConfiguration.CustomQueries.ContainsKey(baseObject))
                {
                    var queriesList = generatorConfiguration.CustomQueries[baseObject];

                    sessionVariables.Add("CustomQueries", queriesList);
                }
                else if (generatorConfiguration.CustomQueries.ContainsNavigationKey(baseObject))
                {
                    var queriesList = generatorConfiguration.CustomQueries.GetNavigationValue(baseObject);

                    sessionVariables.Add("CustomQueries", queriesList);
                }

                if (baseObject is IEntityWithPrefix)
                {
                    var entityWithPrefix = baseObject.CastTo <IEntityWithPrefix>();

                    fileLocation = PathCombine(controllersFolderPath, entityWithPrefix.PathPrefix, controllerName);

                    controllerName = entityWithPrefix.ControllerNamePrefix + controllerName;
                }
                else
                {
                    fileLocation = PathCombine(controllersFolderPath, controllerName);
                }

                filePath = PathCombine(fileLocation, controllerName + "Controller.cs");
                fileInfo = new FileInfo(filePath);

                if (baseObject is IElementWithSurrogateTemplateType)
                {
                    var elementWithSurrogateTemplateType = (IElementWithSurrogateTemplateType)baseObject;

                    if (elementWithSurrogateTemplateType.HasSurrogateTemplateType <WebAPIControllerClassTemplate>())
                    {
                        var templateType = elementWithSurrogateTemplateType.GetSurrogateTemplateType <WebAPIControllerClassTemplate>();

                        output = host.Generate(templateType, sessionVariables, false);

                        if (generatorConfiguration.FileSystem.Contains(fileInfo.FullName))
                        {
                            if (pass != GeneratorPass.HierarchyOnly)
                            {
                                var file = (AbstraX.FolderStructure.File)generatorConfiguration.FileSystem[fileInfo.FullName];

                                if (file.Hash != output.GetHashCode())
                                {
                                    // DebugUtils.Break();
                                }
                            }
                        }
                        else
                        {
                            generatorConfiguration.CreateFile(fileInfo, output, FileKind.Services, () => generatorConfiguration.GenerateInfo(sessionVariables, "WebAPIController Class"));
                        }

                        return;
                    }
                }

                if (isIdentityEntity)
                {
                    output = host.Generate <WebAPIIdentityControllerClassTemplate>(sessionVariables, false);
                }
                else
                {
                    output = host.Generate <WebAPIControllerClassTemplate>(sessionVariables, false);
                }

                if (generatorConfiguration.FileSystem.Contains(fileInfo.FullName))
                {
                    if (pass != GeneratorPass.HierarchyOnly)
                    {
                        var file = (AbstraX.FolderStructure.File)generatorConfiguration.FileSystem[fileInfo.FullName];

                        if (file.Hash != output.GetHashCode())
                        {
                            // DebugUtils.Break();
                        }
                    }
                }
                else
                {
                    generatorConfiguration.CreateFile(fileInfo, output, FileKind.Services, () => generatorConfiguration.GenerateInfo(sessionVariables, "WebAPIController Class"));
                }
            }
            catch (Exception e)
            {
                generatorConfiguration.AppGeneratorEngine.WriteError(e.ToString());
            }
        }
Exemplo n.º 2
0
        public QueryPathQueue(QueryPathAttribute queryPath, IBase baseObject)
        {
            var currentObject = baseObject;
            var expression    = queryPath.XPathExpression;
            var partQueue     = new Queue <IXPathPart>(queryPath.PartQueue);
            QueryPathQueueItem currentQueueItem = null;

            this.Items          = new Queue <QueryPathQueueItem>();
            this.LoadParentPath = queryPath;
            this.BaseObject     = baseObject;
            this.QueryPathKind  = queryPath.QueryPathKind;

            while (partQueue.Count > 0)
            {
                var part = partQueue.Dequeue();

                if (part is XPathElement element)
                {
                    var parentObject = (IParentBase)currentObject;
                    var childObject  = parentObject.ChildNodes.SingleOrDefault(n => n.Name == element.Text);

                    if (childObject == null)
                    {
                        if (parentObject is NavigationProperty)
                        {
                            childObject = parentObject.ChildNodes.Single();

                            if (this.QueryPathKind == QueryKind.LoadParentReference && partQueue.Count == 0)
                            {
                                currentQueueItem.AssumeSingleOperator = true;
                            }

                            currentQueueItem = new QueryPathQueueItem(childObject);
                            this.Items.Enqueue(currentQueueItem);

                            parentObject = (IParentBase)childObject;

                            childObject = parentObject.ChildNodes.SingleOrDefault(n => n.Name == element.Text);
                        }
                    }

                    currentQueueItem = new QueryPathQueueItem(childObject);
                    this.Items.Enqueue(currentQueueItem);

                    currentObject = childObject;
                    parentObject  = (IParentBase)currentObject;

                    if (element.Predicates != null && element.Predicates.Count > 0)
                    {
                        QueryPathQueueItem dequeuedItem = null;

                        foreach (var predicate in element.Predicates)
                        {
                            var left = predicate.Left;
                            var op   = predicate.Operator;
                            IQueryPathOperand       queueLeft  = null;
                            IQueryPathOperand       queueRight = null;
                            QueryPathQueuePredicate queuePredicate;

                            if (left is XPathAttribute xpathAttribute)
                            {
                                var parentSetElement = (IElement)parentObject.ChildNodes.Single();
                                var attribute        = (IAttribute)parentSetElement.Attributes.Single(n => n.Name == xpathAttribute.Name);
                                var queueAttribute   = new QueryPathQueueAttribute(attribute);
                                var nextPart         = partQueue.Peek();

                                if (nextPart is XPathElement xpathElement)
                                {
                                    if (xpathElement.Text == parentSetElement.Name)
                                    {
                                        part         = partQueue.Dequeue();
                                        dequeuedItem = new QueryPathQueueItem(parentSetElement);
                                    }

                                    currentObject = parentSetElement;
                                }

                                queueLeft = queueAttribute;
                            }
                            else
                            {
                                DebugUtils.Break();
                            }

                            if (op != CodePlex.XPathParser.XPathOperator.Unknown)
                            {
                                var right = predicate.Right;

                                if (right is XPathFunction pathFunction)
                                {
                                    var kind          = EnumUtils.GetValue <QueryPathFunctionKind>(pathFunction.Name);
                                    var args          = pathFunction.Args;
                                    var queueFunction = new QueryPathQueueFunction(kind, args);

                                    queueRight = queueFunction;
                                }
                                else
                                {
                                    DebugUtils.Break();
                                }
                            }

                            queuePredicate = new QueryPathQueuePredicate(queueLeft, op, queueRight);
                            currentQueueItem.Predicates.Add(queuePredicate);

                            if (dequeuedItem != null)
                            {
                                currentQueueItem = dequeuedItem;
                                this.Items.Enqueue(currentQueueItem);
                            }
                        }
                    }
                }
                else if (part is XPathAxis pathAxis)
                {
                    if (pathAxis.Axis == CodePlex.XPathParser.XPathAxis.Root)
                    {
                        currentObject = baseObject.GetContainer();
                        var queueItem = new QueryPathQueueItem(currentObject);

                        this.Items.Enqueue(queueItem);

                        currentQueueItem = queueItem;
                    }
                    else
                    {
                        DebugUtils.Break();
                    }
                }
                else
                {
                    DebugUtils.Break();
                }
            }
        }