Useful Groovy Scripts. Part 7.


Previous parts:

Clear Specific Cache Region

This approach is useful for troubleshooting database interaction. After cleaning cache before the operation, next commands or requests will be performed with no data in the caches, and you can be ensured that all SQL statements generated after this point of time will be properly logged in the JDBC log (at least once).
defaultCacheController.clearCache(entityCacheRegion);
defaultCacheController.clearCache(queryCacheRegion);
defaultCacheController.clearCache(mediaCacheRegion);
defaultCacheController.clearCache(cmsCacheRegion);

Turn Off Caching Subsystem

Warning! Your system will work tremendously slow after applying this setting. You can turn cache off, perform some operations and turn it on again for debugging purposes or to collect performance metrics.
tenant.getCurrentTenant().getCache().setEnabled(false);
In order to turn it on, change false to true.

Turn Off the Cronjobs

It is convinient for debugging the components called from not only the code you troubleshoot, but from cronjobs too. To ease the process, turn all cronjobs off by setting <<active>> to false.
q="select {pk} from {CronJob} where {active} = true";
flexibleSearchService.search(q).getResult().each {
it.setActive(false); modelService.save(it);
}

Type system: Get information about a type

Just change Product with your type in the code below and you will see all attributes, their types, and type classes.
de.hybris.platform.core.Registry.getPersistenceManager().getPersistenceInfo("Product");

Create a groovy-powered REST API Server

Thanks hybrisarchitect.com for a great article about the REST API server powered by Groovy in hybris. This approach is great for adding temporary functionality accessible via http protocol.  https://hybrisarchitect.com/how-to-build-a-rapid-prototype-rest-api-server-using-sap-hybris-commerce/

CMS: Print all page/slot/component structure

import de.hybris.platform.cms2.enums.CmsPageStatus;
import de.hybris.platform.cms2.model.pages.AbstractPageModel;

void showComponentDetails(component) {
println " \\-- "+component.getUid();
}
void showSlotDetails(slot) {
println "* "+slot.getUid();
}
void showPageDetails(page) {
println "["+page.getUid()+"]";
}

void showComponents(slot) {
showSlotDetails(slot);
components = slot.getCMSComponents();
components.each {
component -> showComponentDetails(component);
}
}
void showSlotsAndComponents(page) {

slots = defaultCMSAdminContentSlotService.getContentSlotsForPage(page);
slots.each {
slot -> showComponents(slot);
}
}
void showPageConfiguration(AbstractPageModel page) {
showPageDetails(page);
template = page.getMasterTemplate();
println template;
showSlotsAndComponents(page);

}

SUCCESS_STATUS = Arrays.asList(CmsPageStatus.ACTIVE);
catalogVersionStaged = catalogVersionService.getCatalogVersion("lynxContentCatalog","Staged");
pages=defaultCMSAdminPageService.getAllPagesForCatalogVersionAndPageStatuses(catalogVersionStaged, SUCCESS_STATUS);
site=defaultCMSAdminSiteService.getSiteForId("lynx");
sessionService.setAttribute("activeCatalogVersion", catalogVersionStaged.getPk());
sessionService.setAttribute("activeSite", site.getPk());
pages.each {
page ->
showPageConfiguration(page);
}

Leave a Reply