Useful Groovy Scripts. Part 4. Finding all external references to an object
Finding all external references to an object (EARLY BETA)
The script I present today is designed for finding the references for any particular item in hybris. It can be useful if you want to figure out if the product or image is used somewhere in the system or it can be safely removed. It is a beta version, and it doesn’t cover everything, so use it at your own risk 🙂 For demonstrating purposes, let’s take the image which is definitely linked to the product. Let’s start with the simplest case,



String pkObj = "8797135929374";The script shows all objects having the references to the object with this PK:



Categories
Let’s take the following example:

- Category/Principal relation,
- Category/Product relation,
- Category/Category relation.

How it works
The system determines a type of the specified PK. For this type, the system extracts the type hierarchy. For each type in the hierarchy, the system finds the types with the attributes of the particular types or involved in hybris relations. For collections and relations, there is special handling. Finally, the system creates an SQL query and executes it. For example, for “Category” object the SQL will look like:SELECT 'ClassAttributeAssignment','cat2attrrel',"(cat2attrrel.p_classificationclass = '8796146172046' AND TypePkString = 8796110258258)", cat2attrrel.pk
FROM cat2attrrel
WHERE
(cat2attrrel.p_classificationclass = '8796146172046' AND TypePkString = 8796110258258)
UNION
SELECT 'CockpitItemTemplate','cockpittemplclassifrels',"(cockpittemplclassifrels.TargetPK = '8796146172046')", cockpittemplclassifrels.pk
FROM cockpittemplclassifrels
WHERE
(cockpittemplclassifrels.TargetPK = '8796146172046')
UNION
SELECT 'TextFieldConfiguratorSetting','configuratorsettings',"(configuratorsettings.p_configurationcategory = '8796146172046' AND TypePkString = 8796109111378)", configuratorsettings.pk
FROM configuratorsettings
WHERE
(configuratorsettings.p_configurationcategory = '8796146172046' AND TypePkString = 8796109111378)
UNION
SELECT 'CategoryCatalogVersionDifference','catverdiffs',"(catverdiffs.p_sourcecategory = '8796146172046' AND TypePkString = 8796110061650) OR (catverdiffs.p_targetcategory = '8796146172046' AND TypePkString = 8796110061650)", catverdiffs.pk
FROM catverdiffs
WHERE
(catverdiffs.p_sourcecategory = '8796146172046' AND TypePkString = 8796110061650) OR
(catverdiffs.p_targetcategory = '8796146172046' AND TypePkString = 8796110061650)
UNION
SELECT 'CMSSite','cmssite',"(cmssite.p_defaultpreviewcategory = '8796146172046' AND TypePkString = 8796105179218)", cmssite.pk
FROM cmssite
WHERE
(cmssite.p_defaultpreviewcategory = '8796146172046' AND TypePkString = 8796105179218)
UNION
SELECT 'SolrCategoryRedirect','solrabskwrdredir',"(solrabskwrdredir.p_redirectitem = '8796146172046' AND TypePkString = 8796119203922)", solrabskwrdredir.pk
FROM solrabskwrdredir
WHERE
(solrabskwrdredir.p_redirectitem = '8796146172046' AND TypePkString = 8796119203922)
UNION
SELECT 'PreviewData','previewdata',"(previewdata.p_previewcategory = '8796146172046' AND TypePkString = 8796126675026)", previewdata.pk
FROM previewdata
WHERE
(previewdata.p_previewcategory = '8796146172046' AND TypePkString = 8796126675026)
UNION
SELECT 'AsCategoryAwareSearchConfiguration','assearchconf',"(assearchconf.p_category = '8796146172046' AND TypePkString = 8796128575570)", assearchconf.pk
FROM assearchconf
WHERE
(assearchconf.p_category = '8796146172046' AND TypePkString = 8796128575570)
UNION
SELECT 'ProductCategoryRestriction','categories',"(restrictions.p_categories LIKE '%8796146172046%')", restrictions.pk
FROM restrictions
WHERE
(restrictions.p_categories LIKE '%8796146172046%')
UNION
SELECT 'Principal','cat2princrel',"(cat2princrel.SourcePK = '8796146172046')", cat2princrel.pk
FROM cat2princrel
WHERE
(cat2princrel.SourcePK = '8796146172046')
UNION
SELECT 'Media','cat2medrel',"(cat2medrel.SourcePK = '8796146172046')", cat2medrel.pk
FROM cat2medrel
WHERE
(cat2medrel.SourcePK = '8796146172046')
UNION
SELECT 'Product','cat2prodrel',"(cat2prodrel.SourcePK = '8796146172046')", cat2prodrel.pk
FROM cat2prodrel
WHERE
(cat2prodrel.SourcePK = '8796146172046')
UNION
SELECT 'Keyword','cat2keywordrel',"(cat2keywordrel.SourcePK = '8796146172046')", cat2keywordrel.pk
FROM cat2keywordrel
WHERE
(cat2keywordrel.SourcePK = '8796146172046')
UNION
SELECT 'Category','cat2catrel',"(cat2catrel.SourcePK = '8796146172046') OR (cat2catrel.TargetPK = '8796146172046')", cat2catrel.pk
FROM cat2catrel
WHERE
(cat2catrel.SourcePK = '8796146172046') OR
(cat2catrel.TargetPK = '8796146172046')
UNION
SELECT 'ProductPromotion','catpromrelations',"(catpromrelations.SourcePK = '8796146172046')", catpromrelations.pk
FROM catpromrelations
WHERE
(catpromrelations.SourcePK = '8796146172046')
UNION
SELECT 'CMSCategoryRestriction','categories4restriction',"(categories4restriction.TargetPK = '8796146172046')", categories4restriction.pk
FROM categories4restriction
WHERE
(categories4restriction.TargetPK = '8796146172046')
UNION
SELECT 'ProductCarouselComponent','catsforprodcarcomprels',"(catsforprodcarcomprels.TargetPK = '8796146172046')", catsforprodcarcomprels.pk
FROM catsforprodcarcomprels
WHERE
(catsforprodcarcomprels.TargetPK = '8796146172046')
FROM cat2attrrel
WHERE
(cat2attrrel.p_classificationclass = '8796146172046' AND TypePkString = 8796110258258)
UNION
SELECT 'CockpitItemTemplate','cockpittemplclassifrels',"(cockpittemplclassifrels.TargetPK = '8796146172046')", cockpittemplclassifrels.pk
FROM cockpittemplclassifrels
WHERE
(cockpittemplclassifrels.TargetPK = '8796146172046')
UNION
SELECT 'TextFieldConfiguratorSetting','configuratorsettings',"(configuratorsettings.p_configurationcategory = '8796146172046' AND TypePkString = 8796109111378)", configuratorsettings.pk
FROM configuratorsettings
WHERE
(configuratorsettings.p_configurationcategory = '8796146172046' AND TypePkString = 8796109111378)
UNION
SELECT 'CategoryCatalogVersionDifference','catverdiffs',"(catverdiffs.p_sourcecategory = '8796146172046' AND TypePkString = 8796110061650) OR (catverdiffs.p_targetcategory = '8796146172046' AND TypePkString = 8796110061650)", catverdiffs.pk
FROM catverdiffs
WHERE
(catverdiffs.p_sourcecategory = '8796146172046' AND TypePkString = 8796110061650) OR
(catverdiffs.p_targetcategory = '8796146172046' AND TypePkString = 8796110061650)
UNION
SELECT 'CMSSite','cmssite',"(cmssite.p_defaultpreviewcategory = '8796146172046' AND TypePkString = 8796105179218)", cmssite.pk
FROM cmssite
WHERE
(cmssite.p_defaultpreviewcategory = '8796146172046' AND TypePkString = 8796105179218)
UNION
SELECT 'SolrCategoryRedirect','solrabskwrdredir',"(solrabskwrdredir.p_redirectitem = '8796146172046' AND TypePkString = 8796119203922)", solrabskwrdredir.pk
FROM solrabskwrdredir
WHERE
(solrabskwrdredir.p_redirectitem = '8796146172046' AND TypePkString = 8796119203922)
UNION
SELECT 'PreviewData','previewdata',"(previewdata.p_previewcategory = '8796146172046' AND TypePkString = 8796126675026)", previewdata.pk
FROM previewdata
WHERE
(previewdata.p_previewcategory = '8796146172046' AND TypePkString = 8796126675026)
UNION
SELECT 'AsCategoryAwareSearchConfiguration','assearchconf',"(assearchconf.p_category = '8796146172046' AND TypePkString = 8796128575570)", assearchconf.pk
FROM assearchconf
WHERE
(assearchconf.p_category = '8796146172046' AND TypePkString = 8796128575570)
UNION
SELECT 'ProductCategoryRestriction','categories',"(restrictions.p_categories LIKE '%8796146172046%')", restrictions.pk
FROM restrictions
WHERE
(restrictions.p_categories LIKE '%8796146172046%')
UNION
SELECT 'Principal','cat2princrel',"(cat2princrel.SourcePK = '8796146172046')", cat2princrel.pk
FROM cat2princrel
WHERE
(cat2princrel.SourcePK = '8796146172046')
UNION
SELECT 'Media','cat2medrel',"(cat2medrel.SourcePK = '8796146172046')", cat2medrel.pk
FROM cat2medrel
WHERE
(cat2medrel.SourcePK = '8796146172046')
UNION
SELECT 'Product','cat2prodrel',"(cat2prodrel.SourcePK = '8796146172046')", cat2prodrel.pk
FROM cat2prodrel
WHERE
(cat2prodrel.SourcePK = '8796146172046')
UNION
SELECT 'Keyword','cat2keywordrel',"(cat2keywordrel.SourcePK = '8796146172046')", cat2keywordrel.pk
FROM cat2keywordrel
WHERE
(cat2keywordrel.SourcePK = '8796146172046')
UNION
SELECT 'Category','cat2catrel',"(cat2catrel.SourcePK = '8796146172046') OR (cat2catrel.TargetPK = '8796146172046')", cat2catrel.pk
FROM cat2catrel
WHERE
(cat2catrel.SourcePK = '8796146172046') OR
(cat2catrel.TargetPK = '8796146172046')
UNION
SELECT 'ProductPromotion','catpromrelations',"(catpromrelations.SourcePK = '8796146172046')", catpromrelations.pk
FROM catpromrelations
WHERE
(catpromrelations.SourcePK = '8796146172046')
UNION
SELECT 'CMSCategoryRestriction','categories4restriction',"(categories4restriction.TargetPK = '8796146172046')", categories4restriction.pk
FROM categories4restriction
WHERE
(categories4restriction.TargetPK = '8796146172046')
UNION
SELECT 'ProductCarouselComponent','catsforprodcarcomprels',"(catsforprodcarcomprels.TargetPK = '8796146172046')", catsforprodcarcomprels.pk
FROM catsforprodcarcomprels
WHERE
(catsforprodcarcomprels.TargetPK = '8796146172046')
THE SCRIPT
(github)© Rauf Aliev, December 2017