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