public ValidationResult Validate(MetadataEntry metadataEntry, MD_Metadata_Type metadata, string rawXmlProcessed)
        {
            ValidationResult validationResult = new ValidationResult();

            if (metadataEntry.HasResourceType("software"))
            {
                validationResult = HasDistributionUrl(metadata, validationResult);
            }
            else
            {
                bool allowSpatialDataThemeError = true;
                bool allowConformityError = true;
                validationResult = new InspireValidator().Validate(rawXmlProcessed, allowSpatialDataThemeError, allowConformityError);
            }

            return validationResult;
        }
예제 #2
0
        public MetadataEntry RetrieveAndValidate(string uuid)
        {
            MetadataEntry metadataEntry = null;

            try
            {
                var getCswRecordRequest = CreateGetCswRecordRequest(uuid);
                Log.Info("Henter metadata for uuid=" + uuid + " fra GeoNorge.");
                string cswRecordResponse = _httpRequestExecutor.PostRequest(EndpointUrlGeoNorgeCsw,
                                                                            ContentTypeXml, ContentTypeXml,
                                                                            getCswRecordRequest);

                /* Quick and dirty hacks to fix exceptions in serialization due to invalid xml */

                Regex fixWrongDecimalInRealElements = new Regex("<gco:Real>([0-9]+),([0-9]+)</gco:Real>");

                var fixedResponse = cswRecordResponse.Replace("<gco:Boolean />", "<gco:Boolean>false</gco:Boolean>")
                                    .Replace("<gco:Real />", "<gco:Real>0.0</gco:Real>")
                                    .Replace("<gco:DateTime />", "")
                                    .Replace("<gmd:MD_TopicCategoryCode />", "");

                var rawXmlProcessed = fixWrongDecimalInRealElements.Replace(fixedResponse, "<gco:Real>$1.$2</gco:Real>");

                GetRecordByIdResponseType getRecordResponse = SerializeUtil.DeserializeFromString <GetRecordByIdResponseType>(rawXmlProcessed);
                MD_Metadata_Type          metadata          = getRecordResponse.Items[0] as MD_Metadata_Type;

                metadataEntry = ParseCswRecordResponse(uuid, metadata);
                ValidationResult validationResult;
                if (metadataEntry.ResourceType == "unknown")
                {
                    validationResult = new ValidationResult
                    {
                        Messages  = "Unknown resource type, please check value of hierarchyLevel element.",
                        Status    = ValidationStatus.NotValidated,
                        Timestamp = DateTime.Now
                    };
                    Log.Info("Validation result: " + validationResult.Messages);
                    metadataEntry.ValidationResults.Add(validationResult);
                }
                else
                {
                    if (metadataEntry.InspireResource)
                    {
                        // Check validation state instead of valdating.
                        Log.Info("Check validation state metadata with INSPIRE-validator.");
                        validationResult = new InspireValidator(_httpRequestExecutor).CheckValidationState(uuid);
                        Log.Info("Validation result: " + validationResult.Messages);
                        metadataEntry.ValidationResults.Add(validationResult);
                    }
                    if (metadataEntry.Keywords.Contains("Norge digitalt"))
                    {
                        Log.Info("Validating metadata with Norge Digitalt-validator.");
                        validationResult = new NorgeDigitaltValidator().Validate(metadataEntry, metadata, rawXmlProcessed);
                        Log.Info("Validation result: " + validationResult.Messages);
                        metadataEntry.ValidationResults.Add(validationResult);
                    }
                }
            }
            catch (Exception e)
            {
                metadataEntry = ParseCswRecordResponse(uuid, null);

                string message = e.Message;
                if (e.InnerException != null)
                {
                    message += e.InnerException.Message;
                }

                metadataEntry.ValidationResults.Add(new ValidationResult {
                    Messages = "Exception during validation: " + message, Status = ValidationStatus.NotValidated, Timestamp = DateTime.Now
                });
                Log.Error("Exception occured for uuid=" + uuid + ", not validated. " + message);
            }
            return(metadataEntry);
        }
        public List<MetadataEntry> GetMetadataList(string organization, string resourceType, bool? inspireResource)
        {
            var metadataEntries = new List<MetadataEntry>();

            NpgsqlConnection connection = GetConnection();
            connection.Open();
            try
            {
                string sql = "SELECT m.uuid, m.title, m.responsible_organization, m.resourcetype, m.inspire_resource, m.keywords, m.contact_information, m.abstract, m.purpose FROM metadata m "
                    + CreateMetaConditionsSql(organization, resourceType, inspireResource);

                using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
                {
                    AddMetadataConditionParametersToCommand(null, organization, resourceType, inspireResource, command);

                    using (NpgsqlDataReader dr = command.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            var metadataEntry = new MetadataEntry()
                            {
                                InspireResource = dr.GetBoolean(4),
                                ResourceType = dr.GetString(3),
                                ResponsibleOrganization = dr.GetString(2),
                                Title = dr.GetString(1),
                                Uuid = dr.GetString(0),
                                Keywords = dr.IsDBNull(5) ? null : dr.GetString(5),
                                ContactInformation = dr.IsDBNull(6) ? null : dr.GetString(6),
                                Abstract = dr.IsDBNull(7) ? null : dr.GetString(7),
                                Purpose = dr.IsDBNull(8) ? null : dr.GetString(8),
                            };
                            metadataEntries.Add(metadataEntry);
                        }
                    }
                }
            }
            finally
            {
                connection.Close();
            }

            return metadataEntries;
        }
 private void UpdateMetadataInformation(MetadataEntry metadata, NpgsqlConnection connection)
 {
     const string sql = "UPDATE metadata SET " +
                        "title = :title, " +
                        "responsible_organization = :responsible_organization, " +
                        "resourcetype = :resourcetype, " +
                        "inspire_resource = :inspire_resource, " +
                        "active = true, " +
                        "keywords = :keywords, " +
                        "contact_information = :contact_information, " +
                        "abstract = :abstract, " +
                        "purpose = :purpose " +
                        "WHERE uuid = :uuid";
     RunInsertUpdateMetadataCommand(metadata, sql, connection);
 }
        private void InsertMetadataValidationResult(MetadataEntry metadata, NpgsqlConnection connection)
        {
            var validationResult = metadata.ValidationResults[0];

            const string sql =
                "INSERT INTO validation_results (uuid, result, messages) VALUES (:uuid, :result, :messages)";
            using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
            {
                command.Parameters.Add(new NpgsqlParameter("uuid", NpgsqlDbType.Varchar) {Value = metadata.Uuid});
                command.Parameters.Add(new NpgsqlParameter("result", NpgsqlDbType.Integer)
                    {
                        Value = validationResult.Result
                    });
                command.Parameters.Add(new NpgsqlParameter("messages", NpgsqlDbType.Varchar)
                    {
                        Value = validationResult.Messages
                    });
                command.ExecuteNonQuery();
            }
        }
 private void InsertMetadataInformation(MetadataEntry metadata, NpgsqlConnection connection)
 {
     const string sql =
         "INSERT INTO metadata (uuid, title, responsible_organization, resourcetype, inspire_resource, active, keywords, contact_information, abstract, purpose) VALUES " +
         "(:uuid, :title, :responsible_organization, :resourcetype, :inspire_resource, true, :keywords, :contact_information, :abstract, :purpose)";
     RunInsertUpdateMetadataCommand(metadata, sql, connection);
 }
 private static void RunInsertUpdateMetadataCommand(MetadataEntry metadata, string sql,
     NpgsqlConnection connection)
 {
     using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
     {
         command.Parameters.Add(new NpgsqlParameter("uuid", NpgsqlDbType.Varchar) {Value = metadata.Uuid});
         command.Parameters.Add(new NpgsqlParameter("title", NpgsqlDbType.Varchar) {Value = metadata.Title});
         command.Parameters.Add(new NpgsqlParameter("responsible_organization", NpgsqlDbType.Varchar)
             {
                 Value = metadata.ResponsibleOrganization
             });
         command.Parameters.Add(new NpgsqlParameter("resourcetype", NpgsqlDbType.Varchar)
             {
                 Value = metadata.ResourceType
             });
         command.Parameters.Add(new NpgsqlParameter("inspire_resource", NpgsqlDbType.Boolean)
             {
                 Value = metadata.InspireResource
             });
         command.Parameters.Add(new NpgsqlParameter("keywords", NpgsqlDbType.Varchar) { Value = metadata.Keywords });
         command.Parameters.Add(new NpgsqlParameter("contact_information", NpgsqlDbType.Varchar) { Value = metadata.ContactInformation });
         command.Parameters.Add(new NpgsqlParameter("abstract", NpgsqlDbType.Varchar) { Value = metadata.Abstract });
         command.Parameters.Add(new NpgsqlParameter("purpose", NpgsqlDbType.Varchar) { Value = metadata.Purpose });
         command.ExecuteNonQuery();
     }
 }
        public void SaveMetadata(MetadataEntry metadata)
        {
            NpgsqlConnection connection = GetConnection();
            connection.Open();
            NpgsqlTransaction transaction = connection.BeginTransaction();
            try
            {
                if (MetadataExists(metadata.Uuid, connection))
                {
                    UpdateMetadataInformation(metadata, connection);
                }
                else
                {
                    InsertMetadataInformation(metadata, connection);
                }

                InsertMetadataValidationResult(metadata, connection);

                transaction.Commit();
            }
            catch (NpgsqlException e)
            {
                throw e;
            }
            finally
            {
                connection.Close();
            }
        }
        public List<MetadataEntry> GetMetadataListWithLatestValidationResult(int? status, string organization, string resourceType, bool? inspireResource)
        {
            var metadataEntries = new List<MetadataEntry>();

            NpgsqlConnection connection = GetConnection();
            connection.Open();
            try
            {
                var sql = SelectWhichSqlToUse(status, organization, resourceType, inspireResource);

                using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
                {
                    AddMetadataConditionParametersToCommand(status, organization, resourceType, inspireResource, command);

                    using (NpgsqlDataReader dr = command.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            var metadataEntry = new MetadataEntry()
                                {
                                    InspireResource = dr.GetBoolean(4),
                                    ResourceType = dr.GetString(3),
                                    ResponsibleOrganization = dr.GetString(2),
                                    Title = dr.GetString(1),
                                    Uuid = dr.GetString(0),
                                    ValidationResults = new List<ValidationResult>()
                                        {
                                            new ValidationResult()
                                                {
                                                    Messages = dr.IsDBNull(6) ? null : dr.GetString(6),
                                                    Result = dr.GetInt32(5),
                                                    Timestamp = dr.GetTimeStamp(7)
                                                }
                                        }
                                };
                            metadataEntries.Add(metadataEntry);
                        }
                    }
                }
            }
            finally
            {
                connection.Close();
            }
            return metadataEntries;
        }