private void LoadFromSchema(CodeTemplate template)
            {
                _objectType = TemplateHelper.ToObjectType(template.CodeTemplateInfo);

                //child, and parent name
                if (IsCollection)
                    _child = (string)template.GetProperty("ChildName");
                if (IsChild)
                    _parent = (string)template.GetProperty("ParentName");
                if (_parent == null) _parent = string.Empty;

                //child collections
                StringCollection types = (StringCollection)template.GetProperty("ChildCollectionTypes");
                StringCollection names = (StringCollection)template.GetProperty("ChildPropertyNames");
                StringCollection sets = (StringCollection)template.GetProperty("ChildEntitySets");
                if (types != null && types.Count > 0)
                {
                    for (int i = 0; i < types.Count; i++)
                    {
                        string type = types[i].TrimEnd();
                        if (type == string.Empty) continue;

                        string name = (names != null && i < names.Count) ? names[i] : type;
                        string set = (sets != null && i < sets.Count) ? sets[i] : types[i];

                        PropertyInfo prop = new PropertyInfo(name, type, set, this);
                        _properties.Add(prop);
                        _childCollection.Add(prop);
                    }
                }

                //table, view schema
                TableSchema table = (TableSchema)template.GetProperty("RootTable");
                ViewSchema view = (ViewSchema)template.GetProperty("RootView");
				CommandSchema command = (CommandSchema)template.GetProperty("RootCommand");
				int resultSetIndex = (int)template.GetProperty("ResultSetIndex");
                if (table == null && view == null && command == null)
                    throw new Exception("RootCommand, RootTable or RootView is required.");

                StringCollection uniqueColumns = (StringCollection)template.GetProperty("UniqueColumnNames");
                if (uniqueColumns == null) uniqueColumns = new StringCollection();

                StringCollection filterColumns = (StringCollection)template.GetProperty("FilterColumnNames");
                if (filterColumns == null) filterColumns = new StringCollection();

				if (command != null)
				{
                    _dbName = ((SchemaObjectBase)command).Database.Name;
                    _entityName = CsHelper.ConvertToSingular(((SchemaObjectBase)command).Name);
                    if (resultSetIndex == 0) _fetchCommand = command.Name;
                    LoadProperties(command, resultSetIndex, uniqueColumns, filterColumns);
				}
                else if (table != null)
                {
                    _dbName = ((SchemaObjectBase)table).Database.Name;
                    _entityName = CsHelper.ConvertToSingular(((SchemaObjectBase)table).Name);
                    LoadProperties(table);
                }
                else
                {
                    _dbName = ((SchemaObjectBase)view).Database.Name;
                    _entityName = CsHelper.ConvertToSingular(((SchemaObjectBase)view).Name);
                    LoadProperties(view, uniqueColumns, filterColumns);
                }
            }
            private void Initialize(CodeTemplate template)
            {
                _namespace = (string)template.GetProperty("ClassNamespace");
                _dalNamespace = (string)template.GetProperty("DalNamespace");
                if (_dalNamespace == string.Empty) _dalNamespace = "Dal";
                _objectName = (string)template.GetProperty("ObjectName");
                _baseClass = (string)template.GetProperty("BaseClass");

                //template settings
                _transactionType = (TransactionalTypes)template.GetProperty("TransactionalType");
                _propertyAuthorization = (PropertyAccessSecurity)template.GetProperty("PropertyAuthorization");
                _useSecurity = (bool)template.GetProperty("AuthorizationRules");
                _codeGenMethod = (CodeGenerationMethod)template.GetProperty("GenerationMethod");
                _classType = (GenerationClassType)template.GetProperty("ClassType");

                //db commands
                _fetchCommand = string.Format(FetchCommandFormat, _objectName);
                _insertCommand = string.Format(InsertCommandFormat, _objectName);
                _updateCommand = string.Format(UpdateCommandFormat, _objectName);
                _deleteCommand = string.Format(DeleteCommandFormat, _objectName);
            }
            private void LoadFromXml(CodeTemplate template)
            {
                //read from xml file
                string path = (string)template.GetProperty("XmlFilePath");

                XmlTextReader xtr = new XmlTextReader(path);

                if(!MoveToObject(xtr, _objectName))
                    throw new ApplicationException(string.Format("Object {0} does not exist!", _objectName));
             
                //read object attributes
                while (xtr.MoveToNextAttribute())
	            {
                    switch (xtr.LocalName.ToLower())
                    {
                        case "namespace":
                            _namespace = xtr.Value;
                            break;
                        case "dalnamespace":
                            _dalNamespace = xtr.Value;
                            break;
                        case "access":                            
                            _access = xtr.Value;
                            break;
                        case "type":
                            _objectType = (ObjectType)Enum.Parse(typeof(ObjectType), xtr.Value, true);
                            break;
                        case "base":
                            _baseClass = xtr.Value;
                            break;
                    }
	            }
                if (_entityName == string.Empty)
                    _entityName = _objectName;

                //read object elements
                while (xtr.Read())
                {
                    if (xtr.NodeType == XmlNodeType.EndElement 
                        && xtr.LocalName.ToLower() == "object")
                        break;  //reach end of object node
                    if (xtr.NodeType == XmlNodeType.Element) {
                        switch (xtr.LocalName.ToLower())
                        {
                            case "properties":
                                LoadProperties(xtr); //read properties
                                break;
                            case "transactionaltype":
                                _transactionType = (TransactionalTypes)Enum.Parse(typeof(TransactionalTypes), xtr.ReadElementString());
                                break;
                            case "propertyauthorization":
                                _propertyAuthorization = (PropertyAccessSecurity)Enum.Parse(typeof(PropertyAccessSecurity), xtr.ReadElementString());
                                break;
                            case "authorizationrules":
                                _useSecurity = bool.Parse(xtr.ReadElementString());
                                break;
                            case "relationship":
                                while (xtr.MoveToNextAttribute())
                                {
                                    switch (xtr.LocalName.ToLower())
                                    {
                                        case "parent":
                                            _parent = xtr.Value;
                                            break;
                                        case "child":
                                            _child = xtr.Value;
                                            break;
                                    }
                                }
                                break;
                            case "dbcommands":
                                _dbName = xtr.GetAttribute("DbName");
                                while (xtr.Read())
                                {
                                    if (xtr.NodeType == XmlNodeType.EndElement
                                        && xtr.LocalName.ToLower() == "dbcommands")
                                        break;  //reach end of properties node
                                    if (xtr.NodeType == XmlNodeType.Element)
                                    {
                                        switch (xtr.LocalName.ToLower())
	                                    {
		                                    case "fetchcommand":
                                                _fetchCommand = xtr.ReadElementString();
                                                break;
                                            case "insertcommand":
                                                _insertCommand = xtr.ReadElementString();
                                                break;
                                            case "updatecommand":
                                                _updateCommand = xtr.ReadElementString();
                                                break;
                                            case "deletecommand":
                                                _deleteCommand = xtr.ReadElementString();
                                                break;
	                                    }
                                    }
                                }
                                break;
                        }
                    }
                    
                }   //whild(xtr.Read())

                xtr.Close();

            }
            public ObjectInfo(CodeTemplate template)
            {
                if (!TemplateHelper.IsObjectType(template.CodeTemplateInfo))
                    throw new ArgumentException(string.Format("Template '{0}' is not a business object template type", template.CodeTemplateInfo.FileName));

                Initialize(template);

                string xmlpath = (string)template.GetProperty("XmlFilePath");
                bool isFromXml = (xmlpath != null && xmlpath.Length > 0);
                if (isFromXml)
                    LoadFromXml(template);
                else
                    LoadFromSchema(template);

                if (_baseClass.Length == 0)
                {
                    //assign base class
                    switch (_objectType)
                    {
                        case ObjectType.EditableRoot:
                        case ObjectType.EditableChild:
                        case ObjectType.EditableSwitchable:
                            _baseClass = BusinessBase;
                            break;
                        case ObjectType.ReadOnlyRoot:
                        case ObjectType.ReadOnlyChild:
                            _baseClass = ReadOnlyBase;
                            break;
                        case ObjectType.EditableChildList:
                        case ObjectType.EditableRootList:
                            _baseClass = BusinessListBase;
                            break;
                        case ObjectType.ReadOnlyRootList:
                        case ObjectType.ReadOnlyChildList:
                            _baseClass = ReadOnlyListBase;
                            break;
                        case ObjectType.NameValueList:
                            _baseClass = NameValueListBase;
                            break;
                    }
                }

                //validate object
                Validate();
            }
Exemplo n.º 5
0
        private void GenerateObject(CslaObjectInfo objInfo, CslaGeneratorUnit unit)
        {
            Metadata.GenerationParameters generationParams = unit.GenerationParams;
            string fileName             = GetBaseFileName(objInfo, false, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, false, false);
            string baseFileName         = GetBaseFileName(objInfo, true, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, false, false);
            string classCommentFileName = string.Empty;

            if (!string.IsNullOrEmpty(generationParams.ClassCommentFilenameSuffix))
            {
                classCommentFileName = GetBaseFileName(objInfo, false, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, true, generationParams.SeparateClassComment);
            }
            FileStream   fsBase = null;
            StreamWriter swBase = null;
            StreamWriter sw     = null;

            try
            {
                string       tPath    = this._fullTemplatesPath + objInfo.OutputLanguage.ToString() + @"\" + GetTemplateName(objInfo);
                CodeTemplate template = GetTemplate(objInfo, tPath);
                if (template != null)
                {
                    StringBuilder errorsOutput   = new StringBuilder();
                    StringBuilder warningsOutput = new StringBuilder();
                    template.SetProperty("ActiveObjects", generationParams.ActiveObjects);
                    template.SetProperty("Errors", errorsOutput);
                    template.SetProperty("Warnings", warningsOutput);
                    template.SetProperty("CurrentUnit", unit);
                    template.SetProperty("DataSetLoadingScheme", objInfo.DataSetLoadingScheme);
                    if (generationParams.BackupOldSource && File.Exists(baseFileName))
                    {
                        FileInfo oldFile = new FileInfo(baseFileName);
                        if (File.Exists(baseFileName + ".old"))
                        {
                            File.Delete(baseFileName + ".old");
                        }
                        oldFile.MoveTo(baseFileName + ".old");
                    }
                    fsBase = File.Open(baseFileName, FileMode.Create);
                    OnGenerationFileName(baseFileName);
                    swBase = new StreamWriter(fsBase);
                    template.Render(swBase);
                    errorsOutput   = (StringBuilder)template.GetProperty("Errors");
                    warningsOutput = (StringBuilder)template.GetProperty("Warnings");
                    if (errorsOutput.Length > 0)
                    {
                        objFailed++;
                        OnGenerationInformation("Failed:" + Environment.NewLine + errorsOutput);
                    }
                    else
                    {
                        if (warningsOutput != null)
                        {
                            if (warningsOutput.Length > 0)
                            {
                                objectWarnings++;
                                OnGenerationInformation("Warning:" + Environment.NewLine + warningsOutput);
                            }
                        }
                        objSuccess++;
                        //OnGenerationInformation("Success");
                    }
                }
                GenerateInheritanceFile(fileName, objInfo, generationParams.ActiveObjects, unit);
                if (!string.IsNullOrEmpty(generationParams.ClassCommentFilenameSuffix))
                {
                    GenerateClassCommentFile(classCommentFileName, objInfo, generationParams.ActiveObjects, unit);
                }
            }
            catch (Exception e)
            {
                objFailed++;
                ShowExceptionInformation(e);
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
                if (swBase != null)
                {
                    swBase.Close();
                }
            }
        }
            private void LoadFromSchema(CodeTemplate template)
            {
                _objectType = TemplateHelper.ToObjectType(template.CodeTemplateInfo);

                //child, and parent name
                if (IsCollection)
                    _child = (string)template.GetProperty("ChildName");
                if (IsChild)
                    _parent = (string)template.GetProperty("ParentName");
                if (_parent == null) _parent = string.Empty;

                //child collections
                StringCollection types = (StringCollection)template.GetProperty("ChildCollectionNames");
                StringCollection names = (StringCollection)template.GetProperty("ChildPropertyNames");
                if (types != null && names != null && types.Count > 0 && names.Count > 0)
                {
                    int maxCount = types.Count < names.Count ? types.Count : names.Count;

                    for (int i = 0; i < maxCount; i++)
                    {
                        if (names[i].TrimEnd() != string.Empty && types[i].TrimEnd() != string.Empty)
                        {
                            PropertyInfo prop = new PropertyInfo(names[i], types[i], this);
                            _properties.Add(prop);
                            _childCollection.Add(prop);
                        }
                    }
                }

                //table, view schema
                TableSchema table = (TableSchema)template.GetProperty("RootTable");
                ViewSchema view = (ViewSchema)template.GetProperty("RootView");
				CommandSchema command = (CommandSchema)template.GetProperty("RootCommand");
				int resultSetIndex = (int)template.GetProperty("ResultSetIndex");
                if (table == null && view == null && command == null)
                    throw new Exception("RootCommand, RootTable or RootView is required.");

                StringCollection uniqueColumns = (StringCollection)template.GetProperty("UniqueColumnNames");
                if (uniqueColumns == null) uniqueColumns = new StringCollection();

                StringCollection filterColumns = (StringCollection)template.GetProperty("FilterColumnNames");
                if (filterColumns == null) filterColumns = new StringCollection();

				if (command != null)
				{
                    if(resultSetIndex==0) _fetchCommand = command.Name;
                    LoadProperties(command, resultSetIndex, uniqueColumns, filterColumns);
				}
                else if (table != null)
                {
                    LoadProperties(table);
                }
                else
                {
                    LoadProperties(view, uniqueColumns, filterColumns);
                }
            }
Exemplo n.º 7
0
        private void GenerateObject(CslaObjectInfo objInfo, CslaGeneratorUnit unit)
        {
            Metadata.GenerationParameters generationParams = unit.GenerationParams;
            string fileName     = GetFileName(objInfo, generationParams.SeparateNamespaces, generationParams.OutputLanguage);
            string baseFileName = GetBaseFileName(objInfo, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.UseDotDesignerFileNameConvention, generationParams.OutputLanguage);

            try
            {
                GenerateInheritanceFile(fileName, objInfo, generationParams.ActiveObjects, unit);
                string       tPath    = this._fullTemplatesPath + generationParams.OutputLanguage.ToString() + @"\" + GetTemplateName(objInfo);
                CodeTemplate template = GetTemplate(objInfo, tPath);
                if (template != null)
                {
                    StringBuilder errorsOutput   = new StringBuilder();
                    StringBuilder warningsOutput = new StringBuilder();
                    template.SetProperty("ActiveObjects", generationParams.ActiveObjects);
                    template.SetProperty("Errors", errorsOutput);
                    template.SetProperty("Warnings", warningsOutput);
                    template.SetProperty("CurrentUnit", unit);
                    template.SetProperty("DataSetLoadingScheme", objInfo.DataSetLoadingScheme);
                    if (generationParams.BackupOldSource && File.Exists(baseFileName))
                    {
                        FileInfo oldFile = new FileInfo(baseFileName);
                        if (File.Exists(baseFileName + ".old"))
                        {
                            File.Delete(baseFileName + ".old");
                        }
                        oldFile.MoveTo(baseFileName + ".old");
                    }
                    FileInfo fi = new FileInfo(baseFileName);
                    using (FileStream fs = fi.Open(FileMode.Create))
                    {
                        OnGenerationFileName(fi.FullName);
                        using (StreamWriter swBase = new StreamWriter(fs))
                        {
                            template.Render(swBase);
                            errorsOutput   = (StringBuilder)template.GetProperty("Errors");
                            warningsOutput = (StringBuilder)template.GetProperty("Warnings");
                            if (errorsOutput.Length > 0)
                            {
                                objFailed++;
                                OnGenerationInformation("Failed:" + Environment.NewLine +
                                                        errorsOutput.ToString(), 2);
                            }
                            else
                            {
                                if (warningsOutput != null)
                                {
                                    if (warningsOutput.Length > 0)
                                    {
                                        objectWarnings++;
                                        OnGenerationInformation("Warning:" + Environment.NewLine + warningsOutput, 2);
                                    }
                                }
                                objSuccess++;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                objFailed++;
                ShowExceptionInformation(e);
            }
        }