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

DEFINE thisfile = 'UBRH#55068_3.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)
SELECT m.mld_melding_key,
       m.mld_melding_externnr,
       SUBSTR (behandeling, 1, 4000) OMS_1,
       SUBSTR (behandeling, 4001, 8000) OMS_2,
       SUBSTR (behandeling, 8001, 12000) OMS_3     
       FROM mld_melding m , ubrh_imp_mld_omschrijvingen oms
     WHERE m.mld_melding_externnr = oms.meldingnummer;

COMMIT; 
     
-- Dan elk blok van 40000 als aparte notitie importeren     
INSERT INTO mld_melding_note (mld_melding_key, mld_melding_note_omschrijving, mld_melding_note_aanmaak)
   SELECT mld_melding_key, 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, oms_2, trunc (SYSDATE) + 1 / 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, oms_3, trunc (SYSDATE) + 2 / 24 / 60
     FROM ubrh_mld_notes n
    WHERE oms_3 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
