Implemented Import and Compile operations on DevEnv.

This commit is contained in:
Torben Nehmer 2016-11-26 22:11:35 +01:00
parent 566d727297
commit d3adf2c725
2 changed files with 80 additions and 1 deletions

View File

@ -67,13 +67,29 @@ namespace NavScm.TestHost
log.DebugFormat("Type {0}, ID {1}, Name {2}, Modified {3} {4}, Version {5}",
o2.Type, o2.ID, o2.Name, o2.Date.ToShortDateString(), o2.Time.ToShortTimeString(), o2.Version_List);
log.Debug("=== Exporting sample objects ===");
DevEnvInterface devenv = new DevEnvInterface("C:\\Program Files (x86)\\Microsoft Dynamics NAV\\tbrt-nav-erp-02\\RoleTailored Client\\finsql.exe",
"tbrt-sql-erp-01", "TERRABIT 2015 DEV");
devenv.Export(loadedObjects["5.80"], $"{Directory.GetCurrentDirectory()}\\CU80.txt");
devenv.Export(loadedObjects["5.99996"], $"{Directory.GetCurrentDirectory()}\\CU99996.txt");
devenv.Export(loadedObjects["5.99997"], $"{Directory.GetCurrentDirectory()}\\CU99997.txt");
devenv.Export(loadedObjects["5.99998"], $"{Directory.GetCurrentDirectory()}\\CU99998.txt");
devenv.Export(loadedObjects["1.13"], $"{Directory.GetCurrentDirectory()}\\TAB13.txt");
//log.Debug("=== Importing TN_WORK ===");
//o2 = devenv.Import(loadedObjects["5.99997"], $"{Directory.GetCurrentDirectory()}\\CU99997.txt");
//log.DebugFormat("Object after import: Type {0}, ID {1}, Name {2}, Modified {3} {4}, Version {5}",
// o2.Type, o2.ID, o2.Name, o2.Date.ToShortDateString(), o2.Time.ToShortTimeString(), o2.Version_List);
//log.Debug("=== Compiling TN_WORK ===");
//o2 = devenv.Compile(loadedObjects["5.99997"]);
//log.DebugFormat("Object after compilation: Type {0}, ID {1}, Name {2}, Modified {3} {4}, Version {5}",
// o2.Type, o2.ID, o2.Name, o2.Date.ToShortDateString(), o2.Time.ToShortTimeString(), o2.Version_List);
log.Info("Shutting down...");
Console.ReadLine();

View File

@ -23,6 +23,11 @@ namespace NavScm.NavInterface
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(DevEnvInterface));
/// <summary>
/// The Context to use for DB operations.
/// </summary>
private NavSQLDataContext navSqlContext;
/// <summary>
/// Helper structure to capture the full execution result of finsql.exe.
/// </summary>
@ -102,6 +107,8 @@ namespace NavScm.NavInterface
DatabaseServer = databaseServer;
DatabaseName = databaseName;
navSqlContext = new NavSQLDataContext($"Data Source=\"{DatabaseServer}\";Initial Catalog=\"{DatabaseName}\";Integrated Security=True");
if (log.IsDebugEnabled)
{
log.Debug($"Constructed and attached to DevEnv {DevEnvPath}");
@ -260,7 +267,7 @@ namespace NavScm.NavInterface
// TODO: Skip Unlicensed objects?
string command = $"Command=ExportObjects,File=\"{destinationFileName}\",Filter=\"{obj.GetFilter()}\"";
log.DebugFormat("Export: Build command string: {0}", command);
log.DebugFormat("Export: Built command string: {0}", command);
var result = ExecuteCommand(command);
if (! result.Success)
{
@ -268,5 +275,61 @@ namespace NavScm.NavInterface
}
}
/// <summary>
/// Imports a given NAV object into the database from the file given. The existing file is overwritten,
/// schema changes are executed forcibly, so beware of possible data loss.
/// </summary>
/// <remarks>
/// <para>The file name must end with .txt, as finsql.exe deduces the export format from the destiation files
/// extension (crap). We have no other option here as to play by these rules.</para>
/// <para>Be aware, that NAV uses some strange mix of CP850 and CP1252 to encode the text files,
/// this is mean stuff here. The call does not try to convert this into something more sensible
/// at this point, especially since the IDE won't be able to handle this properly if you have to
/// work with the files manually. Checked with NAV 2015, YMMV.</para></remarks>
/// <para>Check http://forum.mibuso.com/discussion/37078/encoding-of-exported-navision-objects-txt-files
/// for further details about this.</para>
/// <param name="obj">The NAV object as taken from the SQL database or from the cache (doesn't matter).</param>
/// <param name="sourceFileName">The name of the source file. The file name must end with .txt.</param>
/// <returns>A new NavObject representing the imported object.</returns>
public NavObject Import(NavObject obj, string sourceFileName)
{
Contract.Requires(obj != null);
Contract.Requires(sourceFileName != "");
Contract.Requires(Path.GetExtension(sourceFileName) == ".txt");
// TODO: Skip Unlicensed objects?
string command = $"Command=ImportObjects,File=\"{sourceFileName}\",ImportAction=overwrite,SynchronizeSchemaChanges=force";
log.DebugFormat("Import: Built command string: {0}", command);
var result = ExecuteCommand(command);
if (!result.Success)
{
throw new ArgumentException($"Cannot import object {obj.NavType} ID {obj.ID} from file {sourceFileName}: {result.ErrorMessage}");
}
return navSqlContext.NavObject.Where(o => o.Type == obj.Type && o.ID == obj.ID).First();
}
/// <summary>
/// Compiles the NavObject given and reloads its object descriptor from the database. Compilation is done with
/// forced schema changes, so beware of possible data loss.
/// </summary>
/// <param name="obj">The NAV object as taken from the SQL database or from the cache (doesn't matter).</param>
/// <returns>A new NavObject representing the imported object.</returns>
public NavObject Compile(NavObject obj)
{
Contract.Requires(obj != null);
// TODO: Skip Unlicensed objects?
string command = $"Command=CompileObjects,Filter=\"{obj.GetFilter()}\",SynchronizeSchemaChanges=force";
log.DebugFormat("Compile: Built command string: {0}", command);
var result = ExecuteCommand(command);
if (!result.Success)
{
throw new ArgumentException($"Cannot compile object {obj.NavType} ID {obj.ID}: {result.ErrorMessage}");
}
return navSqlContext.NavObject.Where(o => o.Type == obj.Type && o.ID == obj.ID).First();
}
}
}