Search in sources :

Example 61 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class Data method getFields.

/**
 * 获取字段
 * 当表单不在管理区域时
 * 会用当前表单覆盖管理表单
 * 配置文件不存在则抛出异常 0x1104
 * @return
 */
@Override
public Map getFields() {
    try {
        return super.getFields();
    } catch (NullPointerException ex) {
    // Nothing todo
    }
    Map fields, fieldx;
    /**
     * 字段以 centra/data 的字段为基础
     * 但可在 centre/data 重设部分字段
     *
     * 配置文件不得放在资源包里面
     * 此处会校验表单文件是否存在
     */
    try {
        if (!new File(Core.CONF_PATH + "/" + conf + Cnst.FORM_EXT + ".xml").exists()) {
            throw new HongsExpedient(0x1104, "Data form conf '" + conf + "' is not exists").setLocalizedOptions(conf);
        }
        fields = FormSet.getInstance(conf).getForm(form);
        if (!comf.equals(conf)) {
            if (!new File(Core.CONF_PATH + "/" + comf + Cnst.FORM_EXT + ".xml").exists()) {
                throw new HongsExpedient(0x1104, "Data form conf '" + comf + "' is not exists").setLocalizedOptions(comf);
            }
            fieldx = FormSet.getInstance(comf).getForm(form);
            // 补充上额外的字段设置
            fieldx = new HashMap(fieldx);
            fieldx.putAll(fields);
            fields = fieldx;
        }
    } catch (HongsException ex) {
        throw ex.toExpedient();
    }
    setFields(fields);
    return fields;
}
Also used : HashMap(java.util.HashMap) HongsException(app.hongs.HongsException) HongsExpedient(app.hongs.HongsExpedient) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 62 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class Form method saveDocument.

private void saveDocument(String path, Document docm) throws HongsException {
    File file = new File(path);
    File fold = file.getParentFile();
    if (!fold.exists()) {
        fold.mkdirs();
    }
    TransformerFactory tf = TransformerFactory.newInstance();
    try {
        Transformer tr = tf.newTransformer();
        DOMSource ds = new DOMSource(docm);
        StreamResult sr = new StreamResult(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
        tr.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        tr.setOutputProperty(OutputKeys.METHOD, "xml");
        tr.setOutputProperty(OutputKeys.INDENT, "yes");
        tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        tr.transform(ds, sr);
    } catch (TransformerConfigurationException e) {
        throw new HongsException.Common(e);
    } catch (IllegalArgumentException e) {
        throw new HongsException.Common(e);
    } catch (TransformerException e) {
        throw new HongsException.Common(e);
    } catch (FileNotFoundException e) {
        throw new HongsException.Common(e);
    } catch (UnsupportedEncodingException e) {
        throw new HongsException.Common(e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HongsException(app.hongs.HongsException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 63 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class Unit method makeDocument.

private Document makeDocument() throws HongsException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new HongsException.Common(e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HongsException(app.hongs.HongsException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 64 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class VerifyHelper method addRulesByForm.

public VerifyHelper addRulesByForm(String conf, String form, Map map) throws HongsException {
    FormSet def = FormSet.getInstance("default");
    Map tps = def.getEnum("__types__");
    Map pts = def.getEnum("__patts__");
    Iterator it = map.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry et = (Map.Entry) it.next();
        String code = (String) et.getKey();
        Map optz = (Map) et.getValue();
        Map opts = new HashMap(optz);
        Object o;
        opts.put("__conf__", conf);
        opts.put("__form__", form);
        opts.put("__name__", code);
        o = opts.get("defiant");
        if (o != null) {
            Rule rule = new Defiant();
            rule.setParams(opts);
            this.addRule(code, rule);
        }
        o = opts.get("default");
        if (o != null) {
            Rule rule = new Default();
            rule.setParams(opts);
            this.addRule(code, rule);
        }
        o = opts.remove("__required__");
        if (!"".equals(o)) {
            if (Synt.declare(o, false)) {
                Rule rule = new Required();
                rule.setParams(opts);
                this.addRule(code, rule);
            } else {
                Rule rule = new Optional();
                rule.setParams(opts);
                this.addRule(code, rule);
            }
        }
        o = opts.remove("__repeated__");
        if (!"".equals(o)) {
            if (Synt.declare(o, false)) {
                Rule rule = new Repeated();
                rule.setParams(opts);
                this.addRule(code, rule);
            } else {
                Rule rule = new NoRepeat();
                rule.setParams(opts);
                this.addRule(code, rule);
            }
        }
        // 可设多个规则, 缺省情况按字符串处理
        List<String> list = Synt.toList(opts.get("__rule__"));
        if (list == null || list.isEmpty()) {
            String type = (String) opts.get("__type__");
            String item;
            if (tps.containsKey(type)) {
                // 类名转换
                item = (String) tps.get(type);
                String c = item.substring(0, 1);
                String n = item.substring(1);
                item = "Is" + c.toUpperCase() + n;
            } else if (pts.containsKey(type)) {
                // 类型正则
                opts.put("pattern", type);
                item = "IsString";
            } else if (!"@".equals(code)) {
                item = "IsString";
            } else {
                item = "Ignore";
            }
            list = Synt.listOf(item);
        }
        // 添加规则实例
        for (String item : list) {
            if (!item.contains(".")) {
                item = Rule.class.getPackage().getName() + "." + item;
            }
            Rule rule;
            try {
                rule = (Rule) (Class.forName(item).newInstance());
            } catch (ClassNotFoundException ex) {
                throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (InstantiationException ex) {
                throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (IllegalAccessException ex) {
                throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            } catch (ClassCastException ex) {
                throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
            }
            rule.setParams(opts);
            addRule(code, rule);
        }
    }
    return this;
}
Also used : Optional(app.hongs.util.verify.Optional) HashMap(java.util.HashMap) Repeated(app.hongs.util.verify.Repeated) Defiant(app.hongs.util.verify.Defiant) Default(app.hongs.util.verify.Default) Required(app.hongs.util.verify.Required) HongsException(app.hongs.HongsException) Iterator(java.util.Iterator) Rule(app.hongs.util.verify.Rule) NoRepeat(app.hongs.util.verify.NoRepeat) HashMap(java.util.HashMap) Map(java.util.Map)

Example 65 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class SelectInvoker method invoke.

@Override
public void invoke(ActionHelper helper, ActionRunner chains, Annotation anno) throws HongsException {
    Select ann = (Select) anno;
    String conf = ann.conf();
    String form = ann.form();
    byte mode = ann.mode();
    if (mode == -1) {
        Set ab = Synt.toTerms(helper.getRequestData().get(Cnst.AB_KEY));
        if (ab != null) {
            if (ab.contains("!enum")) {
                mode = 0;
            } else {
                if (ab.contains(".enum")) {
                    mode += 1;
                }
                if (ab.contains("_enum")) {
                    mode += 2;
                }
                if (ab.contains("_time")) {
                    mode += 4;
                }
                if (ab.contains("_link")) {
                    mode += 8;
                }
                if (mode >= 0) {
                    mode += 1;
                }
            }
        }
    }
    // 为 0 则不执行, 仅取 enum 数据
    Map rsp;
    if (mode == 0) {
        mode = 1;
        rsp = new HashMap();
    } else if (mode == -1) {
        chains.doAction();
        return;
    } else {
        chains.doAction();
        rsp = helper.getResponseData();
        if (!Synt.declare(rsp.get("ok"), false)) {
            return;
        }
    }
    // 识别路径
    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);
        }
        SelectHelper sup;
        sup = new SelectHelper().addItemsByForm(conf, data);
        sup.select(rsp, mode);
    } catch (HongsException ex) {
        int ec = ex.getErrno();
        if (ec != 0x10e8 && ec != 0x10e9 && ec != 0x10ea) {
            throw ex;
        }
    }
    // 返回数据
    helper.reply(rsp);
}
Also used : Set(java.util.Set) FormSet(app.hongs.action.FormSet) HashMap(java.util.HashMap) HongsException(app.hongs.HongsException) Map(java.util.Map) HashMap(java.util.HashMap) SelectHelper(app.hongs.action.SelectHelper)

Aggregations

HongsException (app.hongs.HongsException)89 Map (java.util.Map)42 HashMap (java.util.HashMap)34 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)15 HashSet (java.util.HashSet)15 LinkedHashMap (java.util.LinkedHashMap)15 Set (java.util.Set)15 List (java.util.List)13 File (java.io.File)11 SQLException (java.sql.SQLException)10 FileNotFoundException (java.io.FileNotFoundException)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 PreparedStatement (java.sql.PreparedStatement)8 Iterator (java.util.Iterator)8 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 HongsExpedient (app.hongs.HongsExpedient)7 FormSet (app.hongs.action.FormSet)7 Table (app.hongs.db.Table)7