Introduction
Understanding every attribute of a Hybris ItemType
, including those inherited from its parent types, is crucial for development and data tasks. Manually tracing these can be time-consuming. This concise Groovy script simplifies the process, fetching all attributes and presenting them in a clean CSV format. Ideal for quick data model insights and debugging.
The Groovy Script
def typeService = spring.getBean("typeService")
def collectAllAttributes(composedTypeModel) {
def collected = [] as Set
def currentType = composedTypeModel
while (currentType != null) {
def declaredAttrs = currentType.declaredattributedescriptors
if (declaredAttrs != null) {
collected.addAll(declaredAttrs)
}
currentType = currentType.superType
}
return collected
}
def productType = typeService.getComposedTypeForClass(de.hybris.platform.core.model.product.ProductModel)
def allAttributes = collectAllAttributes(productType)
def sortedAttrs = allAttributes.collect { attr ->
def qualifier = attr.qualifier
def typeCode = attr.attributeType?.code ?: "unknown"
[qualifier: qualifier, type: typeCode]
}.sort { a, b -> a.qualifier <=> b.qualifier }
// Print CSV header
println "Qualifier,Type"
// Print each attribute as CSV row, with proper escaping of commas/quotes if needed
sortedAttrs.each { attr ->
def qualifier = attr.qualifier?.replaceAll('"', '""')
def type = attr.type?.replaceAll('"', '""')
println "\"${qualifier}\",\"${type}\""
}
How it Works
This script efficiently retrieves the TypeService
to access Hybris’s data model. The core collectAllAttributes
function recursively traverses an ItemType
‘s inheritance chain (from currentType
to superType
), collecting all declared attributes into a Set
to ensure no duplicates. Finally, it formats these attributes, including their qualifiers and types, into a clean CSV output, ready for analysis. Simply adjust ProductModel
to your desired ItemType
class (e.g., CategoryModel
).
Leave a Reply
You must be logged in to post a comment.