Esempio n. 1
0
        /// <summary>
        /// Encode du type de données.
        /// </summary>
        /// <param name="type">Type de données à encoder.</param>
        /// <param name="encChars">Caractères d'encodage utilisés.</param>
        /// <param name="subComponent">Indique si l'on encode un sous-composant.</param>
        /// <returns></returns>
        public static string Encode(IType type, EncodingCharacters encChars, bool subComponent = false)
        {
            StringBuilder retType = new StringBuilder();

            if (type is ITypePrimitive)
            {
                ITypePrimitive primitive = type as ITypePrimitive;
                if (primitive == null)
                {
                    throw new EncodingException("Une erreur s'est produite à la conversion de 'IType' vers 'ITypePrimitive'.");
                }
                else
                {
                    retType.Append(PipeParser.Encode(primitive, encChars));
                }
            }
            else
            {
                ITypeComposite composite = type as ITypeComposite;
                if (composite == null)
                {
                    throw new EncodingException("Une erreur s'est produite à la conversion de 'IType' vers 'ITypeComposite'.");
                }
                else
                {
                    StringBuilder retComp       = new StringBuilder();
                    char          compDelimiter = subComponent ? encChars.SubComponentSeparator : encChars.ComponentSeparator;

                    for (int i = 0; i < composite.Components.Length; i++)
                    {
                        retComp.Append(PipeParser.Encode(composite.Components[i], encChars, true));
                        if (i < composite.Components.Length - 1)
                        {
                            retComp.Append(compDelimiter);
                        }
                    }

                    retType.Append(InteropUtil.RemoveExtraDelimiters(retComp.ToString(), compDelimiter));
                }
            }

            return(retType.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Encodage d'un message.
        /// </summary>
        /// <param name="message">Message à encoder.</param>
        /// <param name="createFiles">Indique si les fichiers doivent être créés.</param>
        /// <returns></returns>
        public string Encode(IMessage message, bool createFiles = true)
        {
            if (createFiles && (this._options.ExportPaths == null || !this._options.ExportPaths.Any()))
            {
                throw new EncodingException("Aucun chemin d'export n'a été renseigné.");
            }

            string dtEvent = DateTimeUtil.DateSecondeToString();

            #region Champs variables - MSH

            // MSH-1
            message.MSH.FieldSeparator.Value = this._options.EncodingChars.FieldSeparator.ToString();

            // MSH-2
            message.MSH.EncodingCharacters.Value = this._options.EncodingChars.ToString();

            // MSH-7
            message.MSH.DateTimeOfMessage.Time.Value = dtEvent;

            // MSH-10
            if (this._options.MsgControlIdInMilliseconds)
            {
                message.MSH.MessageControlId.Value = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
            }
            else
            {
                message.MSH.MessageControlId.Value = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            }

            // MSH-11
#if DEBUG
            message.MSH.ProcessingId.ProcessingId.Value = ProcessingID.DEBUGGING;
#else
            message.MSH.ProcessingId.ProcessingId.Value = ProcessingID.PRODUCTION;
#endif

            // MSH-12
            message.MSH.VersionId.VersionId.Value = this._options.Version;
            message.MSH.VersionId.InternationalizationCode.Identifier.Value = this._options.CountryCode;
            message.MSH.VersionId.InternationalizationCode.Text.Value       = CountryCode.Description[this._options.CountryCode];
            message.MSH.VersionId.InternationalVersionId.Identifier.Value   = this._options.Version;

            // MSH-17
            message.MSH.CountryCode.Value = this._options.CountryCode;

            #endregion

            #region Champs variables - EVN

            // EVN-2
            message.EVN.RecordedDateTime.Time.Value = dtEvent;

            // TODO: initialize EVN-5
            if (message.EventOperator != null)
            {
            }

            #endregion

            #region Encodage, création des fichiers et retour

            string encodingText = PipeParser.Encode(message);

            if (createFiles)
            {
                foreach (string path in this._options.ExportPaths)
                {
                    // TODO: pouvoir choisir de préfixer/suffixer avec le type d'événement (A28, A01, ...)
                    string fileName      = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
                    bool   errCreateFile = false;

                    try
                    {
                        FileUtil.CreateFile(fileName, this._options.FileExtension, encodingText, path);
                    }
                    catch
                    {
                        errCreateFile = true;
                        throw;
                    }
                    finally
                    {
                        if (!errCreateFile && this._options.CreateValidationFile)
                        {
                            FileUtil.CreateFile(fileName, this._options.ValidationFileExtension, encodingText, path);
                        }
                    }
                }
            }

            return(encodingText);

            #endregion
        }