	/*  
	
	create_views.sql -	create materialized views for Collaboration Area Data Integrity Check 

	Copyright 2005, IBM Corporation
	Version 2, August 2, 2005
	Version 3, August 3, 2005 (corrected S1 query)
	version 4, August , 2005
	version 5, August 18, 2005

	Instructions for using this script  (see report.sql and flush.sql for more complete instructions):

	1. Run the SQL command "select * from coa" in your WPC DB Admin, or sqlplus, window, 
		to see a list of collab areas and their ids.

	2. Find (the name of) the collab area you want to create materialized views for.
		 and write down its id (i.e. the value of its "coa_id" field).

	3. Text edit this SQL script, to put in its id 
		in each of the approximately twenty occurrences of the default value of "" below.

	4. Run the drop_views.sql script.

	5. Run this create_views.sql script.

	END of Instructions

	
	Note: There are two kinds of (Oracle) views created below, one consisting of a one-column
		table of primary keys (a "primary key" view), and the other consisting of
		a one-column table of item ids (an "item id" view).

	Note: "CA" is used as an abbreviation for "collaboration Area" below.  "CAE" refers
		to the CAE database table.

	*/



	/* 
	Define SRC_CHECKED_OUT as:  "The set of Source Entries checked out to this CA" -
	all distinct entries in the source container (i.e.all rows in ITM) that contain this collab area's id 
	in their itm_collab_info fields.
	The collab area id, if present, must either be in the interior or at the very end of the collab_info string.
	If in the interior, it is preceded by '::' and followed by '||'.
	If at the end, it is preceded by '::'.	
	*/
create materialized view SRC_CHECKED_OUT_PRIMARY_KEY as 
(select distinct itm_primary_key from itm, coa 
where instr(itm_collab_info, '::'||to_char(coa_id)||'||') > 0 
and itm_container_id = coa_src_container_id
and itm_next_version_id=999999999 
and COA_ID=clnCoaId)
union
(select distinct itm_primary_key from itm, coa 
where instr(itm_collab_info,('::'||to_char(coa_id)),length(itm_collab_info)-length(to_char(coa_id))-1)>0
and itm_container_id = coa_src_container_id
and itm_next_version_id=999999999 
and COA_ID=clnCoaId);


	/*
	Define CA_ALL as: "the set of CA Entries present in the CA Container" - 
	all distinct entries in the collab area container 
	(i.e. rows in the ITM or CAT table having this CA's Container as their Container).
	*/
	
create materialized view CA_ALL_PRIMARY_KEY as 
select distinct itm_primary_key
  from itm, coa 
 where itm_container_id = COA_DST_CONTAINER_ID 
 	and itm_next_version_id=999999999 
	and COA_ID=clnCoaId;
    
	/*
	Define SRC_ALL as: all primary keys in source container, whether
	checked out to this collaboration area or not.
	*/
	/*
create materialized view SRC_ALL_PRIMARY_KEY as 
select distinct i.itm_primary_key from itm i, coa
where i.itm_next_version_id=999999999
and i.itm_container_id = COA_SRC_CONTAINER_ID
and COA_ID=clnCoaId;
	*/

/*
create materialized view ERROR_EVENT_ITEM_ID as 
select distinct( wee_entry_id )
  from wee, wfe
 where wfe_id = wee_event_id
		and wfe_collaboration_area_id = clnCoaId
   and wfe_event_status = 'ERROR';

create materialized view NEW_EVENT_ITEM_ID as 
select distinct( wee_entry_id )
  from wee, wfe
 where wfe_id = wee_event_id
		and wfe_collaboration_area_id = clnCoaId
   and wfe_event_status = 'NEW';

create materialized view DONE_EVENT_ITEM_ID as 
select distinct( wee_entry_id )
  from wee, wfe
 where wfe_id = wee_event_id
		and wfe_collaboration_area_id = clnCoaId
   and wfe_event_status = 'DONE';

create materialized view PROCESSING_EVENT_ITEM_ID as 
select distinct( wee_entry_id )
  from wee, wfe
 where wfe_id = wee_event_id	
	and wfe_collaboration_area_id = clnCoaId
   and wfe_event_status = 'PROCESSING';


create materialized view GOOD_EVENT_ITEM_ID as 
(select distinct wee_entry_id from wee, wfe
	where wfe_id = wee_event_id
 	and wfe_collaboration_area_id = clnCoaId)
minus
((select * from ERROR_EVENT_ITEM_ID) union (select * from PROCESSING_EVENT_ITEM_ID));
*/




    /* It's interesting to know which primary keys are (at least) in the CEM, 
    without reference to CAE or other tables,
    but for ghost items checking, we'll subsume that in the next view
    SRC_PRIMARY_KEY_IN_CEM_CAE.  So omit this view.
    */
/*
create materialized view SRC_PRIMARY_KEY_IN_CEM as 
select distinct i.itm_primary_key from itm i, coa
where i.itm_next_version_id=999999999
and i.itm_container_id = COA_SRC_CONTAINER_ID
and COA_ID=clnCoaId
and exists (select 1 from itm j, cem, GOOD_EVENT_ITEM_ID where 
	i.itm_primary_key = j.itm_primary_key
	and j.itm_next_version_id=999999999 
	and j.itm_container_id = COA_DST_CONTAINER_ID
	and cem_src_entry_id = i.itm_id 
	and cem_ca_entry_id = j.itm_id
	and cem_ca_entry_id =  GOOD_EVENT_ITEM_ID.wee_entry_id
);
*/


/* Note: for greatest generality, we (effectively) scan entire SRC_ALL_PRIMARY_KEY here, not just SRC_CHECKED_OUT_PRIMARY_KEY. */
/* OLD: We apply the following self-evident condition: if an item is in CEM and CAE, then it must have had at least one event. */
create materialized view SRC_PRIMARY_KEY_IN_CEM_CAE as
select distinct i.itm_primary_key from itm i, coa
where i.itm_next_version_id=999999999
and i.itm_container_id = COA_SRC_CONTAINER_ID
and COA_ID=clnCoaId
and exists (select 1 from itm j, cem, cae where 
	i.itm_primary_key = j.itm_primary_key
	and j.itm_next_version_id=999999999 
	and j.itm_container_id = COA_DST_CONTAINER_ID
	and cem_src_entry_id = i.itm_id 
	and cem_ca_entry_id = j.itm_id
	and cem_ca_entry_id = cae_entry_id
);


/*
create materialized view SRC_PRIMARY_KEY_NOT_IN_CEM_CAE as 
(select * from SRC_ALL_PRIMARY_KEY) minus (select * from SRC_PRIMARY_KEY_IN_CEM_CAE);
*/

/*
-- Only 'NEW' status qualifies as good here; 'DONE' and 'ERROR' and 'PROCESSING' do not.
-- Can have a source item id if its in, and only in, a NEW checkout event.
-- The NEW event, if present, is assumed to be an checkout or other that will create a CAE row.
-- Assumed integrity constraint: if an item is in the CA catalog, it must be in the CEM table.
-- So the second branch of the OR is a source item (which presumably is recorded as checked
-- out in the collab_info field of the source item), and which is in the CA catalog, isn't
-- in any step, is in CEM, has a NEW event in the queue.
create materialized view SRC_PRIMARY_KEY_EVENT_ONLY as 
select distinct s1.itm_primary_key from coa, itm s1, SRC_PRIMARY_KEY_NOT_IN_CEM_CAE, 
	GOOD_EVENT_ITEM_ID, NEW_EVENT_ITEM_ID
where
COA_ID=clnCoaId
and s1.itm_primary_key = SRC_PRIMARY_KEY_NOT_IN_CEM_CAE.itm_primary_key
and s1.itm_next_version_id=999999999
and s1.itm_id  =  GOOD_EVENT_ITEM_ID.wee_entry_id
and s1.itm_id  =  NEW_EVENT_ITEM_ID.wee_entry_id
and (
	s1.itm_container_id = COA_SRC_CONTAINER_ID 
	or 
	(
	s1.itm_container_id = COA_DST_CONTAINER_ID
	and (not exists (select 1 from cae where cae_entry_id = s1.itm_id))
	and (exists (select 1 from cem where cem_ca_entry_id = s1.itm_id))
    	)
    );	



create materialized view SRC_PRIMARY_KEY_OK_CEL as
select distinct s1.itm_primary_key from SRC_CHECKED_OUT_PRIMARY_KEY s1, itm i
where 
i.itm_primary_key = s1.itm_primary_key
and i.itm_next_version_id=999999999
and 
( 
(not exists (select 1 from cel where i.itm_id  = cel_entry_id))
 or
( exists (select 1 from cel, cae where i.itm_id  = cel_entry_id 
  		   and i.itm_id  = cae_entry_id and cae_lock_id = cel_id))
);
*/

/* In general, SRC_PRIMARY_KEY_IN_CEM_CAE and SRC_PRIMARY_KEY_EVENT_ONLY are not restricted to 
(primary keys of) checked out source catalog items, however SRC_PRIMARY_KEY_OK_CEL is,
so in the end, this SRC_GOOD_PRIMARY_KEY is a subset of (primary keys of) checked out source items.
*/
/*
create materialized view SRC_GOOD_PRIMARY_KEY as
(select * from SRC_PRIMARY_KEY_IN_CEM_CAE);
*/
--((select * from SRC_PRIMARY_KEY_IN_CEM_CAE) union (select * from SRC_PRIMARY_KEY_EVENT_ONLY))
--intersect (select * from SRC_PRIMARY_KEY_OK_CEL);


create materialized view SRC_BAD_PRIMARY_KEY as 
((select * from SRC_CHECKED_OUT_PRIMARY_KEY) minus (select * from SRC_PRIMARY_KEY_IN_CEM_CAE))
union
((select * from SRC_PRIMARY_KEY_IN_CEM_CAE) minus (select * from SRC_CHECKED_OUT_PRIMARY_KEY));


	/*
	Note: ADD_PRIMARY_KEY may consist of (primary keys of) both good and bad items.
    Note: CA_ALL_PRIMARY_KEY - SRC_CHECKED_OUT_PRIMARY_KEY is the right way to partition
    the primary keys of the CA catalog into "those checked out from source" and "those newly added in CA".
	*/
create materialized view ADD_PRIMARY_KEY as
(select * from CA_ALL_PRIMARY_KEY) minus (select * from SRC_CHECKED_OUT_PRIMARY_KEY);


/* OLD: We apply the following self-evident condition: if an item is in CEM and CAE, 
then it must have had at least one event. */
create materialized view ADD_PRIMARY_KEY_IN_CEM_CAE as 
select distinct s1.itm_primary_key from ADD_PRIMARY_KEY s1, itm i,coa
where 
i.itm_primary_key = s1.itm_primary_key
and i.itm_next_version_id=999999999
and i.itm_container_id = COA_DST_CONTAINER_ID
and COA_ID=clnCoaId
and exists (select 1 from cem, cae where 
	cem_ca_entry_id = i.itm_id 
	and cem_ca_entry_id = cae_entry_id
);

/*
create materialized view ADD_PRIMARY_KEY_NOT_IN_CEM_CAE as 
(select * from ADD_PRIMARY_KEY) minus (select * from ADD_PRIMARY_KEY_IN_CEM_CAE);
*/

/*
-- Only 'NEW' status qualifies as good here; 'DONE' and 'ERROR' and 'PROCESSING' do not.
-- Assumed integrity constraint: if an item is in the CA catalog, it must be in the CEM table.
-- The NEW event, if present, is assumed to be an EnterStepsEvent or other that will create a CAE row.
create materialized view ADD_PRIMARY_KEY_EVENT_ONLY as 
select distinct i.itm_primary_key from coa, itm i, ADD_PRIMARY_KEY_NOT_IN_CEM_CAE,
	GOOD_EVENT_ITEM_ID, NEW_EVENT_ITEM_ID
where
COA_ID=clnCoaId
and i.itm_primary_key = ADD_PRIMARY_KEY_NOT_IN_CEM_CAE.itm_primary_key
and i.itm_next_version_id=999999999
and i.itm_id  =  GOOD_EVENT_ITEM_ID.wee_entry_id
and i.itm_id  =  NEW_EVENT_ITEM_ID.wee_entry_id
and i.itm_container_id = COA_DST_CONTAINER_ID
and (
	(not exists (select 1 from cae where cae_entry_id = i.itm_id))
	and (exists (select 1 from cem where cem_ca_entry_id = i.itm_id))
    );


create materialized view ADD_PRIMARY_KEY_OK_CEL as
select distinct s1.itm_primary_key from ADD_PRIMARY_KEY s1, itm i
where 
i.itm_primary_key = s1.itm_primary_key
and 
( 
(not exists (select 1 from cel where i.itm_id  = cel_entry_id))
 or
( exists (select 1 from cel, cae where i.itm_id  = cel_entry_id 
  		   and i.itm_id  = cae_entry_id and cae_lock_id = cel_id))
);
*/
/*
create materialized view ADD_GOOD_PRIMARY_KEY as
(select * from ADD_PRIMARY_KEY_IN_CEM_CAE);
*/
--((select * from ADD_PRIMARY_KEY_IN_CEM_CAE) union (select * from ADD_PRIMARY_KEY_EVENT_ONLY))
--intersect (select * from ADD_PRIMARY_KEY_OK_CEL);


create materialized view ADD_BAD_PRIMARY_KEY as 
((select * from ADD_PRIMARY_KEY) minus (select * from ADD_PRIMARY_KEY_IN_CEM_CAE))
union
((select * from ADD_PRIMARY_KEY_IN_CEM_CAE) minus (select * from ADD_PRIMARY_KEY));


	/*
	Define IN_INITIAL_INITIAL as:  "the set of CA Entries in the Initial step" - 
	all distinct entry ids in rows of the CAE table, that are in the Initial Step, for this CA.
	*/
	/*
create materialized view IN_INITIAL_PRIMARY_KEY as 
select distinct itm_primary_key
  from itm, cae, coa, wfd, spt
 where CAE_ENTRY_ID=itm_id
   and itm_next_version_id=999999999 
   and coa_wfl_id=wfd_id
   and coa_wfl_id=spt_leaf_wfl_id
   and cae_step_path_id=spt_id
   and spt_leaf_step_text_path like 'Initial'
   and CAE_CA_ID=clnCoaId;
   */

	
	/* 
	CA_ITM_NO_ITA = Entries in the collab area container with no corresponding ITA/CAB rows. */
	/*
create materialized view CA_ITM_NO_ITA_PRIMARY_KEY as
select distinct itm_primary_key
  from itm, coa 
 where itm_container_id = COA_DST_CONTAINER_ID
   and itm_next_version_id=999999999 
   and COA_ID=clnCoaId
   and not exists
(
  select 1
    from ita
   where ITA_ITEM_ID=itm_id
     and ita_next_version_id = 999999999
);
*/



create materialized view CAE_ORPHAN_ITEM_ID as	
select distinct cae_entry_id
  from cae
 where cae_ca_id = clnCoaId
   and not exists
(
  select 1
    from itm, coa
   where itm_next_version_id = 999999999
	and COA_ID=clnCoaId
	and itm_container_id = COA_DST_CONTAINER_ID
     	and itm_id = cae_entry_id	
);

create materialized view CEM_SRC_ORPHAN_ITEM_ID as
select distinct cem_src_entry_id
  from cem
 where cem_ca_id = clnCoaId
   and cem_src_entry_id > 0 -- avoid the -1 id for items added in the collab area
   and not exists
(
  select 1
    from itm, coa
   where itm_next_version_id = 999999999
	and COA_ID=clnCoaId
	and itm_container_id = COA_SRC_CONTAINER_ID
     and itm_id = cem_src_entry_id
);

create materialized view CEM_CA_ORPHAN_ITEM_ID as
select distinct cem_ca_entry_id
  from cem
 where cem_ca_id = clnCoaId
   and not exists
(
  select 1
    from itm, coa
   where itm_next_version_id = 999999999
	and COA_ID=clnCoaId
	and itm_container_id = COA_DST_CONTAINER_ID
     	and itm_id = cem_ca_entry_id
);

/*
create materialized view CEL_CA_ORPHAN_ITEM_ID as
select distinct cel_entry_id
  from cel
 where cel_container_id = clnCoaId
   and not exists
(
  select 1
    from itm, coa
   where itm_next_version_id = 999999999
	and COA_ID=clnCoaId
	and itm_container_id = COA_DST_CONTAINER_ID
     	and itm_id = cel_entry_id
);
*/

/*
-- If an entry_id has all and only DONE events, don't call it an orphan (item was in CA but is now gone).  Otherwise DO.
create materialized view WEE_CA_ORPHAN_ITEM_ID as
select distinct wee_entry_id
  from wee, wfe
 where wfe_collaboration_area_id = clnCoaId
   and wfe_id = wee_event_id
   and ( wfe_event_status = 'NEW' OR wfe_event_status = 'PROCESSING' OR wfe_event_status = 'ERROR')
   and not exists
(
  select 1
    from itm, coa
   	where itm_next_version_id = 999999999
	and COA_ID=clnCoaId
	and itm_container_id = COA_DST_CONTAINER_ID
     	and itm_id = wee_entry_id
);
*/



/* Note that we only check for the ITA vs. ITM corruptions in the collab area
	catalog, and not the source catalog.  I.e. we assume such corruptions don't occur
	in the source catalog.
*/

/* 
	CA_ITA_NO_ITM = Entries in ITA in the collab area container with no corresponding ITM rows. 
	*/
	/*
create materialized view CA_ITA_NO_ITM as
select distinct ita_item_id
  from ita, coa 
 where COA_ID = clnCoaId
   and ita_catalog_id = COA_DST_CONTAINER_ID
   and ita_next_version_id = 999999999 
   and not exists
 (
  select 1
    from itm
   where ITA_ITEM_ID = itm_id
     and itm_next_version_id = 999999999
 );
 */
	
/* 
	CA_ITM_NO_ITA = Entries in the collab area container with no corresponding ITA/CAB rows. 
	*/
	/*
create materialized view CA_ITM_NO_ITA as
select distinct itm_id
  from itm, coa 
 where COA_ID = clnCoaId
   and itm_container_id = COA_DST_CONTAINER_ID
   and itm_next_version_id = 999999999 
   and not exists
 (
  select 1
    from ita
   where ITA_ITEM_ID = itm_id
     and ita_next_version_id = 999999999
 );
 */


	/* 
	TOTAL_BAD = All bad Entries for which we have a primary key. 
		By implication, they are bad both in source and in CA catalogs. */
create materialized view TOTAL_BAD_PRIMARY_KEY as
( 
--(select * from IN_INITIAL_PRIMARY_KEY) union
((select * from SRC_BAD_PRIMARY_KEY ) union (select * from ADD_BAD_PRIMARY_KEY))
-- union (select * from CA_ITM_NO_ITA_PRIMARY_KEY)
);

/*
  Total "bad event" CA Entry ids, i.e. entry ids of "bad" WEE (i.e. event queue) rows, where "bad" rows are:
	1) All "ERROR" and "PROCESSING" events,
		note:  we assume wflengine properly shutdown before this report run, and so, 
		NEW events shouldn't be considered "bad".  "DONE" events are obviously not bad.
	2) All "WEE orphan" rows for "NEW", "PROCESSING", and "ERROR" events ("DONE" events are most likely for
		items that have previously passed through and left the CA; nonetheless, when we back up WEE 
		for bad pks or ca item ids, we back up all their WEE rows, including "DONE" ones.
*/
/*
create materialized view TOTAL_BAD_ITEM_EVTQ as
(select * from ERROR_EVENT_ITEM_ID ) union
(select * from PROCESSING_EVENT_ITEM_ID ) union
(select * from WEE_CA_ORPHAN_ITEM_ID);
*/

	/* 
	ADDITIONAL_CA_BAD = All bad CA orphan items and ITM-ITA cross-corruption, by item id in CA catalog. */
create materialized view ADDITIONAL_BAD_CA_ITEM_ID as
(select * from CAE_ORPHAN_ITEM_ID) union
-- (select * from CEL_CA_ORPHAN_ITEM_ID) union -- 2005 Aug 19 exclude CEL orphan checks for now because of bug: no CEL obsolete delete
(select * from CEM_CA_ORPHAN_ITEM_ID);
--union
--(select * from TOTAL_BAD_ITEM_EVTQ) union
--(select * from CA_ITA_NO_ITM) union
--(select * from CA_ITM_NO_ITA);

	/* 
	ADDITIONAL_BAD_SRC = All bad source items, by item id in source catalog. */
	/*
	-- If an when have more components here, replace use of CEM_SRC_ORPHAN_ITEM_ID below by this.
create materialized view ADDITIONAL_BAD_SRC_ITEM_ID as
(select * from CEM_SRC_ORPHAN_ITEM_ID);
*/

	/* 
	TOTAL_BAD_CA = All bad CA Entries, by item id. */
create materialized view TOTAL_BAD_CA_ITEM_ID as
((select distinct itm_id
  from itm x, TOTAL_BAD_PRIMARY_KEY y, coa
	where x.itm_primary_key = y.itm_primary_key
 	and coa_id = clnCoaId
        and x.itm_container_id  = coa_dst_container_id
        and x.itm_next_version_id=999999999 )
	union
 (select * from ADDITIONAL_BAD_CA_ITEM_ID));


	/* 
	TOTAL_BAD_SRC = All bad SRC Entries, by item id. */
create materialized view TOTAL_BAD_SRC_ITEM_ID as
((select distinct itm_id
  from itm x, TOTAL_BAD_PRIMARY_KEY y, coa
	where x.itm_primary_key = y.itm_primary_key
 	and coa_id = clnCoaId
        and x.itm_container_id  = coa_src_container_id
        and x.itm_next_version_id=999999999 )
	union
-- (select * from ADDITIONAL_BAD_SRC_ITEM_ID)); -- should be this
   (select * from CEM_SRC_ORPHAN_ITEM_ID));

	/* 
	TOTAL_GOOD = All GOOD Entries, by primary key. */
create materialized view TOTAL_GOOD_PRIMARY_KEY as
(
-- ((select * from ADD_GOOD_PRIMARY_KEY) union (select * from SRC_GOOD_PRIMARY_KEY))
((select * from ADD_PRIMARY_KEY_IN_CEM_CAE) union (select * from SRC_PRIMARY_KEY_IN_CEM_CAE))
minus (select * from TOTAL_BAD_PRIMARY_KEY)
);


	/* 
	TOTAL_GOOD_CA = All GOOD CA Entries, by CA item id. */
create materialized view TOTAL_GOOD_CA_ITEM_ID as
(select distinct itm_id
  from itm x, TOTAL_GOOD_PRIMARY_KEY y, coa
	where x.itm_primary_key = y.itm_primary_key
 	and coa_id = clnCoaId
        and x.itm_container_id  = coa_dst_container_id
        and x.itm_next_version_id=999999999 );

	/* 
	TOTAL_GOOD_SRC = All GOOD SRC Entries, by src item id. */
create materialized view TOTAL_GOOD_SRC_ITEM_ID as
(select distinct itm_id
  from itm x, TOTAL_GOOD_PRIMARY_KEY y, coa
	where x.itm_primary_key = y.itm_primary_key
 	and coa_id = clnCoaId
        and x.itm_container_id  = coa_src_container_id
        and x.itm_next_version_id=999999999 );


commit;

/* end of create_views script */
