*** IMPORT DATA

update ips_voyage_members set vsl_id = null where id in (
select vm.id from IPS_VOYAGE_MEMBERS vm left join ips_vessels vs on vm.vsl_id = vs.id
where vs.id is null)

*** IPS_VOYAGE_MEMBERS
SELECT IPS_VOYAGE_MEMBERS
REINDEX
SELECT ID, COUNT(*) AS NR FROM IPS_VOYAGE_MEMBERS GROUP BY ID HAVING COUNT(*) > 1 INTO CURSOR TT
SELECT TT
SCAN
	SELECT IPS_VOYAGE_MEMBERS
	IF SEEK(TT.ID,'IPS_VOYAGE_MEMBERS',"ID")
		DELETE
	ENDIF 
ENDSCAN 
SELECT ips_voyage_members
COPY TO e:\voyage_members.csv TYPE csv

*** IPS_CARGOES
SELECT IPS_CARGOES
REINDEX
SELECT ID, COUNT(*) AS NR FROM IPS_CARGOES GROUP BY ID HAVING COUNT(*) > 1 INTO CURSOR TT
SELECT TT
SCAN
	SELECT IPS_CARGOES
	IF SEEK(TT.ID,'IPS_CARGOES',"ID")
		DELETE
	ENDIF 
ENDSCAN 

UPDATE IPS_CARGOES SET bill_date = {//::} WHERE LEFT(TTOC(bill_date,1),4) = '0000'
UPDATE IPS_CARGOES SET BILL_NR = STRTRAN(BILL_NR, '"', '') WHERE !EMPTY(NVL(BILL_NR, ''))

select id, vms_id, gds_id, ballast, container, source, cty_id, destinatio, quantity, unit, bill_nr, CAST(IIF(EMPTY(NVL(bill_date,'//::')), '', TTOC(bill_date,1)) as C(20)) as bill_date, delivery from ips_cargoes INTO CURSOR tt
SELECT TT
COPY TO e:\cargoes.csv TYPE csv

**** IPS_VESSELS
SELECT ips_vessels
REINDEX

SELECT ID, COUNT(*) AS NR FROM IPS_VESSELS GROUP BY ID HAVING COUNT(*) > 1 INTO CURSOR TT
SELECT TT
SCAN
	SELECT IPS_VESSELS
	IF SEEK(TT.ID,'IPS_VESSELS',"ID")
		DELETE
	ENDIF 
ENDSCAN 

UPDATE IPS_vessels SET registrati = STRTRAN(registrati, '"', '') WHERE !EMPTY(NVL(registrati, ''))
UPDATE IPS_vessels SET name = STRTRAN(name, '"', '') WHERE !EMPTY(NVL(name, ''))
SELECT ips_vessels 
COPY TO e:\vessels.csv TYPE csv

**** IPS_VOYAGE_LOCKS
SELECT ips_voyage_locks
REINDEX

SELECT ID, COUNT(*) AS NR FROM ips_voyage_locks GROUP BY ID HAVING COUNT(*) > 1 INTO CURSOR TT
SELECT TT
SCAN
	SELECT ips_voyage_locks
	IF SEEK(TT.ID,'ips_voyage_locks',"ID")
		DELETE
	ENDIF 
ENDSCAN 

UPDATE ips_voyage_locks SET date = {//::} WHERE LEFT(TTOC(date,1),4) = '0000'

SELECT id, vye_id, CAST(IIF(EMPTY(NVL(date,'//::')), '', TTOC(date,1)) as C(20)) as date, rpt_id, op_min, op_prep, id_lock, lock_name FROM ips_voyage_locks INTO CURSOR tt
SELECT tt
COPY TO e:\voyage_locks.csv TYPE csv


**** IPS_VOYAGES
SELECT ips_voyages
REINDEX

SELECT ID, COUNT(*) AS NR FROM ips_voyages GROUP BY ID HAVING COUNT(*) > 1 INTO CURSOR TT
SELECT TT
SCAN
	SELECT ips_voyages
	IF SEEK(TT.ID,'ips_voyages',"ID")
		DELETE
	ENDIF 
ENDSCAN 

UPDATE ips_voyages SET generation = {//::} WHERE LEFT(TTOC(generation,1),4) = '0000'
UPDATE ips_voyages SET notificati = {//::} WHERE LEFT(TTOC(notificati,1),4) = '0000'
UPDATE ips_voyages SET convoy_nam = STRTRAN(convoy_nam, '"', '') WHERE !EMPTY(NVL(convoy_nam, ''))

select id, CAST(IIF(EMPTY(NVL(generation,'//::')), '', TTOC(generation,1)) as C(20)) as generation, ctt_id, vms_id, convoy_nam, declaratio, CAST(IIF(EMPTY(NVL(notificati,'//::')), '', TTOC(notificati,1)) as C(20)) as notificati, notificat2, origin, destinatio, state from ips_voyages INTO CURSOR tt
SELECT tt
COPY TO e:\voyages.csv TYPE csv

*** IPS_BERTHINGS
SELECT IPS_BERTHINGS
REINDEX
SELECT ID, COUNT(*) AS NR FROM IPS_BERTHINGS GROUP BY ID HAVING COUNT(*) > 1 INTO CURSOR TT
SELECT TT
SCAN
	lnDuplicate = nr
	FOR lnDuplicat = 1 TO lnDuplicate - 1
		SELECT IPS_BERTHINGS
		IF SEEK(TT.ID,'IPS_BERTHINGS',"ID")
			DELETE
		ENDIF 
	ENDFOR 
ENDSCAN 

UPDATE IPS_BERTHINGS SET arrival_ti = {//::} WHERE LEFT(TTOC(arrival_ti,1),4) = '0000'
UPDATE IPS_BERTHINGS SET departure_ = {//::} WHERE LEFT(TTOC(departure_,1),4) = '0000'

SELECT id, CAST(IIF(EMPTY(NVL(arrival_ti,'//::')), '', TTOC(arrival_ti,1)) as C(20)) as arrival_ti, voy_id, CAST(IIF(EMPTY(NVL(departure_,'//::')), '', TTOC(departure_,1)) as C(20)) as departure_, rpt_id, ctt_id, ves_id FROM ips_berthings INTO CURSOR tt
SELECT TT
COPY TO e:\berthings.csv TYPE csv

*** IPS_ROUTE_POINTS
SELECT IPS_ROUTE_POINTS
REINDEX
SELECT ID, COUNT(*) AS NR FROM IPS_ROUTE_POINTS GROUP BY ID HAVING COUNT(*) > 1 INTO CURSOR TT
SELECT TT
SCAN
	SELECT IPS_ROUTE_POINTS
	IF SEEK(TT.ID,'IPS_ROUTE_POINTS',"ID")
		DELETE
	ENDIF 
ENDSCAN 

SELECT id, acronym, name, cnl_id FROM ips_route_points INTO CURSOR tt
SELECT TT
COPY TO e:\route_points.csv TYPE csv


*********************************************************************************************
*********************************************************************************************
*********************************************************************************************
*** DATABASE LOAD TABLES FROM CSV
TEXT TO lcSql noshow

-- Create table
create table IPS_VOYAGE_MEMBERS_EXT
(
  id         VARCHAR2(36),
  vye_id     VARCHAR2(36),
  vsl_id     VARCHAR2(36),
  ctt_id     VARCHAR2(36),
  ctt_id_has NUMBER(12),
  ctt_id_ha2 NUMBER(12),
  confirm_pa VARCHAR2(20),
  dirty_tank VARCHAR2(1),
  place_barg VARCHAR2(2),
  rstart_id  VARCHAR2(36),
  rfin_id    VARCHAR2(36),
  distance   VARCHAR2(20),
  cnt_mnvr   VARCHAR2(10),
  distance_c VARCHAR2(20)
)
ORGANIZATION EXTERNAL
(TYPE ORACLE_LOADER
DEFAULT DIRECTORY DMPDIR
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
)
LOCATION ('voyage_members.csv')
);


insert into ips_voyage_members
  (id,
   vye_id,
   vsl_id,
   ctt_id,
   ctt_id_has,
   ctt_id_ha2,
   confirm_pa,
   dirty_tank,
   place_barg,
   rstart_id,
   rfin_id,
   distance,
   cnt_mnvr,
   distance_c)
  select id,
         vye_id,
         vsl_id,
         ctt_id,
         ctt_id_has,
         ctt_id_ha2,
         confirm_pa,
         dirty_tank,
         place_barg,
         rstart_id,
         rfin_id,
         TO_NUMBER(distance, '99999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as distance,
         cnt_mnvr,
         TO_NUMBER(distance_c,
                   '99999999D99',
                   'NLS_NUMERIC_CHARACTERS=''.,''') as distance_c
    from ips_voyage_members_ext;


-- Create table
create table IPS_CARGOES_EXT
(
  id         VARCHAR2(36),
  vms_id     VARCHAR2(36),
  gds_id     VARCHAR2(36),
  ballast    VARCHAR2(20),
  container  VARCHAR2(10),
  source     VARCHAR2(100),
  cty_id     VARCHAR2(36),
  destinatio VARCHAR2(100),
  quantity   VARCHAR2(20),
  unit       VARCHAR2(10),
  bill_nr    VARCHAR2(6),
  bill_date  DATE,
  delivery   VARCHAR2(5)
)
ORGANIZATION EXTERNAL
(TYPE ORACLE_LOADER
DEFAULT DIRECTORY DMPDIR
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(id, vms_id, gds_id, ballast, container, source, cty_id, destinatio, quantity, unit, bill_nr, bill_date date 'yyyymmddhh24miss', delivery)
)
LOCATION ('cargoes.csv')
);


insert into ips_cargoes
  (id,
   vms_id,
   gds_id,
   ballast,
   container,
   source,
   cty_id,
   destinatio,
   quantity,
   unit,
   bill_nr,
   bill_date,
   delivery)
  select id,
         vms_id,
         gds_id,
         TO_NUMBER(ballast, '99999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as ballast,
         TO_NUMBER(container, '9999', 'NLS_NUMERIC_CHARACTERS=''.,''') as container,
         source,
         cty_id,
         destinatio,
         TO_NUMBER(quantity, '99999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as quantity,
         unit,
         bill_nr,
         bill_date,
         delivery
    from ips_cargoes_ext;


create table IPS_VESSELS_EXT
(
  id         VARCHAR2(36),
  registrati VARCHAR2(240),
  name       VARCHAR2(240),
  lbd        VARCHAR2(20),
  max_draugh VARCHAR2(20),
  cruising_s VARCHAR2(20),
  callsign   VARCHAR2(240),
  mmsi       VARCHAR2(40),
  length     VARCHAR2(20),
  breadth    VARCHAR2(20),
  ctt_id     VARCHAR2(36),
  cty_id     VARCHAR2(12),
  deadweight VARCHAR2(20),
  gross_tonn VARCHAR2(20),
  net_tonnag VARCHAR2(20),
  horsepower VARCHAR2(12),
  gps_from_b VARCHAR2(20),
  gps_from_c VARCHAR2(20),
  gps_from_s VARCHAR2(20),
  inmarsat_n VARCHAR2(240),
  trn        VARCHAR2(20),
  vtp_id     VARCHAR2(36),
  vtmis_id   VARCHAR2(36)
)
ORGANIZATION EXTERNAL
(TYPE ORACLE_LOADER
DEFAULT DIRECTORY DMPDIR
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
)
LOCATION ('vessels.csv')
);

insert into ips_vessels
  (id,
   registrati,
   name,
   lbd,
   max_draugh,
   cruising_s,
   callsign,
   mmsi,
   length,
   breadth,
   ctt_id,
   cty_id,
   deadweight,
   gross_tonn,
   net_tonnag,
   horsepower,
   gps_from_b,
   gps_from_c,
   gps_from_s,
   inmarsat_n,
   trn,
   vtp_id,
   vtmis_id)
  select id,
         registrati,
         name,
         TO_NUMBER(lbd, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as lbd,
         TO_NUMBER(max_draugh, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as max_draugh,
         TO_NUMBER(cruising_s, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as cruising_s,
         callsign,
         mmsi,
         TO_NUMBER(length, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as length,
         TO_NUMBER(breadth, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as breadth,
         ctt_id,
         TO_NUMBER(cty_id, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as cty_id,
         TO_NUMBER(deadweight, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as deadweight,
         TO_NUMBER(gross_tonn, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as gross_tonn,
         TO_NUMBER(net_tonnag, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as net_tonnag,
         TO_NUMBER(horsepower, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as horsepower,
         TO_NUMBER(gps_from_b, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as gps_from_b,
         TO_NUMBER(gps_from_c, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as gps_from_c,
         TO_NUMBER(gps_from_s, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as gps_from_s,
         inmarsat_n,
         TO_NUMBER(trn, '9999999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as trn,
         vtp_id,
         vtmis_id
    from ips_vessels_ext;


create table IPS_VOYAGE_LOCKS_EXT
(
  id        VARCHAR2(36),
  vye_id    VARCHAR2(36),
  "date"    DATE,
  rpt_id    VARCHAR2(36),
  op_min    VARCHAR2(36),
  op_prep   VARCHAR2(36),
  id_lock   VARCHAR2(36),
  lock_name VARCHAR2(16)
)
ORGANIZATION EXTERNAL
(TYPE ORACLE_LOADER
DEFAULT DIRECTORY DMPDIR
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(id, vye_id, "date" date 'yyyymmddhh24miss', rpt_id, op_min, op_prep, id_lock, lock_name)
)
LOCATION ('voyage_locks.csv')
);


insert into ips_voyage_locks
  (id, vye_id, "date", rpt_id, op_min, op_prep, id_lock, lock_name)
  select id,
         vye_id,
         "date",
         rpt_id,
         TO_NUMBER(op_min, '99999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as op_min,
         TO_NUMBER(op_prep, '99999999D99', 'NLS_NUMERIC_CHARACTERS=''.,''') as op_prep,
         id_lock,
         lock_name
    from ips_voyage_locks_ext;
    
    
create table IPS_VOYAGES_EXT
(
  id         VARCHAR2(36),
  generation DATE,
  ctt_id     VARCHAR2(36),
  vms_id     VARCHAR2(36),
  convoy_nam VARCHAR2(100),
  declaratio VARCHAR2(8),
  notificati DATE,
  notificat2 VARCHAR2(20),
  origin     VARCHAR2(100),
  destinatio VARCHAR2(100),
  state      VARCHAR2(1)
)
ORGANIZATION EXTERNAL
(TYPE ORACLE_LOADER
DEFAULT DIRECTORY DMPDIR
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(id,generation date 'yyyymmddhh24miss',ctt_id,vms_id,convoy_nam,declaratio,notificati date 'yyyymmddhh24miss',notificat2,origin,destinatio,state)
)
LOCATION ('voyages.csv')
);


insert into ips_voyages
  (id,
   generation,
   ctt_id,
   vms_id,
   convoy_nam,
   declaratio,
   notificati,
   notificat2,
   origin,
   destinatio,
   state)
  select id,
         generation,
         ctt_id,
         vms_id,
         convoy_nam,
         declaratio,
         notificati,
         notificat2,
         origin,
         destinatio,
         TO_NUMBER(state, '9', 'NLS_NUMERIC_CHARACTERS=''.,''') as state
    from ips_voyages_ext;    
        

CREATE TABLE ips_berthings_ext
(
	id varchar2 (36), 
	arrival_ti DATE , 
	voy_id varchar2 (36) , 
	departure_ DATE , 
	rpt_id varchar2 (36) , 
	ctt_id varchar2 (36) , 
	ves_id varchar2 (36) 
)
   ORGANIZATION EXTERNAL 
    ( TYPE ORACLE_LOADER
      DEFAULT DIRECTORY "DMPDIR"
      ACCESS PARAMETERS
      ( RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(id, arrival_ti date 'yyyymmddhh24miss', voy_id, departure_ date 'yyyymmddhh24miss', rpt_id, ctt_id, ves_id)
    )
      LOCATION
       ( 'berthings.csv'
       )
    )
  ;

insert into ips_berthings
  (id, arrival_ti, voy_id, departure_, rpt_id, ctt_id, ves_id)
select id, arrival_ti, voy_id, departure_, rpt_id, ctt_id, ves_id from ips_berthings_ext;



CREATE TABLE ips_route_points_ext
(
	id varchar2 (36) , 
	acronym varchar2 (6), 
	name varchar2 (100), 
	cnl_id NUMBER (2, 0)
)
   ORGANIZATION EXTERNAL 
    ( TYPE ORACLE_LOADER
      DEFAULT DIRECTORY "DMPDIR"
      ACCESS PARAMETERS
      ( RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(id, acronym, name, cnl_id)
    )
      LOCATION
       ( 'route_points.csv'
       )
    )
  ;
INSERT INTO ips_route_points 
	(id, acronym, name, cnl_id)
SELECT id, acronym, name, cnl_id FROM ips_route_points_ext;

ENDTEXT 


