public void RegisterFile([NotNull] string fileName, [NotNull] string description, bool displayResultFileEntry, ResultFileID rfid1,
                                 [NotNull] HouseholdKey householdKey, TargetDirectory targetDirectory, CalcOption enablingOption,
                                 [CanBeNull] string additionalFileIndex = null)
        {
            if (!HouseholdRegistry.IsHouseholdRegistered(householdKey))
            {
                throw new LPGException("Forgotten Household Key: " + householdKey);
            }

            if (fileName.Contains(".."))
            {
                throw new LPGException("Filename with two dots. This looks bad. Please fix: " + fileName);
            }
            if (fileName.Contains("  "))
            {
                throw new LPGException("Filename with two spaces. This looks bad. Please fix: " + fileName);
            }
            string          fullFileName = Path.Combine(GetFullPathForTargetdirectry(targetDirectory), fileName);
            FileInfo        fi           = new FileInfo(fullFileName);
            ResultFileEntry rfe          = new ResultFileEntry(description, fi, displayResultFileEntry, rfid1, householdKey.Key,
                                                               additionalFileIndex, enablingOption);

            _inputDataLogger.Save(rfe);
            ResultFileList.ResultFiles.Add(rfe.HashKey, rfe);
        }
        public void ReadExistingFilesFromSql()
        {
            SqlResultLoggingService srls        = new SqlResultLoggingService(_baseResultpath);
            HouseholdKeyLogger      hhKeyLogger = new HouseholdKeyLogger(srls);
            var hhkes = hhKeyLogger.Load();

            foreach (HouseholdKeyEntry entry in hhkes)
            {
                HouseholdRegistry.AddSavedEntry(entry);
            }

            var rfel = new ResultFileEntryLogger(srls);
            var rfes = rfel.Load();

            ResultFileList.AddExistingEntries(rfes);
            _inputDataLogger.AddSaver(rfel);
        }
 public void RegisterGeneralHouse()
 {
     HouseholdRegistry.RegisterHousehold(Constants.GeneralHouseholdKey, "General Information",
                                         HouseholdKeyType.General, _inputDataLogger, "General", null, null);
 }
 public void RegisterHousehold([NotNull] HouseholdKey householdKey, [NotNull] string name, HouseholdKeyType type,
                               [NotNull] string description, [CanBeNull] string houseName, [CanBeNull] string houseDescription)
 {
     HouseholdRegistry.RegisterHousehold(householdKey, name, type, _inputDataLogger, description, houseName, houseDescription);
 }
        public T MakeFile <T>([NotNull] string fileName, [NotNull] string description,
                              bool displayResultFileEntry, ResultFileID rfid1,
                              [NotNull] HouseholdKey householdKey,
                              TargetDirectory targetDirectory, TimeSpan timeResolution,
                              CalcOption enablingOption,
                              [CanBeNull] LoadTypeInformation lti    = null,
                              [CanBeNull] PersonInformation pi       = null,
                              [CanBeNull] string additionalFileIndex = null)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new LPGException("Filename was empty");
            }
            if (!HouseholdRegistry.IsHouseholdRegistered(householdKey))
            {
                throw new LPGException("Forgotten Household Key: " + householdKey);
            }
            if (fileName.Contains(".."))
            {
                throw new LPGException("Filename with two dots. This looks bad. Please fix: " + fileName);
            }
            if (fileName.Contains("  "))
            {
                throw new LPGException("Filename with two spaces. This looks bad. Please fix: " + fileName);
            }
            var    rfid          = rfid1;
            var    cleanFileName = AutomationUtili.CleanFileName(fileName);
            string targetDir     = GetFullPathForTargetdirectry(targetDirectory);

            if (!Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
            }
            var fileInfo = new FileInfo(Path.Combine(targetDir, cleanFileName));

            if (fileInfo.FullName.Length > 250)
            {
                throw new LPGException("The result filename " + fileInfo.FullName + " was longer than 250 characters which is a problem for Windows. Maybe chose a shorter directory name or nest less deep and try again?");
            }
            if (fileInfo.Exists)
            {
                throw new LPGException("The file " + fileInfo.Name + " already exists. Can't create it again!");
            }
            bool tryagain;

            if (!_allValidIds.Contains((int)rfid))
            {
                throw new LPGException("Invalid result file ID");
            }
            do
            {
                try {
                    if (typeof(T) == typeof(StreamWriter))
                    {
                        var stream = _getStream.GetWriterStream(fileInfo.FullName);
#pragma warning disable S2930 // "IDisposables" should be disposed
                        var sw = new StreamWriter(stream);
#pragma warning restore S2930 // "IDisposables" should be disposed
                        var ret = (T)(object)sw;
                        var rfe = new ResultFileEntry(description, fileInfo, displayResultFileEntry, sw,
                                                      null, stream, rfid, _calcObjectName, householdKey.Key,
                                                      lti, pi, additionalFileIndex, timeResolution, enablingOption);
                        ResultFileList.ResultFiles.Add(rfe.HashKey, rfe);
                        _inputDataLogger.Save(rfe);
                        return(ret);
                    }
                    if (typeof(T) == typeof(BinaryWriter))
                    {
                        var stream = _getStream.GetWriterStream(fileInfo.FullName);
#pragma warning disable S2930 // "IDisposables" should be disposed
                        var bw = new BinaryWriter(stream);
#pragma warning restore S2930 // "IDisposables" should be disposed
                        var ret = (T)(object)bw;
                        var rfe = new ResultFileEntry(description, fileInfo, displayResultFileEntry, null, bw,
                                                      stream, rfid, _calcObjectName, householdKey.Key, lti, pi,
                                                      additionalFileIndex, timeResolution, enablingOption);
                        ResultFileList.ResultFiles.Add(rfe.HashKey, rfe);
                        _inputDataLogger.Save(rfe);
                        return(ret);
                    }
                    if (typeof(T) == typeof(Stream))
                    {
                        var stream = _getStream.GetWriterStream(fileInfo.FullName);
                        var ret    = (T)(object)stream;
                        var rfe    = new ResultFileEntry(description, fileInfo, displayResultFileEntry, null, null,
                                                         stream, rfid, _calcObjectName, householdKey.Key, lti, pi,
                                                         additionalFileIndex, timeResolution, enablingOption);
                        ResultFileList.ResultFiles.Add(rfe.HashKey, rfe);
                        _inputDataLogger.Save(rfe);
                        return(ret);
                    }
                    if (typeof(T) == typeof(FileStream))
                    {
                        var stream = _getStream.GetWriterStream(fileInfo.FullName);
                        var ret    = (T)(object)stream;
                        var rfe    = new ResultFileEntry(description, fileInfo, displayResultFileEntry, null, null,
                                                         stream, rfid, _calcObjectName, householdKey.Key, lti, pi,
                                                         additionalFileIndex, timeResolution, enablingOption);
                        ResultFileList.ResultFiles.Add(rfe.HashKey, rfe);
                        _inputDataLogger.Save(rfe);
                        return(ret);
                    }

                    throw new LPGException("Unknown stream type in Makefile<T>");
                }
                catch (IOException ioe) {
                    if (!Config.IsInUnitTesting)
                    {
                        var errormessage = "The file " + fileInfo.FullName +
                                           " could not be opened. The exact error message was:" + Environment.NewLine + ioe.Message +
                                           Environment.NewLine + Environment.NewLine;
                        errormessage += " Maybe you forgot to close Excel? Press YES to try again!";
                        var dr = MessageWindowHandler.Mw.ShowYesNoMessage(errormessage, "Error opening file!");
                        if (dr == LPGMsgBoxResult.Yes)
                        {
                            tryagain = true;
                        }
                        else
                        {
                            tryagain = false;
                        }
                    }
                    else
                    {
                        tryagain = false;
                    }
                }
            } while (tryagain);
            throw new DataIntegrityException("Couldn't open file \"" + fileInfo.FullName + "\". Aborting.");
        }