Search in sources :

Example 1 with VerifyHelper

use of app.hongs.action.VerifyHelper in project HongsCORE by ihongs.

the class SearchCmdlet method update.

@Cmdlet("update")
public void update(String[] args) throws HongsException {
    Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "name=s", "id*s" });
    String conf = Synt.asString(opts.remove("conf"));
    String name = Synt.asString(opts.remove("name"));
    List<String> ds = Synt.asList(opts.remove("id"));
    ActionHelper ah = Core.getInstance(ActionHelper.class);
    LuceneRecord so = LuceneRecord.getInstance(conf, name);
    Map rd = ah.getRequestData();
    if (!rd.isEmpty()) {
        // 有数据则校验数据
        VerifyHelper vh = new VerifyHelper();
        vh.addRulesByForm(conf, name);
        rd = vh.verify(rd);
        try {
            so.begin();
            for (String id : ds) {
                so.set(id, rd);
            }
            so.commit();
        } catch (HongsException ex) {
            so.revert();
            throw ex;
        } finally {
            so.close();
        }
    } else {
        // 不给内容即为删除
        try {
            so.begin();
            for (String id : ds) {
                so.del(id);
            }
            so.commit();
        } catch (HongsException ex) {
            so.revert();
            throw ex;
        } finally {
            so.close();
        }
    }
}
Also used : VerifyHelper(app.hongs.action.VerifyHelper) HongsException(app.hongs.HongsException) ActionHelper(app.hongs.action.ActionHelper) LuceneRecord(app.hongs.dh.lucene.LuceneRecord) Map(java.util.Map) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 2 with VerifyHelper

use of app.hongs.action.VerifyHelper in project HongsCORE by ihongs.

the class IsForm method verify.

@Override
public Object verify(Object value) throws Wrongs, HongsException {
    if (value == null || "".equals(value)) {
        // 允许为空
        return null;
    }
    String conf = Synt.asString(params.get("conf"));
    String name = Synt.asString(params.get("form"));
    if (conf == null || "".equals(conf)) {
        conf = Synt.declare(params.get("__conf__"), "");
    }
    if (name == null || "".equals(name)) {
        name = Synt.declare(params.get("__name__"), "");
    }
    Map data = Synt.asMap(value);
    VerifyHelper hlpr = new VerifyHelper();
    hlpr.addRulesByForm(conf, name);
    hlpr.isUpdate(helper.isUpdate());
    hlpr.isPrompt(helper.isPrompt());
    return hlpr.verify(data);
}
Also used : VerifyHelper(app.hongs.action.VerifyHelper) Map(java.util.Map)

Example 3 with VerifyHelper

use of app.hongs.action.VerifyHelper in project HongsCORE by ihongs.

the class VerifyInvoker method invoke.

@Override
public void invoke(ActionHelper helper, ActionRunner chains, Annotation anno) throws HongsException {
    Verify ann = (Verify) anno;
    String conf = ann.conf();
    String form = ann.form();
    byte mode = ann.mode();
    byte type = ann.type();
    byte trim = ann.trim();
    // 准备数据
    Map<String, Object> dat = helper.getRequestData();
    Object id = dat.get(Cnst.ID_KEY);
    String at = chains.getAction();
    if (mode == -1) {
        Set ab = Synt.toTerms(helper.getRequestData().get(Cnst.AB_KEY));
        if (ab != null) {
            if (ab.contains(".errs")) {
                mode = 1;
            } else if (ab.contains("!errs")) {
                mode = 2;
            }
        }
    }
    if (type == -1) {
        Boolean up = Synt.asBool(helper.getAttribute(Cnst.UPDATE_MODE));
        if (up == null) {
            type = at.endsWith("/update") || (null != id && !"".equals(id)) ? (byte) 1 : (byte) 0;
        } else {
            type = up ? (byte) 1 : (byte) 0;
        }
    }
    boolean prp = mode <= 0;
    boolean upd = type == 1;
    boolean cln = trim == 1;
    // 识别路径
    if (form.length() == 0) {
        form = chains.getEntity();
    }
    if (conf.length() == 0) {
        conf = chains.getModule();
        // 照顾 Module Action 的配置规则
        if (FormSet.hasConfFile(conf + "/" + form)) {
            conf = conf + "/" + form;
        }
    }
    // 执行校验
    try {
        Map data = (Map) helper.getAttribute("form:" + conf + "." + form);
        if (data == null) {
            data = FormSet.getInstance(conf).getForm(form);
        }
        VerifyHelper ver = new VerifyHelper();
        ver.addRulesByForm(conf, form, data);
        ver.isPrompt(prp);
        ver.isUpdate(upd);
        Map vls = ver.verify(dat);
        if (cln)
            dat.clear();
        dat.putAll(vls);
    } catch (Wrongs err) {
        dat = err.toReply(prp ? 0 : mode);
        helper.reply(dat);
        // Servlet 环境下设置状态码为 400 (错误的请求)
        if (helper.getResponse() != null) {
            helper.getResponse().setStatus(SC_BAD_REQUEST);
        }
        return;
    } catch (HongsException ex) {
        int ec = ex.getErrno();
        if (ec != 0x10e8 && ec != 0x10e9 && ec != 0x10ea) {
            throw ex;
        }
    }
    chains.doAction();
}
Also used : VerifyHelper(app.hongs.action.VerifyHelper) Set(java.util.Set) FormSet(app.hongs.action.FormSet) HongsException(app.hongs.HongsException) Wrongs(app.hongs.util.verify.Wrongs) Map(java.util.Map)

Aggregations

VerifyHelper (app.hongs.action.VerifyHelper)3 Map (java.util.Map)3 HongsException (app.hongs.HongsException)2 ActionHelper (app.hongs.action.ActionHelper)1 FormSet (app.hongs.action.FormSet)1 Cmdlet (app.hongs.cmdlet.anno.Cmdlet)1 LuceneRecord (app.hongs.dh.lucene.LuceneRecord)1 Wrongs (app.hongs.util.verify.Wrongs)1 Set (java.util.Set)1