Search in sources :

Example 1 with LoopException

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

the class EntityOperator method addUpdateForIgnoreNull.

public List<Pojo> addUpdateForIgnoreNull(final Entity<?> en, final Object obj, final FieldMatcher fm) {
    if (null == en)
        return null;
    final FieldMatcher newFM;
    if (null == fm)
        newFM = FieldMatcher.make(null, null, true);
    else {
        newFM = fm;
        newFM.setIgnoreNull(true);
    }
    final List<Pojo> re = new ArrayList<Pojo>(Lang.length(obj));
    Lang.each(obj, new Each<Object>() {

        public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
            Pojo pojo = dao.pojoMaker.makeUpdate(en, ele).append(Pojos.Items.cndAuto(en, ele)).setOperatingObject(ele);
            pojo.getContext().setFieldMatcher(newFM);
            re.add(pojo);
        }
    });
    pojoList.addAll(re);
    return re;
}
Also used : Pojo(org.nutz.dao.sql.Pojo) ExitLoop(org.nutz.lang.ExitLoop) LoopException(org.nutz.lang.LoopException) ArrayList(java.util.ArrayList) FieldMatcher(org.nutz.dao.FieldMatcher)

Example 2 with LoopException

use of org.nutz.lang.LoopException 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 3 with LoopException

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

the class DoInsertLinkVisitor method visit.

public void visit(final Object obj, final LinkField lnk) {
    final Object value = lnk.getValue(obj);
    if (Lang.length(value) == 0)
        return;
    // 从宿主对象更新关联对象
    opt.add(Pojos.createRun(new PojoCallback() {

        public Object invoke(Connection conn, ResultSet rs, Pojo pojo, Statement stmt) throws SQLException {
            lnk.updateLinkedField(obj, value);
            return pojo.getOperatingObject();
        }
    }).setOperatingObject(obj));
    // 为其循环生成插入语句 : holder.getEntityBy 会考虑到集合和数组的情况的
    final Entity<?> en = lnk.getLinkedEntity();
    Lang.each(value, new Each<Object>() {

        public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
            if (ele == null)
                throw new NullPointerException("null ele in linked field!!");
            // 执行插入
            opt.addInsert(en, ele);
            // 更新字段
            opt.add(Pojos.createRun(new PojoCallback() {

                public Object invoke(Connection conn, ResultSet rs, Pojo pojo, Statement stmt) throws SQLException {
                    lnk.saveLinkedField(obj, pojo.getOperatingObject());
                    return pojo.getOperatingObject();
                }
            }).setOperatingObject(ele));
        }
    });
}
Also used : Pojo(org.nutz.dao.sql.Pojo) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) PojoCallback(org.nutz.dao.sql.PojoCallback) ExitLoop(org.nutz.lang.ExitLoop) LoopException(org.nutz.lang.LoopException) ResultSet(java.sql.ResultSet)

Example 4 with LoopException

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

the class PojoEachRecordCallback 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;
    // 开始执行
    ResultSetLooping ing = new ResultSetLooping() {

        protected boolean createObject(int index, ResultSet rs, SqlContext context, int rowCount) {
            Object obj = Record.create(rs);
            try {
                each.invoke(index, obj, rowCount);
            } 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) {
        throw new SQLException(e.getCause().getMessage());
    }
    // 返回数量
    return ing.getIndex() + 1;
}
Also used : Loop(org.nutz.lang.Loop) ExitLoop(org.nutz.lang.ExitLoop) 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) ResultSet(java.sql.ResultSet)

Example 5 with LoopException

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

the class DoClearRelationByLinkedFieldLinkVisitor method visit.

public void visit(Object obj, LinkField lnk) {
    if (lnk instanceof ManyManyLinkField) {
        final ManyManyLinkField mm = (ManyManyLinkField) lnk;
        Object value = mm.getValue(obj);
        if (Lang.length(value) == 0)
            return;
        final Pojo pojo = opt.maker().makeDelete(mm.getRelationName());
        pojo.append(Pojos.Items.cndColumn(mm.getToColumnName(), mm.getLinkedField(), null));
        Lang.each(value, new Each<Object>() {

            public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
                pojo.addParamsBy(mm.getLinkedField().getValue(ele));
            }
        });
        opt.add(pojo);
    }
}
Also used : Pojo(org.nutz.dao.sql.Pojo) ExitLoop(org.nutz.lang.ExitLoop) LoopException(org.nutz.lang.LoopException) ManyManyLinkField(org.nutz.dao.impl.entity.field.ManyManyLinkField)

Aggregations

ExitLoop (org.nutz.lang.ExitLoop)8 LoopException (org.nutz.lang.LoopException)8 Pojo (org.nutz.dao.sql.Pojo)5 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)2 ManyManyLinkField (org.nutz.dao.impl.entity.field.ManyManyLinkField)2 ResultSetLooping (org.nutz.dao.pager.ResultSetLooping)2 SqlContext (org.nutz.dao.sql.SqlContext)2 ContinueLoop (org.nutz.lang.ContinueLoop)2 Loop (org.nutz.lang.Loop)2 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 Map (java.util.Map)1 FieldMatcher (org.nutz.dao.FieldMatcher)1 PojoCallback (org.nutz.dao.sql.PojoCallback)1