其中Type是我的hybris型号项目?

时间:2013-09-11 11:11:54

标签: types model hybris

在我的hybris事件监听器中,我有一个项目的PK,而不是模型项目。如何判断该PK所属的项目类型?

在hybris wiki中,他们给出了这个例子,以便您知道某个项目属于Product:

//The product deployment code is "1"
if (1 == pk.getTypeCode())
{
    final ProductModel product = modelService.get(pk);
    //Put your business code here
}

但我不喜欢硬编码我想要处理的类型的TypeCode。

3 个答案:

答案 0 :(得分:2)

要在源代码中不硬连接TypeCode,您必须先在数据库中找到您的项目,然后您可以通过两种不同的方式找到它的类型:

final ItemModel item = modelService.get(pk);

if (ProductModel._TYPECODE.equals(item.getItemtype()))
{
    LOG.debug("ProductModel being edited");
}

//or

if (item instanceof ProductModel) {
    LOG.debug("ProductModel being edited");
}

虽然这可能会减慢AfterSaveEvent侦听器中的内容,因为将为hybris服务器中编辑或创建或删除的每个对象调用此侦听器。

答案 1 :(得分:0)

HAC可用于在Hybris系统中查找已定义类型的类型代码:

转到:: / hac / maintain / deployments

这将为您提供以下信息:

  1. 类型代码
  2. 类型
  3. 扩展

答案 2 :(得分:0)

以下是在hybris 4.x中执行此操作的示例groovy脚本。

import de.hybris.platform.core.PK;
import de.hybris.platform.jalo.type.TypeManager; // this class is deprecated though

def pkString = 8796093054980; // PK of admin
def typeService = ctx.getBean("typeService");
def modelService= ctx.getBean("modelService");

def composedType = TypeManager.getInstance().getRootComposedType(PK.fromLong(pkString).getTypeCode());
def composedTypeModel = modelService.toModelLayer(composedType);
out.println typeService.getModelClass(composedTypeModel);

结果:class de.hybris.platform.core.model.user.UserModel