Search in sources :

Example 1 with AddTarget

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

the class AddTargetHandler method execute.

@Override
public CommandResult execute(AddTarget cmd, User user) throws CommandException {
    TargetDTO form = cmd.getTargetDTO();
    UserDatabase db = em.find(UserDatabase.class, cmd.getDatabaseId());
    Partner partner = null;
    if (form.get("partnerId") != null) {
        partner = em.find(Partner.class, form.get("partnerId"));
    }
    Project project = null;
    if (form.get("projectId") != null) {
        project = em.find(Project.class, form.get("projectId"));
    }
    AdminEntity adminEntity = null;
    // if(form.getAdminEntity() != null){
    // adminEntity = em.find(AdminEntity.class,
    // form.getAdminEntity().getId());
    // }
    Target target = new Target();
    target.setName(form.getName());
    target.setUserDatabase(db);
    target.setAdminEntity(adminEntity);
    target.setPartner(partner);
    target.setProject(project);
    target.setDate1(form.getDate1());
    target.setDate2(form.getDate2());
    db.setLastSchemaUpdate(new Date());
    em.persist(target);
    em.persist(db);
    db.getTargets().add(target);
    // }
    if (project != null) {
        project.getTargets().add(target);
    }
    if (partner != null) {
        partner.getTargets().add(target);
    }
    return new CreateResult(target.getId());
}
Also used : Project(org.activityinfo.server.database.hibernate.entity.Project) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) Target(org.activityinfo.server.database.hibernate.entity.Target) AddTarget(org.activityinfo.shared.command.AddTarget) CreateResult(org.activityinfo.shared.command.result.CreateResult) UserDatabase(org.activityinfo.server.database.hibernate.entity.UserDatabase) TargetDTO(org.activityinfo.shared.dto.TargetDTO) Partner(org.activityinfo.server.database.hibernate.entity.Partner) Date(java.util.Date)

Example 2 with AddTarget

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

the class TargetTest method deleteTargetTest.

@Test
public void deleteTargetTest() {
    TargetDTO target = createTarget();
    CreateResult cresult = execute(new AddTarget(db.getId(), target));
    int newId = cresult.getNewId();
    /*
         * Load Targets to verify the changes have stuck
         */
    List<TargetDTO> targets = execute(new GetTargets(db.getId())).getData();
    TargetDTO dto = getTargetById(targets, newId);
    Assert.assertEquals("name", "Target0071", dto.getName());
    /*
         * Delete new target now
         */
    execute(new Delete(dto));
    /*
         * Verify if target is deleted.
         */
    targets = execute(new GetTargets()).getData();
    TargetDTO deleted = getTargetById(targets, newId);
    Assert.assertNull(deleted);
}
Also used : Delete(org.activityinfo.shared.command.Delete) GetTargets(org.activityinfo.shared.command.GetTargets) CreateResult(org.activityinfo.shared.command.result.CreateResult) AddTarget(org.activityinfo.shared.command.AddTarget) TargetDTO(org.activityinfo.shared.dto.TargetDTO) Test(org.junit.Test)

Example 3 with AddTarget

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

the class TargetTest method testTarget.

@Test
public void testTarget() throws CommandException {
    /*
         * Create a new Target
         */
    TargetDTO target = createTarget();
    CreateResult cresult = execute(new AddTarget(db.getId(), target));
    int newId = cresult.getNewId();
    /*
         * Load Targets to verify the changes have stuck
         */
    List<TargetDTO> targets = execute(new GetTargets(db.getId())).getData();
    TargetDTO dto = getTargetById(targets, newId);
    Assert.assertNotNull(dto);
    Assert.assertEquals("name", "Target0071", dto.getName());
}
Also used : GetTargets(org.activityinfo.shared.command.GetTargets) CreateResult(org.activityinfo.shared.command.result.CreateResult) AddTarget(org.activityinfo.shared.command.AddTarget) TargetDTO(org.activityinfo.shared.dto.TargetDTO) Test(org.junit.Test)

Example 4 with AddTarget

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

the class DbTargetEditor method onAdd.

@Override
protected void onAdd() {
    final TargetDTO newTarget = new TargetDTO();
    this.view.showAddDialog(newTarget, db, new FormDialogCallback() {

        @Override
        public void onValidated(final FormDialogTether dlg) {
            service.execute(new AddTarget(db.getId(), newTarget), dlg, new AsyncCallback<CreateResult>() {

                @Override
                public void onFailure(Throwable caught) {
                    MessageBox.alert(I18N.CONSTANTS.error(), I18N.CONSTANTS.errorOnServer(), null);
                }

                @Override
                public void onSuccess(CreateResult result) {
                    newTarget.setId(result.getNewId());
                    PartnerDTO partner = db.getPartnerById((Integer) newTarget.get("partnerId"));
                    newTarget.setPartner(partner);
                    ProjectDTO project = db.getProjectById((Integer) newTarget.get("projectId"));
                    newTarget.setProject(project);
                    store.add(newTarget);
                    store.commitChanges();
                    eventBus.fireEvent(AppEvents.SCHEMA_CHANGED);
                    dlg.hide();
                }
            });
        }
    });
}
Also used : ProjectDTO(org.activityinfo.shared.dto.ProjectDTO) FormDialogCallback(org.activityinfo.client.page.common.dialog.FormDialogCallback) CreateResult(org.activityinfo.shared.command.result.CreateResult) PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) FormDialogTether(org.activityinfo.client.page.common.dialog.FormDialogTether) AddTarget(org.activityinfo.shared.command.AddTarget) TargetDTO(org.activityinfo.shared.dto.TargetDTO)

Aggregations

AddTarget (org.activityinfo.shared.command.AddTarget)4 CreateResult (org.activityinfo.shared.command.result.CreateResult)4 TargetDTO (org.activityinfo.shared.dto.TargetDTO)4 GetTargets (org.activityinfo.shared.command.GetTargets)2 Test (org.junit.Test)2 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1 Date (java.util.Date)1 FormDialogCallback (org.activityinfo.client.page.common.dialog.FormDialogCallback)1 FormDialogTether (org.activityinfo.client.page.common.dialog.FormDialogTether)1 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)1 Partner (org.activityinfo.server.database.hibernate.entity.Partner)1 Project (org.activityinfo.server.database.hibernate.entity.Project)1 Target (org.activityinfo.server.database.hibernate.entity.Target)1 UserDatabase (org.activityinfo.server.database.hibernate.entity.UserDatabase)1 Delete (org.activityinfo.shared.command.Delete)1 PartnerDTO (org.activityinfo.shared.dto.PartnerDTO)1 ProjectDTO (org.activityinfo.shared.dto.ProjectDTO)1