@Button(label = "Okay", validate = true, order = 3) public Navigation okayClick() { AbstractWebApplication application = (AbstractWebApplication) getApplication(); JdbcTemplate jdbcTemplate = application.getJdbcTemplate(); StringBuffer ddl = new StringBuffer(); ddl.append( "update " + TableUtilities.getTableName(application.getUserEntity()) + " set " + AbstractUser.PASSWORD + " = ?, " + AbstractUser.DISABLE + " = ? where " + AbstractUser.ID + " = ?"); jdbcTemplate.update(ddl.toString(), this.password, this.disable, this.userId); jdbcTemplate.update( "delete from " + TableUtilities.getTableName(RoleUser.class) + " where " + RoleUser.USER_ID + " = ?", this.userId); if (roles != null && roles.length > 0) { SimpleJdbcInsert mapping = new SimpleJdbcInsert(jdbcTemplate); mapping.withTableName(TableUtilities.getTableName(RoleUser.class)); for (Role role : roles) { Map<String, Object> pp = new HashMap<String, Object>(); pp.put(RoleUser.ROLE_ID, role.getId()); pp.put(RoleUser.USER_ID, this.userId); mapping.execute(pp); } } jdbcTemplate.update( "delete from " + TableUtilities.getTableName(UserGroup.class) + " where " + UserGroup.USER_ID + " = ?", this.userId); if (groups != null && groups.length > 0) { SimpleJdbcInsert mapping = new SimpleJdbcInsert(jdbcTemplate); mapping.withTableName(TableUtilities.getTableName(UserGroup.class)); for (Group group : groups) { Map<String, Object> pp = new HashMap<String, Object>(); pp.put(UserGroup.USER_ID, this.userId); pp.put(UserGroup.GROUP_ID, group.getId()); mapping.execute(pp); } } return new Navigation(application.getUserManagementPage()); }
public static void insert( DataSource dataSource, String tableName, List<String> usingColumns, List<Map<String, Object>> fields) { SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(dataSource); jdbcInsert.withTableName(tableName); List<String> pp = new ArrayList<>(); pp.addAll(usingColumns); pp.add(tableName + "_id"); jdbcInsert.usingColumns(pp.toArray(new String[usingColumns.size()])); for (Map<String, Object> field : fields) { field.put(tableName + "_id", UUID.randomUUID().toString()); } Map<String, Object>[] batch = fields.toArray(new Map[fields.size()]); jdbcInsert.executeBatch(batch); }
public synchronized CookingRecipe insertCookingRecipe(CookingRecipe recipe) throws Exception { System.out.println("Database: insertCookingRecipe(" + recipe + ")"); SimpleJdbcInsert insert = new SimpleJdbcInsert(jdbcTemplate); insert.withTableName("recipes"); insert.setGeneratedKeyName("id"); Map<String, Object> mapa = new HashMap<String, Object>(); mapa.put("recipe_name", recipe.getName()); mapa.put("author", recipe.getAuthor()); mapa.put("guide", recipe.getGuide()); // mapa.put("incident_ID", incidentID); Number insertedRecipeId = insert.executeAndReturnKey(mapa); recipe.setId(insertedRecipeId.longValue()); insertIngredients(recipe.getIngredients(), insertedRecipeId.longValue()); for (CookingIngredient ci : recipe.getIngredients()) { ci.setRecipe_id(insertedRecipeId.longValue()); } return recipe; }