Search in sources :

Example 76 with HongsException

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

the class Model method exists.

/**
 * 检查是否存在
 *
 * @param rd
 * @param caze
 * @return 存在为true, 反之为false
 * @throws app.hongs.HongsException
 */
public boolean exists(Map rd, FetchCase caze) throws HongsException {
    if (rd == null) {
        rd = new HashMap();
    }
    if (caze == null) {
        caze = fetchCase();
    }
    // 默认不关联
    if (!caze.hasOption("ASSOCS") && !caze.hasOption("ASSOC_TYPES") && !caze.hasOption("ASSOC_JOINS")) {
        caze.setOption("ASSOCS", new HashSet());
    }
    // 是否缺少n或v参数
    if (!rd.containsKey("n") || !rd.containsKey("v")) {
        throw new HongsException(0x109a, "Param n or v can not be empty");
    }
    String n = (String) rd.get("n");
    String v = (String) rd.get("v");
    Map columns = this.table.getFields();
    // 是否缺少n对应的字段
    if (!columns.containsKey(n)) {
        throw new HongsException(0x109b, "Column " + n + " is not exists");
    }
    caze.filter("`" + this.table.name + "`.`" + n + "` = ?", v);
    Iterator it = rd.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        String field = (String) entry.getKey();
        String value = (String) entry.getValue();
        if (field.equals(this.table.primaryKey) || field.equals(Cnst.ID_KEY)) {
            if (value != null && !value.equals("")) {
                caze.filter("`" + this.table.name + "`.`" + this.table.primaryKey + "` != ?", value);
            }
        } else if (columns.containsKey(field)) {
            caze.filter("`" + this.table.name + "`.`" + field + "` = ?", value);
        }
    }
    Map row = this.table.fetchLess(caze);
    return !row.isEmpty();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HongsException(app.hongs.HongsException) Iterator(java.util.Iterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 77 with HongsException

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

the class Model method get.

public Map get(String id, FetchCase caze) throws HongsException {
    if (id == null || id.length() == 0) {
        throw new HongsException(0x1094, "ID can not be empty for get");
    }
    Map rd = new HashMap();
    rd.put(this.table.primaryKey, id);
    // 调用filter进行过滤
    caze = caze != null ? caze.clone() : fetchCase();
    caze.setOption("MODEL_START", "get");
    this.filter(caze, rd);
    return this.table.fetchLess(caze);
}
Also used : HongsException(app.hongs.HongsException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 78 with HongsException

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

the class Model method permit.

// ** 辅助过滤方法 **/
/**
 * 操作确认
 *
 * <pre>
 * 作用于 update,delete 上
 *
 * 如需添加过滤条件, 请重写此方法;
 * 有不可操作的行时, 通过 caze 上的 MODEL_START 来区分异常:
 * update 对应 0x1096
 * delete 对应 0x1097
 * 其他的 对应 0x1098
 * 描述为 Can not update|delete|search by id: ID1, ID2, IDn
 * </pre>
 *
 * @param caze
 * @param rd
 * @param id
 * @throws HongsException
 */
protected void permit(FetchCase caze, Map rd, Set id) throws HongsException {
    Map wh = new HashMap();
    if (rd.containsKey(Cnst.AR_KEY)) {
        wh.put(Cnst.AR_KEY, rd.get(Cnst.AR_KEY));
    }
    if (rd.containsKey(Cnst.OR_KEY)) {
        wh.put(Cnst.OR_KEY, rd.get(Cnst.OR_KEY));
    }
    if (wh.isEmpty()) {
        return;
    }
    // 组织查询
    wh.put(table.primaryKey, id);
    wh.put(Cnst.RB_KEY, Synt.setOf(table.primaryKey));
    caze.use(db).from(table.tableName, table.name);
    caze.setOption("OBJECT_MODE", false);
    filter(caze, wh);
    Set xd = new HashSet();
    for (Map row : caze.oll()) {
        xd.add(row.get(table.primaryKey).toString());
    }
    // 对比数量, 取出多余的部分作为错误消息抛出
    if (xd.size() != id.size()) {
        Set zd = new HashSet(id);
        zd.removeAll(xd);
        String er = zd.toString();
        String mm = caze.getOption("MODEL_START", "");
        if ("update".equals(mm)) {
            throw new HongsException(0x1096, "Can not update by id: " + er);
        } else if ("delete".equals(mm)) {
            throw new HongsException(0x1097, "Can not delete by id: " + er);
        } else {
            throw new HongsException(0x1098, "Can not search by id: " + er);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HongsException(app.hongs.HongsException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 79 with HongsException

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

the class Table method validateException.

private HongsException validateException(int code, String error, String fieldName, String... otherParams) {
    List<String> trans = new ArrayList();
    trans.add(db.name + "." + name);
    trans.add(/*the*/
    fieldName);
    trans.addAll(Arrays.asList(otherParams));
    return new HongsException(code, error + " (Table:" + name + ")").setLocalizedOptions(trans.toArray(new String[] {}));
}
Also used : HongsException(app.hongs.HongsException) ArrayList(java.util.ArrayList)

Example 80 with HongsException

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

the class Link method updates.

/**
 * 更新方法
 * @param sql
 * @param params
 * @return 更新的条数
 * @throws HongsException
 */
public int updates(String sql, Object... params) throws HongsException {
    this.ready();
    if (0 < Core.DEBUG && 8 != (8 & Core.DEBUG)) {
        StringBuilder sb = new StringBuilder(sql);
        List paramz = new ArrayList(Arrays.asList(params));
        checkSQLParams(sb, paramz);
        mergeSQLParams(sb, paramz);
        CoreLogger.debug("DB.updates: " + sb.toString());
    }
    PreparedStatement ps = this.prepareStatement(sql, params);
    try {
        return ps.executeUpdate();
    } catch (SQLException ex) {
        throw new HongsException(0x104e, ex);
    } finally {
        this.closeStatement(ps);
    }
}
Also used : SQLException(java.sql.SQLException) HongsException(app.hongs.HongsException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement)

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