All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mg.common.metadata.freeMarker.directive.BaseDirective Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package com.mg.common.metadata.freeMarker.directive;

import com.mg.common.entity.vo.PermissionActionEnum;
import com.mg.common.metadata.util.MetaDataUtils;
import com.mg.framework.entity.metadata.MirrorPropertyEntity;
import freemarker.core.Environment;
import freemarker.template.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.io.IOException;
import java.util.Map;

/**
 * Created by liukefu on 2015/9/30.
 */
public abstract class BaseDirective  implements TemplateDirectiveModel {

    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {

    }

    public String createLabel(MirrorPropertyEntity mirrorProperty){
        StringBuilder sb = new StringBuilder();
        sb.append("");

        return sb.toString();
    }

    public String createField(MirrorPropertyEntity mPropertyEntity){
        StringBuilder sb = new StringBuilder();
        sb.append("
"); switch (mPropertyEntity.getControllerType()){ case text: sb.append(""); break; case number: sb.append(""); break; case bool: sb.append(""); break; case date: sb.append(""); break; case mEnum: sb.append(""); //名称域 sb.append(""); break; } sb.append("
"); return sb.toString(); } public String createViewField(Environment env,MirrorPropertyEntity mPropertyEntity){ boolean isPermitted = isPermitted(mPropertyEntity, PermissionActionEnum.action_update,getObjectId(env)); StringBuilder sb = new StringBuilder(); sb.append("
"); sb.append("
"); sb.append(viewFieldValue(env,mPropertyEntity)); if(isPermitted) { sb.append(" "); } sb.append("
"); if(isPermitted){ sb.append("
"); sb.append(createField(mPropertyEntity)); sb.append(" "); sb.append(" "); sb.append("
"); } sb.append("
"); return sb.toString(); } public String viewFieldValue(Environment env,MirrorPropertyEntity mPropertyEntity){ StringBuilder sb = new StringBuilder(); if(isPermitted(mPropertyEntity,PermissionActionEnum.action_view,getObjectId(env))) { switch (mPropertyEntity.getControllerType()) { case text: case number: case date: sb.append("{{object.").append(mPropertyEntity.getPropertyPath()).append("}}"); break; case bool: sb.append("{{object.").append(mPropertyEntity.getPropertyPath()).append(" | booleanFilter}}"); break; case mEnum: sb.append("{{object.").append(mPropertyEntity.getPropertyPath()).append(" | selectFilter:selects.").append(mPropertyEntity.getPropertyPath()).append("}}"); break; case object: sb.append("{{object.").append(MetaDataUtils.getObjectFieldValue(mPropertyEntity)).append("}}"); break; } } return sb.toString(); } protected String getObjectId(Environment environment){ try { TemplateHashModel model = environment.getDataModel(); return ((SimpleScalar) model.get("id")).getAsString(); } catch (TemplateModelException e) { e.printStackTrace(); return null; } } public String getPermissionName(MirrorPropertyEntity mPropertyEntity, PermissionActionEnum action, String id){ StringBuilder sb = new StringBuilder(); sb.append(mPropertyEntity.getBelongMObject().getTableName()).append(":") .append(mPropertyEntity.getFieldName()).append(":") .append(action).append(":").append(id); return sb.toString(); } public boolean isPermitted(MirrorPropertyEntity mPropertyEntity, PermissionActionEnum action, String id){ if(mPropertyEntity==null){ return false; } String permission = getPermissionName(mPropertyEntity,action,id); Subject subject = SecurityUtils.getSubject(); return subject.isPermitted(permission); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy