Search in sources :

Example 1 with UserDatabaseMeta

use of org.activityinfo.model.database.UserDatabaseMeta in project activityinfo by bedatadriven.

the class LockedPeriodTest method createForFolder.

@Test
public void createForFolder() {
    setUser(1);
    LockedPeriodDTO dto = new LockedPeriodDTO();
    dto.setName("NFI ");
    dto.setFromDate(new LocalDate(2011, 1, 1));
    dto.setToDate(new LocalDate(2011, 1, 31));
    dto.setEnabled(true);
    CreateLockedPeriod create = new CreateLockedPeriod(dto);
    create.setFolderId(1);
    execute(create);
    // Now verify that they appear
    SchemaDTO schema = execute(new GetSchema());
    assertThat(schema.getDatabaseById(1).getFolderById(1).getLockedPeriods(), Matchers.hasSize(1));
    LockedPeriodSet locks = new LockedPeriodSet(schema);
    assertTrue(locks.isActivityLocked(1, new LocalDate(2011, 1, 1)));
    // Verify that the new code works too...
    DatabaseProviderImpl provider = new DatabaseProviderImpl(Providers.of(em));
    UserDatabaseMeta metadata = provider.getDatabaseMetadata(CuidAdapter.databaseId(1), 1);
    ArrayList<RecordLock> folderLocks = Lists.newArrayList(metadata.getEffectiveLocks(CuidAdapter.folderId(1)));
    ArrayList<RecordLock> formLocks = Lists.newArrayList(metadata.getEffectiveLocks(CuidAdapter.activityFormClass(1)));
    assertThat(folderLocks, Matchers.hasSize(2));
    assertThat(formLocks, Matchers.hasSize(3));
}
Also used : DatabaseProviderImpl(org.activityinfo.server.endpoint.rest.DatabaseProviderImpl) RecordLock(org.activityinfo.model.database.RecordLock) LockedPeriodSet(org.activityinfo.legacy.shared.model.LockedPeriodSet) LocalDate(com.bedatadriven.rebar.time.calendar.LocalDate) SchemaDTO(org.activityinfo.legacy.shared.model.SchemaDTO) GetSchema(org.activityinfo.legacy.shared.command.GetSchema) UserDatabaseMeta(org.activityinfo.model.database.UserDatabaseMeta) CreateLockedPeriod(org.activityinfo.legacy.shared.command.CreateLockedPeriod) LockedPeriodDTO(org.activityinfo.legacy.shared.model.LockedPeriodDTO) Test(org.junit.Test)

Example 2 with UserDatabaseMeta

use of org.activityinfo.model.database.UserDatabaseMeta in project activityinfo by bedatadriven.

the class FormResource method getMetadataResponse.

@GET
@NoCache
@Produces(JSON_CONTENT_TYPE)
public FormMetadata getMetadataResponse(@InjectParam DatabaseProviderImpl databaseProvider, @InjectParam AuthenticatedUser user, @QueryParam("localVersion") Long localVersion) {
    Optional<FormStorage> storage = backend.getStorage().getForm(formId);
    if (!storage.isPresent()) {
        return FormMetadata.notFound(formId);
    }
    ResourceId databaseId = storage.get().getFormClass().getDatabaseId();
    UserDatabaseMeta databaseMetadata;
    try {
        databaseMetadata = databaseProvider.getDatabaseMetadata(databaseId, user.getUserId());
    } catch (Exception e) {
        // We are initially using this just for locks,
        // not actually permissions, so just log the warning for now.
        LOGGER.log(Level.SEVERE, "Failed to retrieve metadata for database " + databaseId + " for user " + user.getUserId(), e);
        databaseMetadata = new UserDatabaseMeta.Builder().setDatabaseId(databaseId).setLabel("").setOwner(false).build();
    }
    FormPermissions permissions = backend.getFormSupervisor().getFormPermissions(formId);
    if (!permissions.isVisible()) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    } else {
        // Workaround for sub form, which we don't yet have indexed to the
        // database in which they live.
        FormClass schema = storage.get().getFormClass();
        RecordLockSet locks;
        if (schema.isSubForm()) {
            locks = databaseMetadata.getEffectiveLocks(schema.getParentFormId().get());
        } else {
            locks = databaseMetadata.getEffectiveLocks(formId);
        }
        return new FormMetadata.Builder().setId(formId).setPermissions(permissions).setSchema(schema).setLocks(locks).setVersion(storage.get().cacheVersion()).build();
    }
}
Also used : VersionedFormStorage(org.activityinfo.store.spi.VersionedFormStorage) FormStorage(org.activityinfo.store.spi.FormStorage) ResourceId(org.activityinfo.model.resource.ResourceId) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) JsonFormTreeBuilder(org.activityinfo.model.formTree.JsonFormTreeBuilder) XlsFormBuilder(org.activityinfo.io.xlsform.XlsFormBuilder) RecordLockSet(org.activityinfo.model.database.RecordLockSet) UserDatabaseMeta(org.activityinfo.model.database.UserDatabaseMeta) NotFoundException(com.sun.jersey.api.NotFoundException) SQLException(java.sql.SQLException) InvalidUpdateException(org.activityinfo.store.query.server.InvalidUpdateException) ParseException(com.vividsolutions.jts.io.ParseException)

Example 3 with UserDatabaseMeta

use of org.activityinfo.model.database.UserDatabaseMeta in project activityinfo by bedatadriven.

the class GetSchemaTest method testDatabaseMetadata.

@Test
public void testDatabaseMetadata() {
    DatabaseProviderImpl impl = injector.getInstance(DatabaseProviderImpl.class);
    UserDatabaseMeta metadata = impl.getDatabaseMetadata(CuidAdapter.databaseId(1), 1);
    System.out.println(Json.stringify(metadata.toJson(), 4));
}
Also used : DatabaseProviderImpl(org.activityinfo.server.endpoint.rest.DatabaseProviderImpl) UserDatabaseMeta(org.activityinfo.model.database.UserDatabaseMeta) Test(org.junit.Test)

Aggregations

UserDatabaseMeta (org.activityinfo.model.database.UserDatabaseMeta)3 DatabaseProviderImpl (org.activityinfo.server.endpoint.rest.DatabaseProviderImpl)2 Test (org.junit.Test)2 LocalDate (com.bedatadriven.rebar.time.calendar.LocalDate)1 NotFoundException (com.sun.jersey.api.NotFoundException)1 ParseException (com.vividsolutions.jts.io.ParseException)1 SQLException (java.sql.SQLException)1 XlsFormBuilder (org.activityinfo.io.xlsform.XlsFormBuilder)1 CreateLockedPeriod (org.activityinfo.legacy.shared.command.CreateLockedPeriod)1 GetSchema (org.activityinfo.legacy.shared.command.GetSchema)1 LockedPeriodDTO (org.activityinfo.legacy.shared.model.LockedPeriodDTO)1 LockedPeriodSet (org.activityinfo.legacy.shared.model.LockedPeriodSet)1 SchemaDTO (org.activityinfo.legacy.shared.model.SchemaDTO)1 RecordLock (org.activityinfo.model.database.RecordLock)1 RecordLockSet (org.activityinfo.model.database.RecordLockSet)1 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)1 JsonFormTreeBuilder (org.activityinfo.model.formTree.JsonFormTreeBuilder)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 InvalidUpdateException (org.activityinfo.store.query.server.InvalidUpdateException)1 FormStorage (org.activityinfo.store.spi.FormStorage)1