Search in sources :

Example 1 with KeyGenerator

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));
}
Also used : Promise(org.activityinfo.promise.Promise) UpdateMonthlyReports(org.activityinfo.legacy.shared.command.UpdateMonthlyReports) ArrayList(java.util.ArrayList) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator)

Example 2 with KeyGenerator

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);
    }
}
Also used : AdminLevelDTO(org.activityinfo.legacy.shared.model.AdminLevelDTO) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) LocationDTO(org.activityinfo.legacy.shared.model.LocationDTO)

Example 3 with KeyGenerator

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");
    }
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) RecordUpdate(org.activityinfo.model.resource.RecordUpdate) ResourceId(org.activityinfo.model.resource.ResourceId) HashMap(java.util.HashMap) FieldMapping(org.activityinfo.geoadmin.merge2.view.mapping.FieldMapping) IOException(java.io.IOException) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator)

Example 4 with KeyGenerator

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);
}
Also used : AttachmentValue(org.activityinfo.model.type.attachment.AttachmentValue) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) Attachment(org.activityinfo.model.type.attachment.Attachment) FormInstance(org.activityinfo.model.form.FormInstance) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) LocalDate(org.activityinfo.model.type.time.LocalDate) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 5 with KeyGenerator

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));
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) GregorianCalendar(java.util.GregorianCalendar) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) CreateSite(org.activityinfo.legacy.shared.command.CreateSite) Test(org.junit.Test)

Aggregations

KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)18 Test (org.junit.Test)9 FormClass (org.activityinfo.model.form.FormClass)6 FormField (org.activityinfo.model.form.FormField)6 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)6 FormInstance (org.activityinfo.model.form.FormInstance)5 ResourceId (org.activityinfo.model.resource.ResourceId)4 GregorianCalendar (java.util.GregorianCalendar)3 CreateSite (org.activityinfo.legacy.shared.command.CreateSite)3 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)3 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)3 FieldValue (org.activityinfo.model.type.FieldValue)3 LocalDate (org.activityinfo.model.type.time.LocalDate)3 OnDataSet (org.activityinfo.server.database.OnDataSet)3 SiteDTO (org.activityinfo.legacy.shared.model.SiteDTO)2 RecordTransactionBuilder (org.activityinfo.model.resource.RecordTransactionBuilder)2 AttachmentValue (org.activityinfo.model.type.attachment.AttachmentValue)2 Quantity (org.activityinfo.model.type.number.Quantity)2 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)2 Optional (com.google.common.base.Optional)1