class ControlledDocument
A class representing a controlled document in a document management system, providing access to document metadata stored in a Neo4j database.
/tf/active/vicechatdev/CDocs single class/db/db_operations.py
958 - 975
simple
Purpose
ControlledDocument serves as a data access object (DAO) for controlled documents in a document management system. It can be instantiated either with a UID to load document data from a Neo4j database, or with pre-existing data. The class provides convenient property accessors for document attributes like UID and current version UID, handling case variations in the underlying data structure. This class is part of a larger document control system (CDocs) that manages versioned documents with audit trails.
Source Code
class ControlledDocument:
def __init__(self, uid=None, data=None):
# Initialize from either uid or data
self._data = data or {}
if uid and not data:
# Load from database by UID
self._data = get_node_by_uid(uid) or {}
@property
def uid(self):
"""Get the UID of the document, handling both case variations"""
return self._data.get('UID') or self._data.get('uid')
@property
def current_version_uid(self):
"""Get the current version UID, handling both case variations"""
return self._data.get('currentVersionUID') or self._data.get('currentversionuid')
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
uid: Optional string identifier for the document. When provided without data parameter, triggers a database lookup using get_node_by_uid() to load the document's data from Neo4j. Should be a valid document UID that exists in the database.
data: Optional dictionary containing pre-loaded document data. When provided, bypasses database lookup and directly initializes the instance with this data. Expected to contain keys like 'UID'/'uid' and 'currentVersionUID'/'currentversionuid'. If both uid and data are provided, data takes precedence.
Return Value
Instantiation returns a ControlledDocument object. The uid property returns a string representing the document's unique identifier (or None if not present). The current_version_uid property returns a string representing the UID of the current version of the document (or None if not present).
Class Interface
Methods
__init__(self, uid=None, data=None)
Purpose: Initialize a ControlledDocument instance either by loading from database using UID or from provided data dictionary
Parameters:
uid: Optional string identifier to load document from databasedata: Optional dictionary containing pre-loaded document data
Returns: None (constructor)
@property uid(self) -> Optional[str]
property
Purpose: Get the unique identifier of the document, handling both 'UID' and 'uid' key variations in the data
Returns: String containing the document's UID, or None if not present in data
@property current_version_uid(self) -> Optional[str]
property
Purpose: Get the UID of the current version of the document, handling both 'currentVersionUID' and 'currentversionuid' key variations
Returns: String containing the current version's UID, or None if not present in data
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
_data |
Dict[str, Any] | Internal dictionary storing the document's data, including UID, version information, and other metadata. Initialized from either database lookup or provided data parameter. | instance |
Dependencies
neo4jlogginguuidtypingdatetimetraceback
Required Imports
from CDocs.db import get_node_by_uid
from CDocs.db import get_driver
from CDocs.db.schema_manager import NodeLabels
from CDocs.db.schema_manager import RelTypes
from CDocs.db.schema_manager import migrate_audit_events
from neo4j import Driver
from neo4j import Session
from neo4j import Transaction
from neo4j import Record
from neo4j.exceptions import ServiceUnavailable
from neo4j.exceptions import ClientError
import logging
import uuid
from typing import Dict, List, Any, Optional, Union, Tuple
from datetime import datetime
import traceback
Usage Example
# Example 1: Load document from database by UID
doc = ControlledDocument(uid='doc-12345')
if doc.uid:
print(f'Document UID: {doc.uid}')
print(f'Current Version: {doc.current_version_uid}')
# Example 2: Create document with pre-loaded data
data = {
'UID': 'doc-67890',
'currentVersionUID': 'ver-001',
'title': 'My Document'
}
doc = ControlledDocument(data=data)
print(f'Document UID: {doc.uid}')
print(f'Current Version: {doc.current_version_uid}')
# Example 3: Create empty document
empty_doc = ControlledDocument()
print(f'UID exists: {empty_doc.uid is not None}') # False
Best Practices
- Always check if uid or current_version_uid properties return None before using them, as documents may not have these fields populated
- When instantiating with uid parameter, ensure the database connection is available and the UID exists to avoid getting an empty document
- The class handles case variations ('UID' vs 'uid', 'currentVersionUID' vs 'currentversionuid') automatically, so either format in the data dictionary will work
- If both uid and data parameters are provided, data takes precedence and no database lookup occurs
- This class is read-only for the properties shown; modifications to _data should be done carefully as there are no validation or persistence methods visible
- The _data attribute is internal and should not be accessed directly; use the provided properties instead
- Consider the database query cost when instantiating multiple documents by UID; batch loading with data parameter may be more efficient
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_document_version 77.8% similar
-
class ControlledDocument_v1 76.9% similar
-
function create_document 76.7% similar
-
class ControlledDocument_v1 74.6% similar
-
function create_document_legacy 74.3% similar