--
-- $Id$
--
-- Converteren Topdesk opmerkingen naar mld notes

DEFINE thisfile = 'UBRH#55068_4.SQL'
DEFINE dbuser   = '^UBRH'

SET ECHO ON
SET DEFINE ON
COLUMN fcltlogfile NEW_VALUE fcltlogfile NOPRINT;
WHENEVER SQLERROR EXIT;
SELECT adm.scriptspoolfile('&dbuser', '&thisfile') AS fcltlogfile FROM DUAL;
WHENEVER SQLERROR CONTINUE;
SPOOL &fcltlogfile
SET DEFINE OFF

------ payload begin ------

--- Eerst de Topdesk notities opknippen in blokken van max. 4000 karakters
INSERT INTO UBRH_MLD_NOTES (mld_melding_key, mld_externnr, OMS_1, OMS_2, OMS_3, OMS_4, OMS_5, OMS_6, OMS_7, OMS_8, OMS_9
, LOMS_1, LOMS_2, LOMS_3, LOMS_4, LOMS_5, LOMS_6, LOMS_7, LOMS_8, LOMS_9)
SELECT m.mld_melding_key,
       m.mld_melding_externnr,
       SUBSTR (behandeling, 1, 3990) OMS_1,
       SUBSTR (behandeling, 3991, 3990) OMS_2,
       SUBSTR (behandeling, 7981, 3990) OMS_3,
       SUBSTR (behandeling, 11971, 3990) OMS_4,
       SUBSTR (behandeling, 15961, 3990) OMS_5,
       SUBSTR (behandeling, 19951, 3990) OMS_6,
       SUBSTR (behandeling, 23941, 3990) OMS_7,
       SUBSTR (behandeling, 27931, 3990) OMS_8,
       SUBSTR (behandeling, 31921, 3990) OMS_9,
       SUBSTR (lange_omschrijving, 1, 3990) LOMS_1,
       SUBSTR (lange_omschrijving, 3991, 3990) LOMS_2,
       SUBSTR (lange_omschrijving, 7981, 3990) LOMS_3,
       SUBSTR (lange_omschrijving, 11971, 3990) LOMS_4,
       SUBSTR (lange_omschrijving, 15961, 3990) LOMS_5,
       SUBSTR (lange_omschrijving, 19951, 3990) LOMS_6,
       SUBSTR (lange_omschrijving, 23941, 3990) LOMS_7,
       SUBSTR (lange_omschrijving, 27931, 3990) LOMS_8,
       SUBSTR (lange_omschrijving, 31921, 3990) LOMS_9
       FROM mld_melding m , ubrh_imp_mld_omschrijvingen oms
     WHERE m.mld_melding_externnr = oms.meldingnummer;

COMMIT;

-- Dan elk blok van 3990 als aparte notitie importeren
INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'V1:' || loms_1, trunc (SYSDATE)
     FROM ubrh_mld_notes n
    WHERE loms_1 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'V2:' || loms_2, trunc (SYSDATE) + 1 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE loms_2 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'V3:' || loms_3, trunc (SYSDATE) + 2 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE loms_3 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'V4:' || loms_4, trunc (SYSDATE) + 3 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE loms_4 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'V5:' || loms_5, trunc (SYSDATE) + 4 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE loms_5 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'V6:' || loms_6, trunc (SYSDATE) + 5 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE loms_6 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'V7:' || loms_7, trunc (SYSDATE) + 6 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE loms_7 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'V8:' || loms_8, trunc (SYSDATE) + 7 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE loms_8 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'V9:' || loms_9, trunc (SYSDATE) + 8 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE loms_9 IS NOT NULL;



-- Dan elk blok van 3990 als aparte notitie importeren
INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'A1:' || oms_1, trunc (SYSDATE)
     FROM ubrh_mld_notes n
    WHERE oms_1 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'A2:' || oms_2, trunc (SYSDATE) + 11 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE oms_2 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'A3:' || oms_3, trunc (SYSDATE) + 12 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE oms_3 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'A4:' || oms_4, trunc (SYSDATE) + 13 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE oms_4 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'A5:' || oms_5, trunc (SYSDATE) + 14 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE oms_5 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'A6:' || oms_6, trunc (SYSDATE) + 15 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE oms_6 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'A7:' || oms_7, trunc (SYSDATE) + 16 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE oms_7 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'A8:' || oms_8, trunc (SYSDATE) + 17 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE oms_8 IS NOT NULL;

INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 'A9:' || oms_9, trunc (SYSDATE) + 18 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE oms_9 IS NOT NULL;

COMMIT;

------ payload end   ------

SET DEFINE OFF
BEGIN adm.systrackscriptId ('$Id$', 1); END;
/

COMMIT;
SET ECHO OFF
SPOOL OFF
SET DEFINE ON
PROMPT Logfile of this upgrade is: &fcltlogfile
