Skip to content

Commit ee57ab5

Browse files
authored
Update }bedrock.hier.leaves.sync (#197)
Update }bedrock.hier.leaves.sync.pro fix for #147
1 parent 827812c commit ee57ab5

1 file changed

Lines changed: 100 additions & 54 deletions

File tree

main/}bedrock.hier.leaves.sync.pro

Lines changed: 100 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
586,"}Dimensions"
55
585,"}Dimensions"
66
564,
7-
565,"v2Ro>?=^[<xzbf5>egIxAPyK?1p;]2H[@>;urjCnA4W19Bd[v9tMDub2+[Qwo}JR;yS1]>[kEqt>rAn@mv^i;@dTCYQ:Jy1bOV6=smDMzlqQof\Gi^K1j8vPu?9kj\@k!kGBS;G=JXYfmoIn7==XF[EB]zk1]TT31GpaHis@\6=Y=iH]aiMv0v:26?d`qLc5S\d21D"
7+
565,"c0vy=UZd]C7bY]Rn?Y<=XpnWWi\cSX;UP=a^UN=T=@k4iKow?9WtC9kUe*[QsIpzNIUZQN^UA1~t;yq_xWQ`noKz9lYQ?PY9n?O6mLIMR1C6Occ\g\M2Cq6mkuW6eZvgm,[JR1zkaVZ{IAEoIAy=]3a:S@]GOEX@0R=wk1@9KjVH7yisty3rfu0hLf^NydQL33uF_1Y="
88
559,1
99
928,0
1010
593,
@@ -25,30 +25,34 @@
2525
569,0
2626
592,0
2727
599,1000
28-
560,5
28+
560,6
2929
pLogOutput
3030
pStrictErrorHandling
3131
pDim
3232
pHier
3333
pDelim
34-
561,5
34+
pReverse
35+
561,6
3536
1
3637
1
3738
2
3839
2
3940
2
40-
590,5
41+
1
42+
590,6
4143
pLogOutput,0
4244
pStrictErrorHandling,0
4345
pDim,""
4446
pHier,""
4547
pDelim,"&"
46-
637,5
48+
pReverse,0
49+
637,6
4750
pLogOutput,"OPTIONAL: Write parameters and action summary to server message log (Boolean True = 1)"
4851
pStrictErrorHandling,"OPTIONAL: On encountering any error, exit with major error status by ProcessQuit after writing to the server message log (Boolean True = 1)"
4952
pDim,"REQUIRED: Dimension, accepts wildcards (all dimensions = *)"
5053
pHier,"OPTIONAL: Hierarchy, accepts wildcards (all hierarchies = *)"
5154
pDelim,"OPTIONAL: Delimiter character (default value if blank = '&')"
55+
pReverse,"OPTIONAL: If true then also add any elements from the Leaves hierarchy not existing in the specified hierarchies to those hierarchies (under a consolidation bucket 'ORPHAN LEAVES')"
5256
577,1
5357
vDimHier
5458
578,1
@@ -62,13 +66,12 @@ vDimHier
6266
582,1
6367
VarType=32 ColType=827
6468
603,0
65-
572,162
69+
572,170
6670
#Region CallThisProcess
6771
# A snippet of code provided as an example how to call this process should the developer be working on a system without access to an editor with auto-complete.
6872
If( 1 = 0 );
69-
ExecuteProcess( '}bedrock.hier.leaves.sync', 'pLogOutput', pLogOutput,
70-
'pStrictErrorHandling', pStrictErrorHandling,
71-
'pDim', '', 'pHier', '', 'pDelim', '&'
73+
ExecuteProcess( '}bedrock.hier.leaves.sync', 'pLogOutput', pLogOutput, 'pStrictErrorHandling', pStrictErrorHandling,
74+
'pDim', '', 'pHier', '', 'pDelim', '&', 'pReverse', 0
7275
);
7376
EndIf;
7477
#EndRegion CallThisProcess
@@ -117,19 +120,20 @@ cDimDimensions = '}Dimensions';
117120
cCharAny = '?';
118121
cStringAny = '*';
119122
cHierLeaves = 'Leaves';
120-
cSubMissing = 'Missing';
123+
cRollupOrphan = 'ORPHAN LEAVES';
121124

122125
### LogOutput parameters
123126
IF( pLogoutput >= 1 );
124127
LogOutput('INFO', Expand( cLogInfo ) );
125128
ENDIF;
126129

127130
### Validate Parameters
128-
nErrors = 0;
129-
nDims = 0;
130-
nDimsChanged = 0;
131-
nElems = 0;
132-
sDimPrev = '';
131+
sDimPrev = '';
132+
nErrors = 0;
133+
nDims = 0;
134+
nDimsChanged = 0;
135+
nElems = 0;
136+
nLeaves = 0;
133137

134138
If( Scan( '*', pDim ) = 0 & Scan( '?', pDim ) = 0 & Scan( pDelim, pDim ) = 0 & Scan( ':', pDim ) > 0 & pHier @= '' );
135139
# A hierarchy has been passed as dimension. Handle the input error by splitting dim:hier into dimension & hierarchy
@@ -149,6 +153,11 @@ If( Trim( pDim ) @= '' );
149153
LogOutput( cMsgErrorLevel, Expand( cMsgErrorContent ) );
150154
EndIf;
151155

156+
### Validate reverse sync option
157+
If( pReverse <> 1 );
158+
pReverse = 0;
159+
EndIf;
160+
152161
### If errors occurred terminate process with a major error status ###
153162
If( nErrors <> 0 );
154163
If( pStrictErrorHandling = 1 );
@@ -172,7 +181,7 @@ Else;
172181
EndIf;
173182
sSearchDim = TRIM( SUBST( sDimTokenizer, 1, nPos - 1 ) );
174183
If( sMDX @= '' );
175-
sMDX = Expand( '{TM1FILTERBYPATTERN( {TM1SUBSETALL([%cDimDimensions%])}, "%sSearchDim%" )}' );
184+
sMDX = Expand( '{TM1FILTERBYPATTERN( {TM1SUBSETALL([%cDimDimensions%])}, "%sSearchDim%*" )}' );
176185
Else;
177186
sMDX = Expand( '%sMDX% + {TM1FILTERBYPATTERN( {TM1SUBSETALL([%cDimDimensions%])}, "%sSearchDim%" )}' );
178187
EndIf;
@@ -189,7 +198,7 @@ If ( TRIM( pHier ) @= cAll );
189198
Else;
190199
sHierTokenizer = TRIM( pHier );
191200
If( sHierTokenizer @= '' );
192-
# we need only same named hierarchies - that means to exclude elements that have : in their names
201+
# if pHier blank then we need only same named hierarchies - that means to exclude elements that have : in their names
193202
sMDX = Expand( '{FILTER( %sMDX1%, INSTR([%cDimDimensions%].CurrentMember.Name, '':'' ) = 0 )}' );
194203
Else;
195204
sMDX = '';
@@ -212,6 +221,7 @@ Else;
212221
EndIf;
213222
EndIf;
214223

224+
sMDX = Expand(' {EXCEPT( %sMDX%, {TM1FILTERBYPATTERN( {TM1SUBSETALL([%cDimDimensions%])}, "*:Leaves" )} )}');
215225
sMDXF = Expand( '{ORDER( %sMDX%, [%cDimDimensions%].CurrentMember.Name, ASC )}' );
216226

217227
### Create dimension:hierarchy subset
@@ -225,58 +235,95 @@ DatasourceNameForServer = cDimDimensions;
225235
DatasourceNameForClient = cDimDimensions;
226236
DataSourceType = 'SUBSET';
227237
DatasourceDimensionSubset = cTempSub;
228-
573,47
238+
239+
### END PROLOG
240+
573,80
241+
229242
#****Begin: Generated Statements***
230243
#****End: Generated Statements****
231244

232-
nDelimHier = SCAN( ':', vDimHier );
245+
# Get dimension & hierarchy from vDimHier
246+
nDelimHier = SCAN( ':', vDimHier );
233247
If ( nDelimHier <> 0 );
234-
sDim = SUBST( vDimHier, 1, nDelimHier - 1);
235-
sHier = SUBST( vDimHier, nDelimHier + 1, LONG( vDimHier ) - nDelimHier );
248+
sDim = SUBST( vDimHier, 1, nDelimHier - 1);
249+
sHier = SUBST( vDimHier, nDelimHier + 1, LONG( vDimHier ) - nDelimHier );
236250
Else;
237-
sDim = vDimHier;
238-
sHier = vDimHier;
251+
sDim = vDimHier;
252+
sHier = vDimHier;
239253
EndIf;
240254

241-
If ( sHier @= cHierLeaves );
242-
ItemSkip;
255+
If ( sHier @= cHierLeaves );
256+
ItemSkip;
243257
EndIf;
244258

245-
# Summary information printout
246-
If ( sDimPrev @<> sDim );
247-
If ( sDimPrev @<> '' & nElems <> 0 );
248-
sElems = NumberToString( nElems );
249-
If ( pLogOutput <> 0 );
250-
LogOutput( cMsgInfoLevel, Expand( 'Added [%sElems%] elements into [%cHierLeaves%] hierarchy of [%sDimPrev%] dimension.' ) );
251-
EndIf;
252-
nDimsChanged = nDimsChanged + 1;
253-
EndIf;
254-
nDims = nDims + 1;
255-
nElems = 0;
256-
sDimPrev = sDim;
259+
# Set check counters
260+
If( sDim @<> sDimPrev );
261+
nDims = nDims + 1;
257262
EndIf;
263+
nElems = 0;
264+
nLeaves = 0;
258265

266+
# Add leaves in hierarchy but (somehow) not in Leaves hierarchy to Leaves
259267
nElem = 1;
260268
nMaxElem = ElementCount( sDim, sHier );
261269
While ( nElem <= nMaxElem );
262-
sElem = ElementName( sDim, sHier, nElem );
263-
If ( ElementLevel( sDim, sHier, sElem ) = 0 & HierarchyExists( sDim, cHierLeaves ) <> 0 );
264-
If ( ElementIndex( sDim, cHierLeaves, sElem ) = 0 );
265-
sType = ElementType( sDim, sHier, sElem );
266-
HierarchyElementInsert( sDim, cHierLeaves, '', sElem, sType );
267-
nElems = nElems + 1;
268-
If ( pLogOutput > 1 );
269-
LogOutput( cMsgInfoLevel, Expand( 'Adding element [%sElem%] of [%sType%] type into [%cHierLeaves%], element was found in hierarchy [%sHier%].' ) );
270-
EndIf;
270+
sElem = ElementName( sDim, sHier, nElem );
271+
If ( ElementLevel( sDim, sHier, sElem ) = 0 & HierarchyExists( sDim, cHierLeaves ) = 1 );
272+
If ( ElementIndex( sDim, cHierLeaves, sElem ) = 0 & ElementType( sDim, sHier, sElem ) @= 'N' );
273+
HierarchyElementInsert( sDim, cHierLeaves, '', sElem, 'N' );
274+
nElems = nElems + 1;
275+
If ( pLogOutput <> 0 );
276+
LogOutput( cMsgInfoLevel, Expand( 'Adding element [%sElem%] of [%sType%] type into [%cHierLeaves%], element was found in hierarchy [%sHier%].' ) );
277+
EndIf;
278+
EndIf;
271279
EndIf;
272-
EndIf;
273-
nElem = nElem + 1;
280+
nElem = nElem + 1;
274281
End;
275282

276-
574,2
283+
# Add leaves not existing in hierarchy to the 'ORPHAN LEAVES' consolidation
284+
If( pReverse = 1 & HierarchyExists( sDim, cHierLeaves ) = 1 );
285+
nLeaf = 1;
286+
nMaxLeaves = ElementCount( sDim, cHierLeaves );
287+
While ( nLeaf <= nMaxLeaves );
288+
sLeaf = ElementName( sDim, cHierLeaves, nLeaf );
289+
If ( ElementIndex( sDim, sHier, sLeaf ) = 0 );
290+
HierarchyElementInsert( sDim, sHier, '', cRollupOrphan, 'C' );
291+
HierarchyElementInsert( sDim, sHier, '', sLeaf, 'N' );
292+
HierarchyElementComponentAdd( sDim, sHier, cRollupOrphan, sLeaf, 1 );
293+
nLeaves = nLeaves + 1;
294+
If ( pLogOutput <> 0 );
295+
LogOutput( cMsgInfoLevel, Expand( 'Adding leaf [%sLeaf%] into hierarchy [%sHier%].' ) );
296+
EndIf;
297+
EndIf;
298+
nLeaf = nLeaf + 1;
299+
End;
300+
EndIf;
301+
302+
# Summary information printout
303+
If( nElems > 0 );
304+
sElems = NumberToString( nElems );
305+
If ( pLogOutput <> 0 );
306+
LogOutput( cMsgInfoLevel, Expand( 'Added [%sElems%] elements from hierarchy [%sHier%] into [%cHierLeaves%] hierarchy of [%sDim%] dimension.' ) );
307+
EndIf;
308+
EndIf;
309+
If( nLeaves > 0 );
310+
sLeaves = NumberToString( nLeaves );
311+
If ( pLogOutput <> 0 );
312+
LogOutput( cMsgInfoLevel, Expand( 'Added [%sLeaves%] leaves into hierarchy [%sHier%] of [%sDim%] dimension.' ) );
313+
EndIf;
314+
EndIf;
315+
If( sDim @<> sDimPrev & (nElems + nLeaves) > 0 );
316+
nDimsChanged = nDimsChanged + 1;
317+
EndIf;
318+
sDimPrev = sDim;
319+
320+
### END METADATA
321+
574,4
322+
277323
#****Begin: Generated Statements***
278324
#****End: Generated Statements****
279-
575,38
325+
326+
575,37
280327
#****Begin: Generated Statements***
281328
#****End: Generated Statements****
282329

@@ -299,8 +346,8 @@ EndIf;
299346
If ( nDims <> 0 );
300347
sDims = NumberToString( nDims );
301348
sDimsChanged = NumberToString( nDimsChanged );
302-
If ( nDimsChanged <> 0 );
303-
sProcessAction = Expand( 'Modified [%sDimsChanged%] dimensions out of [%sDims%] matching.' );
349+
If ( nDimsChanged > 0 );
350+
sProcessAction = Expand( 'Modified [%sDimsChanged%] dimensions out of [%sDims%] matching the filter.' );
304351
Else;
305352
sProcessAction = Expand( 'Scanned [%sDims%] dimensions, all are ok.' );
306353
EndIf;
@@ -313,8 +360,7 @@ If( pLogoutput <> 0 );
313360
LogOutput( cMsgInfoLevel, Expand( sProcessAction ) );
314361
EndIf;
315362

316-
317-
363+
### END EPILOG
318364
576,
319365
930,0
320366
638,1

0 commit comments

Comments
 (0)