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;
}
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));
}
}
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)));
}
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);
}
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"));
}
Aggregations