|
267 | 267 | (error "lookup-by-location-string found redis carmine exception. Will return nil result." e) |
268 | 268 | (throw e))))) |
269 | 269 |
|
| 270 | +(defn- skip-long-name-validation? |
| 271 | + "Returns true if the long name should be skipped in validation. |
| 272 | + Long name is skipped if it's nil, empty string, or 'Not provided' (case-insensitive)." |
| 273 | + [long-name] |
| 274 | + (or (nil? long-name) |
| 275 | + (string/blank? long-name) |
| 276 | + (when (string? long-name) |
| 277 | + (= "not provided" (string/lower-case (string/trim long-name)))))) |
| 278 | + |
270 | 279 | (defn- remove-long-name-from-kms-index |
271 | 280 | "Removes long-name from the umm-c-index keys in order to prevent validation when |
272 | 281 | long-name is not present in the umm-c-keyword. We only want to validate long-name if it is not nil." |
|
296 | 305 | (error "lookup-by-umm-c-keyword-data-format found redis carmine exception. Will return nil result." e) |
297 | 306 | (throw e))))) |
298 | 307 |
|
| 308 | +(defn- lookup-by-umm-c-keyword-with-long-name |
| 309 | + "Generic lookup function for keyword types that have long-name field (platforms, instruments, projects). |
| 310 | + Takes a keyword as represented in the UMM concepts as a map and returns the KMS keyword map |
| 311 | + as its stored in the cache. Returns nil if a keyword is not found. Comparison is made case insensitively. |
| 312 | + The transform-fn parameter is an optional function to apply additional transformations to the keyword |
| 313 | + before lookup (e.g., renaming :type to :category for platforms)." |
| 314 | + ([context keyword-scheme umm-c-keyword] |
| 315 | + (lookup-by-umm-c-keyword-with-long-name context keyword-scheme umm-c-keyword identity)) |
| 316 | + ([context keyword-scheme umm-c-keyword transform-fn] |
| 317 | + (try |
| 318 | + (let [umm-c-keyword (csk-extras/transform-keys csk/->kebab-case umm-c-keyword) |
| 319 | + umm-c-keyword (transform-fn umm-c-keyword) |
| 320 | + skip-long-name? (skip-long-name-validation? (get umm-c-keyword :long-name)) |
| 321 | + ;; If long-name should be skipped (nil, empty, or "Not provided"), remove it before normalization |
| 322 | + keyword-for-comparison (if skip-long-name? (dissoc umm-c-keyword :long-name) umm-c-keyword) |
| 323 | + comparison-map (normalize-for-lookup keyword-for-comparison (kms-scheme->fields-for-umm-c-lookup keyword-scheme)) |
| 324 | + umm-c-cache (hash-cache/context->cache context kms-umm-c-cache-key) |
| 325 | + [tm value] (util/time-execution (hash-cache/get-value umm-c-cache kms-umm-c-cache-key keyword-scheme)) |
| 326 | + _ (rl-util/log-redis-read-complete (str "lookup-by-umm-c-keyword-" keyword-scheme) kms-umm-c-cache-key tm) |
| 327 | + ;; When skipping long-name, remove it from KMS index keys to enable matching on remaining fields |
| 328 | + value-for-lookup (if skip-long-name? (remove-long-name-from-kms-index value) value)] |
| 329 | + (get value-for-lookup comparison-map)) |
| 330 | + (catch Exception e |
| 331 | + (if (clojure.string/includes? (ex-message e) "Carmine connection error") |
| 332 | + (error (str "lookup-by-umm-c-keyword-" keyword-scheme " found redis carmine exception. Will return nil result.") e) |
| 333 | + (throw e)))))) |
| 334 | + |
299 | 335 | (defn lookup-by-umm-c-keyword-platforms |
300 | 336 | "Takes a keyword as represented in the UMM concepts as a map and returns the KMS keyword map |
301 | 337 | as its stored in the cache. Returns nil if a keyword is not found. Comparison is made case insensitively." |
302 | 338 | [context keyword-scheme umm-c-keyword] |
303 | | - (try |
304 | | - (let [umm-c-keyword (csk-extras/transform-keys csk/->kebab-case umm-c-keyword) |
305 | | - ;; CMR-3696 This is needed to compare the keyword category, which is mapped |
306 | | - ;; to the UMM Platform Type field. This will avoid complications with facets. |
307 | | - umm-c-keyword (set/rename-keys umm-c-keyword {:type :category}) |
308 | | - comparison-map (normalize-for-lookup umm-c-keyword (kms-scheme->fields-for-umm-c-lookup keyword-scheme)) |
309 | | - umm-c-cache (hash-cache/context->cache context kms-umm-c-cache-key) |
310 | | - [tm value] (util/time-execution (hash-cache/get-value umm-c-cache kms-umm-c-cache-key keyword-scheme)) |
311 | | - _ (rl-util/log-redis-read-complete "lookup-by-umm-c-keyword-platforms" kms-umm-c-cache-key tm)] |
312 | | - (if (get umm-c-keyword :long-name) |
313 | | - ;; Check both longname and shortname |
314 | | - (get-in value [comparison-map]) |
315 | | - ;; Check just shortname |
316 | | - (-> value |
317 | | - (remove-long-name-from-kms-index) |
318 | | - (get comparison-map)))) |
319 | | - (catch Exception e |
320 | | - (if (clojure.string/includes? (ex-message e) "Carmine connection error") |
321 | | - (error "lookup-by-umm-c-keyword-platforms found redis carmine exception. Will return nil result." e) |
322 | | - (throw e))))) |
| 339 | + ;; CMR-3696 This is needed to compare the keyword category, which is mapped |
| 340 | + ;; to the UMM Platform Type field. This will avoid complications with facets. |
| 341 | + (lookup-by-umm-c-keyword-with-long-name |
| 342 | + context |
| 343 | + keyword-scheme |
| 344 | + umm-c-keyword |
| 345 | + #(set/rename-keys % {:type :category}))) |
| 346 | + |
| 347 | +(defn lookup-by-umm-c-keyword-instruments |
| 348 | + "Takes a keyword as represented in the UMM concepts as a map and returns the KMS keyword map |
| 349 | + as its stored in the cache. Returns nil if a keyword is not found. Comparison is made case insensitively." |
| 350 | + [context keyword-scheme umm-c-keyword] |
| 351 | + (lookup-by-umm-c-keyword-with-long-name context keyword-scheme umm-c-keyword)) |
| 352 | + |
| 353 | +(defn lookup-by-umm-c-keyword-projects |
| 354 | + "Takes a keyword as represented in the UMM concepts as a map and returns the KMS keyword map |
| 355 | + as its stored in the cache. Returns nil if a keyword is not found. Comparison is made case insensitively." |
| 356 | + [context keyword-scheme umm-c-keyword] |
| 357 | + (lookup-by-umm-c-keyword-with-long-name context keyword-scheme umm-c-keyword)) |
323 | 358 |
|
324 | 359 | (defn lookup-by-umm-c-keyword |
325 | 360 | "Takes a keyword as represented in UMM concepts as a map and returns the KMS keyword as it exists |
|
329 | 364 | (when-not (:ignore-kms-keywords context) |
330 | 365 | (case keyword-scheme |
331 | 366 | :platforms (lookup-by-umm-c-keyword-platforms context keyword-scheme umm-c-keyword) |
| 367 | + :instruments (lookup-by-umm-c-keyword-instruments context keyword-scheme umm-c-keyword) |
| 368 | + :projects (lookup-by-umm-c-keyword-projects context keyword-scheme umm-c-keyword) |
332 | 369 | :granule-data-format (lookup-by-umm-c-keyword-data-format context keyword-scheme umm-c-keyword) |
333 | 370 | ;; default |
334 | 371 | (let [umm-c-keyword (csk-extras/transform-keys csk/->kebab-case umm-c-keyword) |
|
0 commit comments