<commit_before>(ns cmr.umm-spec.field-update
 "Functions to apply an update of a particular type to a field-translation"
 (:require
  [cmr.common.mime-types :as mt]
  [cmr.common.util :as util]
  [cmr.umm-spec.date-util :as date-util]
  [cmr.umm-spec.umm-spec-core :as spec-core]))

(def data-center-url
  "Data center URL's content type."
  "DataCenterURL")

(def home-page
  "Data center URL's type."
  "HOME PAGE")

(defn field-update-functions
  "Partial update functions for handling specific update cases"
  [umm]
  {[:Instruments] (partial util/update-in-each umm [:Platforms])})

(defn- get-home-page-url
  "Get the data center home page url from a list of related urls.
   If there are more than one returned, arbitrarily choose the first one."
  [related-urls]
  (first (for [{:keys [URLContentType Type URL]} related-urls
               :when (and (= data-center-url URLContentType)
                          (= home-page Type))]
           {:URLContentType data-center-url
            :Type home-page
            :URL URL})))

(defn- home-page-url?
  "Check if a related url is a data center home page url."
  [related-url]
  (let [{:keys [URLContentType Type]} related-url]
    (and (= data-center-url URLContentType) (= home-page Type))))

(defn- home-page-url-update
  "Apply special home-page-url update to data-center using the update-value.
   If the new home-page-url is provided in the update-value.
      a. If data-center-related-urls contains home-page-url, update it with the new home-page-url.
         if the new home-page-url is not present in update-value, remove the home-page-url from data center.
      b. If data-center-related-urls doesn't contain home-page-url, add the new home-page-url to it.
      Note: Anything other than the home-page-url in update-value are ignored."
  [data-center update-value]
  (let [update-value-related-urls (get-in update-value [:ContactInformation :RelatedUrls])
        update-value-home-page-url (get-home-page-url update-value-related-urls)
        data-center-related-urls (get-in data-center [:ContactInformation :RelatedUrls])]
    (if (seq update-value-home-page-url)
      (if (some home-page-url? data-center-related-urls)
        (update-in data-center [:ContactInformation :RelatedUrls]
          #(map (fn [x] (if (home-page-url? x) update-value-home-page-url x)) %))
        (let [new-related-urls (conj data-center-related-urls update-value-home-page-url)]
          (assoc-in data-center [:ContactInformation :RelatedUrls] new-related-urls)))
      (update-in data-center [:ContactInformation :RelatedUrls]
          #(remove home-page-url? %)))))

(defn- remove-data-center-empty-related-urls
  "Remove RelatedUrls from data center's ContactInformation if it's empty"
  [data-center]
  (if (seq (get-in data-center [:ContactInformation :RelatedUrls]))
    data-center
    (update-in data-center [:ContactInformation] dissoc :RelatedUrls))) 

(defn- remove-data-center-empty-contact-info
  "Remove data center's ContactInformation if it's empty"
  [data-center]
  (if (seq (:ContactInformation data-center))
    data-center
    (dissoc data-center :ContactInformation))) 

(defn- data-center-update
  "Apply update to data-center using the update-value. Returns data center.
   It includes special update to the home page url inside ContactInformation
   and regular update to everything outside of ContactInformation."<commit_msg>CMR-4605: Modified a doc string.<commit_after>(ns cmr.umm-spec.field-update
 "Functions to apply an update of a particular type to a field-translation"
 (:require
  [cmr.common.mime-types :as mt]
  [cmr.common.util :as util]
  [cmr.umm-spec.date-util :as date-util]
  [cmr.umm-spec.umm-spec-core :as spec-core]))

(def data-center-url
  "Data center URL's content type."
  "DataCenterURL")

(def home-page
  "Data center URL's type."
  "HOME PAGE")

(defn field-update-functions
  "Partial update functions for handling specific update cases"
  [umm]
  {[:Instruments] (partial util/update-in-each umm [:Platforms])})

(defn- get-home-page-url
  "Get the data center home page url from a list of related urls.
   If there are more than one returned, arbitrarily choose the first one."
  [related-urls]
  (first (for [{:keys [URLContentType Type URL]} related-urls
               :when (and (= data-center-url URLContentType)
                          (= home-page Type))]
           {:URLContentType data-center-url
            :Type home-page
            :URL URL})))

(defn- home-page-url?
  "Check if a related url is a data center home page url."
  [related-url]
  (let [{:keys [URLContentType Type]} related-url]
    (and (= data-center-url URLContentType) (= home-page Type))))

(defn- home-page-url-update
  "Apply special home-page-url update to data-center using the update-value.
   If the new home-page-url is provided in the update-value.
      a. If data-center-related-urls contains home-page-url, update it with the new home-page-url.
      b. If data-center-related-urls doesn't contain home-page-url, add the new home-page-url to it.
   If the new home-page-url is not present in update-value, remove the home-page-url from data center.
   Note: Anything other than the home-page-url in update-value are ignored."
  [data-center update-value]
  (let [update-value-related-urls (get-in update-value [:ContactInformation :RelatedUrls])
        update-value-home-page-url (get-home-page-url update-value-related-urls)
        data-center-related-urls (get-in data-center [:ContactInformation :RelatedUrls])]
    (if (seq update-value-home-page-url)
      (if (some home-page-url? data-center-related-urls)
        (update-in data-center [:ContactInformation :RelatedUrls]
          #(map (fn [x] (if (home-page-url? x) update-value-home-page-url x)) %))
        (let [new-related-urls (conj data-center-related-urls update-value-home-page-url)]
          (assoc-in data-center [:ContactInformation :RelatedUrls] new-related-urls)))
      (update-in data-center [:ContactInformation :RelatedUrls]
          #(remove home-page-url? %)))))

(defn- remove-data-center-empty-related-urls
  "Remove RelatedUrls from data center's ContactInformation if it's empty"
  [data-center]
  (if (seq (get-in data-center [:ContactInformation :RelatedUrls]))
    data-center
    (update-in data-center [:ContactInformation] dissoc :RelatedUrls))) 

(defn- remove-data-center-empty-contact-info
  "Remove data center's ContactInformation if it's empty"
  [data-center]
  (if (seq (:ContactInformation data-center))
    data-center
    (dissoc data-center :ContactInformation))) 

(defn- data-center-update
  "Apply update to data-center using the update-value. Returns data center.
   It includes special update to the home page url inside ContactInformation
   and regular update to everything outside of ContactInformation."
{'fraction_non_alphanumeric': 0.12159789288849868, 'fraction_numerical': 0.000585308750365818, 'mean_word_length': 3.3369289340101522, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5003979', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2127, 'n_words': 649}