use of org.activityinfo.model.legacy.KeyGenerator in project activityinfo by bedatadriven.
the class UpdateMonthlyReportsAsync method executeUpdates.
private Promise<VoidResult> executeUpdates(SqlTransaction tx, UpdateMonthlyReports command, Map<Month, Integer> periodMap) {
KeyGenerator generator = new KeyGenerator();
List<Promise<Void>> pendingUpdates = new ArrayList<>();
for (UpdateMonthlyReports.Change change : command.getChanges()) {
Integer periodId = periodMap.get(change.getMonth());
if (periodId == null) {
periodId = generator.generateInt();
periodMap.put(change.getMonth(), periodId);
pendingUpdates.add(insertPeriod(tx, command.getSiteId(), periodId, change.getMonth()));
}
pendingUpdates.add(deleteValue(tx, periodId, change.getIndicatorId()));
if (change.getValue() != null) {
pendingUpdates.add(insertValue(tx, periodId, change.getIndicatorId(), change.getValue()));
}
}
return Promise.waitAll(pendingUpdates).then(Functions.<VoidResult>constant(null));
}
use of org.activityinfo.model.legacy.KeyGenerator in project activityinfo by bedatadriven.
the class LocationForm method saveNewLocation.
private void saveNewLocation() {
if (coordinateFields.validate() && nameField.validate() && axeField.validate()) {
LocationDTO newLocation = new LocationDTO();
newLocation.setId(new KeyGenerator().generateInt());
newLocation.setLocationTypeId(locationType.getId());
newLocation.setName(nameField.getValue());
newLocation.setAxe(axeField.getValue());
newLocation.setLatitude(coordinateFields.getLatitudeField().getValue());
newLocation.setLongitude(coordinateFields.getLongitudeField().getValue());
for (AdminLevelDTO level : adminPresenter.getAdminLevels()) {
newLocation.setAdminEntity(level.getId(), adminPresenter.getAdminEntity(level));
}
newLocationPresenter.accept(newLocation);
}
}
use of org.activityinfo.model.legacy.KeyGenerator in project activityinfo by bedatadriven.
the class ImportView method runUpdate.
/**
* Based on the users explict choices and the automatic matching / mapping,
* build a transaction to effect the import.
* @param client
*/
public void runUpdate(GeoAdminClient client) {
RecordTransactionBuilder tx = new RecordTransactionBuilder();
ResourceId targetFormId = model.getTargetFormId().get();
KeyGenerator generator = new KeyGenerator();
Map<ResourceId, ResourceId> idMap = new HashMap<>();
MatchTable matchTable = getMatchTable();
int numRows = matchTable.getRowCount();
for (int i = 0; i < numRows; i++) {
MatchRow matchRow = matchTable.get(i);
if (!matchRow.isMatched(MatchSide.SOURCE)) {
// no corresponding row in the source:
// delete unmatched target
tx.delete(targetFormId, matchRow.getTargetId().get());
} else {
RecordUpdate update;
ResourceId targetId;
if (matchRow.isMatched(MatchSide.TARGET)) {
// update target with properties from the source
targetId = matchRow.getTargetId().get();
update = tx.update(targetFormId, targetId);
} else {
// create a new instance with properties from the source
targetId = CuidAdapter.entity(generator.generateInt());
update = tx.create(targetFormId, targetId);
}
idMap.put(matchRow.getSourceId().get(), targetId);
// apply properties from field mapping
for (FieldMapping fieldMapping : mapping.get().getFieldMappings()) {
update.setFieldValue(fieldMapping.getTargetFieldId(), fieldMapping.mapFieldValue(matchRow.getSourceRow()));
}
}
}
client.executeTransaction(tx);
try {
updateGeometry(client, idMap);
} catch (IOException e) {
throw new RuntimeException("Exception updating geometry");
}
}
use of org.activityinfo.model.legacy.KeyGenerator in project activityinfo by bedatadriven.
the class GcsBlobFieldStorageServiceTest method blobPermissionAttack.
/**
* 1. user1 : persist blob with FormInstance1 (FormClass1) user1
* 2. user2 : persist the same blob with FormInstance2 (FormClass2) -> try to steal blob access
*/
@Test
@OnDataSet("/dbunit/sites-simple-blob-security.db.xml")
public void blobPermissionAttack() throws IOException {
blobService.setTestBucketName();
int activityId = 1;
int databaseId = 1;
int locationType = 10;
ResourceId attachmentFieldId = ResourceId.generateFieldId(AttachmentType.TYPE_CLASS);
FormClass formClass = addAttachmentField(activityId, attachmentFieldId);
blobId = BlobId.generate();
blobService.put(user, "attachment;filename=" + FILE_NAME, MimeTypeUtil.mimeTypeFromFileName(FILE_NAME), blobId, formClass.getId(), GcsBlobFieldStorageServiceTest.class.getResourceAsStream("goabout.png"));
FormInstance instance = new FormInstance(CuidAdapter.cuid(SITE_DOMAIN, new KeyGenerator().generateInt()), formClass.getId());
Attachment attachment = new Attachment();
attachment.setMimeType(MimeTypeUtil.mimeTypeFromFileName(FILE_NAME));
attachment.setBlobId(blobId.asString());
attachment.setFilename(FILE_NAME);
AttachmentValue attachmentValue = new AttachmentValue();
attachmentValue.getValues().add(attachment);
instance.set(indicatorField(1), 1);
instance.set(indicatorField(2), 2);
instance.set(attachmentFieldId, attachmentValue);
instance.set(locationField(activityId), locationRef(CuidAdapter.locationFormClass(locationType), 1));
instance.set(partnerField(activityId), partnerRef(databaseId, 1));
instance.set(projectField(activityId), projectRef(databaseId, 1));
instance.set(field(formClass.getId(), START_DATE_FIELD), new LocalDate(2014, 1, 1));
instance.set(field(formClass.getId(), END_DATE_FIELD), new LocalDate(2014, 1, 1));
instance.set(field(formClass.getId(), COMMENT_FIELD), "My comment");
assertResolves(locator.persist(instance));
assertInstanceExists(formClass.getId(), instance.getId());
AuthenticationModuleStub.setUserId(USER_WITHOUT_ACCESS_TO_DB_1);
int anotherActivityId = 32;
ResourceId newAttachmentFieldId = ResourceId.generateFieldId(AttachmentType.TYPE_CLASS);
addAttachmentField(anotherActivityId, newAttachmentFieldId);
instance.setId(CuidAdapter.cuid(SITE_DOMAIN, new KeyGenerator().generateInt()));
instance.setClassId(CuidAdapter.activityFormClass(anotherActivityId));
instance.set(newAttachmentFieldId, attachmentValue);
instance.set(field(instance.getFormId(), START_DATE_FIELD), new LocalDate(2014, 1, 1));
instance.set(field(instance.getFormId(), END_DATE_FIELD), new LocalDate(2014, 1, 1));
instance.set(partnerField(anotherActivityId), partnerRef(databaseId, 1));
boolean persisted = true;
try {
// this must fail because of blob permission check
assertResolves(locator.persist(instance));
} catch (RuntimeException e) {
e.printStackTrace();
persisted = false;
}
assertFalse("Access to blob is stolen! Permissions check for blobs is broken.", persisted);
}
use of org.activityinfo.model.legacy.KeyGenerator in project activityinfo by bedatadriven.
the class CreateSiteTest method testAllAttribsFalse.
@Test
public void testAllAttribsFalse() throws CommandException {
// create a new detached, client model
SiteDTO newSite = new SiteDTO();
newSite.setId(new KeyGenerator().generateInt());
newSite.setActivityId(1);
newSite.setLocationId(1);
newSite.setPartner(new PartnerDTO(1, "Foobar"));
newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
newSite.setLocationName("Virunga");
newSite.setAttributeValue(1, false);
newSite.setAttributeValue(2, false);
newSite.setProject(new ProjectDTO(1, "SomeProject"));
// create command
CreateSite cmd = new CreateSite(newSite);
assertThat((Integer) cmd.getProperties().get("locationId"), equalTo(1));
// execute the command
setUser(1);
CreateResult result = execute(cmd);
// let the client know the command has succeeded
newSite.setId(result.getNewId());
// try to retrieve what we've created
SiteDTO secondRead = readSite(newSite.getId());
// confirm that the changes are there
Assert.assertEquals("site.attribute[2]", false, secondRead.getAttributeValue(1));
Assert.assertEquals("site.attribute[2]", false, secondRead.getAttributeValue(2));
}
Aggregations