Exemplo n.º 1
0
 /// <summary>
 /// Add a method to the definition for the entity.
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="method"></param>
 private static void AddMethodToEntity(OrmEntity entity, MethodDescription method)
 {
     OrmEntityMethod targetMethod = null;
     targetMethod = method.CreateMethod(entity);
     OrmMethodTarget targetStep = method.CreateTarget(AUTOWIRE_HEADER);
     TargetMethodCollection targetColl = null;
     switch (method.StepType)
     {
         case TargetStepType.PreExecute:
             targetColl = targetMethod.PreExecuteTargets;
             break;
         case TargetStepType.PostFlush:
             targetColl = targetMethod.PostFlushTargets;
             break;
         case TargetStepType.PostExecute:
             targetColl = targetMethod.PostExecuteTargets;
             break;
         case TargetStepType.Primary:
         default:
             targetColl = targetMethod.MethodTargets;
             break;
     }
     targetColl.Add(targetStep);
     entity.Methods.Add(targetMethod);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Add a method to the definition for the entity.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="method"></param>
        private static void AddMethodToEntity(OrmEntity entity, MethodDescription method)
        {
            OrmEntityMethod targetMethod = null;

            targetMethod = method.CreateMethod(entity);
            OrmMethodTarget        targetStep = method.CreateTarget(AUTOWIRE_HEADER);
            TargetMethodCollection targetColl = null;

            switch (method.StepType)
            {
            case TargetStepType.PreExecute:
                targetColl = targetMethod.PreExecuteTargets;
                break;

            case TargetStepType.PostFlush:
                targetColl = targetMethod.PostFlushTargets;
                break;

            case TargetStepType.PostExecute:
                targetColl = targetMethod.PostExecuteTargets;
                break;

            case TargetStepType.Primary:
            default:
                targetColl = targetMethod.MethodTargets;
                break;
            }
            targetColl.Add(targetStep);
            entity.Methods.Add(targetMethod);
        }
Exemplo n.º 3
0
 public RelationshipInfo(OrmEntity parentEntity, OrmEntity childEntity, bool isInverted)
 {
     _relationship = null;
     _isOneToMany = false;
     _propertyName = (isInverted
                          ? parentEntity
                          : childEntity).Name;
 }
        protected override CodeTemplate GetOutputTemplate(OrmEntity entity)
        {
            var model = (OrmModel)entity.Model;
            var templates = model.GetCodeTemplates(null, "SDataClientEntity");
            if (templates.Count > 0)
                return templates[0];

            throw new BuildException("Missing sdata client entity code generation template");
        }
Exemplo n.º 5
0
        protected override CodeTemplate GetOutputTemplate(OrmEntity entity)
        {
            var model     = (OrmModel)entity.Model;
            var templates = model.GetCodeTemplates(null, "SDataClientEntity");

            if (templates.Count > 0)
            {
                return(templates[0]);
            }

            throw new BuildException("Missing sdata client entity code generation template");
        }
Exemplo n.º 6
0
        /// <summary>
        /// Create a new method or return existing method if appropriate
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public OrmEntityMethod CreateMethod(OrmEntity entity)
        {
            if (MethodType == Sage.Platform.Orm.Entities.MethodType.Rule)
            {
                OrmEntityMethod method = new OrmEntityMethod
                {
                    ActionType = ActionType.None,
                    ReturnType = ReturnType,
                    MethodName = Name,
                    MethodType = MethodType
                };
                if (ParameterTypes != null && ParameterTypes.Length > 0)
                {
                    //MethodParameterCollection paramColl = new MethodParameterCollection(method);
                    foreach (ParamDescription p in ParameterTypes)
                    {
                        method.MethodParameters.Add(new OrmEntityMethodParam {
                            ParamName = p.Name, ParamType = p.ParamType
                        });
                    }
                }
                return(method);
            }
            else if (MethodType == Sage.Platform.Orm.Entities.MethodType.CrudEvent)
            {
                switch (EventType)
                {
                case AutoWire.EventType.BeforeUpdate:
                    return(entity.OnBeforeUpdateMethod);

                case AutoWire.EventType.AfterUpdate:
                    return(entity.OnAfterUpdateMethod);

                case AutoWire.EventType.BeforeInsert:
                    return(entity.OnBeforeInsertMethod);

                case AutoWire.EventType.AfterInsert:
                    return(entity.OnAfterInsertMethod);

                case AutoWire.EventType.BeforeDelete:
                    return(entity.OnBeforeDeleteMethod);

                default:
                    throw new Exception("Invalid event type " + EventType + " for AutoWire method " + DeclaringType + "." + Name);
                }
            }
            else
            {
                throw new Exception("Unsupported method type " + MethodType + " for AutoWire method " + DeclaringType + "." + Name);
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Create a new method or return existing method if appropriate
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public OrmEntityMethod CreateMethod(OrmEntity entity)
 {
     if (MethodType == Sage.Platform.Orm.Entities.MethodType.Rule)
     {
         OrmEntityMethod method = new OrmEntityMethod
         {
             ActionType = ActionType.None,
             ReturnType = ReturnType,
             MethodName = Name,
             MethodType = MethodType
         };
         if (ParameterTypes != null && ParameterTypes.Length > 0)
         {
             //MethodParameterCollection paramColl = new MethodParameterCollection(method);
             foreach (ParamDescription p in ParameterTypes)
             {
                 method.MethodParameters.Add(new OrmEntityMethodParam { ParamName = p.Name, ParamType = p.ParamType });
             }
         }
         return method;
     }
     else if (MethodType == Sage.Platform.Orm.Entities.MethodType.CrudEvent)
     {
         switch (EventType)
         {
             case AutoWire.EventType.BeforeUpdate:
                 return entity.OnBeforeUpdateMethod;
             case AutoWire.EventType.AfterUpdate:
                 return entity.OnAfterUpdateMethod;
             case AutoWire.EventType.BeforeInsert:
                 return entity.OnBeforeInsertMethod;
             case AutoWire.EventType.AfterInsert:
                 return entity.OnAfterInsertMethod;
             case AutoWire.EventType.BeforeDelete:
                 return entity.OnBeforeDeleteMethod;
             default:
                 throw new Exception("Invalid event type " + EventType + " for AutoWire method " + DeclaringType + "." + Name);
         }
     }
     else
     {
         throw new Exception("Unsupported method type " + MethodType + " for AutoWire method " + DeclaringType + "." + Name);
     }
 }
Exemplo n.º 8
0
        internal static bool IsEntityNewer(BuildType buildType, DateTime baseDate, OrmEntity entity)
        {
            if (buildType == BuildType.BuildAll)
            {
                return(true);
            }

            entity.FilePath.Refresh();

            if (baseDate < entity.FilePath.LastWriteTimeUtc)
            {
                return(true);
            }

            //check all business rule methods and events
            IFileInfo[] methods = entity.FilePath.Directory.GetFiles(EntityModelUrlConstants.QRY_ALL_METHODS);
            foreach (IFileInfo methodFile in methods)
            {
                if (baseDate < methodFile.LastWriteTimeUtc)
                {
                    return(true);
                }
            }

            if (IsNewer(entity.ParentEntities, baseDate))
            {
                return(true);
            }

            if (IsNewer(entity.ChildEntities, baseDate))
            {
                return(true);
            }

            if (IsNewer(entity.ExtensionEntities, baseDate))
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Any method that is on the entity and is NOT in the methodsToCreate should be removed
        /// (this also considers the step type, eg, if the method is specified as Post, and the current one is a Pre, it will be removed)
        /// Any method that is on the entity is IS in the methodsToCreate should be removed from methodsToCreate.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>true if entity was modified as a result</returns>
        private static bool CleanAutowireMethods(OrmEntity entity, List <MethodDescription> methodsToCreate)
        {
            List <OrmEntityMethod> methodsToRemove = new List <OrmEntityMethod>();
            bool modified = false;

            foreach (OrmEntityMethod method in entity.Methods.Union(new OrmEntityMethod[] {
                entity.OnAfterInsertMethod, entity.OnAfterDeleteMethod, entity.OnBeforeDeleteMethod,
                entity.OnBeforeInsertMethod, entity.OnBeforeUpdateMethod, entity.OnAfterUpdateMethod
            }))
            {
                if (method == null)
                {
                    // happens for the "event" methods - some of them are null
                    continue;
                }
                modified = CleanAutowireMethodsTarget(TargetStepType.Primary, method.MethodTargets, methodsToCreate) || modified;
                modified = CleanAutowireMethodsTarget(TargetStepType.PreExecute, method.PreExecuteTargets, methodsToCreate) || modified;
                modified = CleanAutowireMethodsTarget(TargetStepType.PostExecute, method.PostExecuteTargets, methodsToCreate) || modified;
                modified = CleanAutowireMethodsTarget(TargetStepType.PostFlush, method.PostFlushTargets, methodsToCreate) || modified;

                if (method.AllTargets.Count() == 0)
                {
                    methodsToRemove.Add(method);
                }
            }
            foreach (OrmEntityMethod method in methodsToRemove)
            {
                if (entity.Methods.Contains(method))
                {
                    modified = true;
                    method.Delete();
                    entity.Methods.Remove(method);
                }
            }
            return(modified);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Any method that is on the entity and is NOT in the methodsToCreate should be removed 
        /// (this also considers the step type, eg, if the method is specified as Post, and the current one is a Pre, it will be removed)
        /// Any method that is on the entity is IS in the methodsToCreate should be removed from methodsToCreate.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>true if entity was modified as a result</returns>
        private static bool CleanAutowireMethods(OrmEntity entity, List<MethodDescription> methodsToCreate)
        {
            List<OrmEntityMethod> methodsToRemove = new List<OrmEntityMethod>();
            bool modified = false;
            foreach (OrmEntityMethod method in entity.Methods.Union(new OrmEntityMethod[] {
                entity.OnAfterInsertMethod, entity.OnAfterDeleteMethod, entity.OnBeforeDeleteMethod,
                entity.OnBeforeInsertMethod, entity.OnBeforeUpdateMethod, entity.OnAfterUpdateMethod }))
            {
                if (method == null)
                    // happens for the "event" methods - some of them are null
                    continue;
                modified = CleanAutowireMethodsTarget(TargetStepType.Primary, method.MethodTargets, methodsToCreate) || modified;
                modified = CleanAutowireMethodsTarget(TargetStepType.PreExecute, method.PreExecuteTargets, methodsToCreate) || modified;
                modified = CleanAutowireMethodsTarget(TargetStepType.PostExecute, method.PostExecuteTargets, methodsToCreate) || modified;
                modified = CleanAutowireMethodsTarget(TargetStepType.PostFlush, method.PostFlushTargets, methodsToCreate) || modified;

                if (method.AllTargets.Count() == 0)
                    methodsToRemove.Add(method);
            }
            foreach (OrmEntityMethod method in methodsToRemove)
            {
                if (entity.Methods.Contains(method))
                {
                    modified = true;
                    method.Delete();
                    entity.Methods.Remove(method);
                }
            }
            return modified;
        }
        protected override void GenerateInternal(OperationStatus op, BuildType buildType)
        {
            if (OrmPackages.Length == 0)
            {
                return;
            }

            EnsureLibraries();

            IFileInfo   projectInfoPath = FileSystem.GetFileInfo(Path.Combine(FullProjectDirectory, "project.info.xml"));
            ProjectInfo local           = ProjectInfo.Load(projectInfoPath);

            if (local == null || local.InstanceId != Project.InstanceId)
            {
                if (buildType == BuildType.Build)
                {
                    Logging.BuildLog.Info("Full build required");
                }

                buildType = BuildType.BuildAll;
                local     = new ProjectInfo {
                    InstanceId = Project.InstanceId
                };

                if (projectInfoPath.Directory.Exists)
                {
                    foreach (var file in projectInfoPath.Directory.GetFiles())
                    {
                        file.Delete();
                    }
                }
            }

            CopyEntityInterfacesAssembly();

            string path = FullProjectDirectory;

            var generatorList = new List <BaseEntityGenerator>();

            // generate entities
            BaseEntityGenerator baseGen = new SDataClientEntityCodeGenerator();

            baseGen.Initialize(WorkingDirectoryPath);
            generatorList.Add(baseGen);

            // Ensure all paths are created
            foreach (var gen in generatorList)
            {
                string relPath = Path.Combine(path, gen.RelativePath);
                if (!Directory.Exists(relPath))
                {
                    Directory.CreateDirectory(relPath);
                }
            }

            var model = (OrmModel)OrmPackages[0].Model;

            var engine = new Engine {
                DefaultToolsVersion = "3.5"
            };
            bool atLeastOneFileWasGenerated = false;
            var  buildItemFiles             = new List <BuildItemFileInfo>();

            foreach (var entity in OrmEntity.GetAll(model.Project).Where(item => item.Generate && item.GenerateSDataFeed))
            {
                if (!op.CanContinue)
                {
                    return;
                }

                foreach (var generator in generatorList)
                {
                    string fullPath = Path.Combine(Path.Combine(path, generator.RelativePath), generator.FormatFileName(entity.Name));
                    var    fi       = new FileInfo(fullPath);

                    if (BuildHelper.IsEntityNewer(buildType, fi.LastWriteTimeUtc, entity))
                    {
                        using (var writer = new StreamWriter(fullPath))
                        {
                            generator.Generate(entity, writer);
                        }
                        atLeastOneFileWasGenerated = true;
                    }

                    buildItemFiles.Add(new BuildItemFileInfo(generator, fullPath));
                }
            }

            if (atLeastOneFileWasGenerated)
            {
                BuildProject(buildItemFiles, engine);
            }

            if (op.CanContinue)
            {
                local.Save(projectInfoPath);
            }
        }
Exemplo n.º 12
0
        private string GenerateReferencePropertyName(OrmEntity parentEntity, OrmEntity childEntity, OrmEntityProperty childProperty)
        {
            string propertyName = childProperty.PropertyName;

            if (propertyName.EndsWith("ID", StringComparison.InvariantCultureIgnoreCase))
            {
                propertyName = propertyName.Substring(0, propertyName.Length - 2).Trim();
            }
            else if (propertyName.EndsWith("CODE", StringComparison.InvariantCultureIgnoreCase))
            {
                propertyName = propertyName.Substring(0, propertyName.Length - 4).Trim();
            }
            else
            {
                propertyName = parentEntity.Name;
            }

            if (StringUtils.CaseInsensitiveEquals(propertyName, parentEntity.Name))
            {
                propertyName = parentEntity.Name;
            }

            return _entityNameCreator.CreateName(childEntity, typeof (OrmEntityProperty), propertyName);
        }
Exemplo n.º 13
0
        private string GenerateCollectionPropertyName(OrmEntity parentEntity, OrmEntity childEntity)
        {
            string baseName = childEntity.Name;
            char lastChar = baseName[baseName.Length - 1];

            if (lastChar == 'y')
            {
                baseName = baseName.Substring(0, baseName.Length - 1) + "ies";
            }
            else if (lastChar != 's')
            {
                baseName += 's';
            }

            return _entityNameCreator.CreateName(parentEntity, typeof (OrmEntityProperty), baseName);
        }
Exemplo n.º 14
0
        private RelationshipInfo CreateRelationship(DataPathJoin join, OrmEntity leftEntity, OrmEntity rightEntity, bool isRightPK)
        {
            string targetField = join.FromField;

            if (targetField.StartsWith("@"))
            {
                targetField = targetField.Substring(1);
            }

            OrmFieldProperty leftProperty = leftEntity.Properties.GetFieldPropertyByFieldName(targetField);

            if (leftProperty == null)
            {
                LogError(leftEntity, "Property based on field '{0}' not found on entity based on table '{1}'", join.FromField, leftEntity.TableName);
                return null;
            }

            targetField = join.ToField;

            if (targetField.StartsWith("@"))
            {
                targetField = targetField.Substring(1);
            }

            OrmFieldProperty rightProperty = rightEntity.Properties.GetFieldPropertyByFieldName(targetField);

            if (rightProperty == null)
            {
                LogError(rightEntity, "Property based on field '{0}' not found on entity based on table '{1}'", join.ToField, rightEntity.TableName);
                return null;
            }

            OrmRelationship ormRelationship = CollectionUtils.Find(
                _ormModel.Relationships,
                delegate(OrmRelationship item)
                    {
                        if (item.Columns.Count > 0)
                        {
                            OrmRelationshipColumn column = item.Columns[0];
                            return ((item.ParentEntity == leftEntity && item.ChildEntity == rightEntity &&
                                     column.ParentProperty == leftProperty && column.ChildProperty == rightProperty &&
                                     (item.Cardinality == OrmRelationship.ManyToOne) == isRightPK &&
                                     item.ParentProperty.Include) ||
                                    (item.ParentEntity == rightEntity && item.ChildEntity == leftEntity &&
                                     column.ParentProperty == rightProperty && column.ChildProperty == leftProperty &&
                                     (item.Cardinality == OrmRelationship.OneToMany) == isRightPK &&
                                     item.ChildProperty.Include));
                        }

                        return false;
                    });

            bool isInverted;

            if (ormRelationship == null)
            {
                bool isLeftDynamic = leftEntity.Package.GetGenerateAssembly();

                if (!isLeftDynamic)
                {
                    LogError("Cannot create the necessary relationship property for '{0}' join", join);
                    return null;
                }

                string cardinality;
                string parentPropertyName;

                if (isRightPK)
                {
                    cardinality = OrmRelationship.ManyToOne;
                    parentPropertyName = GenerateReferencePropertyName(rightEntity, leftEntity, leftProperty);
                }
                else
                {
                    cardinality = OrmRelationship.OneToMany;
                    parentPropertyName = GenerateCollectionPropertyName(leftEntity, rightEntity);
                }

                ormRelationship = new OrmRelationship(
                    leftEntity,
                    rightEntity,
                    cardinality,
                    leftProperty,
                    rightProperty,
                    CascadeOption.SaveUpdate);
                ormRelationship.ParentProperty.PropertyName = parentPropertyName;
                leftEntity.ChildEntities.Add(ormRelationship);
                rightEntity.ParentEntities.Add(ormRelationship);
                ormRelationship.ChildProperty.Include = false;
                isInverted = false;
            }
            else
            {
                isInverted = (ormRelationship.ParentEntity == rightEntity);
            }

            return new RelationshipInfo(ormRelationship, isInverted);
        }
Exemplo n.º 15
0
        private RelationshipInfo CreateExtentedEntity(OrmEntity parentEntity, OrmEntity childEntity, bool isInverted)
        {
            if (childEntity.ExtendedEntity != null && childEntity.ExtendedEntity != parentEntity)
            {
                LogError(childEntity, "Entity '{0}' already extends entity '{1}'", childEntity, childEntity.ExtendedEntity);
                return null;
            }

            Debug.Assert(parentEntity != childEntity);
            object dummy = parentEntity.ExtensionEntities;
            childEntity.ExtendedEntity = parentEntity;
            childEntity.IsExtension = true;
            return new RelationshipInfo(parentEntity, childEntity, isInverted);
        }
Exemplo n.º 16
0
        private RelationshipInfo CreateExtentedEntity(DataPathJoin join, OrmEntity leftEntity, OrmEntity rightEntity, string leftKeyField, string rightKeyField)
        {
            if (leftEntity.ExtendedEntity == rightEntity)
            {
                return new RelationshipInfo(rightEntity, leftEntity, true);
            }
            else if (rightEntity.ExtendedEntity == leftEntity)
            {
                return new RelationshipInfo(leftEntity, rightEntity, false);
            }
            else
            {
                int leftSimilarity = MeasureSimilarity(join.FromTable, leftKeyField);
                int rightSimilarity = MeasureSimilarity(join.ToTable, rightKeyField);

                if (leftSimilarity == rightSimilarity)
                {
                    LogError("Unable to establish owning table in join '{0}'", join);
                    return null;
                }

                RelationshipInfo relationship = (rightSimilarity > leftSimilarity
                                                     ? CreateExtentedEntity(rightEntity, leftEntity, true)
                                                     : CreateExtentedEntity(leftEntity, rightEntity, false));
                leftEntity.Validate();
                leftEntity.Save();
                rightEntity.Validate();
                rightEntity.Save();
                return relationship;
            }
        }