Beispiel #1
0
        /// <summary>
        /// The serialize with hash.
        /// </summary>
        /// <param name="person">
        /// The person.
        /// </param>
        /// <param name="messages">
        /// The messages.
        /// </param>
        /// <param name="filePath">
        /// The file path.
        /// </param>
        /// <param name="xmlNamespaceResolver">
        /// The xml namespace resolver.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public static bool SerializeWithHash(
            BasePersonTemplate person,
            IEnumerable messages,
            string filePath,
            IXmlNamespaceResolver xmlNamespaceResolver)
        {
            try
            {
                var fomsLogPrefix = Hl7Helper.FomsLogPrefix;
                FomsLogger.WriteLog(
                    LogType.Local,
                    "Сериализация пакета Hl7. " + person.BeginPacket.RetrieveShortInfo(),
                    fomsLogPrefix,
                    LogErrorType.None);
                FileSystemPhysical.RemoveFile(filePath);
                using (var writer = new Hl7Writer(filePath, person, xmlNamespaceResolver))
                {
                    writer.WriteMessages(messages);
                    writer.FinalizeWrite();
                }

                return(true);
            }
            catch (Exception exception)
            {
                FomsLogger.WriteError(exception, null);
                return(false);
            }
        }
Beispiel #2
0
        /// <summary>
        ///   The deserialize.
        /// </summary>
        /// <returns>
        ///   The <see cref="BaseAnswerMessageTemplate" />.
        /// </returns>
        internal BaseAnswerMessageTemplate Deserialize()
        {
            try
            {
                if (FileSystemPhysical.FileExists(filePath))
                {
                    switch (Path.GetExtension(filePath).ToLower())
                    {
                    case ".rsp_zk1":
                        return(XmlStreamer.Deserialize <RSP_ZK1>(filePath));

                    case ".rsp_zk2":
                        return(XmlStreamer.Deserialize <RSP_ZK2>(filePath));

                    case ".rsp_zk4":
                        return(XmlStreamer.Deserialize <RSP_ZK4>(filePath));

                    case ".rsp_zk5":
                        return(XmlStreamer.Deserialize <RSP_ZK5>(filePath));
                    }

                    return(XmlStreamer.Deserialize <Ack>(filePath));
                }
            }
            catch (Exception exception)
            {
                FomsLogger.WriteError(exception, null);
            }

            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// The do calculate and save hash.
        /// </summary>
        /// <param name="stream">
        /// The stream.
        /// </param>
        /// <param name="hash">
        /// The hash.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        internal static bool DoCalculateAndSaveHash(Stream stream, out string hash)
        {
            hash = DoScanHash(stream);
            if (hash == null)
            {
                return(false);
            }

            var bytes = Encoding.Default.GetBytes(hash);

            if (stream.CanSeek)
            {
                var position = stream.Position;
                if (!DoLocateSequence(stream, Hl7Helper.NodeTag1251_BatchChecksum_end, null, null))
                {
                    return(false);
                }

                var num2 = (stream.Position - position) - Hl7Helper.NodeTag1251_BatchChecksum_end.LongLength;
                if (num2 != bytes.LongLength)
                {
                    var fomsLogPrefix = Hl7Helper.FomsLogPrefix;
                    FomsLogger.WriteError(
                        LogType.Local,
                        string.Format("Под контрольную сумму выделено {0} байт; ожидается {1} байт.", num2, bytes.LongLength),
                        fomsLogPrefix);
                    return(false);
                }

                stream.Position = position;
            }

            stream.Write(bytes, 0, bytes.Length);
            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// The serialize with hash.
        /// </summary>
        /// <param name="person">
        /// The person.
        /// </param>
        /// <param name="filePath">
        /// The file path.
        /// </param>
        /// <param name="xmlNamespaceResolver">
        /// The xml namespace resolver.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public static bool SerializeWithHash(object person, string filePath, IXmlNamespaceResolver xmlNamespaceResolver)
        {
            try
            {
                FileSystemPhysical.RemoveFile(filePath);
                using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                {
                    using (var writer = new XmlTextWriter(stream, Encoding.Default))
                    {
                        // XmlHelper.SerializeObject(writer, person, xmlNamespaceResolver);
                        if (!CalculateAndSaveHash(stream, true))
                        {
                            throw new InvalidOperationException(string.Format("Не удалось прописать контрольную сумму: ", filePath));
                        }
                    }
                }

                return(true);
            }
            catch (Exception exception)
            {
                FomsLogger.WriteError(exception, null);
                return(false);
            }
        }
Beispiel #5
0
 /// <summary>
 /// The do write message.
 /// </summary>
 /// <param name="message">
 /// The message.
 /// </param>
 private void DoWriteMessage(object message)
 {
     try
     {
         if (WriteHl7(writer, message))
         {
             messagesWritten += 1L;
         }
     }
     catch (Exception exception)
     {
         var fomsLogPrefix = Hl7Helper.FomsLogPrefix;
         FomsLogger.WriteError(LogType.Local, exception, fomsLogPrefix);
         throw;
     }
 }
Beispiel #6
0
        /// <summary>
        /// The serialize.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        internal bool Serialize(object message)
        {
            try
            {
                if (message != null)
                {
                    XmlStreamer.Serialize(message, filePath, null);
                    return(true);
                }
            }
            catch (Exception exception)
            {
                FomsLogger.WriteError(exception, null);
            }

            return(false);
        }
Beispiel #7
0
        /// <summary>
        /// The deserialize.
        /// </summary>
        /// <param name="filePath">
        /// The file path.
        /// </param>
        /// <typeparam name="T">
        /// Тип пакета
        /// </typeparam>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        public static T Deserialize <T>(string filePath) where T : class
        {
            T local;

            try
            {
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    var serializer = new XmlSerializer(typeof(T));
                    local = (T)serializer.Deserialize(stream);
                }
            }
            catch (Exception exception)
            {
                FomsLogger.WriteError(exception, null);
                local = default(T);
            }

            return(local);
        }
Beispiel #8
0
 /// <summary>
 /// The serialize.
 /// </summary>
 /// <param name="obj">
 /// The obj.
 /// </param>
 /// <param name="filePath">
 /// The file path.
 /// </param>
 /// <param name="xmlNamespaceResolver">
 /// The xml namespace resolver.
 /// </param>
 public static void Serialize(object obj, string filePath, IXmlNamespaceResolver xmlNamespaceResolver)
 {
     try
     {
         if ((obj != null) && !string.IsNullOrEmpty(filePath))
         {
             FileSystemPhysical.RemoveFile(filePath);
             using (var stream = new FileStream(filePath, FileMode.Create))
             {
                 using (var writer = new XmlTextWriter(stream, Encoding.Default))
                 {
                     //// XmlHelper.SerializeObject(writer, obj, xmlNamespaceResolver);
                 }
             }
         }
     }
     catch (Exception exception)
     {
         FomsLogger.WriteError(exception, null);
     }
 }
Beispiel #9
0
        /// <summary>
        /// The deserialize with hash.
        /// </summary>
        /// <param name="type">
        /// The type.
        /// </param>
        /// <param name="filePath">
        /// The file path.
        /// </param>
        /// <returns>
        /// The <see cref="BasePersonTemplate"/>.
        /// </returns>
        public static BasePersonTemplate DeserializeWithHash(Type type, string filePath)
        {
            try
            {
                using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    if (CalculateAndCheckHash(stream))
                    {
                        stream.Position = 0L;
                        var serializer = new XmlSerializer(type);
                        return((BasePersonTemplate)serializer.Deserialize(stream));
                    }
                }
            }
            catch (Exception exception)
            {
                FomsLogger.WriteError(exception, null);
            }

            return(null);
        }
Beispiel #10
0
        /// <summary>
        /// The validate.
        /// </summary>
        /// <param name="schemaLocationAttribute">
        /// The schema location attribute.
        /// </param>
        /// <returns>
        /// The <see cref="FileXmlStatus"/>.
        /// </returns>
        public FileXmlStatus Validate(out string schemaLocationAttribute)
        {
            schemaLocationAttribute = null;
            validationStatus        = FileXmlStatus.Unknown;
            try
            {
                var set = new XmlSchemaSet();
                set.Add(null, XsdFilePath);
                var settings2 = new XmlReaderSettings
                {
                    ValidationType = ValidationType.Schema,
                    Schemas        = set,
                    IgnoreComments = true,
                    IgnoreProcessingInstructions = true,
                    IgnoreWhitespace             = true
                };
                var settings = settings2;
                settings.ValidationEventHandler += ValidationHandler;
                settings.ValidationFlags         = XmlSchemaValidationFlags.ProcessIdentityConstraints
                                                   | XmlSchemaValidationFlags.ReportValidationWarnings
                                                   | XmlSchemaValidationFlags.ProcessInlineSchema;
                if (!IgnoreSchemaLocationAttribute)
                {
                    settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
                }

                using (var reader = XmlReader.Create(XmlFilePath, settings))
                {
                    if ((reader.MoveToContent() == XmlNodeType.Element) && reader.MoveToFirstAttribute())
                    {
                        do
                        {
                            if ((string.Compare(
                                     reader.NamespaceURI,
                                     "http://www.w3.org/2001/XMLSchema-instance",
                                     StringComparison.Ordinal) == 0) &&
                                (string.Compare(reader.LocalName, "schemaLocation", StringComparison.Ordinal) == 0))
                            {
                                schemaLocationAttribute = reader.Value;
                                break;
                            }
                        }while (reader.MoveToNextAttribute());
                    }

                    while (reader.Read())
                    {
                    }
                }

                validationStatus = string.IsNullOrEmpty(errorMessage)
                             ? FileXmlStatus.ValidatedSuccessfully
                             : FileXmlStatus.XsdValidationFailed;
            }
            catch (Exception exception)
            {
                var fomsLogPrefix = HL7Helper.FomsLogPrefix;
                FomsLogger.WriteError(
                    LogType.Local,
                    string.Format(
                        "Ошибка во время проверки XSD-схемой. File name: {1}{0}{2}",
                        Environment.NewLine,
                        XmlFilePath,
                        exception),
                    fomsLogPrefix);
                errorMessage     = exception.Message;
                validationStatus = FileXmlStatus.XmlValidationFailed;
            }

            return(validationStatus);
        }
Beispiel #11
0
        public static void PreCacheSerializersStart(ProcessingTarget targets)
        {
            ThreadStart start = null;

            if (targets != ProcessingTarget.NoTargets)
            {
                if (start == null)
                {
                    start = delegate
                    {
                        var fomsLogPrefix = FomsLogPrefix;
                        FomsLogger.WriteLog(
                            LogType.Local,
                            string.Format("Предварительное кеширование классов сериализации: {0}", targets),
                            fomsLogPrefix,
                            LogErrorType.None);
                        try
                        {
                            if ((targets & ProcessingTarget.XElement) == ProcessingTarget.XElement)
                            {
                                new XmlSerializer(typeof(XElement));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.Ack))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.Ack))
                            {
                                new XmlSerializer(typeof(Ack));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.RSP_ZK1))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.RSP_ZK1))
                            {
                                new XmlSerializer(typeof(RSP_ZK1));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.RSP_ZK2))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.RSP_ZK2))
                            {
                                new XmlSerializer(typeof(RSP_ZK2));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.RSP_ZK4))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.RSP_ZK4))
                            {
                                new XmlSerializer(typeof(RSP_ZK4));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.RSP_ZK5))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.RSP_ZK5))
                            {
                                new XmlSerializer(typeof(RSP_ZK5));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.ZPI_ZA1))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.ZPI_ZA1))
                            {
                                new XmlSerializer(typeof(ZPI_ZA1));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.ZPI_ZA7))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.ZPI_ZA7))
                            {
                                new XmlSerializer(typeof(ZPI_ZA7));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.ADT_A01))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.ADT_A01))
                            {
                                new XmlSerializer(typeof(ADT_A01));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.ADT_A03))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.ADT_A03))
                            {
                                new XmlSerializer(typeof(ADT_A03));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.ADT_A24))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.ADT_A24))
                            {
                                new XmlSerializer(typeof(ADT_A24));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.ADT_A37))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.ADT_A37))
                            {
                                new XmlSerializer(typeof(ADT_A37));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.QBP_ZP1))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.QBP_ZP1))
                            {
                                new XmlSerializer(typeof(QBP_ZP1));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.QBP_ZP2))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.QBP_ZP2))
                            {
                                new XmlSerializer(typeof(QBP_ZP2));
                            }

                            if ((targets & (ProcessingTarget.NoTargets | ProcessingTarget.QBP_ZP4))
                                == (ProcessingTarget.NoTargets | ProcessingTarget.QBP_ZP4))
                            {
                                new XmlSerializer(typeof(QBP_ZP4));
                            }

                            if ((targets & ProcessingTarget.BHS) == ProcessingTarget.BHS)
                            {
                                new XmlSerializer(typeof(BHS));
                            }

                            if ((targets & ProcessingTarget.BTS) == ProcessingTarget.BTS)
                            {
                                new XmlSerializer(typeof(BTS));
                            }

                            if ((targets & ProcessingTarget.PersonErp) == ProcessingTarget.PersonErp)
                            {
                                new XmlSerializer(typeof(PersonErp));
                            }

                            if ((targets & ProcessingTarget.PersonCard) == ProcessingTarget.PersonCard)
                            {
                                new XmlSerializer(typeof(PersonCard));
                            }

                            var prefix = FomsLogPrefix;
                            FomsLogger.WriteLog(
                                LogType.Local,
                                string.Format("Завершено кеширование классов сериализации: {0}", targets),
                                prefix,
                                LogErrorType.None);
                        }
                        catch (Exception exception)
                        {
                            var str3 = FomsLogPrefix;
                            FomsLogger.WriteError(
                                LogType.Local,
                                string.Format(
                                    "Исключение при кешировании классов сериализации: {0}",
                                    exception.Message),
                                str3);
                            throw;
                        }
                        finally
                        {
                            lock (preCacheThreads)
                            {
                                preCacheThreads.Remove(Thread.CurrentThread);
                            }
                        }
                    };
                }

                var item = new Thread(start);
                lock (preCacheThreads)
                {
                    preCacheThreads.Add(item);
                }

                item.Start();
            }
        }