Search in sources :

Example 1 with CreateLockedPeriod

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

the class LockedPeriodsPresenter method onCreate.

@Override
public void onCreate(CreateEvent event) {
    view.getCreatingMonitor().beforeRequest();
    final LockedPeriodDTO lockedPeriod = view.getValue();
    CreateLockedPeriod lockUserDatabase = new CreateLockedPeriod(lockedPeriod);
    if (lockedPeriod.getParent() instanceof ActivityDTO) {
        lockUserDatabase.setActivityId(lockedPeriod.getParent().getId());
    }
    if (lockedPeriod.getParent() instanceof ProjectDTO) {
        lockUserDatabase.setProjectId(lockedPeriod.getParent().getId());
    }
    if (lockedPeriod.getParent() instanceof UserDatabaseDTO) {
        lockUserDatabase.setUserDatabaseId(lockedPeriod.getParent().getId());
    }
    service.execute(lockUserDatabase, new AsyncCallback<CreateResult>() {

        @Override
        public void onFailure(Throwable caught) {
            view.getCreatingMonitor().onServerError();
            MessageBox.alert(I18N.CONSTANTS.error(), I18N.CONSTANTS.errorOnServer() + "\n\n" + caught.getMessage(), null);
        }

        @Override
        public void onSuccess(CreateResult result) {
            // Update the Id for the child instance
            lockedPeriod.setId(result.getNewId());
            // Tell the view there's a new kid on the block
            view.create(lockedPeriod);
            // Actually add the lock to it's parent
            lockedPeriod.getParent().getLockedPeriods().add(lockedPeriod);
        }
    });
}
Also used : ProjectDTO(org.activityinfo.shared.dto.ProjectDTO) CreateResult(org.activityinfo.shared.command.result.CreateResult) UserDatabaseDTO(org.activityinfo.shared.dto.UserDatabaseDTO) ActivityDTO(org.activityinfo.shared.dto.ActivityDTO) CreateLockedPeriod(org.activityinfo.shared.command.CreateLockedPeriod) LockedPeriodDTO(org.activityinfo.shared.dto.LockedPeriodDTO)

Example 2 with CreateLockedPeriod

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

the class LockedPeriodTest method createTest.

@Test
@OnDataSet("/dbunit/sites-simple1.db.xml")
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.setUserDatabaseId(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.shared.command.result.CreateResult) UpdateEntity(org.activityinfo.shared.command.UpdateEntity) LocalDate(com.bedatadriven.rebar.time.calendar.LocalDate) CreateLockedPeriod(org.activityinfo.shared.command.CreateLockedPeriod) LockedPeriodDTO(org.activityinfo.shared.dto.LockedPeriodDTO) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 3 with CreateLockedPeriod

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

the class CreateLockedPeriodHandler method execute.

@Override
public CommandResult execute(CreateLockedPeriod cmd, User user) throws CommandException {
    Activity activity = null;
    UserDatabase database = null;
    Project project = null;
    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.getUserDatabseId() != 0) {
        database = em.find(UserDatabase.class, cmd.getUserDatabseId());
        lockedPeriod.setUserDatabase(database);
        databaseId = database.getId();
    } else if (cmd.getProjectId() != 0) {
        project = em.find(Project.class, cmd.getProjectId());
        lockedPeriod.setProject(project);
        databaseId = project.getUserDatabase().getId();
    } else if (cmd.getActivityId() != 0) {
        activity = em.find(Activity.class, cmd.getActivityId());
        lockedPeriod.setActivity(activity);
        databaseId = activity.getDatabase().getId();
    } else {
        throw new CommandException("One of the following must be provdied: userDatabaseId, projectId, activityId");
    }
    UserDatabase db = em.find(UserDatabase.class, databaseId);
    em.persist(lockedPeriod);
    db.setLastSchemaUpdate(new Date());
    em.persist(db);
    if (database != null) {
        database.getLockedPeriods().add(lockedPeriod);
    }
    if (project != null) {
        project.getLockedPeriods().add(lockedPeriod);
    }
    if (activity != null) {
        activity.getLockedPeriods().add(lockedPeriod);
    }
    return new CreateResult(lockedPeriod.getId());
}
Also used : Project(org.activityinfo.server.database.hibernate.entity.Project) CreateResult(org.activityinfo.shared.command.result.CreateResult) UserDatabase(org.activityinfo.server.database.hibernate.entity.UserDatabase) Activity(org.activityinfo.server.database.hibernate.entity.Activity) CommandException(org.activityinfo.shared.exception.CommandException) CreateLockedPeriod(org.activityinfo.shared.command.CreateLockedPeriod) LockedPeriod(org.activityinfo.server.database.hibernate.entity.LockedPeriod) Date(java.util.Date) LockedPeriodDTO(org.activityinfo.shared.dto.LockedPeriodDTO)

Aggregations

CreateLockedPeriod (org.activityinfo.shared.command.CreateLockedPeriod)3 CreateResult (org.activityinfo.shared.command.result.CreateResult)3 LockedPeriodDTO (org.activityinfo.shared.dto.LockedPeriodDTO)3 LocalDate (com.bedatadriven.rebar.time.calendar.LocalDate)1 Date (java.util.Date)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 Activity (org.activityinfo.server.database.hibernate.entity.Activity)1 LockedPeriod (org.activityinfo.server.database.hibernate.entity.LockedPeriod)1 Project (org.activityinfo.server.database.hibernate.entity.Project)1 UserDatabase (org.activityinfo.server.database.hibernate.entity.UserDatabase)1 UpdateEntity (org.activityinfo.shared.command.UpdateEntity)1 ActivityDTO (org.activityinfo.shared.dto.ActivityDTO)1 ProjectDTO (org.activityinfo.shared.dto.ProjectDTO)1 UserDatabaseDTO (org.activityinfo.shared.dto.UserDatabaseDTO)1 CommandException (org.activityinfo.shared.exception.CommandException)1 Test (org.junit.Test)1