<commit_before>(ns cmr.ingest.services.ingest-service.service
  (:require
    [clojure.edn :as edn]
    [clojure.string :as string]
    [cmr.common.api.context :as context-util]
    [cmr.common.mime-types :as mt]
    [cmr.common.services.errors :as errors]
    [cmr.common.util :refer [defn-timed]]
    [cmr.common.validations.core :as cv]
    [cmr.ingest.services.ingest-service.util :as util]
    [cmr.ingest.services.messages :as msg]
    [cmr.transmit.metadata-db :as mdb]
    [cmr.transmit.metadata-db2 :as mdb2]))

(def ^:private update-service-validations
  "Service level validations when updating a service."
  [(cv/field-cannot-be-changed :service-name)
   ;; Originator id cannot change but we allow it if they don't specify a
   ;; value.
   (cv/field-cannot-be-changed :originator-id true)])

(defn validate-update-service
  "Validates a service update."
  [existing-service updated-service]
  (cv/validate!
   update-service-validations
   (assoc updated-service :existing existing-service)))

(defn overwrite-service-tombstone
  "This function is called when the service exists but was previously
  deleted."
  [context concept service user-id]
  (mdb2/save-concept context
                    (-> concept
                        (assoc :metadata (pr-str service)
                               :deleted false
                               :user-id user-id)
                        (dissoc :revision-date)
                        (update-in [:revision-id] inc))))

(defn- service->new-concept
  "Converts a service into a new concept that can be persisted in metadata
  db."
  [service]
  {:concept-type :service
   :native-id (:native-id service)
   :metadata (pr-str service)
   :user-id (:originator-id service)
   ;; The first version of a service should always be revision id 1. We
   ;; always specify a revision id when saving variables to help avoid
   ;; conflicts
   :revision-id 1
   :format mt/edn
   :extra-fields {
     :service-name (:name service)}})<commit_msg>CMR-4467 - Add a workaround for provider-id in services ingest until CMR-4468.<commit_after>(ns cmr.ingest.services.ingest-service.service
  (:require
    [clojure.edn :as edn]
    [clojure.string :as string]
    [cmr.common.api.context :as context-util]
    [cmr.common.mime-types :as mt]
    [cmr.common.services.errors :as errors]
    [cmr.common.util :refer [defn-timed]]
    [cmr.common.validations.core :as cv]
    [cmr.ingest.services.ingest-service.util :as util]
    [cmr.ingest.services.messages :as msg]
    [cmr.transmit.metadata-db :as mdb]
    [cmr.transmit.metadata-db2 :as mdb2]))

(def ^:private update-service-validations
  "Service level validations when updating a service."
  [(cv/field-cannot-be-changed :service-name)
   ;; Originator id cannot change but we allow it if they don't specify a
   ;; value.
   (cv/field-cannot-be-changed :originator-id true)])

(defn validate-update-service
  "Validates a service update."
  [existing-service updated-service]
  (cv/validate!
   update-service-validations
   (assoc updated-service :existing existing-service)))

(defn overwrite-service-tombstone
  "This function is called when the service exists but was previously
  deleted."
  [context concept service user-id]
  (mdb2/save-concept context
                    (-> concept
                        (assoc :metadata (pr-str service)
                               :deleted false
                               :user-id user-id)
                        (dissoc :revision-date)
                        (update-in [:revision-id] inc))))

(defn- service->new-concept
  "Converts a service into a new concept that can be persisted in metadata
  db."
  [service]
  {:concept-type :service
   ;; The following is a placeholder until UMM-Service fully supports
   ;; provider ids; see CMR-4468
   :provider-id (or (:provider-id service) "CMR")
   :native-id (:native-id service)
   :metadata (pr-str service)
   :user-id (:originator-id service)
   ;; The first version of a service should always be revision id 1. We
   ;; always specify a revision id when saving variables to help avoid
   ;; conflicts
   :revision-id 1
   :format mt/edn
   :extra-fields {
     :service-name (:name service)}})
{'fraction_non_alphanumeric': 0.11269614835948645, 'fraction_numerical': 0.005230622919638612, 'mean_word_length': 2.683887915936953, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '136475', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1297, 'n_words': 377}