use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class MySqlStorageProvider method getFormClasses.
@Override
public Map<ResourceId, FormClass> getFormClasses(Collection<ResourceId> formIds) {
Map<ResourceId, FormClass> resultMap = new HashMap<>();
Set<ResourceId> toFetch = new HashSet<>();
// First check sessionCache for any collections which are already loaded
for (ResourceId collectionId : formIds) {
Optional<FormStorage> collection = sessionCache.getIfPresent(collectionId);
if (collection != null && collection.isPresent()) {
resultMap.put(collectionId, collection.get().getFormClass());
} else {
toFetch.add(collectionId);
}
}
// Now consult each of our providers for collections
try {
for (FormProvider provider : providers) {
Map<ResourceId, FormStorage> fetched = provider.openForms(executor, toFetch);
for (Map.Entry<ResourceId, FormStorage> entry : fetched.entrySet()) {
if (resultMap.containsKey(entry.getKey())) {
throw new IllegalStateException("Collection " + entry.getKey() + " returned by multiple providers");
}
sessionCache.put(entry.getKey(), Optional.of(entry.getValue()));
resultMap.put(entry.getKey(), entry.getValue().getFormClass());
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
// Finally, verify that all collections were loaded
for (ResourceId collectionId : formIds) {
if (!resultMap.containsKey(collectionId)) {
throw new FormNotFoundException(collectionId);
}
}
return resultMap;
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class HrdCatalogTest method simpleFormTest.
@Test
public void simpleFormTest() {
ResourceId collectionId = ResourceId.generateId();
ResourceId villageField = ResourceId.valueOf("FV");
ResourceId countField = ResourceId.valueOf("FC");
FormClass formClass = new FormClass(collectionId);
formClass.setParentFormId(ResourceId.valueOf("foo"));
formClass.setLabel("NFI Distributions");
formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
formClass.addField(countField).setLabel("Number of Beneficiaries").setCode("BENE").setType(new QuantityType("Beneficiaries"));
HrdStorageProvider catalog = new HrdStorageProvider();
catalog.create(formClass);
Optional<FormStorage> storage = catalog.getForm(collectionId);
assertTrue(storage.isPresent());
TypedRecordUpdate village1 = new TypedRecordUpdate();
village1.setUserId(userId);
village1.setRecordId(ResourceId.generateSubmissionId(formClass));
village1.set(villageField, TextValue.valueOf("Rutshuru"));
village1.set(countField, new Quantity(1000));
TypedRecordUpdate village2 = new TypedRecordUpdate();
village2.setUserId(userId);
village2.setRecordId(ResourceId.generateSubmissionId(formClass));
village2.set(villageField, TextValue.valueOf("Beni"));
village2.set(countField, new Quantity(230));
storage.get().add(village1);
storage.get().add(village2);
QueryModel queryModel = new QueryModel(collectionId);
queryModel.selectResourceId().as("id");
queryModel.selectField("VILLAGE").as("village");
queryModel.selectField("BENE").as("family_count");
queryModel.selectExpr("BENE*5").as("individual_count");
ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
ColumnSet columnSet = builder.build(queryModel);
System.out.println(columnSet);
assertThat(columnSet.getNumRows(), equalTo(2));
List<RecordVersion> versions1 = ((VersionedFormStorage) storage.get()).getVersions(village1.getRecordId());
assertThat(versions1, hasSize(1));
RecordVersion version = versions1.get(0);
assertThat(version.getRecordId(), equalTo(village1.getRecordId()));
assertThat(version.getUserId(), equalTo((long) userId));
assertThat(version.getType(), equalTo(RecordChangeType.CREATED));
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class HrdCatalogTest method versionRangeTest.
@Test
public void versionRangeTest() {
ResourceId collectionId = ResourceId.generateId();
ResourceId villageField = ResourceId.valueOf("FV");
ResourceId countField = ResourceId.valueOf("FC");
FormClass formClass = new FormClass(collectionId);
formClass.setParentFormId(ResourceId.valueOf("foo"));
formClass.setLabel("NFI Distributions");
formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
formClass.addField(countField).setLabel("Number of Beneficiaries").setCode("BENE").setType(new QuantityType("Beneficiaries"));
HrdStorageProvider catalog = new HrdStorageProvider();
catalog.create(formClass);
VersionedFormStorage formStorage = (VersionedFormStorage) catalog.getForm(collectionId).get();
// Initially, with no records added, the form should be at version 1
// and the version range (0, 1] should be empty.
assertThat(formStorage.cacheVersion(), equalTo(1L));
FormSyncSet updatedRecords = formStorage.getVersionRange(0, 1L, resourceId -> true);
assertTrue(updatedRecords.isEmpty());
// Add a new record
TypedRecordUpdate firstUpdate = new TypedRecordUpdate();
firstUpdate.setUserId(1);
firstUpdate.setRecordId(ResourceId.generateId());
firstUpdate.set(villageField, TextValue.valueOf("Goma"));
catalog.getForm(collectionId).get().add(firstUpdate);
// Verify that the version is incremented and the version range
// (0, 2] includes our new record
assertThat(formStorage.cacheVersion(), equalTo(2L));
FormSyncSet updated = formStorage.getVersionRange(0, 2L, resourceId -> true);
assertThat(updated.getUpdatedRecordCount(), equalTo(1));
// Update the first record and add a new one
TypedRecordUpdate secondUpdate = new TypedRecordUpdate();
secondUpdate.setUserId(1);
secondUpdate.setRecordId(ResourceId.generateId());
secondUpdate.set(villageField, TextValue.valueOf("Rutshuru"));
catalog.getForm(collectionId).get().add(firstUpdate);
// Verify that the version is incremented and the version range
// (1, 2] includes our new record
assertThat(formStorage.cacheVersion(), equalTo(3L));
updated = formStorage.getVersionRange(2L, 3L, resourceId -> true);
assertThat(updated.getUpdatedRecordCount(), equalTo(1));
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class FixSubForm method copySnapshots.
private void copySnapshots(PrintWriter logger, ResourceId parentFormId, ResourceId subFormId, ResourceId newSubFormId) {
QueryResultIterable<FormRecordSnapshotEntity> subRecords = ofy().load().type(FormRecordSnapshotEntity.class).ancestor(FormEntity.key(subFormId)).iterable();
for (FormRecordSnapshotEntity subRecord : subRecords) {
ResourceId parentRecordId = ResourceId.valueOf(subRecord.getParentRecordId());
String name = "sub record snapshot " + subRecord.getRecordId() + "@" + subRecord.getVersion();
if (isSubRecordOf(parentFormId, parentRecordId)) {
logger.println(name + " belongs to " + parentRecordId + " in " + parentFormId);
copySnapshot(subRecord, newSubFormId);
} else {
logger.println(name + " does not belong");
}
}
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class FixSubForm method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
final PrintWriter logger = new PrintWriter(resp.getOutputStream());
resp.setContentType("text/plain");
try (MySqlQueryExecutor executor = new MySqlQueryExecutor()) {
try (ResultSet rs = executor.query("select distinct i.activityid from indicator i " + "left join activity a on (a.activityid=i.activityid) " + "left join userdatabase db on (a.databaseid=db.databaseid) " + "where type='subform' and " + "i.datedeleted is null and " + "a.datedeleted is null and " + "db.datedeleted is null")) {
while (rs.next()) {
final ResourceId parentFormId = CuidAdapter.activityFormClass(rs.getInt(1));
if (maybeFixForm(logger, executor, parentFormId)) {
return;
}
}
}
} catch (Exception e) {
e.printStackTrace(logger);
} finally {
logger.flush();
}
}
Aggregations