Search in sources :

Example 1 with ContinueLoop

use of org.nutz.lang.ContinueLoop in project nutz by nutzam.

the class FilePostSender method export.

public static void export(Map<String, Object> params, OutputStream out, final String boundary, final String enc) throws IOException {
    final DataOutputStream outs = new DataOutputStream(out);
    for (Entry<String, ?> entry : params.entrySet()) {
        final String key = entry.getKey();
        Object val = entry.getValue();
        if (val == null)
            val = "";
        Lang.each(val, new Each<Object>() {

            @Override
            public void invoke(int index, Object ele, int length) throws ExitLoop, ContinueLoop, LoopException {
                try {
                    outs.writeBytes("--" + boundary + SEPARATOR);
                    if (ele != null && ele instanceof File) {
                        writeFile((File) ele, key, outs, boundary, enc);
                        return;
                    }
                    outs.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + SEPARATOR + SEPARATOR);
                    outs.write(String.valueOf(ele).getBytes(enc));
                    outs.writeBytes(SEPARATOR);
                } catch (Exception e) {
                    throw Lang.wrapThrow(e);
                }
            }
        });
    }
    outs.writeBytes("--" + boundary + "--" + SEPARATOR);
    Streams.safeFlush(outs);
    Streams.safeClose(outs);
}
Also used : ExitLoop(org.nutz.lang.ExitLoop) LoopException(org.nutz.lang.LoopException) DataOutputStream(java.io.DataOutputStream) ContinueLoop(org.nutz.lang.ContinueLoop) File(java.io.File) IOException(java.io.IOException) HttpException(org.nutz.http.HttpException) LoopException(org.nutz.lang.LoopException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with ContinueLoop

use of org.nutz.lang.ContinueLoop in project nutz by nutzam.

the class PojoEachEntityCallback method invoke.

@SuppressWarnings("unchecked")
public Object invoke(Connection conn, ResultSet rs, Pojo pojo, Statement stmt) throws SQLException {
    // 得到回调
    final Each<Object> each = pojo.getContext().attr(Each.class);
    // 没有回调,什么都不用执行了
    if (null == each)
        return null;
    // 开始执行
    final Entity<?> en = pojo.getEntity();
    ResultSetLooping ing = new ResultSetLooping() {

        protected boolean createObject(int index, ResultSet rs, SqlContext context, int rowCount) {
            Object obj = en.getObject(rs, context.getFieldMatcher());
            try {
                each.invoke(index, obj, rowCount);
            } catch (ContinueLoop e) {
            } catch (LoopException e) {
                throw Lang.wrapThrow(e);
            }
            return false;
        }
    };
    try {
        // 循环开始
        if (each instanceof Loop)
            if (!((Loop<?>) each).begin())
                return 0;
        // 循环中
        ing.doLoop(rs, pojo.getContext());
        // 循环结束
        if (each instanceof Loop)
            ((Loop<?>) each).end();
    } catch (ExitLoop e) {
    } catch (LoopException e) {
        SQLException e2 = new SQLException();
        e2.initCause(e.getCause());
        throw e2;
    }
    // 返回数量
    return ing.getIndex() + 1;
}
Also used : Loop(org.nutz.lang.Loop) ExitLoop(org.nutz.lang.ExitLoop) ContinueLoop(org.nutz.lang.ContinueLoop) SqlContext(org.nutz.dao.sql.SqlContext) LoopException(org.nutz.lang.LoopException) ExitLoop(org.nutz.lang.ExitLoop) ResultSetLooping(org.nutz.dao.pager.ResultSetLooping) SQLException(java.sql.SQLException) ContinueLoop(org.nutz.lang.ContinueLoop) ResultSet(java.sql.ResultSet)

Aggregations

ContinueLoop (org.nutz.lang.ContinueLoop)2 ExitLoop (org.nutz.lang.ExitLoop)2 LoopException (org.nutz.lang.LoopException)2 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ResultSetLooping (org.nutz.dao.pager.ResultSetLooping)1 SqlContext (org.nutz.dao.sql.SqlContext)1 HttpException (org.nutz.http.HttpException)1 Loop (org.nutz.lang.Loop)1