Search in sources :

Example 1 with DatabaseTargetForm

use of org.activityinfo.store.mysql.metadata.DatabaseTargetForm in project activityinfo by bedatadriven.

the class TargetFormProvider method openForms.

@Override
public Map<ResourceId, FormStorage> openForms(QueryExecutor executor, Set<ResourceId> formIds) throws SQLException {
    Set<Integer> targetIds = Sets.newHashSet();
    for (ResourceId resourceId : formIds) {
        if (accept(resourceId)) {
            targetIds.add(CuidAdapter.getLegacyIdFromCuid(resourceId));
        }
    }
    Map<ResourceId, FormStorage> collectionMap = Maps.newHashMap();
    if (!targetIds.isEmpty()) {
        Map<Integer, DatabaseTargetForm> targetMap = Maps.newHashMap();
        try (ResultSet rs = executor.query("SELECT " + "D.DatabaseId, " + "D.Name, " + "I.IndicatorId, " + "I.Name, " + "I.Units, " + "A.schemaVersion " + " FROM userdatabase D " + " LEFT JOIN activity A ON (D.DatabaseId = A.DatabaseId and A.dateDeleted IS NULL) " + " LEFT JOIN indicator I ON (A.ActivityId=I.ActivityId and I.dateDeleted IS NULL and I.type = 'QUANTITY') " + " WHERE D.databaseID IN (" + Joiner.on(',').join(targetIds) + ")")) {
            while (rs.next()) {
                int databaseId = rs.getInt(1);
                DatabaseTargetForm target = targetMap.get(databaseId);
                if (target == null) {
                    target = new DatabaseTargetForm(databaseId, rs.getString(2), rs.getLong(6));
                    targetMap.put(databaseId, target);
                }
                int indicatorId = rs.getInt(3);
                if (!rs.wasNull()) {
                    target.addIndicator(indicatorId, rs.getString(4), rs.getString(5));
                }
            }
        }
        for (DatabaseTargetForm target : targetMap.values()) {
            collectionMap.put(target.getFormClassId(), new TargetFormStorage(executor, target));
        }
    }
    return collectionMap;
}
Also used : FormStorage(org.activityinfo.store.spi.FormStorage) ResourceId(org.activityinfo.model.resource.ResourceId) ResultSet(java.sql.ResultSet) DatabaseTargetForm(org.activityinfo.store.mysql.metadata.DatabaseTargetForm)

Aggregations

ResultSet (java.sql.ResultSet)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 DatabaseTargetForm (org.activityinfo.store.mysql.metadata.DatabaseTargetForm)1 FormStorage (org.activityinfo.store.spi.FormStorage)1