USE gringlobal
GO

--==================================================================================================
-- ORDER
--==================================================================================================
DELETE FROM taxonomy_classification
GO

INSERT INTO taxonomy_classification (classification_name)
(SELECT DISTINCT suprafamily_rank_name FROM taxonomy_family WHERE suprafamily_rank_name IS NOT NULL) 

--==================================================================================================
-- FAMILY
--==================================================================================================
DELETE FROM taxonomy_family_parent
GO

SET IDENTITY_INSERT taxonomy_family_parent ON
INSERT INTO 
	taxonomy_family_parent 
	(taxonomy_family_parent_id,
	 accepted_taxonomy_family_map_id,
	 type_taxonomy_genus_id,
	 taxonomy_classification_id,
	 family_name,
	 family_authority,
	 alternate_name,
	 family_type_code,
	 note,
	 created_by,
	 created_date,
	 modified_by,
	 modified_date,
	 owned_by,
	 owned_date)
SELECT taxonomy_family_id AS taxonomy_family_parent_id
  , current_taxonomy_family_id AS accepted_taxonomy_family_map_id
  , type_taxonomy_genus_id
  , (SELECT taxonomy_classification_id FROM taxonomy_classification WHERE classification_name = suprafamily_rank_name) AS taxonomy_classification_id
  , family_name
  , family_authority
  , alternate_name
  , CASE WHEN family_type_code IS NOT NULL THEN family_type_code
      WHEN NOTE LIKE 'dicot%' THEN 'DICOT'
      WHEN NOTE LIKE 'monocot%' THEN 'MONOCOT'
      WHEN NOTE LIKE 'fern%' THEN 'DICOT'
      WHEN NOTE LIKE 'gymnosperm%' THEN 'GYMNOSPERM'
      WHEN NOTE LIKE 'virus%' THEN 'VIRUS'
      WHEN NOTE LIKE 'bacterial%' THEN 'BACTERIAL'
      ELSE NULL
    END as family_type_code
  , note, created_by, created_date, modified_by, modified_date, owned_by, owned_date
FROM 
	taxonomy_family
WHERE 
	COALESCE(subfamily_name, tribe_name, subtribe_name) IS NULL
SET IDENTITY_INSERT taxonomy_family_parent OFF

/* 
==================================================================================================
taxonomy_subfamily
==================================================================================================
*/
DELETE FROM taxonomy_subfamily

SET IDENTITY_INSERT taxonomy_subfamily ON
INSERT INTO taxonomy_subfamily
(
	taxonomy_subfamily_id,
	accepted_taxonomy_family_map_id,
	type_taxonomy_genus_id,
    taxonomy_family_parent_id,
    subfamily_name,
	subfamily_authority,
	note, 
	created_by,
	created_date,
	modified_by,
	modified_date,
	owned_by,
	owned_date
)
SELECT taxonomy_family_id AS taxonomy_subfamily_id
  , current_taxonomy_family_id AS accepted_taxonomy_family_map_id
  , type_taxonomy_genus_id
  , (SELECT taxonomy_family_parent_id FROM taxonomy_family_parent WHERE family_name = tf.family_name) AS taxonomy_family_parent_id
  , subfamily_name
  , family_authority as subfamily_authority
  , note, created_by, created_date, modified_by, modified_date, owned_by, owned_date
FROM taxonomy_family tf
WHERE subfamily_name is not null AND coalesce(tribe_name, subtribe_name) IS NULL
SET IDENTITY_INSERT taxonomy_subfamily OFF

/* 
==================================================================================================
taxonomy_tribe
==================================================================================================
*/
DELETE FROM taxonomy_tribe
GO

SET IDENTITY_INSERT taxonomy_tribe ON
INSERT INTO taxonomy_tribe
(
	taxonomy_tribe_id,
	accepted_taxonomy_family_map_id,
	type_taxonomy_genus_id,
    taxonomy_family_parent_id,
	taxonomy_subfamily_id,
    tribe_name,
	tribe_authority,
	note, 
	created_by,
	created_date,
	modified_by,
	modified_date,
	owned_by,
	owned_date
)
(SELECT taxonomy_family_id AS taxonomy_tribe_id
  , current_taxonomy_family_id AS accepted_taxonomy_family_map_id
  , type_taxonomy_genus_id
  , (SELECT taxonomy_family_parent_id FROM taxonomy_family_parent WHERE family_name = tf.family_name) AS taxonomy_family_parent_id
  , (SELECT taxonomy_subfamily_id FROM taxonomy_subfamily
      WHERE subfamily_name = tf.subfamily_name AND taxonomy_family_parent_id = (SELECT taxonomy_family_parent_id FROM taxonomy_family_parent WHERE family_name = tf.family_name) 
    ) AS taxonomy_subfamily_id
  , tribe_name
  , family_authority as tribe_authority
  , note, created_by, created_date, modified_by, modified_date, owned_by, owned_date
FROM taxonomy_family tf
WHERE tribe_name is not null AND subtribe_name IS NULL
)
SET IDENTITY_INSERT taxonomy_tribe OFF

/* 
==================================================================================================
taxonomy_subtribe
==================================================================================================
*/

DELETE FROM taxonomy_subtribe
GO

SET IDENTITY_INSERT taxonomy_subtribe ON
INSERT INTO taxonomy_subtribe
(
	taxonomy_subtribe_id,
	accepted_taxonomy_family_map_id,
	type_taxonomy_genus_id,
	taxonomy_family_parent_id,
	taxonomy_subfamily_id,
	taxonomy_tribe_id,
	subtribe_name,
	subtribe_authority,
	note, 
	created_by,
	created_date ,
	modified_by,
	modified_date ,
	owned_by,
	owned_date
)
(SELECT taxonomy_family_id AS taxonomy_subtribe_id
  , current_taxonomy_family_id AS accepted_taxonomy_family_map_id
  , type_taxonomy_genus_id
  , (SELECT taxonomy_family_parent_id FROM taxonomy_family_parent WHERE family_name = tf.family_name) AS taxonomy_family_parent_id
  , (SELECT taxonomy_subfamily_id FROM taxonomy_subfamily
      WHERE subfamily_name = tf.subfamily_name AND taxonomy_family_parent_id = (SELECT taxonomy_family_parent_id FROM taxonomy_family_parent WHERE family_name = tf.family_name) 
    ) AS taxonomy_subfamily_id
  , (SELECT taxonomy_tribe_id FROM taxonomy_tribe
      WHERE tribe_name = tf.tribe_name AND taxonomy_family_parent_id = (SELECT taxonomy_family_parent_id FROM taxonomy_family_parent WHERE family_name = tf.family_name) 
    ) AS taxonomy_tribe_id
  , subtribe_name
  , family_authority as subtribe_authority
  , note, created_by, created_date, modified_by, modified_date, owned_by, owned_date
FROM taxonomy_family tf
WHERE subtribe_name IS NOT NULL
)
SET IDENTITY_INSERT taxonomy_subtribe OFF

/* 
==================================================================================================
taxonomy_family_map
==================================================================================================
*/
DELETE FROM taxonomy_family_map
GO

SET IDENTITY_INSERT taxonomy_family_map ON
INSERT INTO taxonomy_family_map
(
	taxonomy_family_map_id,
	taxonomy_family_map_accepted_id,
	authority,
	taxonomy_family_parent_id,
	taxonomy_subfamily_id,
	taxonomy_tribe_id,
    taxonomy_subtribe_id,
  	created_by,
	created_date,
	modified_by,
	modified_date,
	owned_by,
	owned_date
)
(
SELECT 
	taxonomy_family_parent_id as taxonomy_family_map_id,
	 accepted_taxonomy_family_map_id
	,family_authority
	,taxonomy_family_parent_id
	, NULL AS taxonomy_subfamily_id
	, NULL AS taxonomy_tribe_id
	, NULL AS taxonomy_subtribe_id
	, created_by, created_date, modified_by, modified_date, owned_by, owned_date
FROM 
	taxonomy_family_parent
UNION
SELECT 
	taxonomy_subfamily_id AS taxonomy_family_map_id
	,accepted_taxonomy_family_map_id
	,subfamily_authority
	, taxonomy_family_parent_id
	,taxonomy_subfamily_id
	, NULL AS taxonomy_tribe_id
	, NULL AS taxonomy_subtribe_id
	, created_by, created_date, modified_by, modified_date, owned_by, owned_date
FROM 
	taxonomy_subfamily
UNION 
SELECT 
	taxonomy_tribe_id AS taxonomy_family_map_id
	,accepted_taxonomy_family_map_id
	,tribe_authority
	, taxonomy_family_parent_id
	,taxonomy_subfamily_id 
	,taxonomy_tribe_id
	, NULL AS taxonomy_subtribe_id
	, created_by, created_date, modified_by, modified_date, owned_by, owned_date
FROM 
	taxonomy_tribe
UNION 
SELECT 
	taxonomy_subtribe_id AS taxonomy_family_map_id
	,accepted_taxonomy_family_map_id
	,subtribe_authority
	, taxonomy_family_parent_id, 
	taxonomy_subfamily_id, 
	taxonomy_tribe_id, 
	taxonomy_subtribe_id
	, created_by, created_date, modified_by, modified_date, owned_by, owned_date
FROM 
	taxonomy_subtribe)
SET IDENTITY_INSERT taxonomy_family_map OFF

