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