Search in sources :

Example 6 with ExitLoop

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

the class DoDeleteLinkVisitor method visit.

public void visit(Object obj, LinkField lnk) {
    Object value = lnk.getValue(obj);
    if (value == null || Lang.length(value) == 0) {
        log.infof("Value of LinkField(@%s-->%s.%s) is null or isEmtry, ingore", lnk.getLinkType(), lnk.getEntity().getType().getSimpleName(), lnk.getHostField().getName());
        return;
    }
    final Pojo pojo = opt.maker().makeDelete(lnk.getLinkedEntity());
    pojo.setOperatingObject(value);
    pojo.append(Pojos.Items.cndAuto(lnk.getLinkedEntity(), null));
    Lang.each(value, new Each<Object>() {

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

Example 7 with ExitLoop

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

the class DoInsertRelationLinkVisitor method visit.

public void visit(final Object obj, LinkField lnk) {
    // 只有多对多的映射才被考虑
    if (lnk instanceof ManyManyLinkField) {
        // 获取两边映射主键的值
        final ManyManyLinkField mm = (ManyManyLinkField) lnk;
        Object value = lnk.getValue(obj);
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(Lang.length(value));
        Lang.each(value, new Each<Object>() {

            public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
                list.add(new RelationObjectMap(mm, obj, ele));
            }
        });
        if (list.isEmpty())
            return;
        Entity<Map<String, Object>> en = holder.makeEntity(mm.getRelationName(), list.get(0));
        Pojo pojo = opt.maker().makeInsert(en);
        pojo.setOperatingObject(list);
        for (Object p : list) pojo.addParamsBy(p);
        opt.add(pojo);
    }
}
Also used : Pojo(org.nutz.dao.sql.Pojo) ArrayList(java.util.ArrayList) ManyManyLinkField(org.nutz.dao.impl.entity.field.ManyManyLinkField) RelationObjectMap(org.nutz.dao.util.RelationObjectMap) ExitLoop(org.nutz.lang.ExitLoop) LoopException(org.nutz.lang.LoopException) RelationObjectMap(org.nutz.dao.util.RelationObjectMap) Map(java.util.Map)

Example 8 with ExitLoop

use of org.nutz.lang.ExitLoop 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

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