Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.92 KB | None | 0 0
  1. create or replace PACKAGE BODY pck_transform IS
  2.  
  3. e_transformation EXCEPTION;
  4.  
  5. -- *********************************************
  6. -- * PUTS AN ERROR IN THE FACT TABLE OF ERRORS *
  7. -- *********************************************
  8. PROCEDURE error_log(p_screen_name t_tel_screen.screen_name%TYPE,
  9. p_hora_deteccao DATE,
  10. p_source_key t_tel_source.source_key%TYPE,
  11. p_iteration_key t_tel_iteration.iteration_key%TYPE,
  12. p_record_id t_tel_error.record_id%TYPE) IS
  13. v_date_key t_tel_date.date_key%TYPE;
  14. v_screen_key t_tel_screen.screen_key%TYPE;
  15. BEGIN
  16. -- obtém o id da dimensão «date» referente ao dia em que o erro foi detectado
  17. BEGIN
  18. SELECT date_key
  19. INTO v_date_key
  20. FROM t_tel_date
  21. WHERE date_full=TO_CHAR(p_hora_deteccao,'DD-MM-YYYY');
  22. EXCEPTION
  23. WHEN NO_DATA_FOUND THEN
  24. pck_log.write_log(' -- ERROR -- could not find date key from "t_tel_date" ['||sqlerrm||']');
  25. RAISE e_transformation;
  26. END;
  27.  
  28. BEGIN
  29. SELECT screen_key
  30. INTO v_screen_key
  31. FROM t_tel_screen
  32. WHERE UPPER(screen_name)=UPPER(p_screen_name);
  33. EXCEPTION
  34. WHEN NO_DATA_FOUND THEN
  35. pck_log.write_log(' -- ERROR -- could not find screen key from "t_tel_screen" ['||sqlerrm||']');
  36. RAISE e_transformation;
  37. END;
  38.  
  39. INSERT INTO t_tel_error (date_key,screen_key,source_key,iteration_key, record_id) VALUES (v_date_key,v_screen_key,p_source_key,p_iteration_key, p_record_id);
  40. EXCEPTION
  41. WHEN OTHERS THEN
  42. pck_log.write_log(' -- ERROR -- could not write quality problem to "t_tel_error" fact table ['||sqlerrm||']');
  43. RAISE e_transformation;
  44. END;
  45.  
  46.  
  47.  
  48. -- *******************************************
  49. -- * DUPLICATES THE LAST SCHEDULED ITERATION *
  50. -- *******************************************
  51. PROCEDURE duplicate_last_iteration(p_start_date t_tel_iteration.iteration_start_date%TYPE) IS
  52. v_last_iteration_key t_tel_iteration.iteration_key%TYPE;
  53. v_new_iteration_key t_tel_iteration.iteration_key%TYPE;
  54.  
  55. CURSOR c_scheduled_screens(p_iteration_key t_tel_iteration.iteration_key%TYPE) IS
  56. SELECT s.screen_key as screen_key,screen_name,screen_order, s.source_key
  57. FROM t_tel_schedule s, t_tel_screen
  58. WHERE iteration_key=p_iteration_key AND
  59. s.screen_key = t_tel_screen.screen_key;
  60. BEGIN
  61. pck_log.write_log(' Creating new iteration by duplicating the previous one');
  62.  
  63. -- FIND THE LAST ITERATIONS'S KEY
  64. BEGIN
  65. SELECT MAX(iteration_key)
  66. INTO v_last_iteration_key
  67. FROM t_tel_iteration;
  68. EXCEPTION
  69. WHEN NO_DATA_FOUND THEN
  70. pck_log.write_log(' -- ERROR -- could not find iteration key ['||sqlerrm||']');
  71. RAISE e_transformation;
  72. END;
  73.  
  74. INSERT INTO t_tel_iteration(iteration_start_date) VALUES (p_start_date) RETURNING iteration_key INTO v_new_iteration_key;
  75. FOR rec IN c_scheduled_screens(v_last_iteration_key) LOOP
  76. -- schedule screen
  77. INSERT INTO t_tel_schedule(screen_key,iteration_key,source_key,screen_order)
  78. VALUES (rec.screen_key,v_new_iteration_key,rec.source_key,rec.screen_order);
  79. END LOOP;
  80. pck_log.write_log(' Done!');
  81. EXCEPTION
  82. WHEN NO_DATA_FOUND THEN
  83. pck_log.write_log(' -- ERROR -- previous iteration has no screens to reschedule');
  84. RAISE e_transformation;
  85. WHEN OTHERS THEN
  86. pck_log.write_uncomplete_task_msg;
  87. RAISE e_transformation;
  88. END;
  89.  
  90.  
  91. PROCEDURE screen_product_dimensions (p_iteration_key t_tel_iteration.iteration_key%TYPE,
  92. p_source_key t_tel_source.source_key%TYPE,
  93. p_screen_order t_tel_schedule.screen_order%TYPE) IS
  94. -- SEARCH FOR EXTRACTED PRODUCTS CONTAINING PROBLEMS
  95. CURSOR products_with_problems IS
  96. SELECT rowid
  97. FROM t_data_products
  98. WHERE rejected_by_screen='0'
  99. AND (((width IS NULL OR height IS NULL OR depth IS NULL) AND UPPER(pack_type) IN (SELECT pack_type
  100. FROM t_lookup_pack_dimensions
  101. WHERE has_dimensions='1'))
  102. OR ((width>=0 OR height>=0 OR depth>=0 AND UPPER(pack_type) IN (SELECT pack_type
  103. FROM t_lookup_pack_dimensions
  104. WHERE has_dimensions='0'))));
  105. i PLS_INTEGER:=0;
  106. v_screen_name VARCHAR2(30):='screen_product_dimensions';
  107. BEGIN
  108. pck_log.write_log(' Starting SCREEN ["'||UPPER(v_screen_name)||'"] with order #'||p_screen_order||'');
  109. FOR rec IN products_with_problems LOOP
  110. -- RECORDS THE ERROR IN THE TRANSFORMATION ERROR LOGGER BUT DOES * NOT REJECT THE LINE *
  111. error_log(v_screen_name,SYSDATE,p_source_key,p_iteration_key,rec.rowid);
  112. i:=i+1;
  113. END LOOP;
  114. pck_log.write_log(' Data quality problems in '|| i || ' row(s).',' Done!');
  115. EXCEPTION
  116. WHEN NO_DATA_FOUND THEN
  117. pck_log.write_log(' No data quality problems found.',' Done!');
  118. WHEN OTHERS THEN
  119. pck_log.write_uncomplete_task_msg;
  120. RAISE e_transformation;
  121. END;
  122.  
  123.  
  124. -- *************************************************************************************
  125. -- * GOAL: detect and reject packed products with an empty liquid weight *
  126. -- * QUALITY CRITERIUM: "Completude" *
  127. -- * PARAMETERS: *
  128. -- * p_iteration_key: key of the iteration in which the screen will be run *
  129. -- * p_source_key: key of the source system related to the screen's execution *
  130. -- * p_screen_order: order number in which the screen is to be executed *
  131. -- *************************************************************************************
  132. PROCEDURE screen_null_liq_weight (p_iteration_key t_tel_iteration.iteration_key%TYPE,
  133. p_source_key t_tel_source.source_key%TYPE,
  134. p_screen_order t_tel_schedule.screen_order%TYPE) IS
  135. CURSOR products_with_problems IS
  136. SELECT rowid
  137. FROM t_data_products
  138. WHERE rejected_by_screen='0' AND ((pack_type is not null AND liq_weight is null) OR ( pack_type is null AND liq_weight is not null));
  139.  
  140. i PLS_INTEGER:=0;
  141. v_screen_name VARCHAR2(30):='screen_null_liq_weight';
  142. BEGIN
  143. pck_log.write_log(' Starting SCREEN ["'||UPPER(v_screen_name)||'"] with order #'||p_screen_order||'');
  144. FOR rec IN products_with_problems LOOP
  145. UPDATE t_data_products SET rejected_by_screen = '1' WHERE rowid = rec.rowid;
  146. error_log(v_screen_name,SYSDATE,p_source_key,p_iteration_key,rec.rowid);
  147. i:=i+1;
  148. END LOOP;
  149. pck_log.write_log(' Data quality problems in '|| i || ' row(s).',' Done!');
  150. EXCEPTION
  151. WHEN NO_DATA_FOUND THEN
  152. pck_log.write_log(' No data quality problems found.',' Done!');
  153. WHEN OTHERS THEN
  154. pck_log.write_uncomplete_task_msg;
  155. RAISE e_transformation;
  156. END;
  157.  
  158.  
  159.  
  160.  
  161. -- *************************************************************************************
  162. -- * GOAL: detect incorrect data in products *
  163. -- * QUALITY CRITERIUM: "Correção" *
  164. -- * PARAMETERS: *
  165. -- * p_iteration_key: key of the iteration in which the screen will be run *
  166. -- * p_source_key: key of the source system related to the screen's execution *
  167. -- * p_screen_order: order number in which the screen is to be executed *
  168. -- *************************************************************************************
  169. PROCEDURE screen_incorrect_brands ( p_iteration_key t_tel_iteration.iteration_key%TYPE,
  170. p_source_key t_tel_source.source_key%TYPE,
  171. p_screen_order t_tel_schedule.screen_order%TYPE) IS
  172. CURSOR products_with_problems IS
  173. SELECT rowid
  174. FROM t_data_products
  175. WHERE rejected_by_screen='0' AND BRAND IN ( SELECT brand_wrong FROM t_lookup_brands );
  176.  
  177. i PLS_INTEGER:=0;
  178. v_screen_name VARCHAR2(30):='screen_incorrect_brands';
  179. BEGIN
  180. pck_log.write_log(' Starting SCREEN ["'||UPPER(v_screen_name)||'"] with order #'||p_screen_order||'');
  181.  
  182. FOR rec IN products_with_problems LOOP
  183. UPDATE t_data_products SET rejected_by_screen = '1' WHERE rowid = rec.rowid;
  184. error_log(v_screen_name,SYSDATE,p_source_key,p_iteration_key,rec.rowid);
  185. i:=i+1;
  186. END LOOP;
  187.  
  188. pck_log.write_log(' Data quality problems in '|| i || ' row(s).',' Done!');
  189. EXCEPTION
  190. WHEN NO_DATA_FOUND THEN
  191. pck_log.write_log(' No data quality problems found.',' Done!');
  192. WHEN OTHERS THEN
  193. pck_log.write_uncomplete_task_msg;
  194. RAISE e_transformation;
  195. END;
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207. -- ####################### TRANSFORMATION ROUTINES #######################
  208.  
  209. PROCEDURE transform_products IS
  210. BEGIN
  211. pck_log.write_log(' Transforming data ["TRANSFORM_PRODUCTS"]');
  212.  
  213. INSERT INTO t_clean_products(id,name,brand,pack_size,pack_type,diet_type,liq_weight,category_name)
  214. SELECT prod.id,prod.name,brand,height||'x'||width||'x'||depth,pack_type,cal.type,liq_weight,categ.name
  215. FROM t_data_products prod, t_lookup_calories cal, t_data_categories categ
  216. WHERE categ.rejected_by_screen='0'
  217. AND prod.rejected_by_screen='0'
  218. AND calories_100g>=cal.min_calories_100g
  219. AND calories_100g<=cal.max_calories_100g
  220. AND categ.id=prod.category_id;
  221.  
  222. -- SOMETHING was MISSING
  223.  
  224. UPDATE t_clean_products p
  225. SET brand = (SELECT brand_transformed FROM t_lookup_brands WHERE p.brand = brand_wrong)
  226. WHERE BRAND IN (SELECT brand_wrong FROM t_lookup_brands);
  227.  
  228. pck_log.write_log(' Done!');
  229. EXCEPTION
  230. WHEN NO_DATA_FOUND THEN
  231. pck_log.write_log(' Found no lines to transform',' Done!');
  232. WHEN OTHERS THEN
  233. pck_log.write_uncomplete_task_msg;
  234. RAISE e_transformation;
  235. END;
  236.  
  237.  
  238.  
  239. -- **********************************************************
  240. -- * TRANSFORMATION OF STORES ACCORDING TO LOGICAL DATA MAP *
  241. -- **********************************************************
  242. PROCEDURE transform_stores IS
  243. BEGIN
  244. pck_log.write_log(' Transforming data ["TRANSFORM_STORES"]');
  245.  
  246. INSERT INTO t_clean_stores(name,reference,address,zip_code,location,district,telephones,fax,status,manager_name,manager_since)
  247. SELECT name,s.reference,CASE building WHEN '-' THEN NULL ELSE building||' - ' END || address||' / '||zip_code||', '||location,zip_code,location,district,SUBSTR(REPLACE(REPLACE(telephones,'.',''),' ',''),1,9),fax,CASE WHEN closure_date IS NULL THEN 'ACTIVE' ELSE 'INACTIVE' END, manager_name,manager_since
  248. FROM (SELECT name,reference,building,address,zip_code,location,district,telephones,fax,closure_date
  249. FROM t_data_stores_new
  250. WHERE rejected_by_screen='0'
  251. MINUS
  252. SELECT name,reference,building,address,zip_code,location,district,telephones,fax,closure_date
  253. FROM t_data_stores_old) s, t_data_managers_new d
  254. WHERE s.reference=d.reference AND
  255. d.rejected_by_screen='0';
  256.  
  257. pck_log.write_log(' Done!');
  258. EXCEPTION
  259. WHEN NO_DATA_FOUND THEN
  260. pck_log.write_log(' Found no lines to transform',' Done!');
  261. WHEN OTHERS THEN
  262. pck_log.write_uncomplete_task_msg;
  263. RAISE e_transformation;
  264. END;
  265.  
  266.  
  267. -- *********************************************************
  268. -- * TRANSFORMATION OF SALES ACCORDING TO LOGICAL DATA MAP *
  269. -- *********************************************************
  270. PROCEDURE transform_sales IS
  271. BEGIN
  272. pck_log.write_log(' Transforming data ["TRANSFORM_SALES"]');
  273.  
  274. INSERT INTO t_clean_sales(id,sale_date,store_id)
  275. SELECT id,sale_date,store_id
  276. FROM t_data_sales
  277. WHERE rejected_by_screen='0';
  278.  
  279. pck_log.write_log(' Done!');
  280. EXCEPTION
  281. WHEN NO_DATA_FOUND THEN
  282. pck_log.write_log(' Found no lines to transform',' Done!');
  283. WHEN OTHERS THEN
  284. pck_log.write_uncomplete_task_msg;
  285. RAISE e_transformation;
  286. END;
  287.  
  288.  
  289.  
  290.  
  291. -- *****************************************************************
  292. -- * TRANSFORMATION OF LINES OF SALE ACCORDING TO LOGICAL DATA MAP *
  293. -- *****************************************************************
  294. PROCEDURE transform_linesofsale IS
  295. BEGIN
  296. pck_log.write_log(' Transforming data ["TRANSFORM_LINESOFSALE"]');
  297.  
  298. INSERT INTO t_clean_linesofsale(id,sale_id,product_id,promo_id,quantity,ammount_paid,line_date)
  299. SELECT los.id,los.sale_id,los.product_id,losp.promo_id,quantity,ammount_paid, los.line_date
  300. FROM t_data_linesofsale los LEFT JOIN (SELECT line_id,promo_id
  301. FROM t_data_linesofsalepromotions
  302. WHERE rejected_by_screen='0') losp ON los.id=losp.line_id, t_data_sales
  303. WHERE los.rejected_by_screen='0' AND
  304. t_data_sales.id=los.sale_id;
  305.  
  306. pck_log.write_log(' Done!');
  307. EXCEPTION
  308. WHEN NO_DATA_FOUND THEN
  309. pck_log.write_log(' Found no lines to transform',' Done!');
  310. WHEN OTHERS THEN
  311. pck_log.write_uncomplete_task_msg;
  312. RAISE e_transformation;
  313. END;
  314.  
  315.  
  316.  
  317. -- *****************************************************************
  318. -- * TRANSFORMATION OF DATA PROMOTIONS ACCORDING TO LOGICAL DATA MAP *
  319. -- *****************************************************************
  320. PROCEDURE transform_promotions IS
  321. BEGIN
  322. pck_log.write_log(' Transforming data ["TRANSFORM_PROMOTIONS"]');
  323.  
  324. INSERT INTO t_clean_promotions(id, name, start_date, end_date, reduction, on_street, on_tv)
  325. SELECT id, name, start_date, end_date, reduction, (CASE WHEN on_outdoor='1' THEN 'YES' ELSE 'NO' END), (CASE WHEN on_tv = '1' THEN 'YES' ELSE 'NO' END)
  326. FROM t_data_promotions
  327. WHERE rejected_by_screen='0';
  328.  
  329. pck_log.write_log(' Done!');
  330. EXCEPTION
  331. WHEN NO_DATA_FOUND THEN
  332. pck_log.write_log(' Found no lines to transform',' Done!');
  333. WHEN OTHERS THEN
  334. pck_log.write_uncomplete_task_msg;
  335. RAISE e_transformation;
  336. END;
  337.  
  338.  
  339. -- *****************************************************************************************************
  340. -- * MAIN *
  341. -- * *
  342. -- * EXECUTE THE TRANSFORMATION PROCESS *
  343. -- * IN *
  344. -- * p_duplicate_last_iteration: TRUE=duplicate last iteration and its schedule (FOR TESTS ONLY!) *
  345. -- *****************************************************************************************************
  346. PROCEDURE main (p_duplicate_last_iteration BOOLEAN) IS
  347.  
  348. -- GET ALL SCHEDULED SCREENS
  349. cursor scheduled_screens_cursor(p_iteration_key t_tel_iteration.iteration_key%TYPE) IS
  350. SELECT UPPER(screen_name) screen_name,source_key,screen_order
  351. FROM t_tel_schedule, t_tel_screen
  352. WHERE iteration_key=p_iteration_key AND
  353. t_tel_schedule.screen_key=t_tel_screen.screen_key;
  354.  
  355. v_iteration_key t_tel_iteration.iteration_key%TYPE;
  356. v_sql VARCHAR(2000);
  357. BEGIN
  358. pck_log.write_log(' ','***** TRANSFORM TRANSFORM TRANSFORM TRANSFORM TRANSFORM TRANSFORM *****'); -- DUPLICATES THE LAST ITERATION AND THE CORRESPONDING SCREEN SCHEDULE
  359. -- DUPLICATES THE LAST ITERATION WITH THEN CORRESPONDING SCHEDULE
  360. IF p_duplicate_last_iteration THEN
  361. duplicate_last_iteration(SYSDATE);
  362. END IF;
  363.  
  364. -- CLEAN ALL _clean TABLES
  365. pck_log.write_log(' Deleting old _clean tables');
  366. DELETE FROM t_clean_products;
  367. DELETE FROM t_clean_linesofsale;
  368. DELETE FROM t_clean_stores;
  369. DELETE FROM t_clean_promotions;
  370. DELETE FROM t_clean_sales;
  371. pck_log.write_log(' Done!');
  372.  
  373. -- FIND THE MOST RECENTLY SCHEDULED ITERATION
  374. BEGIN
  375. v_sql := 'SELECT MAX(iteration_key) FROM T_TEL_ITERATION';
  376. EXECUTE IMMEDIATE v_sql INTO v_iteration_key;
  377. EXCEPTION
  378. WHEN OTHERS THEN
  379. RAISE e_transformation;
  380. END;
  381.  
  382. -- RUN ALL SCHEDULED SCREENS
  383. FOR rec IN scheduled_screens_cursor(v_iteration_key) LOOP
  384.  
  385. v_sql := 'BEGIN pck_transform.'||rec.screen_name||'('||v_iteration_key||','||rec.source_key||','||rec.screen_order||'); END;';
  386. EXECUTE IMMEDIATE v_sql;
  387.  
  388. END LOOP;
  389.  
  390.  
  391. pck_log.write_log(' All screens have been run.');
  392. -- EXECUTE THE TRANSFORMATION ROUTINES
  393. transform_products;
  394. transform_promotions;
  395. transform_stores;
  396. transform_sales;
  397. transform_linesofsale;
  398.  
  399.  
  400. COMMIT;
  401. pck_log.write_log(' All transformed data commited to database.');
  402. EXCEPTION
  403. WHEN e_transformation THEN
  404. pck_log.write_halt_msg;
  405. ROLLBACK;
  406. WHEN OTHERS THEN
  407. ROLLBACK;
  408. pck_log.write_uncomplete_task_msg;
  409. pck_log.write_halt_msg;
  410. END;
  411.  
  412. end pck_transform;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement