Search in sources :

Example 1 with CreateLockedPeriod

use of org.activityinfo.legacy.shared.command.CreateLockedPeriod 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 CreateLockedPeriod

use of org.activityinfo.legacy.shared.command.CreateLockedPeriod in project activityinfo by bedatadriven.

the class LockedPeriodTest method createTest.

@Test
public void createTest() throws CommandException {
    setUser(1);
    LockedPeriodDTO dto = new LockedPeriodDTO();
    dto.setName("my name");
    dto.setFromDate(new LocalDate(2011, 1, 1));
    dto.setToDate(new LocalDate(2011, 1, 31));
    dto.setEnabled(true);
    CreateLockedPeriod create = new CreateLockedPeriod(dto);
    create.setDatabaseId(1);
    CreateResult result = execute(create);
    Map<String, Object> changes = Maps.newHashMap();
    changes.put("toDate", new LocalDate(2011, 2, 28));
    execute(new UpdateEntity("LockedPeriod", result.getNewId(), changes));
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) UpdateEntity(org.activityinfo.legacy.shared.command.UpdateEntity) LocalDate(com.bedatadriven.rebar.time.calendar.LocalDate) CreateLockedPeriod(org.activityinfo.legacy.shared.command.CreateLockedPeriod) LockedPeriodDTO(org.activityinfo.legacy.shared.model.LockedPeriodDTO) Test(org.junit.Test)

Example 3 with CreateLockedPeriod

use of org.activityinfo.legacy.shared.command.CreateLockedPeriod in project activityinfo by bedatadriven.

the class CreateLockedPeriodHandler method execute.

@Override
public CommandResult execute(CreateLockedPeriod cmd, User user) {
    Activity activity;
    Database database;
    Project project;
    Folder folder;
    LockedPeriod lockedPeriod = new LockedPeriod();
    LockedPeriodDTO lockedPeriodDTO = cmd.getLockedPeriod();
    lockedPeriod.setFromDate(lockedPeriodDTO.getFromDate().atMidnightInMyTimezone());
    lockedPeriod.setToDate(lockedPeriodDTO.getToDate().atMidnightInMyTimezone());
    lockedPeriod.setName(lockedPeriodDTO.getName());
    lockedPeriod.setEnabled(lockedPeriodDTO.isEnabled());
    int databaseId;
    if (cmd.getDatabaseId() != 0) {
        database = em.find(Database.class, cmd.getDatabaseId());
        lockedPeriod.setDatabase(database);
        databaseId = database.getId();
        permissionOracle.assertDesignPrivileges(database, user);
    } else if (cmd.getProjectId() != 0) {
        project = em.find(Project.class, cmd.getProjectId());
        lockedPeriod.setProject(project);
        lockedPeriod.setDatabase(project.getDatabase());
        databaseId = project.getDatabase().getId();
        permissionOracle.assertDesignPrivileges(project.getDatabase(), user);
    } else if (cmd.getActivityId() != 0) {
        activity = em.find(Activity.class, cmd.getActivityId());
        lockedPeriod.setActivity(activity);
        lockedPeriod.setDatabase(activity.getDatabase());
        databaseId = activity.getDatabase().getId();
        permissionOracle.assertDesignPrivileges(activity, user);
    } else if (cmd.getFolderId() != 0) {
        folder = em.find(Folder.class, cmd.getFolderId());
        lockedPeriod.setFolder(folder);
        lockedPeriod.setDatabase(folder.getDatabase());
        databaseId = folder.getDatabase().getId();
        permissionOracle.assertDesignPrivileges(folder, user);
    } else {
        throw new CommandException("One of the following must be provided: userDatabaseId, projectId, activityId, folderId");
    }
    Database db = em.find(Database.class, databaseId);
    em.persist(lockedPeriod);
    db.setLastSchemaUpdate(new Date());
    em.persist(db);
    return new CreateResult(lockedPeriod.getId());
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) CommandException(org.activityinfo.legacy.shared.exception.CommandException) CreateLockedPeriod(org.activityinfo.legacy.shared.command.CreateLockedPeriod) Date(java.util.Date) LockedPeriodDTO(org.activityinfo.legacy.shared.model.LockedPeriodDTO)

Aggregations

CreateLockedPeriod (org.activityinfo.legacy.shared.command.CreateLockedPeriod)3 LockedPeriodDTO (org.activityinfo.legacy.shared.model.LockedPeriodDTO)3 LocalDate (com.bedatadriven.rebar.time.calendar.LocalDate)2 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)2 Test (org.junit.Test)2 Date (java.util.Date)1 GetSchema (org.activityinfo.legacy.shared.command.GetSchema)1 UpdateEntity (org.activityinfo.legacy.shared.command.UpdateEntity)1 CommandException (org.activityinfo.legacy.shared.exception.CommandException)1 LockedPeriodSet (org.activityinfo.legacy.shared.model.LockedPeriodSet)1 SchemaDTO (org.activityinfo.legacy.shared.model.SchemaDTO)1 RecordLock (org.activityinfo.model.database.RecordLock)1 UserDatabaseMeta (org.activityinfo.model.database.UserDatabaseMeta)1 DatabaseProviderImpl (org.activityinfo.server.endpoint.rest.DatabaseProviderImpl)1