@Filters(@By(type = AjaxCheckSession.class, args = "me")) @At("/update") public Object updateInfo( String nickName, String passwd, @Attr("me") User me, HttpSession session) { if (!Strings.isBlank(nickName) && !nickName.startsWith("_") // 系统默认生成的nickName以_开头 && me.getNickName().startsWith("_") // 只允许修改一次nickName && nickName.trim().length() > 1 && nickName.trim().length() < 10 && nickName.indexOf("<") < 0 && nickName.indexOf(">") < 0 && nickName.indexOf("@") < 0 && nickName.indexOf("#") < 0 && nickName.indexOf(" ") < 0 && nickName.indexOf("&") < 0) { try { dao.update( User.class, Chain.make("nickName", nickName.trim()), Cnd.where("id", "=", me.getId())); } catch (Throwable e) { return Ajax.fail().setMsg("Nickname is dup or it is BAD!"); } } if (!Strings.isBlank(passwd) && passwd.trim().length() > 5 && passwd.trim().length() < 40) { dao.update( User.class, Chain.make("passwd", xMD5(passwd.trim())), Cnd.where("id", "=", me.getId())); } session.setAttribute("me", dao.fetch(User.class, Cnd.where("id", "=", me.getId()))); return Ajax.ok(); }
public int update(String tableName, Chain chain, Condition cnd) { if (chain.isSpecial()) return Daos.updateBySpecialChain(this, null, tableName, chain, cnd); EntityOperator opt = _optBy(chain.toEntityMap(tableName)); if (null == opt) return 0; opt.addUpdate(cnd); opt.exec(); return opt.getUpdateCount(); }
public void insert(String tableName, Chain chain) { if (chain.isSpecial()) { Daos.insertBySpecialChain(this, null, tableName, chain); return; } EntityOperator opt = _optBy(chain.toEntityMap(tableName)); if (null == opt) return; opt.addInsert(); opt.exec(); }
/** * 根据一个对象的字段 生成一个 Chain 对象 * * <p>这个对象可以是一个 POJO 或者是一个 Map。 * * <p>支持 FieldMatcher,即你可以通过 FieldMatcher 来指定你需要哪些字段加入 Chain * * @param obj 对象,可以是一个 POJO 或者是一个 Map * @param fm 指明可用字段,null 表示全部字段可用 * @return Chain 对象,null 表示对象中没有可用字段 * @see org.nutz.dao.FieldMatcher */ public static Chain from(Object obj, FieldMatcher fm) { if (null == obj) return null; Chain c = null; /* * Is Map */ if (obj instanceof Map<?, ?>) { for (Map.Entry<?, ?> en : ((Map<?, ?>) obj).entrySet()) { Object key = en.getKey(); if (null == key) continue; String name = key.toString(); if (null != fm && !fm.match(name)) continue; Object v = en.getValue(); if (null != fm) { if (null == v) { if (fm.isIgnoreNull()) continue; } else if (fm.isIgnoreBlankStr() && v instanceof String) { if (Strings.isBlank((String) v)) continue; } } if (c == null) { c = Chain.make(name, v); } else { c = c.add(name, v); } } } /* * Is POJO */ else { Mirror<?> mirror = Mirror.me(obj.getClass()); for (Field f : mirror.getFields()) { if (null != fm && !fm.match(f.getName())) continue; Object v = mirror.getValue(obj, f.getName()); if (null == v) { if (fm.isIgnoreNull()) continue; } else if (fm.isIgnoreBlankStr() && v instanceof String) { if (Strings.isBlank((String) v)) continue; } if (c == null) { c = Chain.make(f.getName(), v); } else { c = c.add(f.getName(), v); } } } return c; }
@Override public boolean updateImgCommentId(long userId, long skuId, List<Long> imgList, long commentId) { Chain chn = Chain.make("commentId", commentId); Condition cnd = Cnd.where("id", "in", imgList).and("userId", "=", userId).and("skuId", "=", skuId); return 1 == dao.update(SkuCommentImg.class, chn, cnd); }
@At @Ok(">>:/admin/permission/category/list.rk") @RequiresPermissions({"permission:edit"}) public boolean update(@Param("name") String name, @Param("id") String id) { permissionCategoryService.update(Chain.make("name", name), Cnd.where("id", "=", id)); return true; }
/** * 执行一个特殊的Chain(事实上普通Chain也能执行,但不建议使用) * * @see org.nutz.dao.Chain#addSpecial(String, Object) */ @SuppressWarnings({"rawtypes"}) public static void insertBySpecialChain(Dao dao, Entity en, String tableName, Chain chain) { if (en != null) tableName = en.getTableName(); if (tableName == null) throw Lang.makeThrow(DaoException.class, "tableName and en is NULL !!"); final StringBuilder sql = new StringBuilder("INSERT INTO ").append(tableName).append(" ("); StringBuilder _value_places = new StringBuilder(" VALUES("); final List<Object> values = new ArrayList<Object>(); final List<ValueAdaptor> adaptors = new ArrayList<ValueAdaptor>(); Chain head = chain.head(); while (head != null) { String colName = head.name(); MappingField mf = null; if (en != null) { mf = en.getField(colName); if (mf != null) colName = mf.getColumnName(); } sql.append(colName); if (head.special()) { _value_places.append(head.value()); } else { if (en != null) mf = en.getField(head.name()); _value_places.append("?"); values.add(head.value()); ValueAdaptor adaptor = Jdbcs.getAdaptorBy(head.value()); if (mf != null && mf.getAdaptor() != null) adaptor = mf.getAdaptor(); adaptors.add(adaptor); } head = head.next(); if (head != null) { sql.append(", "); _value_places.append(", "); } } sql.append(")"); _value_places.append(")"); sql.append(_value_places); if (log.isDebugEnabled()) log.debug(sql); dao.run( new ConnCallback() { public void invoke(Connection conn) throws Exception { PreparedStatement ps = conn.prepareStatement(sql.toString()); try { for (int i = 0; i < values.size(); i++) adaptors.get(i).set(ps, values.get(i), i + 1); ps.execute(); } finally { Daos.safeClose(ps); } } }); }
public int update(Class<?> classOfT, Chain chain, Condition cnd) { if (chain.isSpecial()) return Daos.updateBySpecialChain(this, getEntity(classOfT), null, chain, cnd); EntityOperator opt = _opt(classOfT); opt.addUpdate(chain, cnd); opt.exec(); return opt.getUpdateCount(); }
public int updateRelation(Class<?> classOfT, String regex, Chain chain, Condition cnd) { if (chain.isSpecial()) throw Lang.noImplement(); EntityOperator opt = this._opt(classOfT); opt.entity.visitManyMany(null, regex, doUpdateRelation(opt, chain, cnd)); opt.exec(); return opt.getUpdateCount(); }
public void insert(Class<?> classOfT, Chain chain) { if (chain.isSpecial()) { Daos.insertBySpecialChain(this, getEntity(classOfT), null, chain); return; } EntityOperator opt = _opt(classOfT); opt.myObj = chain; opt.addInsertSelfOnly(); // insert(chain.toObject(classOfT));// TODO 这样的效率,未免太低了,需要改进 opt.exec(); }
@At("/passwd/reset/callback") public Object resetPasswdCallback(String token) { PasswordReset reset = dao.fetch(PasswordReset.class, Cnd.where("token", "=", token)); if (reset != null) { dao.clear(PasswordReset.class, Cnd.where("token", "=", token)); if (System.currentTimeMillis() - reset.getCreateTime().getTime() > 30 * 60 * 1000) return Ajax.fail().setMsg("token is expise"); String passwd = R.sg(12).next(); dao.update( User.class, Chain.make("passwd", xMD5(passwd)), Cnd.where("id", "=", reset.getUid())); String email = dao.fetch(User.class, Cnd.where("id", "=", reset.getUid())).getEmail(); mailService.add2Queue(email, "推爸密码重置邮件", "Your password : "******"Reset success!! Check you email!"); } return Ajax.fail().setMsg("Token not found!!"); }
private Chain cov(Parameter pa, boolean ignoreNull) { FieldMatcher fm = FieldMatcher.make(null, "parameterID|ids|station$", ignoreNull); Chain ch = Chain.from(pa, fm); return ch; }
/** * 执行一个特殊的Chain(事实上普通Chain也能执行,但不建议使用) * * @see org.nutz.dao.Chain#addSpecial(String, Object) */ @SuppressWarnings({"rawtypes"}) public static int updateBySpecialChain( Dao dao, Entity en, String tableName, Chain chain, Condition cnd) { if (en != null) tableName = en.getTableName(); if (tableName == null) throw Lang.makeThrow(DaoException.class, "tableName and en is NULL !!"); final StringBuilder sql = new StringBuilder("UPDATE ").append(tableName).append(" SET "); Chain head = chain.head(); final List<Object> values = new ArrayList<Object>(); final List<ValueAdaptor> adaptors = new ArrayList<ValueAdaptor>(); while (head != null) { MappingField mf = null; if (en != null) mf = en.getField(head.name()); String colName = head.name(); if (mf != null) colName = mf.getColumnName(); sql.append(colName).append("="); if (head.special()) { if (head.value() != null && head.value() instanceof String) { String str = (String) head.value(); if (str.length() > 0) { switch (str.charAt(0)) { case '+': case '-': case '*': case '/': case '%': case '&': case '^': case '|': sql.append(colName); break; } } } sql.append(head.value()); } else { sql.append("?"); values.add(head.value()); ValueAdaptor adaptor = Jdbcs.getAdaptorBy(head.value()); if (mf != null && mf.getAdaptor() != null) adaptor = mf.getAdaptor(); adaptors.add(adaptor); } sql.append(" "); head = head.next(); if (head != null) sql.append(", "); } if (cnd != null) sql.append(" ").append(cnd.toSql(en)); if (log.isDebugEnabled()) log.debug(sql); final int[] ints = new int[1]; dao.run( new ConnCallback() { public void invoke(Connection conn) throws Exception { PreparedStatement ps = conn.prepareStatement(sql.toString()); try { for (int i = 0; i < values.size(); i++) adaptors.get(i).set(ps, values.get(i), i + 1); ints[0] = ps.executeUpdate(); } finally { Daos.safeClose(ps); } } }); return ints[0]; }
private LinkVisitor doUpdateRelation(EntityOperator opt, Chain chain, Condition cnd) { return new DoUpdateRelationLinkVisitor(chain.toMap(), cnd).opt(opt); }