use of org.nutz.lang.ExitLoop 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;
}
use of org.nutz.lang.ExitLoop 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);
}
use of org.nutz.lang.ExitLoop 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));
}
});
}
use of org.nutz.lang.ExitLoop 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;
}
use of org.nutz.lang.ExitLoop 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);
}
}
Aggregations