DB connection for DDL in groovy

DANGER!!!

In hybris hac, you cannot run DDL (data definition language) commands. You need unprotected connection to db. You can use below example. DDL commands are dangerous!!!

import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

def config = spring.getBean("configurationService").getConfiguration();

DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(config.getString("db.driver"));
dataSource.setUrl(config.getString("db.url"));
dataSource.setUsername(config.getString("db.username"));
dataSource.setPassword(config.getString("db.password"));
JdbcTemplate jdbc = new JdbcTemplate(dataSource)
jdbc.execute("ALTER TABLE MYTABLE ALTER (P_MYFIELD BIGINT)");

Using separate file for different class logging

Below example logging flexiblesearch class to separate file with time based trigger policy. Also we can add rollover strategy for removing old files.

log4j2.appender.FlexlogAppender.type=RollingFile log4j2.appender.FlexlogAppender.name=FlexlogAppender
log4j2.appender.FlexlogAppender.fileName=${HYBRIS_LOG_DIR}/Flexlog.log
log4j2.appender.FlexlogAppender.filePattern=${HYBRIS_LOG_DIR}/Flexlog.log-%d{yyyy-MM-dd}.log
log4j2.appender.FlexlogAppender.ImmediateFlush=true log4j2.appender.FlexlogAppender.layout.type=PatternLayout
log4j2.appender.FlexlogAppender.layout.pattern=%m%n log4j2.appender.FlexlogAppender.policies.type = Policies
log4j2.appender.FlexlogAppender.policies.time.type = TimeBasedTriggeringPolicy log4j2.appender.FlexlogAppender.policies.time.interval = 1000000
log4j2.appender.FlexlogAppender.policies.time.modulate = true log4j2.logger.flexible.name = de.hybris.platform.jalo.flexiblesearch
log4j2.logger.flexible.level = DEBUG log4j2.logger.flexible.appenderRef.stdout.ref = FlexlogAppender log4j2.logger.flexible.additivity = false

Known hybris strategies

Strategy Interface Description
DefaultCommerceCloneSavedCartStrategy  Clone cart.
DefaultCloneAbstractOrderStrategy  Clone order.
DefaultCommerceAddToCartStrategy CommerceAddToCartStrategy Adding product to cart.
DefaultMultiCodeCouponRedemptionStrategy CouponRedemptionStrategy<>  Single code coupon redemption.
DefaultSingleCodeCouponRedemptionStrategy CouponRedemptionStrategy<>   Multi code coupon redemption.

 

Delete all item by type

You can delete all item by type with impex. Below example removing all stock levels.
REMOVE StockLevel[batchmode=true];itemType(code)[unique=true]
;StockLevel

Update object private property with groovy

You can update private field an object with below sample code.

def myBean = spring.getBean("myBean");

def f = myBean.getClass().getDeclaredField("myField");
f.setAccessible(true);
f.set(myBean, "new string value");

Changing item attribute type in hybris

Some times you need attribute type change in hybris. Hybris as own ORM and it is not support updating attribute type. If you change attribute type you will get error like this:

de.hybris.platform.servicelayer.exceptions.ModelSavingException: <########## NewPropError ##########> value type mismatch in unlocalized property ‘myproperty’ of ‘myitem’ : expected ‘class java.lang.Integer’ but got ‘class java.lang.Long’

If you don’t want to adding new field than migrating data and removing old, you can use below procedure.

  • Change column type in DB. You cannot do this on hac because DDL disabled.
  • Update system in hac (Platform -> Update without Create essential data option).

Known hybris configurations

Email settings

Parameter Default Description
mail.smtp.server
mail.smtp.port
mail.smtp.user
mail.smtp.password
mail.pop3.beforesmtp  false
mail.pop3.password
mail.pop3.server
mail.pop3.user
mail.from
mail.replyto
mail.use.tls  false

(daha…)

Reload cockpit xml configurations

When you make change on product cockpit list, editor, advanced search, base, etc you need to reload configuration xml files to system. Below BeanShell code do this.

import de.hybris.platform.core.initialization.SystemSetupContext;
import de.hybris.platform.core.initialization.SystemSetup;
import de.hybris.platform.core.Registry;
SystemSetupContext setupCtx = new SystemSetupContext(new HashMap(), SystemSetup.Type.PROJECT, "mycustomcockpits");

Registry.getApplicationContext().getBean("cockpitImportConfig").importCockpitConfig(setupCtx);

Log SQL queries which are created with flex

You can add below properties to your local.properties file.

log4j2.config.xml=
log4j2.logger.flexible-search.name = de.hybris.platform.jalo.flexiblesearch
log4j2.logger.flexible-search.level = debug
log4j2.logger.flexible-search.appenderRef.stdout.ref = STDOUT

After configuration you can manage log level in hac.

Adding isempty and in for enum operator for productcockpit

Below configuration adding isEmpty operator for REFERENCE type and in operator for ENUM.

<bean id="editorConditionConfiguration" parent="myCockpitEditorConditionConfiguration" scope="tenant"/>

<bean id="myCockpitEditorConditionConfiguration" parent="defaultEditorConditionConfiguration" scope="tenant"
      class="de.hybris.platform.cockpit.model.advancedsearch.config.impl.DefaultEditorConditionConfiguration" abstract="true">
    <property name="defaultConditionsMap">
        <map merge="true">
            <entry key="REFERENCE" value="isEmpty"/>
            <entry key="ENUM" value="in"/>
        </map>
    </property>
</bean>

<bean class="de.hybris.platform.cockpit.services.search.impl.ConditionTranslatorRegistry.ConditionTranslatorMapping">
    <property name="attributeTypes">
        <set>
            <value>REFERENCE</value>
        </set>
    </property>
    <property name="operatorQualifiers">
        <set>
            <value>isEmpty</value>
        </set>
    </property>
    <property name="translator" ref="genericQueryConditionTranslator"/>
</bean>

<bean class="de.hybris.platform.cockpit.services.search.impl.ConditionTranslatorRegistry.ConditionTranslatorMapping">
    <property name="attributeTypes">
        <set>
            <value>ENUM</value>
        </set>
    </property>
    <property name="operatorQualifiers">
        <set>
            <value>in</value>
        </set>
    </property>
    <property name="translator" ref="genericQueryConditionTranslator"/>
</bean>

You need to add this operator to your referenced property with below code in your advanced search configuration (advancedSearch_VariantProduct.xml).

<property qualifier="product.galleryImages" visible="false">
	<conditions mode="append">
		<condition operator="isEmpty"></condition>
	</conditions>
</property>