GetDirective.java 文件源码

java
阅读 22 收藏 0 点赞 0 评论 0

项目:geeCommerce-Java-Shop-Software-and-PIM 作者:
@SuppressWarnings("rawtypes")
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
    throws TemplateException, IOException {
    if (log.isTraceEnabled()) {
        log.trace(params);
    }

    SimpleScalar pType = (SimpleScalar) params.get("type");
    TemplateModel pId = (TemplateModel) params.get("id");
    SimpleScalar pVar = (SimpleScalar) params.get("var");

    String type = null;
    String varName = null;

    if (pType != null)
        type = pType.getAsString();

    if (pVar != null)
        varName = pVar.getAsString();

    if (pId != null && !Str.isEmpty(type)) {
        Object beanModel = null;

        if (pId instanceof SimpleScalar)
            beanModel = ((SimpleScalar) pId).getAsString();

        if (pId instanceof SimpleNumber)
            beanModel = ((SimpleNumber) pId).getAsNumber();

        else if (pId instanceof StringModel)
            beanModel = ((StringModel) pId).getAsString();

        else if (pId instanceof NumberModel)
            beanModel = ((NumberModel) pId).getAsNumber();

        else if (pId instanceof BeanModel)
            beanModel = ((BeanModel) pId).getWrappedObject();

        Id id = Id.valueOf(beanModel);

        Class<? extends Model> modelClass = Models.findBy(type);

        if (modelClass != null) {
            Object obj = restService.get(modelClass, id);

            if (obj != null)
                env.setVariable(Str.isEmpty(varName) ? modelClass.getSimpleName() : varName,
                    DefaultObjectWrapper.getDefaultInstance().wrap(obj));
        }
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号