Search in sources :

Example 1 with AddPartner

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

the class DbPartnerEditor method onAdd.

// public void onSelectionChanged(PartnerDTO selectedItem) {
// this.view.setActionEnabled(UIActions.delete, selectedItem != null);
// }
@Override
protected void onAdd() {
    final PartnerDTO newPartner = new PartnerDTO();
    this.view.showAddDialog(newPartner, new FormDialogCallback() {

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

                @Override
                public void onFailure(Throwable caught) {
                    Log.debug("DbPartnerEditor caught exception while executing command AddPartner: ", caught);
                }

                @Override
                public void onSuccess(CreateResult result) {
                    if (result instanceof DuplicateCreateResult) {
                        Log.debug("DbPartnerEditor tried to add partner '" + newPartner.getName() + "' to database " + db.getId() + " but it already exists");
                        MessageBox.alert(I18N.CONSTANTS.newPartner(), I18N.CONSTANTS.duplicatePartner(), null);
                    } else {
                        Log.debug("DbPartnerEditor added new partner '" + newPartner.getName() + "' to database " + db.getId());
                        newPartner.setId(result.getNewId());
                        store.add(newPartner);
                        eventBus.fireEvent(AppEvents.SCHEMA_CHANGED);
                        dlg.hide();
                    }
                }
            });
        }
    });
}
Also used : DuplicateCreateResult(org.activityinfo.shared.command.result.DuplicateCreateResult) PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) FormDialogCallback(org.activityinfo.client.page.common.dialog.FormDialogCallback) CreateResult(org.activityinfo.shared.command.result.CreateResult) DuplicateCreateResult(org.activityinfo.shared.command.result.DuplicateCreateResult) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) FormDialogTether(org.activityinfo.client.page.common.dialog.FormDialogTether) AddPartner(org.activityinfo.shared.command.AddPartner)

Example 2 with AddPartner

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

the class AddPartnerHandler method execute.

@Override
@SuppressWarnings("unchecked")
public CommandResult execute(AddPartner cmd, User user) throws CommandException {
    UserDatabase db = em.find(UserDatabase.class, cmd.getDatabaseId());
    if (db.getOwner().getId() != user.getId()) {
        UserPermission perm = db.getPermissionByUser(user);
        if (perm == null || !perm.isAllowManageAllUsers()) {
            throw new IllegalAccessCommandException("The user does not have the manageAllUsers permission.");
        }
    }
    // first check to see if an organization by this name is already
    // a partner
    Set<Partner> dbPartners = db.getPartners();
    for (Partner partner : dbPartners) {
        if (partner.getName().equals(cmd.getPartner().getName())) {
            return new DuplicateCreateResult();
        }
    }
    // now try to match this partner by name
    List<Partner> allPartners = em.createQuery("select p from Partner p where p.name = ?1").setParameter(1, cmd.getPartner().getName()).getResultList();
    if (allPartners.size() != 0) {
        db.getPartners().add(allPartners.get(0));
        return new CreateResult(allPartners.get(0).getId());
    }
    // nope, have to create a new record
    Partner newPartner = new Partner();
    newPartner.setName(cmd.getPartner().getName());
    newPartner.setFullName(cmd.getPartner().getFullName());
    em.persist(newPartner);
    db.setLastSchemaUpdate(new Date());
    em.persist(db);
    db.getPartners().add(newPartner);
    return new CreateResult(newPartner.getId());
}
Also used : DuplicateCreateResult(org.activityinfo.shared.command.result.DuplicateCreateResult) CreateResult(org.activityinfo.shared.command.result.CreateResult) DuplicateCreateResult(org.activityinfo.shared.command.result.DuplicateCreateResult) IllegalAccessCommandException(org.activityinfo.shared.exception.IllegalAccessCommandException) UserDatabase(org.activityinfo.server.database.hibernate.entity.UserDatabase) Partner(org.activityinfo.server.database.hibernate.entity.Partner) AddPartner(org.activityinfo.shared.command.AddPartner) Date(java.util.Date) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Example 3 with AddPartner

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

the class LocalSiteCreateTest method siteRemovePartnerConflict.

@Test
@OnDataSet("/dbunit/sites-simple1.db.xml")
public void siteRemovePartnerConflict() {
    // FIRST U1 adds a new partner
    int databaseId = 1;
    PartnerDTO iom = new PartnerDTO();
    iom.setName("IOM");
    CreateResult result = executeRemotely(new AddPartner(databaseId, iom));
    iom.setId(result.getNewId());
    // Now U2 synchronizes, and adds a new site with this partner
    synchronizeFirstTime();
    SiteDTO site = new SiteDTO();
    site.setId(3343234);
    site.setActivityId(1);
    site.setPartner(iom);
    site.setDate1(new Date());
    site.setDate2(new Date());
    site.setLocationId(1);
    executeLocally(new CreateSite(site));
    // At T+3, U2 thinks better, removes IOM
    executeRemotely(new RemovePartner(databaseId, iom.getId()));
    // At T+4, U1 synchronizes, and IOM is removed, but site remains
    synchronize();
    // Verify that there is still a label for this partner
    SiteResult sites = executeLocally(GetSites.byId(site.getId()));
    assertThat(sites.getTotalLength(), equalTo(1));
    assertThat(sites.getData().get(0).getName(), equalTo(site.getName()));
}
Also used : PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) CreateResult(org.activityinfo.shared.command.result.CreateResult) SiteResult(org.activityinfo.shared.command.result.SiteResult) RemovePartner(org.activityinfo.shared.command.RemovePartner) AddPartner(org.activityinfo.shared.command.AddPartner) SiteDTO(org.activityinfo.shared.dto.SiteDTO) Date(java.util.Date) CreateSite(org.activityinfo.shared.command.CreateSite) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 4 with AddPartner

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

the class PartnerTest method testAddPartner.

@Test
public void testAddPartner() throws Exception {
    PartnerDTO newPartner = new PartnerDTO();
    newPartner.setName("Solidarites");
    CreateResult cr = execute(new AddPartner(PEAR_PLUS_DB_ID, newPartner));
    Assert.assertEquals(SOL_ID, cr.getNewId());
    SchemaDTO schema = execute(new GetSchema());
    PartnerDTO partner = schema.getDatabaseById(PEAR_PLUS_DB_ID).getPartnerById(SOL_ID);
    Assert.assertNotNull(partner);
    Assert.assertEquals(newPartner.getName(), partner.getName());
}
Also used : PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) CreateResult(org.activityinfo.shared.command.result.CreateResult) DuplicateCreateResult(org.activityinfo.shared.command.result.DuplicateCreateResult) AddPartner(org.activityinfo.shared.command.AddPartner) SchemaDTO(org.activityinfo.shared.dto.SchemaDTO) GetSchema(org.activityinfo.shared.command.GetSchema) Test(org.junit.Test)

Example 5 with AddPartner

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

the class PartnerTest method testAddNewPartner.

@Test
public void testAddNewPartner() throws Exception {
    PartnerDTO newPartner = new PartnerDTO();
    newPartner.setName("VDE");
    newPartner.setFullName("Vision d'Espoir");
    CreateResult cr = execute(new AddPartner(1, newPartner));
    SchemaDTO schema = execute(new GetSchema());
    PartnerDTO partner = schema.getDatabaseById(1).getPartnerById(cr.getNewId());
    Assert.assertNotNull(partner);
    Assert.assertEquals("VDE", partner.getName());
    Assert.assertEquals("Vision d'Espoir", partner.getFullName());
}
Also used : PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) CreateResult(org.activityinfo.shared.command.result.CreateResult) DuplicateCreateResult(org.activityinfo.shared.command.result.DuplicateCreateResult) AddPartner(org.activityinfo.shared.command.AddPartner) SchemaDTO(org.activityinfo.shared.dto.SchemaDTO) GetSchema(org.activityinfo.shared.command.GetSchema) Test(org.junit.Test)

Aggregations

AddPartner (org.activityinfo.shared.command.AddPartner)6 CreateResult (org.activityinfo.shared.command.result.CreateResult)6 DuplicateCreateResult (org.activityinfo.shared.command.result.DuplicateCreateResult)5 PartnerDTO (org.activityinfo.shared.dto.PartnerDTO)5 Test (org.junit.Test)3 Date (java.util.Date)2 GetSchema (org.activityinfo.shared.command.GetSchema)2 SchemaDTO (org.activityinfo.shared.dto.SchemaDTO)2 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1 FormDialogCallback (org.activityinfo.client.page.common.dialog.FormDialogCallback)1 FormDialogTether (org.activityinfo.client.page.common.dialog.FormDialogTether)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 Partner (org.activityinfo.server.database.hibernate.entity.Partner)1 UserDatabase (org.activityinfo.server.database.hibernate.entity.UserDatabase)1 UserPermission (org.activityinfo.server.database.hibernate.entity.UserPermission)1 CreateSite (org.activityinfo.shared.command.CreateSite)1 RemovePartner (org.activityinfo.shared.command.RemovePartner)1 SiteResult (org.activityinfo.shared.command.result.SiteResult)1 SiteDTO (org.activityinfo.shared.dto.SiteDTO)1 IllegalAccessCommandException (org.activityinfo.shared.exception.IllegalAccessCommandException)1