--------------------------------------------------------------------------------
-- ENABLLING LOGGING

BEGIN
SYS.DBMS_CUBE_LOG.ENABLE(NULL, NULL, 3, 'CUBE_BUILD_LOG');
END ;
/

--------------------------------------------------------------------------------
-- LOAD DIMENSIONS

BEGIN

EXECUTE IMMEDIATE ' ALTER SESSION ENABLE PARALLEL DML ' ;
EXECUTE IMMEDIATE ' ALTER TABLE CRM_DW_OWN.DIM_CONTACT_PARTNERSHIP PARALLEL 12 ' ;

  DBMS_CUBE.BUILD(
    '
  CRM_DW_OWN.CONTACT_PARTNERSHIPS_DIM USING
  (
    LOAD NO SYNCH,
    COMPILE SORT
  ),
  CRM_DW_OWN.FIRM_DIM USING
  (
    LOAD NO SYNCH,
    COMPILE SORT
  ),
  CRM_DW_OWN.OFFICE_DIM USING
  (
    LOAD NO SYNCH,
    COMPILE SORT
  ),
  CRM_DW_OWN.RETAILPRODUCT_DIM USING
  (
    LOAD NO SYNCH,
    COMPILE SORT
  ),
  CRM_DW_OWN.TERRITORY_2_DIM USING
  (
    LOAD NO SYNCH,
    COMPILE SORT
  ),
  CRM_DW_OWN.TERRITORY_3_DIM USING
  (
    LOAD NO SYNCH,
    COMPILE SORT
  ),
  CRM_DW_OWN.TERRITORY_4_DIM USING
  (
    LOAD NO SYNCH,
    COMPILE SORT
  ),
  CRM_DW_OWN.TERRITORY_5_DIM USING
  (
    LOAD NO SYNCH,
    COMPILE SORT
  ),
  CRM_DW_OWN.TERRITORY_DIM USING
  (
    LOAD NO SYNCH,
    COMPILE SORT
  )',
    'CCCCCCCCC', -- refresh method
false, -- refresh after errors
    2, -- parallelism
false, -- atomic refresh
false, -- automatic order
false  -- add dimensions
);

EXECUTE IMMEDIATE ' ALTER TABLE CRM_DW_OWN.DIM_CONTACT_PARTNERSHIP NOPARALLEL ' ;

END;
/

--------------------------------------------------------------------------------
-- LOAD TRANSCTION_CUBE

BEGIN

EXECUTE IMMEDIATE ' ALTER SESSION ENABLE PARALLEL DML ' ;
EXECUTE IMMEDIATE ' ALTER TABLE CRM_DW_OWN.FACT_TRANSACTIONS PARALLEL 8 ' ;

DBMS_CUBE.BUILD(
' CRM_DW_OWN.TRANSACTION_CUBE USING
  ( LOAD PRUNE,
    SOLVE
  )',
'S', -- refresh method
false, -- refresh after errors
6, -- parallelism
false, -- atomic refresh
true, -- automatic order
false -- add dimensions
);

EXECUTE IMMEDIATE ' ALTER TABLE CRM_DW_OWN.FACT_TRANSACTIONS NOPARALLEL ' ;

END;
/

--------------------------------------------------------------------------------
-- UPDATE BUILD_TYPE

UPDATE CRM_DW_OWN.BUILD_TYPE
SET BUILD_FLAG = 'T',
UPDATE_DT = SYSTIMESTAMP
WHERE (TARGET_NAME = 'CRM_DW_OWN.DIM_FIRM' AND BUILD_FLAG = 'F')
OR (TARGET_NAME = 'CRM_DW_OWN.DIM_OFFICE' AND BUILD_FLAG = 'F')
OR (TARGET_NAME = 'CRM_DW_OWN.DIM_CONTACT_PARTNERSHIP' AND BUILD_FLAG = 'F')
OR (TARGET_NAME = 'CRM_DW_OWN.FACT_TRANSACTIONS' AND BUILD_FLAG = 'F');

COMMIT ;
