public SchemaProvider ReadApplication(string ApplicationName, string ApplicationFolder, string exeName, string XPOConnectionString, string DBConnectionString)
        {
            IList<string> moduleList = new List<string>(){
               "Para.Core.Modules.SystemModule.Win.dll",
               "Para.Model.Definition.dll",
               "Para.Modules.WorkflowLight.dll",
               "Para.Core.Modules.SpellCheckerModule.dll",
               "Para.Modules.App.dll",
               "Para.Modules.App.Win.dll",
               "Para.Modules.ABF.dll",
               "Para.Modules.BH.dll",
               "Para.Modules.RA.dll",
               "Para.Modules.FV.dll",
               "Para.Modules.Workflow.dll",
               "Para.Modules.Document.Activities.dll",
               "Para.Modules.Intern.Win.dll",
               "Para.Modules.MV.dll"
            };

            IList<Assembly> assemblies = new List<Assembly>();
            foreach (var file in moduleList)
                assemblies.Add(Assembly.LoadFile(ApplicationFolder + file));

            ControllersManager controllerManager = new ControllersManager();

            ApplicationModulesManager man = new ApplicationModulesManager(controllerManager, ApplicationFolder);
            man.AddModuleFromAssemblies(assemblies.Select(ass => ass.FullName).ToArray());

            var sec = new SecurityDummy();

            SAConnection conn = new SAConnection(DBConnectionString);
            conn.Open();

            XpoDefault.DataLayer = XpoDefault.GetDataLayer("XpoProvider=Asa;DataSourceName='paradat'", new ReflectionDictionary(), AutoCreateOption.None);

            XPObjectSpaceProvider provider = new XPObjectSpaceProvider(new ConnectionStringDataStoreProvider(XPOConnectionString));

            DesignerModelFactory dmf = new DesignerModelFactory();

            string ApplicationFileName = ApplicationFolder + exeName;
            string ApplicationConfigFileName = String.Format("{0}{1}.config", ApplicationFolder, exeName);

            var app = dmf.CreateApplicationByConfigFile(ApplicationConfigFileName, ApplicationFileName, ref ApplicationFolder);
            app.DatabaseVersionMismatch += app_DatabaseVersionMismatch;
            app.Setup("paraOffice", provider, man, sec);

            var dict = (app.CreateObjectSpace() as XPObjectSpace).Session.Dictionary;

            SchemaProvider schemaProvider = new SchemaProvider();
            foreach (var classinfo in dict.Classes.Cast<XPClassInfo>().Where(c => c.IsPersistent))
            {
                if (classinfo.Table.IsView)
                {
                    schemaProvider.Views.Add(new FastSchemaProvider.View() { Name = classinfo.Table.Name });
                    continue;
                }

                var table = new Table();
                table.ActualName = classinfo.Table.Name;

                foreach (var column in classinfo.Table.Columns)
                    table.Columns.Add(new Column() { ActualName = column.Name, DataType = column.ColumnType.ToSchemaDbType(), MaxLength = column.Size, IsPrimaryKeyColumn = column.IsKey, IsIdentity = column.IsIdentity });

                table.BuildPrimaryKey();

                foreach (var index in classinfo.Table.Indexes)
                {
                    var schemaIndex = new Index() { IndexName = index.Name, IndexType = index.IsUnique ? IndexTypes.Unqiue : IndexTypes.NonUnqiue };
                    int i = 1;
                    foreach (var col in index.Columns)
                    {
                        var indexCol = new IndexColumn();
                        indexCol.ColumnName = col;
                        indexCol.Sequence = i;
                        indexCol.Order = Ordering.Ascending;
                        i++;

                        schemaIndex.Columns.Add(indexCol);
                    }
                    table.Indizes.Add(schemaIndex);
                }

                foreach (var fk in classinfo.Table.ForeignKeys)
                {
                    var foreignKey = new ForeignKey();
                    foreignKey.Name = fk.Name;
                    foreignKey.Columns.AddRange(fk.Columns.Cast<string>());
                    foreignKey.DetailTable = classinfo.TableName;
                    foreignKey.MasterTable = fk.PrimaryKeyTable;
                    foreignKey.UniqueColumns.AddRange(fk.PrimaryKeyTableKeyColumns.Cast<string>());

                    table.ForeignKeys.Add(foreignKey);
                }

                schemaProvider.Tables.Add(table);
            }

            return schemaProvider;
        }
Exemplo n.º 2
0
 private static int Main(string[] args)
 {
     ExitCode exitCode = ExitCode.UpdateError;
     WriteHeader();
     if(args.Length == 0) {
         Console.WriteLine("Usage: {0}", usage);
         Console.WriteLine("Exit codes: 0 - " + updateCompleted + ".");
         Console.WriteLine("            1 - " + updateError + ".");
         Console.WriteLine("            2 - " + updateNotNeeded + ".");
     }
     else {
         try {
             string configFileName = null;
             bool silent = false;
             string applicationFileName = null;
             string customConnectionString = null;
             if(args[0] == STR_Silent) {
                 silent = true;
                 Console.WriteLine("Silent mode ON");
                 if(args.Length > 1) {
                     configFileName = args[1];
                     if((args.Length > 2) && (args[2] != STR_ConnectionString)) {
                         applicationFileName = args[2];
                     }
                 }
                 else {
                     throw new Exception("The application configuration file parameter couldn't be found.");
                 }
             }
             else {
                 Console.WriteLine("Silent mode OFF");
                 configFileName = args[0];
                 if((args.Length > 1) && (args[1] != STR_ConnectionString)) {
                     applicationFileName = args[1];
                 }
             }
             if(!File.Exists(configFileName)) {
                 Exception e = null;
                 try {
                     configFileName = Path.Combine(System.Environment.CurrentDirectory, configFileName);
                     configFileName = Path.GetFullPath(configFileName);
                 }
                 catch(Exception ex) {
                     e = ex;
                 }
                 if(!File.Exists(configFileName) || (e != null)) {
                     throw new Exception(String.Format("The application configuration file '{0}' couldn't be found.", configFileName), e);
                 }
             }
             if(!string.IsNullOrEmpty(applicationFileName)) {
                 Exception e = null;
                 try {
                     applicationFileName = Path.GetFullPath(applicationFileName);
                 }
                 catch(Exception ex) {
                     e = ex;
                 }
                 if(!File.Exists(applicationFileName) || (e != null)) {
                     throw new Exception(String.Format("The application assembly file '{0}' couldn't be found.", applicationFileName), e);
                 }
             }
             for(int i = 0; i < args.Length; i++) {
                 if(args[i] == STR_ConnectionString) {
                     if((i + 1) == (args.Length - 1)) {
                         customConnectionString = args[i + 1];
                         if(string.IsNullOrEmpty(customConnectionString)) {
                             throw new Exception("The specified connection string parameter value is empty.");
                         }
                         break;
                     }
                     else {
                         throw new Exception("The connection string parameter couldn't be found. Ensure that it is enclosed in double quotes.");
                     }
                 }
             }
             string connectionString;
             string modules;
             string tablePrefixes;
             GetDBUpdaterParameters(configFileName, out connectionString, out modules, out tablePrefixes);
             if(!string.IsNullOrEmpty(customConnectionString)) {
                 connectionString = customConnectionString;
             }
             string diffsPath = Path.GetDirectoryName(configFileName);
             if(configFileName.ToLower().IndexOf("web.config") == -1) {
                 diffsPath = Path.Combine(diffsPath, @"..\..\");
             }
             DesignerModelFactory dmf = new DesignerModelFactory();
             string assembliesPath = string.Empty;
             try {
                 dmf.GetFileContainingApplicationType(configFileName, applicationFileName, ref assembliesPath);
             }
             catch(DesignerModelFactory_FindApplicationAssemblyException e) {
                 throw new Exception(e.Message + Environment.NewLine +
                     "To avoid ambiguity, specify assembly file name as the third parameter." + Environment.NewLine + usage);
             }
             XafApplication application = dmf.CreateApplicationByConfigFile(configFileName, applicationFileName, ref assembliesPath);
             ISupportSplashScreen temp = application as ISupportSplashScreen;
             if(temp != null) {
                 temp.RemoveSplash();
             }
             if(!string.IsNullOrEmpty(connectionString)) {
                 application.ConnectionString = connectionString;
             }
             else {
                 Console.WriteLine("ConnectionString is not found in the configuration file. DBUpdater will use the ConnectionString specified in the Application designer.");
             }
             if(tablePrefixes != null) {
                 application.TablePrefixes = tablePrefixes;
             }
             application.Setup(application.ApplicationName, application.ConnectionString, modules.Split(';'));
             DatabaseUpdater dbUpdater = application.CreateDatabaseUpdater();
             ApplicationStatusUpdater.UpdateStatus += new EventHandler<UpdateStatusEventArgs>(ApplicationStatusUpdater_UpdateStatus);
             Console.WriteLine("Updating database via the following connection string:");
             Console.WriteLine(application.ObjectSpaceProvider.ConnectionString);
             Console.WriteLine();
             CompatibilityError error = dbUpdater.CheckCompatibility();
             if(error != null) {
                 exitCode = UpdateDataBase(error, dbUpdater, silent);
             }
             else {
                 Console.WriteLine(updateNotNeeded + ".");
                 exitCode = ExitCode.UpdateNotNeeded;
             }
             application.Dispose();
             ApplicationStatusUpdater.UpdateStatus -= new EventHandler<UpdateStatusEventArgs>(ApplicationStatusUpdater_UpdateStatus);
         }
         catch(Exception e) {
             Tracing.Tracer.LogError(e);
             exitCode = ExitCode.UpdateError;
             Console.WriteLine(updateError + ".");
             string exceptionReport = Tracing.Tracer.FormatExceptionReport(e);
             Console.WriteLine(exceptionReport);
         }
     }
     return (int)exitCode;
 }