public List <NavDB_Object> GetObjects(ENUM_NavObjetType objecttype, NavisionConnection connection)
        {
            List <NavDB_Object> NavobjectList;

            using (IDatabase db = DBFactory.CreateDatabase(connection))
            {
                string sql = string.Format("select * from [dbo].[Object] where [Type] = {0}", (int)objecttype);

                NavobjectList = db.Fetch <NavDB_Object>(sql).ToList();

                return(NavobjectList);
            }
        }
        public List <NavisionObject> GetObjectList(ENUM_NavObjetType objecttype, NavisionConnection connection)
        {
            var objects = _repository.GetObjects(objecttype, connection);

            List <NavisionObject> navobjects = new List <NavisionObject>();

            foreach (NavDB_Object obj in objects)
            {
                navobjects.Add(
                    new NavisionObject()
                {
                    ObjectId       = obj.ID,
                    ObjectType     = obj.Type,
                    Name           = obj.Name,
                    ObjectTypeName = ((ENUM_NavObjetType)obj.Type).ToString()
                });
            }

            return(navobjects);
        }
        public List <NavisionConnection> GetConectionsList()
        {
            List <NavisionConnection> developerConnectionList = new List <NavisionConnection>();

            string xml = NavisionObjectImporterExporterTool.Properties.Settings.Default.ConnectionFile;

#if DEBUG
            xml = string.Format(@"{0}\Connections.xml",
                                System.IO.Path.GetDirectoryName(
                                    System.Reflection.Assembly.GetExecutingAssembly().Location
                                    )
                                );
#endif

            if (!File.Exists(xml))
            {
                throw new Exception(string.Format("No existe el fichero de conexiones {0}", xml));
            }

            foreach (XElement xelement in XElement.Load(xml).Elements((XName)"connection").Select <XElement, XElement>((Func <XElement, XElement>)(conn => conn)))
            {
                NavisionConnection developerConnection = new NavisionConnection();
                developerConnection.Name     = xelement.Element((XName)"name").Value;
                developerConnection.Server   = xelement.Element((XName)"server").Value;
                developerConnection.Database = xelement.Element((XName)"database").Value;
                if (xelement.Element((XName)"ntauthentication").Value.ToLower() == "true")
                {
                    developerConnection.ntauthentication = true;
                }
                else
                {
                    developerConnection.ntauthentication = false;
                    developerConnection.username         = xelement.Element((XName)"username").Value;
                    developerConnection.password         = xelement.Element((XName)"password").Value;
                }
                developerConnectionList.Add(developerConnection);
            }
            return(developerConnectionList);
        }
        public static Database CreateDatabase(NavisionConnection navconnection)
        {
            DatabaseFactory DbFactory;

            try
            {
                string ConnectionString = string.Empty;

                ConnectionString += string.Format("Server = {0}", navconnection.Server);
                ConnectionString += string.Format(";Database = {0}", navconnection.Database);

                if (navconnection.ntauthentication)
                {
                    //Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;
                    ConnectionString += string.Format(";Trusted_Connection = True");
                }
                else
                {
                    //Server = myServerAddress; Database = myDataBase; User Id = myUsername; Password = myPassword;
                    ConnectionString += string.Format("; User Id = {0}", navconnection.username);
                    ConnectionString += string.Format("; Password = {0}", navconnection.password);
                }


                DbFactory = DatabaseFactory.Config(x =>
                {
                    x.UsingDatabase(() => new Database(ConnectionString, DatabaseType.SqlServer2012));
                });

                return(DbFactory.GetDatabase());
            }
            catch (Exception excep)
            {
                throw new Exception("Error creating the sql database object" + navconnection.Server + ". " + excep.Message);
            }
        }
 public List <NavisionObject> GetObjectList(ENUM_NavObjetType objecttype, NavisionConnection connection)
 {
     throw new NotImplementedException();
 }
        /*
         * finsql.exe
         *      command=exportobjects,
         *      file=<exportfile>,
         *      [servername=<server>,]
         *      [database=<database>,]
         *      [logfile=<path and filename>,]
         *      [filter=<filter>,]
         *      [username=<username>,]
         *      [password=<password>,]
         *      [ntauthentication=<yes|no|1|0>]
         */

        public List <string> GenerateCommands(
            NavisionConnection SourceConnection,
            NavisionObjectType ObjectType,
            string ObjectID,
            List <NavisionConnection> DestinationConnections,
            string BackupPath,
            string WorkPath,
            bool IncludeTimestamp,
            bool MakeDestinationBackup,
            bool Backup_FOB,
            bool Backup_TXT,
            bool InsertPauseBeforeImport
            )
        {
            List <string> ExportCommand = new List <string>();

            //export filter
            string _filter = string.Format("filter={0}Type={1};ID={2}{0}", '"', ObjectType.ObjectTypeName, ObjectID);

            //backup path
            string _backuppath = BackupPath.EndsWith(@"\") ? BackupPath : string.Format(@"{0}\", BackupPath);

            //working path
            string _workpath = WorkPath;

            _workpath = _workpath.EndsWith(@"\") ? _workpath : string.Format(@"{0}\", _workpath);

            //include timestamp in filenames
            string timestamp = string.Empty;

            if (IncludeTimestamp)
            {
                timestamp = String.Format("_{0:yyyyMMddHHmmssfff}", System.DateTime.Now);
            }

            string cleanobjectid = ObjectID.Replace("|", "_").Replace("..", "_");

            //nombre del archivo sin extension ni ruta
            string objname;

            objname = string.Format("{0}_{1}_{2}_{3}{4}",
                                    SourceConnection.Server.Replace(@"\", "_"),
                                    SourceConnection.Database,
                                    ObjectType.ObjectTypeName,
                                    cleanobjectid,
                                    timestamp);

            //FOB Full path completa
            string fobexportedobjectpath = string.Format("{0}{1}.{2}", _workpath, objname, "fob");

            string command = string.Empty;

            ExportCommand.Add(string.Format("REM {0} {1}", ObjectType.ObjectTypeName.ToUpper(), ObjectID.ToUpper()));
            ExportCommand.Add(Environment.NewLine);

            ExportCommand.Add(string.Format("REM Export ORIGEN -FOB-"));
            command  = string.Empty;
            command += string.Format("finsql.exe command=exportobjects, ");
            command += string.Format("servername={0}, ", SourceConnection.Server);
            command += string.Format("database={0}, ", SourceConnection.Database);
            command += string.Format("ntauthentication=1, ");
            command += string.Format("file={0}{1}.{2}, ", _workpath, objname, "fob");
            command += string.Format("{0}, ", _filter);
            command += string.Format("logfile={0}LOG_{1}.txt", _workpath, objname);
            command += Environment.NewLine;

            ExportCommand.Add(command);
            ExportCommand.Add(string.Format("REM Resultado export origen"));
            ExportCommand.Add(string.Format("type {0}navcommandresult.txt", _workpath));
            ExportCommand.Add(Environment.NewLine);

            //backup de destino
            if (MakeDestinationBackup)
            {
                List <string> exporttype = new List <string>();
                if (Backup_TXT)
                {
                    exporttype.Add("txt");
                }
                if (Backup_FOB)
                {
                    exporttype.Add("fob");
                }

                foreach (var conn in DestinationConnections)
                {
                    command = string.Empty;
                    objname = string.Format("{0}_{1}_{2}_{3}{4}", conn.Server.Replace(@"\", "_"), conn.Database, ObjectType.ObjectTypeName, cleanobjectid, timestamp);

                    foreach (string ty in exporttype)
                    {
                        ExportCommand.Add(string.Format("REM Backup Destino {0} {1} {2}", conn.Server, conn.Database, ty.ToUpper()));
                        command  = string.Empty;
                        command += string.Format("finsql.exe command=exportobjects, ");
                        command += string.Format("servername={0}, ", conn.Server);
                        command += string.Format("database={0}, ", conn.Database);
                        command += string.Format("ntauthentication=1, ");
                        command += string.Format("file={0}{1}.{2}, ", _backuppath, objname, ty);
                        command += string.Format("{0}, ", _filter);
                        command += string.Format("logfile={0}LOG_{1}.txt", _workpath, objname);
                        command += Environment.NewLine;
                        ExportCommand.Add(command);
                        ExportCommand.Add(string.Format("REM Resultado Backup"));
                        ExportCommand.Add(string.Format("type {0}navcommandresult.txt", _workpath));
                        ExportCommand.Add(Environment.NewLine);
                    }
                }
            }

            if (ObjectType.ObjectType != (int)ENUM_NavObjetType.Table)
            {
                //IMPORT EN DESTINO
                foreach (var conn in DestinationConnections)
                {
                    ExportCommand.Add(string.Format("REM IMPORT {0} {1}", conn.Server, conn.Database));

                    if (InsertPauseBeforeImport)
                    {
                        ExportCommand.Add(string.Format("REM Se importará el objeto de origen en {0} {1} {2}. Pulse cualquier tecla para continuar", conn.Server, conn.Database, fobexportedobjectpath));
                        ExportCommand.Add(string.Format("PAUSE"));
                    }

                    //finsql.exe command=importobjects, file=<importfile>, [servername=<server>,] [database=<database>,] [logfile=<path and filename>,] [importaction=<default|overwrite|skip|0|1|2>,] [username=<username>,] [password=<password>,] [ntauthentication=<yes|no|1|0>,] [synchronizeschemachanges=<yes|no|force>,] [navservername=<server name>,] [navserverinstance=<instance>,] [navservermanagementport=<port>,] [tenant=<tenant ID>]
                    //finsql.exe command=importobjects, file=C:\NewObjects.fob, servername=TestComputer01, database="Demo Database NAV (9-0)", ImportAction=overwrite

                    command = string.Empty;
                    objname = string.Format("{0}_{1}_{2}_{3}{4}", conn.Server.Replace(@"\", "_"), conn.Database, ObjectType.ObjectTypeName, cleanobjectid, timestamp);

                    //ExportCommand.Add(string.Format("REM IMPORTANDO EN {0} {1}", conn.Connection.Server, conn.Connection.Database));
                    command  = string.Empty;
                    command += string.Format("finsql.exe command=importobjects, ");
                    command += string.Format("servername={0}, ", conn.Server);
                    command += string.Format("database={0}, ", conn.Database);
                    command += string.Format("ntauthentication=1, ");
                    command += string.Format("file={0},", fobexportedobjectpath);
                    //if ((SynchronizeSchemaChanges) && (SelectedObjectTypeList.ObjectType == (int)ENUM_NavObjetType.Table))
                    //    command += string.Format("synchronizeschemachanges=yes,");
                    command += string.Format("ImportAction=overwrite,");
                    command += string.Format("logfile={0}LOG_IMPORT_{1}.txt", _workpath, objname);
                    command += Environment.NewLine;
                    ExportCommand.Add(command);
                    ExportCommand.Add(string.Format("REM Resultado importacion"));
                    ExportCommand.Add(string.Format("type {0}LOG_IMPORT_{1}.txt", _workpath, objname));
                    ExportCommand.Add(Environment.NewLine);
                }
            }
            ExportCommand.Add(string.Format("REM Proceso finalizado"));
            ExportCommand.Add(string.Format("PAUSE"));

            return(ExportCommand);
        }