< Summary

Information
Class: Morpho25.Management.Workspace
Assembly: Morpho25
File(s): D:\a\Morpho\Morpho\project\Morpho\Morpho25\Management\Workspace.cs
Line coverage
12%
Covered lines: 19
Uncovered lines: 137
Coverable lines: 156
Total lines: 307
Line coverage: 12.1%
Branch coverage
21%
Covered branches: 3
Total branches: 14
Branch coverage: 21.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

D:\a\Morpho\Morpho\project\Morpho\Morpho25\Management\Workspace.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using System.Reflection;
 4
 5namespace Morpho25.Management
 6{
 7    /// <summary>
 8    /// Database source enum.
 9    /// </summary>
 10    public enum DatabaseSource
 11    {
 12        User,
 13        Project
 14    }
 15
 16    /// <summary>
 17    /// Workspace class.
 18    /// </summary>
 19    public class Workspace
 20    {
 21        /// <summary>
 22        /// Default envimet folder.
 23        /// </summary>
 24        public const string DEFAULT_FOLDER = "ENVImet5";
 25        #region System details
 26        private const string VERSION = "7174219";
 27        private const string ENCRIPTION_LEVEL = "5194660";
 28        private const string CHECK_SUM = "18076640";
 29        private const string SYSTEM_FOLDER = "sys.basedata";
 30        private const string PYTHON_FOLDER = "sys.python";
 31        #endregion
 32
 33        #region User details
 34        private const string USER_FOLDER = "sys.userdata";
 35        private const string USER_SETTINGS = "usersettings.setx";
 36        private const string USER_DB = "userdatabase.edb";
 37        public const string FOLDER_NAME = "ENVI-met";
 38        #endregion
 39
 40        #region Project details
 41        private const string PROJECT_INFOX = "project.infox";
 42        private const string PROJECT_DB = "projectdatabase.edb";
 43        private const string SYSTEM_DB = "database.edb";
 44        #endregion
 45
 46        #region Main paths
 47        private string _workspaceFolder;
 48        private string _projectFolder;
 49        private string _pythonFolder;
 50        #endregion
 51
 52        #region DB paths
 53        private string _systemFolder;
 54        private string _userFolder;
 55        #endregion
 56
 057        public string EnvimetFolder { get; set; }
 58
 59        /// <summary>
 60        /// Workspace folder.
 61        /// </summary>
 62        public string WorkspaceFolder
 63        {
 064            get { return _workspaceFolder; }
 65            private set
 066            {
 067                string folder = GetFolder(value);
 068                _workspaceFolder = folder;
 069            }
 70        }
 71        /// <summary>
 72        /// Project folder.
 73        /// </summary>
 74        public string ProjectFolder
 75        {
 076            get { return _projectFolder; }
 77            private set
 078            {
 079                string folder = GetFolder(value);
 080                _projectFolder = value;
 081            }
 82        }
 83        /// <summary>
 84        /// Project name.
 85        /// </summary>
 086        public string ProjectName { get; private set; }
 87        /// <summary>
 88        /// Model name.
 89        /// </summary>
 090        public string ModelName { get; private set; }
 91        /// <summary>
 92        /// Model path.
 93        /// </summary>
 094        public string ModelPath { get; private set; }
 95        /// <summary>
 96        /// Project database location.
 97        /// </summary>
 098        public string ProjectDB { get; private set; }
 99        /// <summary>
 100        /// System database location.
 101        /// </summary>
 0102        public string SystemDB { get; private set; }
 103        /// <summary>
 104        /// User database location.
 105        /// </summary>
 0106        public string UserDB { get; private set; }
 107
 108        private string GetFolder(string folder)
 0109        {
 0110            if (!(Directory.Exists(folder)))
 0111            {
 0112                Directory.CreateDirectory(folder);
 0113            }
 0114            return folder;
 0115        }
 116        /// <summary>
 117        /// Create a workspace object.
 118        /// </summary>
 119        /// <param name="workspaceFolder">Workspace folder.</param>
 120        /// <param name="databaseSource">Database source.</param>
 121        /// <param name="envimetFolder">Envimet folder on machine.</param>
 122        /// <exception cref="ArgumentOutOfRangeException">Envimet not found.</exception>
 4123        public Workspace(string workspaceFolder,
 4124            DatabaseSource databaseSource,
 4125            string envimetFolder = null)
 4126        {
 4127            _systemFolder = GetEnvimetSystemFolder(SYSTEM_FOLDER, envimetFolder);
 4128            _userFolder = GetEnvimetSystemFolder(USER_FOLDER, envimetFolder);
 4129            _pythonFolder = GetEnvimetSystemFolder(PYTHON_FOLDER, envimetFolder);
 130
 4131            if (_systemFolder == null)
 4132                throw new ArgumentOutOfRangeException("Envimet not found! " +
 4133                    "Check on your machine or set EnvimetFolder.");
 134
 0135            WorkspaceFolder = workspaceFolder;
 0136            ProjectName = "Project";
 0137            ModelName = "Model";
 138
 0139            ProjectFolder = Path.Combine(WorkspaceFolder, ProjectName);
 0140            ModelPath = Path.Combine(ProjectFolder, ModelName + ".inx");
 141
 0142            string projectInfoxAbsPath = Path.Combine(ProjectFolder, PROJECT_INFOX);
 143
 0144            SetUserSettings();
 0145            SetProjectInfox(projectInfoxAbsPath, databaseSource);
 146
 0147            SetDatabase(databaseSource, _systemFolder, _userFolder);
 0148        }
 149        /// <summary>
 150        /// Create a workspace object.
 151        /// </summary>
 152        /// <param name="workspaceFolder">Workspace folder.</param>
 153        /// <param name="databaseSource">Database source.</param>
 154        /// <param name="projectName">Project name.</param>
 155        /// <param name="modelName">Model name.</param>
 156        /// <param name="envimetFolder">Envimet folder.</param>
 157        public Workspace(string workspaceFolder, DatabaseSource databaseSource,
 158            string projectName, string modelName, string envimetFolder)
 0159            : this(workspaceFolder, databaseSource, envimetFolder)
 0160        {
 0161            ProjectName = projectName;
 0162            ProjectFolder = Path.Combine(WorkspaceFolder, ProjectName);
 0163            ModelName = modelName;
 0164            ModelPath = Path.Combine(ProjectFolder, ModelName + ".inx");
 165
 0166            string projectInfoxAbsPath = Path.Combine(ProjectFolder, PROJECT_INFOX);
 167
 0168            SetUserSettings();
 0169            SetProjectInfox(projectInfoxAbsPath, databaseSource);
 170
 0171            SetDatabase(databaseSource, _systemFolder, _userFolder);
 0172        }
 173        /// <summary>
 174        /// String representation of the workspace object.
 175        /// </summary>
 176        /// <returns>String representation.</returns>
 177        public override string ToString()
 0178        {
 0179            return String.Format("Workspace::{0}::{1}",
 0180                ProjectName, ModelName);
 0181        }
 182
 183        private void SetDatabase(DatabaseSource databaseSource,
 184            string systemFolder, string userFolder)
 0185        {
 0186            SystemDB = Path.Combine(systemFolder, SYSTEM_DB);
 187
 0188            if (databaseSource == DatabaseSource.Project)
 0189            {
 0190                ProjectDB = Path.Combine(ProjectFolder, PROJECT_DB);
 0191                if (!File.Exists(ProjectDB))
 0192                    SetProjectDB();
 0193            }
 194            else
 0195            {
 0196                UserDB = Path.Combine(userFolder, USER_DB);
 0197            }
 0198        }
 199
 200        private string GetEnvimetSystemFolder(string folderName,
 201            string envimetFolder)
 12202        {
 12203            string root = System.IO.Path.GetPathRoot(Environment
 12204                .GetFolderPath(Environment.SpecialFolder.ApplicationData));
 12205            string directory = System.IO.Path.Combine(root,
 12206                DEFAULT_FOLDER + $"\\{folderName}\\");
 207
 12208            if (envimetFolder != null)
 0209                directory = System.IO.Path.Combine(envimetFolder, $"{folderName}\\");
 210
 12211            if (System.IO.Directory.Exists(directory))
 0212                return directory;
 213            else
 12214                return null;
 12215        }
 216
 217        private void SetUserSettings()
 0218        {
 0219            var folder = Environment.GetFolderPath(
 0220                Environment.SpecialFolder.ApplicationData);
 0221            var targetFolder = Path.Combine(folder, FOLDER_NAME);
 0222            var targetFile = Path.Combine(targetFolder, USER_SETTINGS);
 223
 0224            if (!Directory.Exists(targetFolder))
 0225                Directory.CreateDirectory(targetFolder);
 226
 0227            WriteUserSettings(targetFile);
 0228        }
 229
 230        private void WriteUserSettings(string userSettingsAbsPath)
 0231        {
 0232            var now = DateTime.Now;
 233
 0234            string[] userSettingsData = {
 0235                "<ENVI-MET_Datafile>",
 0236                "<Header>",
 0237                "<filetype>SETX ENVI-met User Settings</filetype>",
 0238                $"<version>{VERSION}</version>",
 0239                $"<revisiondate>{now.ToString("yyyy-MM-dd HH:mm: ss")}</revisiondate>",
 0240                "<remark></remark>",
 0241                "<checksum>0</checksum>",
 0242                "<encryptionlevel>0</encryptionlevel>",
 0243                "</Header>",
 0244                "<current_workspace>",
 0245                $"<absolute_path> {WorkspaceFolder} </absolute_path>",
 0246                $"<last_active> {ProjectName} </last_active>",
 0247                "</current_workspace>",
 0248                "<python_settings>",
 0249                $"<selectedPython> {_pythonFolder} </selectedPython>",
 0250                "</python_settings>",
 0251                "<userpathinfo>",
 0252                "<userpathmode> 1 </userpathmode>",
 0253                $"<userpathinfo> {_userFolder} </userpathinfo>",
 0254                "</userpathinfo>",
 0255                "</ENVI-MET_Datafile>"
 0256            };
 257
 0258            File.WriteAllLines(userSettingsAbsPath, userSettingsData);
 0259        }
 260
 261        private void SetProjectDB()
 0262        {
 0263            var now = DateTime.Now;
 264
 0265            string[] projectDBdata = {
 0266                "<ENVI-MET_Datafile>",
 0267                "<Header>",
 0268                "<filetype>DATA</filetype>",
 0269                "<version>1</version>",
 0270                String.Format("<revisiondate>{0}</revisiondate>",
 0271                now.ToString("yyyy-MM-dd HH:mm:ss")),
 0272                "<remark>Envi-Data</remark>",
 0273                "<checksum>0</checksum>",
 0274                "<encryptionlevel>1</encryptionlevel>",
 0275                "</Header>",
 0276                "</ENVI-MET_Datafile>"
 0277            };
 278
 0279            File.WriteAllLines(ProjectDB, projectDBdata);
 0280        }
 281
 282        private void SetProjectInfox(string projectInfoxAbsPath, DatabaseSource databaseSource)
 0283        {
 0284            var now = DateTime.Now;
 285
 0286            string[] projectInfoData = {
 0287                "<ENVI-MET_Datafile>",
 0288                "<Header>",
 0289                "<filetype>infoX ENVI-met Project Description File</filetype>",
 0290                $"<version>{VERSION}</version>",
 0291                $"<revisiondate>{now.ToString("yyyy-MM-dd HH:mm:ss")}</revisiondate>",
 0292                "<remark></remark>",
 0293                $"<checksum>{CHECK_SUM}</checksum>",
 0294                $"<encryptionlevel>{ENCRIPTION_LEVEL}</encryptionlevel>",
 0295                "</Header>",
 0296                "<project_description>",
 0297                $"<name> {ProjectName} </name>",
 0298                "<description>  </description>",
 0299                $"<useProjectDB> {(int) databaseSource} </useProjectDB>",
 0300                "</project_description>",
 0301                "</ENVI-MET_Datafile>"
 0302            };
 303
 0304            File.WriteAllLines(projectInfoxAbsPath, projectInfoData);
 0305        }
 306    }
 307}