-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb04f46
commit 5b178f1
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import re | ||
from typing import TYPE_CHECKING | ||
|
||
from sqids import Sqids | ||
|
||
from . import Identifier, IdentifierSchema | ||
|
||
if TYPE_CHECKING: | ||
from caselawclient.Client import MarklogicApiClient | ||
|
||
|
||
VALID_FCLID_PATTERN = re.compile(r"^[bcdfghjkmnpqrstvwxyz23456789]{4,}$") | ||
|
||
FCLID_MINIMUM_LENGTH = 8 | ||
FCLID_ALPHABET = "bcdfghjkmnpqrstvwxyz23456789" | ||
|
||
sqids = Sqids( | ||
min_length=FCLID_MINIMUM_LENGTH, | ||
alphabet=FCLID_ALPHABET, | ||
) | ||
|
||
|
||
class FindCaseLawIdentifierSchema(IdentifierSchema): | ||
""" | ||
Identifier schema describing a Find Case Law Identifier. | ||
""" | ||
|
||
name = "Find Case Law Identifier" | ||
namespace = "fclid" | ||
|
||
@classmethod | ||
def validate_identifier(cls, value: str) -> bool: | ||
return bool(VALID_FCLID_PATTERN.match(value)) | ||
|
||
@classmethod | ||
def compile_identifier_url_slug(cls, value: str) -> str: | ||
return "tna." + value | ||
|
||
@classmethod | ||
def mint(cls, api_client: "MarklogicApiClient") -> "FindCaseLawIdentifier": | ||
"""Generate a totally new Find Case Law identifier.""" | ||
next_sequence_number = api_client.get_next_document_sequence_number() | ||
new_identifier = sqids.encode([next_sequence_number]) | ||
return FindCaseLawIdentifier(value=new_identifier) | ||
|
||
|
||
class FindCaseLawIdentifier(Identifier): | ||
schema = FindCaseLawIdentifierSchema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/caselawclient/xquery/get_next_document_sequence_number.xqy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
xquery version "1.0-ml"; | ||
declare option xdmp:transaction-mode "update"; | ||
|
||
let $_ := xdmp:set-transaction-mode("update") | ||
let $state_doc := fn:doc("state.xml") | ||
let $counter_node := $state_doc/state/document_counter | ||
|
||
let $current_counter := $counter_node/text() | ||
let $new_counter := fn:sum(($current_counter, 1)) | ||
|
||
let $_ := xdmp:node-replace($counter_node, <document_counter>{$new_counter}</document_counter>) | ||
let $_ := xdmp:commit() | ||
|
||
return $new_counter |