Search in sources :

Example 11 with LocalDate

use of org.activityinfo.model.type.time.LocalDate in project activityinfo by bedatadriven.

the class QueryFilter method dateFilter.

private Collection<FunctionCallNode> dateFilter(String dateField, DateRange range) {
    SymbolNode dateExpr = new SymbolNode(dateField);
    List<FunctionCallNode> conditions = Lists.newArrayList();
    if (range != null) {
        if (range.getMinLocalDate() != null) {
            conditions.add(new FunctionCallNode(GreaterOrEqualFunction.INSTANCE, dateExpr, new ConstantNode(new LocalDate(range.getMinDate()))));
        }
        if (range.getMaxLocalDate() != null) {
            conditions.add(new FunctionCallNode(LessOrEqualFunction.INSTANCE, dateExpr, new ConstantNode(new LocalDate(range.getMaxDate()))));
        }
    }
    return conditions;
}
Also used : LocalDate(org.activityinfo.model.type.time.LocalDate)

Example 12 with LocalDate

use of org.activityinfo.model.type.time.LocalDate in project activityinfo by bedatadriven.

the class YearFracFunctionTest method check.

private void check(String startDateString, String endDateString, double expectedFrac) {
    LocalDate startDate = LocalDate.parse(startDateString);
    LocalDate endDate = LocalDate.parse(endDateString);
    double frac = YearFracFunction.compute(startDate, endDate);
    double diff = Math.abs(frac - expectedFrac);
    if (diff > 0.001) {
        throw new AssertionError(String.format("Expected YEARFRAC(%s, %s) = %10.5f, but was %10.5f", startDateString, endDateString, expectedFrac, frac));
    }
}
Also used : LocalDate(org.activityinfo.model.type.time.LocalDate)

Example 13 with LocalDate

use of org.activityinfo.model.type.time.LocalDate in project activityinfo by bedatadriven.

the class YearFracFunctionTest method evaluation.

@Test
public void evaluation() {
    LocalDate x = new LocalDate(2016, 1, 1);
    LocalDate y = new LocalDate(2017, 1, 1);
    Quantity z = (Quantity) YearFracFunction.INSTANCE.apply(Arrays.<FieldValue>asList(x, y));
    assertThat(z, equalTo(new Quantity(1.0)));
}
Also used : Quantity(org.activityinfo.model.type.number.Quantity) FieldValue(org.activityinfo.model.type.FieldValue) LocalDate(org.activityinfo.model.type.time.LocalDate) Test(org.junit.Test)

Example 14 with LocalDate

use of org.activityinfo.model.type.time.LocalDate 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 15 with LocalDate

use of org.activityinfo.model.type.time.LocalDate in project activityinfo by bedatadriven.

the class ActivityFormClassBuilderTest method nullLocationTypeIsNotVisible.

@Test
@OnDataSet("/dbunit/chad-form.db.xml")
public void nullLocationTypeIsNotVisible() {
    setUser(9944);
    int databaseId = 1470;
    FormClass formClass = assertResolves(locator.getFormClass(CuidAdapter.activityFormClass(11218)));
    ResourceId locationFieldId = CuidAdapter.field(formClass.getId(), CuidAdapter.LOCATION_FIELD);
    assertThat(formClass.getFields(), not(hasItem(withId(locationFieldId))));
    // Make sure we can update if location is not specified
    FormInstance instance = new FormInstance(CuidAdapter.newLegacyFormInstanceId(formClass.getId()), formClass.getId());
    instance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.START_DATE_FIELD), new LocalDate(2014, 1, 1));
    instance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.END_DATE_FIELD), new LocalDate(2014, 1, 2));
    instance.set(CuidAdapter.field(formClass.getId(), CuidAdapter.PARTNER_FIELD), CuidAdapter.partnerRef(databaseId, 1734));
    instance.set(ResourceId.valueOf("Q0000031845"), new EnumValue(CuidAdapter.attributeField(166617)));
    assertResolves(locator.persist(instance));
    // Make sure the null location object is visible to legacy code
    SiteDTO site = execute(GetSites.byId(CuidAdapter.getLegacyIdFromCuid(instance.getId()))).getData().get(0);
    assertThat(site.getLocationName(), equalTo("Chad"));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) SiteDTO(org.activityinfo.legacy.shared.model.SiteDTO) FormInstance(org.activityinfo.model.form.FormInstance) LocalDate(org.activityinfo.model.type.time.LocalDate) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Aggregations

LocalDate (org.activityinfo.model.type.time.LocalDate)24 Test (org.junit.Test)11 Quantity (org.activityinfo.model.type.number.Quantity)8 FormInstance (org.activityinfo.model.form.FormInstance)7 RecordRef (org.activityinfo.model.type.RecordRef)7 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)6 ResourceId (org.activityinfo.model.resource.ResourceId)5 OnDataSet (org.activityinfo.server.database.OnDataSet)5 FormClass (org.activityinfo.model.form.FormClass)4 FieldValue (org.activityinfo.model.type.FieldValue)4 ReferenceValue (org.activityinfo.model.type.ReferenceValue)4 Date (java.util.Date)3 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)3 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)3 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)3 FormField (org.activityinfo.model.form.FormField)2 FormulaSyntaxException (org.activityinfo.model.formula.diagnostic.FormulaSyntaxException)2 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)2 FilterConfig (com.sencha.gxt.data.shared.loader.FilterConfig)1 Calendar (java.util.Calendar)1