SET ECHO ON
SPOOL xAKZA37.lst
SET DEFINE OFF;

-- Importfuncties voor inlezen appointments en opslaan Exchange syncstate
-- fac_functie WEB_PRSSYS = 203
INSERT INTO fac_import_app
   (fac_import_app_code, fac_import_app_oms, fac_import_app_xsl, fac_functie_key,
    fac_import_app_folder, fac_import_app_files, fac_import_app_prefix, fac_import_app_action)
 VALUES
   ('EXCHANGE', 'EXCHANGE-import', 'import/exchange.xsl', 203,
    './xml', 'calitem*.xml', 'AKZA', 'ORACLE');

INSERT INTO fac_import_app
   (fac_import_app_code, fac_import_app_oms, fac_import_app_xsl, fac_functie_key,
    fac_import_app_folder, fac_import_app_files, fac_import_app_action)
 VALUES
   ('EXCHSYNC', 'EXCHANGE-sync', 'import/exchange.xsl', 203,
    './xml', 'sync*.xml', 'ORACLE');

-- Importtabel
CREATE TABLE akza_imp_exchange (
   room_id        VARCHAR(200),
   subject        VARCHAR(200),
   starttime      DATE,
   endtime        DATE,
   organizer      VARCHAR(200),
   att_mail       VARCHAR(200),
   att_name       VARCHAR(200),
   modifier       VARCHAR(1),
   appt_id        VARCHAR(500),
   recur_id       VARCHAR(500),
   seq_nr         NUMBER(10),
   fac_import_key NUMBER(10),
   gelukt         NUMBER(1)
);

-- Last-minute wijzigingen op DB27
ALTER TABLE res_rsv_ruimte MODIFY (res_rsv_ruimte_externnr VARCHAR2(2048) );
CREATE OR REPLACE PROCEDURE fac_import_exchsync (p_import_key IN NUMBER)
AS
   v_newline          fac_imp_file.fac_imp_file_line%TYPE;                             -- Input line
   v_fielddelimitor   VARCHAR2 (1);
   v_errormsg         VARCHAR2 (200);
   oracle_err_mes     VARCHAR2 (200);
   header_is_valid    NUMBER;
   --
   v_res_ruimte_key   res_ruimte.res_ruimte_key%TYPE;
   v_syncstate        res_ruimte.res_ruimte_syncstate%TYPE;

   CURSOR c1
   IS
        SELECT *
          FROM fac_imp_file
         WHERE fac_import_key = p_import_key
      ORDER BY fac_imp_file_index;

   rec1               c1%ROWTYPE;
BEGIN
   v_fielddelimitor := ';';
   header_is_valid := 0;

   -- Clear old importdata: we kunnen wel zonder eigen tabel
   SELECT fac_import_refkey
     INTO v_res_ruimte_key
     FROM fac_import
    WHERE fac_import_key = p_import_key;

   FOR rec1 IN c1 LOOP
      BEGIN
         v_newline := rec1.fac_imp_file_line;
         fac.imp_getfield (v_newline, v_fielddelimitor, v_syncstate);

         IF header_is_valid = 0
         THEN
            IF UPPER (TRIM (v_syncstate)) LIKE '%SYNCSTATE%'
            THEN
               header_is_valid := 1;
               DBMS_OUTPUT.put_line ('hoeie header');
            END IF;
         ELSE
            v_errormsg := 'Fout bij update van syncstate';
            DBMS_OUTPUT.put_line ('echte data');

            UPDATE res_ruimte
               SET res_ruimte_syncstate = TRIM(v_syncstate)
             WHERE res_ruimte_key = v_res_ruimte_key AND res_ruimte_extern_id IS NOT NULL;
         END IF;
      END;
   END LOOP;
EXCEPTION
   WHEN OTHERS
   THEN
      oracle_err_mes := SUBSTR (SQLERRM, 1, 1000);
      v_errormsg := oracle_err_mes;
      fac.imp_writelog (p_import_key,
                        'E',
                        v_errormsg,
                        'res_ruimte_key: ' || p_import_key);
END;
/

-- Notificatie(job) naar bezoekers
INSERT INTO fac_srtnotificatie (
   fac_srtnotificatie_code,
   fac_srtnotificatie_mode,
   fac_srtnotificatie_oms,
   fac_srtnotificatie_xmlnode,
   fac_srtnotificatie_delay)
VALUES (
   'CUST04',
   2,
   'Welcome to the AkzoNobel Meeting Center Amsterdam',
   'afspraak',
   0);
INSERT INTO fac_notificatie_job (
   fac_notificatie_job_view,
   fac_notificatie_job_oms,
   fac_notificatie_job_mode,
   fac_notificatie_job_interval,
   fac_notificatie_job_nextrun,
   fac_notificatie_job_flags)
VALUES (
   'AKZA_V_NOTI_BEZOEKERS',
   'Welkomstmail naar bezoekers AMB',
   2,
   24,
   TO_DATE('1-1-2100', 'DD-MM-YYYY'),
   1);

-- Al eerder handmatig aangemaakte reserveringen in AMB verwijderen
UPDATE res_rsv_ruimte
   SET res_rsv_ruimte_verwijder = SYSDATE
 WHERE res_rsv_ruimte_key IN (
         SELECT rr.res_rsv_ruimte_key
           FROM res_rsv_ruimte rr, res_ruimte_opstelling ro,
                res_ruimte r, res_alg_ruimte rar,
                alg_v_aanwezigruimte ar, alg_verdieping v, alg_gebouw g
          WHERE ro.res_ruimte_opstel_key = rr.res_ruimte_opstel_key
            AND r.res_ruimte_key = ro.res_ruimte_key
            AND rar.res_ruimte_key = r.res_ruimte_key
            AND rar.res_alg_ruimte_verwijder IS NULL
            AND ar.alg_ruimte_key = rar.alg_ruimte_key
            AND v.alg_verdieping_key = ar.alg_verdieping_key
            AND g.alg_gebouw_key = v.alg_gebouw_key
            AND g.alg_locatie_key = 201
            AND rr.res_rsv_ruimte_verwijder IS NULL
            AND rr.res_rsv_ruimte_aanmaak < SYSDATE-1);

BEGIN adm.systrackscriptId('$Id$', 0); END;
/
COMMIT;
SPOOL OFF

@akza.sql
@akza_exchange.sql