List of usage examples for org.apache.commons.dbutils.handlers BeanListHandler BeanListHandler
public BeanListHandler(Class<T> type)
From source file:org.kaaproject.kaa.server.datamigration.AbstractCtlMigration.java
/** * Do main part of data migration from old tables to new ctl based ones. * * @throws SQLException the sql exception *///from w w w . ja va2 s. co m protected List<Schema> transform() throws SQLException { // fetch schemas of appropriate feature like configuration List<Schema> schemas = runner.query(connection, "select " + "f.id as id, created_time as createdTime, created_username as createdUsername, " + "description, name, schems, version, application_id as appId " + "from " + getPrefixTableName() + "_schems f join schems s on f.id = s.id", new BeanListHandler<>(Schema.class)); // delete the fetched ids from schema table String toDelete = schemas.stream().map(s -> s.getId().toString()).collect(joining(", ")); String notEmptyIdSet = "^[\\s]*([0-9]+(\\,\\s)?)+"; if (toDelete.matches(notEmptyIdSet)) { runner.update(connection, "delete from schems where id in (" + toDelete + ")"); } // shift ids in order to avoid PK constraint violation during adding record to base_schema Long shift = runner.query(connection, "select max(id) as max_id from " + getPrefixTableName() + "_schems", rs -> rs.next() ? rs.getLong("max_id") : null); idShift = BaseSchemaIdCounter.getInstance().getAndShift(shift); runner.update(connection, "update " + getPrefixTableName() + "_schems set id = id + " + idShift + " order by id desc"); schemas.forEach(s -> s.setId(s.getId() + idShift)); return schemas; }
From source file:org.kaaproject.kaa.server.datamigration.CtlAggregation.java
/** * Return schemas and CTLs map that further used for creating new records in base_schema table. *//*from w w w . j av a 2 s . com*/ public Map<Ctl, List<Schema>> aggregate(List<Schema> schemas) throws SQLException, ConfigurationGenerationException, IOException { Long currentCtlMetaId = runner.query(connection, "select max(id) as max_id from ctl_metainfo", rs -> rs.next() ? rs.getLong("max_id") : null); Long currentCtlId = runner.query(connection, "select max(id) as max_id from ctl", rs -> rs.next() ? rs.getLong("max_id") : null); List<FlatCtl> flatCtls = runner.query(connection, "select c.id as ctlId, body, m.id as metaInfoId, fqn, application_id as appId, " + "tenant_id as tenantId from ctl c join ctl_metainfo m on c.metainfo_id = m.id", new BeanListHandler<>(FlatCtl.class)); // fetch existed CTLs for (FlatCtl flatCtl : flatCtls) { Ctl ctl = flatCtl.toCtl(); ctl.setExistInDb(true); ctls.add(ctl); schemasToCtl.put(ctl, new ArrayList<>()); } // CTL creation for (Schema schema : schemas) { currentCtlMetaId++; currentCtlId++; org.apache.avro.Schema schemaBody = new org.apache.avro.Schema.Parser().parse(schema.getSchems()); String fqn = schemaBody.getFullName(); RawSchema rawSchema = new RawSchema(schemaBody.toString()); DefaultRecordGenerationAlgorithm<RawData> algorithm = new DefaultRecordGenerationAlgorithmImpl<>( rawSchema, new RawDataFactory()); String defaultRecord = algorithm.getRootData().getRawData(); Long tenantId; if (schema.getAppId() != null) { tenantId = runner.query(connection, "select tenant_id from application where id = " + schema.getAppId(), rs -> rs.next() ? rs.getLong("tenant_id") : null); } else { tenantId = runner.query(connection, "select tenant_id from events_class where id = " + schema.getId(), rs -> rs.next() ? rs.getLong("tenant_id") : null); } Ctl ctl = new Ctl(currentCtlId, new CtlMetaInfo(currentCtlMetaId, fqn, schema.getAppId(), tenantId), defaultRecord); // aggregation if (ctls.isEmpty()) { schemasToCtl.put(ctl, new ArrayList<>(asList(schema))); ctls.add(ctl); } else { Ctl ctlToCompare = sameFqn(ctls, ctl); if (ctlToCompare != null) { if (bothAppIdNull(ctlToCompare, ctl)) { if (sameTenant(ctlToCompare, ctl)) { aggregateSchemas(ctlToCompare, ctl, schema); } else { putToMapSchema(ctlToCompare, ctl, schema, "tenant"); } } else { if (sameAppId(ctlToCompare, ctl)) { aggregateSchemas(ctlToCompare, ctl, schema); } else { putToMapSchema(ctlToCompare, ctl, schema, "application"); } } } else { ctlToCompare = sameBody(ctls, ctl); if (ctlToCompare != null) { LOG.warn("Schemas {} and {} have different fqn but same body {}", ctl.getMetaInfo().getFqn(), ctlToCompare.getMetaInfo().getFqn(), ctl.getDefaultRecord()); } schemasToCtl.put(ctl, new ArrayList<>(asList(schema))); ctls.add(ctl); } } } // add CTLs to database for (Ctl ctl : ctls) { if (ctl.isExistInDb()) { continue; } CtlMetaInfo mi = ctl.getMetaInfo(); Schema schema = schemasToCtl.get(ctl).get(0); runner.insert(connection, "insert into ctl_metainfo values(?, ?, ?, ?)", new ScalarHandler<Long>(), mi.getId(), mi.getFqn(), mi.getAppId(), mi.getTenantId()); runner.insert(connection, "insert into ctl values(?, ?, ?, ?, ?, ?, ?)", new ScalarHandler<Long>(), ctl.getId(), schema.getSchems(), schema.getCreatedTime(), schema.getCreatedUsername(), ctl.getDefaultRecord(), schema.getVersion(), mi.getId()); } return schemasToCtl; }
From source file:org.kaaproject.kaa.server.datamigration.CtlEventsMigration.java
@Override protected List<Schema> transform() throws SQLException { // fetch schemas of appropriate feature like configuration List<Schema> schemas = new ArrayList<>(); final List<EventSchemaVersion> oldEsvs = runner.query(connection, "SELECT id, schems, created_time, created_username FROM " + EVENT_SCHEMA_VERSION_TABLE_NAME, new BeanListHandler<EventSchemaVersion>(EventSchemaVersion.class)); final List<EventClass> oldECs = runner .query(connection,// ww w. ja v a2 s . com "SELECT id, schems, version FROM " + EVENT_CLASS_TABLE_NAME + " WHERE schems not like '{\"type\":\"enum\"%'", new BeanListHandler<>(EventClass.class)); runner.update(connection, "ALTER TABLE " + EVENT_SCHEMA_VERSION_TABLE_NAME + " DROP COLUMN schems"); runner.update(connection, "ALTER TABLE " + EVENT_SCHEMA_VERSION_TABLE_NAME + " RENAME " + EVENT_CLASS_FAMILY_VERSION_TABLE_NAME); runner.update(connection, "ALTER TABLE " + EVENT_CLASS_TABLE_NAME + " CHANGE events_class_family_id events_class_family_versions_id bigint(20)"); runner.update(connection, "ALTER TABLE " + EVENT_CLASS_TABLE_NAME + " DROP COLUMN schems"); runner.update(connection, "ALTER TABLE " + EVENT_CLASS_TABLE_NAME + " DROP COLUMN version"); for (EventClass ec : oldECs) { updateFamilyVersionId(ec, oldEsvs, runner); } for (EventClass ec : oldECs) { EventSchemaVersion esv = findParent(ec, oldEsvs); Long id = ec.getId(); Long createdTime = esv.getCreatedTime(); String createUsername = esv.getCreatedUsername(); String description = null; String name = parseName(ec.getSchems()); String schems = ec.getSchems(); Integer version = ec.getVersion(); Long applicationId = null; String type = null; //fixme: what is type? Schema schema = new Schema(id, version, name, description, createUsername, createdTime, applicationId, schems, type); schemas.add(schema); } // shift ids in order to avoid PK constraint violation during adding record to base_schema Long shift = runner.query(connection, "select max(id) as max_id from " + EVENT_CLASS_TABLE_NAME, rs -> rs.next() ? rs.getLong("max_id") : null); Long idShift = BaseSchemaIdCounter.getInstance().getAndShift(shift); runner.update(connection, "update " + EVENT_CLASS_TABLE_NAME + " set id = id + " + idShift + " order by id desc"); runner.update(connection, "update " + APPLICATION_EVENT_MAP_TABLE_NAME + " set events_class_id = events_class_id + " + idShift + " order by id desc"); schemas.forEach(s -> s.setId(s.getId() + idShift)); return schemas; }
From source file:org.kaaproject.kaa.server.datamigration.UpdateUuidsMigration.java
/** * Change encoding of uuids from Latin1 to Base64 in relational and NoSQL databases. * *//*from ww w . jav a 2 s . co m*/ public void transform() throws IOException, SQLException { QueryRunner run = new QueryRunner(); ResultSetHandler<List<Configuration>> rsHandler = new BeanListHandler<>(Configuration.class); List<Configuration> configs = run.query(connection, "SELECT * FROM configuration", rsHandler); for (Configuration config : configs) { JsonNode json = new ObjectMapper().readTree(config.getConfigurationBody()); JsonNode jsonEncoded = encodeUuids(json); byte[] encodedConfigurationBody = jsonEncoded.toString().getBytes(); int updates = run.update(connection, "UPDATE configuration SET configuration_body=? WHERE id=?", encodedConfigurationBody, config.getId()); if (updates != 1) { System.err.println("Error: failed to update configuration: " + config); } } if (nosql.equals(Options.DEFAULT_NO_SQL)) { MongoDatabase database = client.getDatabase(dbName); MongoCollection<Document> userConfiguration = database.getCollection("user_configuration"); FindIterable<Document> documents = userConfiguration.find(); for (Document d : documents) { String body = (String) d.get("body"); JsonNode json = new ObjectMapper().readTree(body); JsonNode jsonEncoded = encodeUuids(json); userConfiguration.updateOne(Filters.eq("_id", d.get("_id")), Filters.eq("$set", Filters.eq("body", jsonEncoded))); } } else { Session session = cluster.connect(dbName); BatchStatement batchStatement = new BatchStatement(); String tableName = "user_conf"; ResultSet results = session.execute(select().from(tableName)); for (Row row : results) { String userId = row.getString("user_id"); String appToken = row.getString("app_token"); int schemaVersion = row.getInt("schema_version"); String body = row.getString("body"); String bodyEncoded = encodeUuids(new ObjectMapper().readTree(body)).toString(); batchStatement.add(update(tableName).with(set("body", bodyEncoded)).where(eq("user_id", userId)) .and(eq("app_token", appToken)).and(eq("schema_version", schemaVersion))); } session.execute(batchStatement); session.close(); cluster.close(); } }
From source file:org.yukung.tasklet.dao.impl.ActivityDaoImpl.java
@Override public List<Activity> findActivitiesByUserId(int userId, int limit, int offset) { String sql = getSQLFromPropertyFile("findActivitiesByUserId"); ResultSetHandler<List<Activity>> rsh = new BeanListHandler<Activity>(Activity.class); try {/* ww w .ja v a2 s . c om*/ return runner.query(sql, rsh, Integer.valueOf(userId), Integer.valueOf(limit), Integer.valueOf(offset)); } catch (SQLException e) { throw new DataAccessException(e.getMessage(), e); } }
From source file:org.yukung.tasklet.dao.impl.ActivityDaoImpl.java
@Override public List<Activity> getSeqByAscending(int userId) { String sql = getSQLFromPropertyFile("getSeqByAscending"); ResultSetHandler<List<Activity>> rsh = new BeanListHandler<Activity>(Activity.class); try {//from w w w . j a v a 2 s. c om return runner.query(sql, rsh, Integer.valueOf(userId)); } catch (SQLException e) { throw new DataAccessException(e.getMessage(), e); } }
From source file:org.yukung.tasklet.dao.impl.ActivityDaoImpl.java
@Override public List<Activity> getSeqByDescending(int userId) { String sql = getSQLFromPropertyFile("getSeqByDescending"); ResultSetHandler<List<Activity>> rsh = new BeanListHandler<Activity>(Activity.class); try {/*w ww . j a va 2s . c o m*/ return runner.query(sql, rsh, Integer.valueOf(userId)); } catch (SQLException e) { throw new DataAccessException(e.getMessage(), e); } }
From source file:org.yukung.tasklet.dao.impl.ActivityDaoImpl.java
@Override public List<Activity> getSortableInfo(int userId) { String sql = getSQLFromPropertyFile("getSortableInfo"); ResultSetHandler<List<Activity>> rsh = new BeanListHandler<Activity>(Activity.class); try {// w ww.j a v a2 s.co m return runner.query(sql, rsh, Integer.valueOf(userId)); } catch (SQLException e) { throw new DataAccessException(e.getMessage(), e); } }
From source file:org.yukung.tasklet.dao.impl.MemoDaoImpl.java
@Override public List<Memo> getMemosByTaskId(int taskId) { String sql = getSQLFromPropertyFile("getMemosByTaskId"); ResultSetHandler<List<Memo>> rsh = new BeanListHandler<Memo>(Memo.class); try {// w ww . ja v a 2 s . c o m return runner.query(sql, rsh, Integer.valueOf(taskId)); } catch (SQLException e) { throw new DataAccessException(e.getMessage(), e); } }
From source file:org.yukung.tasklet.dao.impl.TaskDaoImpl.java
@Override public List<Task> findTasksByActivityId(int activityId) { String sql = getSQLFromPropertyFile("findTasksByActivityId"); ResultSetHandler<List<Task>> rsh = new BeanListHandler<Task>(Task.class); try {/* w w w .ja va 2s .c o m*/ return runner.query(sql, rsh, Integer.valueOf(activityId)); } catch (SQLException e) { throw new DataAccessException(e.getMessage(), e); } }