private PartialFileRecord WriteSingleFile(string _filename_wo_ext, int _nr_locks, ComponentManagerType _user, StringBuilder _sb)
        {
            string content       = _sb.ToString();
            string filename_part = _filename_wo_ext + "_" +
                                   ComponentUtils.ComponentManagerTypeToAbbrevEN(_user) + "." +
                                   ParamStructFileExtensions.FILE_EXT_COMPONENTS;

            try
            {
                using (FileStream fs = File.Create(filename_part))
                {
                    byte[] content_B = System.Text.Encoding.UTF8.GetBytes(content);
                    fs.Write(content_B, 0, content_B.Length);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error saving partial file: " + filename_part,
                                MessageBoxButton.OK, MessageBoxImage.Error);
            }

            PartialFileRecord record = new PartialFileRecord()
            {
                FileName     = filename_part,
                Manager      = _user,
                LastModified = DateTime.Now,
                NrLocks      = _nr_locks
            };

            return(record);
        }
        // any role can do this, i.e. save the project under a new name
        public void SaveFilesAs(string _filename_w_ext)
        {
            if (string.IsNullOrEmpty(_filename_w_ext) || this.COMP_Factory == null)
            {
                return;
            }
            if (_filename_w_ext.Length < 5)
            {
                return;
            }

            StringBuilder sb = new StringBuilder();
            string        filename_wo_ext = _filename_w_ext.Substring(0, _filename_w_ext.Length - ParamStructFileExtensions.FILE_EXT_PROJECT.Length - 1);

            // create the export stringS
            Dictionary <ComponentManagerType, StringBuilder> exports = this.COMP_Factory.ExportRecordDistributed();

            this.SingleFileNames = new List <string>();
            foreach (var entry in exports)
            {
                // export to a separate file
                int nr_locks             = 0;
                PartialFileRecord record = this.WriteSingleFile(filename_wo_ext, nr_locks, entry.Key, entry.Value);
                this.SingleFileNames.Add(record.FileName);
                record.AddToExport(ref sb);
            }

            // write the summary file
            this.WriteSummaryFile(_filename_w_ext, ref sb);
        }
 private void SetFileManagementContent(string _file_name, ComponentManagerType _user)
 {
     this.current_file_record = null;
     this.loaded_file_records = new List <PartialFileRecord>();
     this.SummaryFileName     = _file_name;
     this.SingleFileNames     = new List <string>();
     this.manager             = _user;
     this.FileRecordsOpen     = false;
     this.write_in_progress   = false;
 }
        private void ParseSummaryFile()
        {
            switch (this.FCode)
            {
            case (int)ParamStructCommonSaveCode.ENTITY_START:
                if (this.current_file_record != null)
                {
                    this.loaded_file_records.Add(this.current_file_record);
                }
                this.current_file_record          = new PartialFileRecord();
                this.current_file_record.FileName = DXFDistributedDecoder.ReconstructAbsolutePathFromRelative(this.SummaryFileName, this.FValue);
                break;

            case (int)ParamStructCommonSaveCode.ENTITY_KEY:
                if (this.current_file_record != null)
                {
                    this.current_file_record.Manager = ComponentUtils.StringToComponentManagerType(this.FValue);
                }
                break;

            case (int)ParamStructCommonSaveCode.TIME_STAMP:
                if (this.current_file_record != null)
                {
                    DateTime dt_tmp;
                    bool     success = DateTime.TryParse(this.FValue, ParamStructTypes.DT_FORMATTER, System.Globalization.DateTimeStyles.None, out dt_tmp);
                    if (success)
                    {
                        this.current_file_record.LastModified = dt_tmp;
                    }
                }
                break;

            case (int)ParamStructCommonSaveCode.X_VALUE:
                if (this.current_file_record != null)
                {
                    int  nr_locks;
                    bool success = Int32.TryParse(this.FValue, out nr_locks);
                    this.current_file_record.NrLocks = nr_locks;
                }
                break;
            }
        }
        // writing the summary file (smn: SIMULTAN)
        // ----------------------------------------------------------------------
        // code 0  : ENTITY_START
        // value   : file name (e.g. architecture: 'ComponentRecord_01ARC.dxf')
        // code 904: ENTITY_KEY
        // value   : role that can open the file with writing access (e.g. administrator: '@')
        // code 902: TIME_STAMP
        // value   : last modified
        // code 910: X_VALUE
        // value   : 1 if locked, 0 if unlocked (this is set during file open, according to role)

        // only the files corsponding to the manager role can be replaced
        // NO !!! (added 01.09.2016)
        // all files can be replaced: user w/o writing access can still have supervizing or release access
        public void SaveFiles(bool _unlock_the_summary_file = false)
        {
            if (this.loaded_file_records.Count == 0)
            {
                return;
            }
            if (string.IsNullOrEmpty(this.SummaryFileName))
            {
                return;
            }
            if (this.SummaryFileName.Length < 5)
            {
                return;
            }

            StringBuilder sb = new StringBuilder();
            string        filename_wo_ext = this.SummaryFileName.Substring(0, this.SummaryFileName.Length - ParamStructFileExtensions.FILE_EXT_PROJECT.Length - 1);

            // create the export stringS
            Dictionary <ComponentManagerType, StringBuilder> exports = this.COMP_Factory.ExportRecordDistributed();
            // prepare for checking the state of the single files (init w ZERO)
            List <int> single_file_state = new List <int>(ComponentUtils.MANAGER_TYPE_OPENING_SIGNATURE_NONE);

            // save the file records
            foreach (var entry in exports)
            {
                // changed 01.09.2016 to allow for release and supervize actions to be recorded
                //if (DXFDistributedDecoder.HasEditingRights(this.manager, entry.Key))
                //{
                // check if the locks are set correctly, if a new single file is to be created (added 07.02.2017)
                if (DXFDistributedDecoder.HasEditingRights(this.manager, entry.Key) && this.nr_locks_record[(int)entry.Key] == 0)
                {
                    this.nr_locks_record[(int)entry.Key] = 1;
                }
                // save file
                PartialFileRecord record = this.WriteSingleFile(filename_wo_ext, this.nr_locks_record[(int)entry.Key], entry.Key, entry.Value);
                single_file_state[(int)entry.Key] = 1;
                // replace record, if it exists; otherwise just add
                PartialFileRecord pfr = this.loaded_file_records.Find(x => x.Manager == entry.Key);
                if (pfr != null)
                {
                    this.loaded_file_records.Remove(pfr);
                }
                this.loaded_file_records.Add(record);
                //}
            }

            // clean up old file records (e.g. when the writing rights of components change a record may become obsolete)
            for (int c = 0; c < single_file_state.Count; c++)
            {
                if (single_file_state[c] > 0)
                {
                    continue;
                }

                // get rid of the record and of the file
                PartialFileRecord pfr = this.loaded_file_records.Find(x => x.Manager == (ComponentManagerType)c);
                if (pfr != null)
                {
                    this.loaded_file_records.Remove(pfr);
                    this.DeleteSingleFile(filename_wo_ext, (ComponentManagerType)c);
                }
            }

            // ... modify the summary file accordingly
            this.SingleFileNames = new List <string>();
            foreach (PartialFileRecord fr in this.loaded_file_records)
            {
                if (_unlock_the_summary_file)
                {
                    fr.NrLocks = 0;
                }
                fr.AddToExport(ref sb);
                this.SingleFileNames.Add(fr.FileName);
            }
            this.WriteSummaryFile(this.SummaryFileName, ref sb);
        }
        private void OnSummaryFileChanged(object sender, FileSystemEventArgs e)
        {
            // reload the summary file to update the number of locks
            if (this.SummaryFileName == null)
            {
                return;
            }
            if (!File.Exists(this.SummaryFileName))
            {
                return;
            }
            string uqfn = DXFDistributedDecoder.GetUnQualifiedFileName(this.SummaryFileName);

            // prepare to calculate the difference btw the previous and current state of the summary file
            List <PartialFileRecord> prev_loaded_file_records = new List <PartialFileRecord>(this.loaded_file_records);

            // reset
            this.current_file_record = null;
            this.loaded_file_records = new List <PartialFileRecord>();

            // debug
            string prefix = "[*]";

#if DEBUG
            prefix = "[D]";
#else
            prefix = "[R]";
#endif

            // re-load
            try
            {
                // read and parse file
                using (FileStream fs = new FileStream(this.SummaryFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    this.FStream = new StreamReader(fs);
                    bool reached_eof = false;
                    while (this.HasNext())
                    {
                        this.Next();
                        if (this.FValue == ParamStructTypes.EOF)
                        {
                            reached_eof = true;
                            if (this.current_file_record != null)
                            {
                                this.loaded_file_records.Add(this.current_file_record);
                            }
                            this.ReleaseRessources();
                            break;
                        }
                        this.ParseSummaryFile();
                    }
                    if (!reached_eof)
                    {
                        this.ReleaseRessources();
                    }
                }

                foreach (PartialFileRecord fr in this.loaded_file_records)
                {
                    if (File.Exists(fr.FileName))
                    {
                        this.nr_locks_record[(int)fr.Manager] = fr.NrLocks;
                    }
                }

                // calculate difference
                if (this.write_in_progress)
                {
                    this.write_in_progress = false;
                }
                else
                {
                    PartialRecordState diff = PartialFileRecord.CompareRecordStates(prev_loaded_file_records, this.loaded_file_records);
                    if (diff == PartialRecordState.UPDATED)
                    {
                        MessageBox.Show("Another user updated the project!", prefix + "Project File State: " + uqfn,
                                        MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                    else if (diff == PartialRecordState.STRUCTURE_CHANGED)
                    {
                        MessageBox.Show("Another user changed the structure of the project!", prefix + "Project File State: " + uqfn,
                                        MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                }
            }
            catch (System.IO.IOException ioex)
            {
                // happens when another process is using the file (e.g. GIT repo copying process)
                // do not user a watcher when saving with GIT - not necessary
                this.ReleaseWatcher();
                string io_tmp = ioex.Message;
            }
            catch (Exception ex)
            {
                this.ReleaseRessources();
                string tmp = ex.Message;
                //MessageBox.Show(ex.Message, prefix + "Error re-reading simultan file: " + uqfn,
                //                    MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }