🔍 Code Extractor

function check_document_exists_by_uid

Maturity: 52

Queries a Neo4j database to check if a ControlledDocument with a specific UID exists and returns the document object if found.

File:
/tf/active/vicechatdev/CDocs/FC_sync.py
Lines:
81 - 108
Complexity:
simple

Purpose

This function provides a lookup mechanism to verify document existence in a Neo4j graph database by UID. It's used for validation before operations like updates or deletions, preventing duplicate document creation, and retrieving document metadata. The function handles database errors gracefully and returns None if the document doesn't exist or if an error occurs.

Source Code

def check_document_exists_by_uid(cdoc_uid: str) -> Optional[ControlledDocument]:
    """
    Check if a document with the given UID exists in the Neo4j database.
    
    Args:
        cdoc_uid: Document UID to check
        
    Returns:
        ControlledDocument: Document object if found, None otherwise
    """
    try:
        result = db.run_query(
            """
            MATCH (doc:ControlledDocument {UID: $cdoc_uid})
            RETURN doc
            LIMIT 1
            """,
            {"cdoc_uid": cdoc_uid}
        )
        
        if result and len(result) > 0:
            doc = ControlledDocument(result[0]['doc'])
            return doc
        return None
        
    except Exception as e:
        logger.error(f"Error checking document existence by UID: {e}")
        return None

Parameters

Name Type Default Kind
cdoc_uid str - positional_or_keyword

Parameter Details

cdoc_uid: String identifier representing the unique ID (UID) of the ControlledDocument to search for in the Neo4j database. This should be a valid document UID that matches the UID property of ControlledDocument nodes in the database.

Return Value

Type: Optional[ControlledDocument]

Returns a ControlledDocument object if a document with the specified UID is found in the database. Returns None if no document exists with that UID or if an error occurs during the database query. The ControlledDocument object is instantiated from the Neo4j node properties.

Dependencies

  • logging
  • typing
  • CDocs.db.db_operations
  • CDocs.models.document

Required Imports

from typing import Optional
from CDocs.db import db_operations as db
from CDocs.models.document import ControlledDocument
import logging

Usage Example

from typing import Optional
from CDocs.db import db_operations as db
from CDocs.models.document import ControlledDocument
import logging

logger = logging.getLogger(__name__)

# Check if a document exists
doc_uid = "DOC-12345"
document = check_document_exists_by_uid(doc_uid)

if document:
    print(f"Document found: {document}")
    # Proceed with operations on the document
else:
    print(f"No document found with UID: {doc_uid}")
    # Handle missing document case

Best Practices

  • Always check if the returned value is None before accessing document properties to avoid AttributeError
  • Use this function before attempting to update or delete documents to ensure they exist
  • The function logs errors but returns None, so check logs if unexpected None returns occur
  • The LIMIT 1 clause ensures only one result is returned even if multiple documents share the same UID (which shouldn't happen with proper constraints)
  • Consider implementing database constraints to ensure UID uniqueness at the database level
  • Handle the None return case explicitly in calling code to distinguish between 'not found' and 'error occurred' scenarios

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function check_document_exists_by_doc_number 75.1% similar

    Queries a Neo4j database to check if a ControlledDocument exists with a specific document number and returns the document object if found.

    From: /tf/active/vicechatdev/CDocs/FC_sync.py
  • function get_document 72.1% similar

    Retrieves comprehensive details of a controlled document by its UID, with optional inclusion of version history, review cycles, and approval cycles.

    From: /tf/active/vicechatdev/document_controller_backup.py
  • function get_document_v1 69.3% similar

    Retrieves comprehensive details of a controlled document by its UID, including optional version history, review cycles, and approval workflows.

    From: /tf/active/vicechatdev/CDocs/controllers/document_controller.py
  • function node_exists 67.2% similar

    Checks if a node with a specific UID exists in a Neo4j graph database by querying for the node and returning a boolean result.

    From: /tf/active/vicechatdev/CDocs/db/db_operations.py
  • function get_document_v5 67.0% similar

    Retrieves all versions of a controlled document from a Neo4j graph database, including metadata about which version is current.

    From: /tf/active/vicechatdev/CDocs/db/db_operations.py
← Back to Browse