Completed
Push — master ( 5353c7...9e0b1e )
by P.R.
02:05
created

RegularDimension.pre_load_data()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 2
cp 0
rs 9.4285
cc 1
crap 2
1
"""
2
ETLT
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
import abc
9
10
11
class RegularDimension(metaclass=abc.ABCMeta):
1 ignored issue
show
Unused Code introduced by
This abstract class does not seem to be used anywhere.
Loading history...
12
    """
13
    Abstract parent class for translating natural key to a technical key of a regular dimension.
14
    """
15
16
    # ------------------------------------------------------------------------------------------------------------------
17
    def __init__(self):
18
        """
19
        Object constructor.
20
        """
21
22
        self._map = {}
23
        """
24
        The map from natural keys to a technical keys.
25
26
        :type: dict[T, int|None]
27
        """
28
29
        # Pre-load look up data in to the map.
30
        self.pre_load_data()
31
32
    # ------------------------------------------------------------------------------------------------------------------
33
    def get_id(self, natural_key, enhancement=None):
34
        """
35
        Returns the technical ID for a natural key or None if the given natural key is not valid.
36
37
        :param T natural_key: The natural key.
38
        :param T enhancement: Enhancement data of the dimension row.
39
40
        :rtype: int|None
41
        """
42
        # If the natural key is known return the technical ID immediately.
43
        if natural_key in self._map:
44
            return self._map[natural_key]
45
46
        # The natural key is not in the map of this dimension. Call a stored procedure for translating the natural key
47
        # to a technical key.
48
        self.pre_call_stored_procedure()
49
        success = False
50
        try:
51
            key = self.call_stored_procedure(natural_key, enhancement)
52
            success = True
53
        finally:
54
            self.post_call_stored_procedure(success)
55
56
        # Add the translation for natural key to technical ID to the map.
57
        self._map[natural_key] = key
58
59
        return key
60
61
    # ------------------------------------------------------------------------------------------------------------------
62
    @abc.abstractmethod
63
    def call_stored_procedure(self, natural_key, enhancement):
64
        """
65
        Calls a stored procedure for getting the technical key of a natural key. Returns the technical ID or None if
66
        the given natural key is not valid.
67
68
        :param T natural_key: The natural key.
69
        :param T enhancement: Enhancement data of the dimension row.
70
71
        :rtype: int|None
72
        """
73
        raise NotImplementedError()
74
75
    # ------------------------------------------------------------------------------------------------------------------
76
    def pre_load_data(self):
77
        """
78
        Can be overridden to pre-load lookup data from a dimension table.
79
80
        :rtype: None
81
        """
82
        pass
83
84
    # ------------------------------------------------------------------------------------------------------------------
85
    def pre_call_stored_procedure(self):
86
        """
87
        This method is invoked before call the stored procedure for getting the technical key of a natural key.
88
89
        In a concurrent environment override this method to acquire a lock on the dimension or dimension hierarchy.
90
91
        :rtype: None
92
        """
93
        pass
94
95
    # ------------------------------------------------------------------------------------------------------------------
96
    def post_call_stored_procedure(self, success):
97
        """
98
        This method is invoked after calling the stored procedure for getting the technical key of a natural key.
99
100
        In a concurrent environment override this method to release a lock on the dimension or dimension hierarchy and
101
        to commit or rollback the transaction.
102
103
        :param bool success: If True the stored procedure is executed successfully. If an exception has occurred.
104
105
        :rtype: None
106
        """
107
        pass
108
109
# ----------------------------------------------------------------------------------------------------------------------
110