Search in sources :

Example 1 with UpdatePartner

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

the class CloneDatabaseHandler method createDefaultPartner.

private void createDefaultPartner(User user) {
    Preconditions.checkNotNull(targetDb);
    Preconditions.checkState(targetDb.getId() > 0);
    PartnerDTO partner = new PartnerDTO();
    partner.setName("Default");
    new UpdatePartnerHandler(em).execute(new UpdatePartner(targetDb.getId(), partner), user);
}
Also used : PartnerDTO(org.activityinfo.legacy.shared.model.PartnerDTO) UpdatePartner(org.activityinfo.legacy.shared.command.UpdatePartner)

Example 2 with UpdatePartner

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

the class UpdatePartnerHandler method copyAndReplace.

/**
 * Originally, partner objects were shared between databases, which meant that it was not possible
 * for a single database owner to change the partner's name or description.
 * <p>
 * <p>To move forward, we apply a "copy-on-write" strategy. If a user wants to change a partner, and it
 * is shared with another database, then create a new copy of the partner with the desired changes,
 * and update all references in this database.</p>
 */
private CreateResult copyAndReplace(Partner sharedPartner, UpdatePartner cmd) {
    LOGGER.info("Copying and replacing " + sharedPartner + " with " + cmd.getPartner());
    Partner newPartner = new Partner();
    newPartner.setName(cmd.getPartner().getName());
    newPartner.setFullName(cmd.getPartner().getFullName());
    em.persist(newPartner);
    assert newPartner.getId() != 0;
    em.createNativeQuery("UPDATE site SET partnerId = ? WHERE partnerId = ? AND activityId IN " + "(SELECT activityid FROM activity WHERE databaseId = ?)").setParameter(1, newPartner.getId()).setParameter(2, sharedPartner.getId()).setParameter(3, cmd.getDatabaseId()).executeUpdate();
    em.createNativeQuery("UPDATE activity SET version = version+1, siteVersion = siteVersion+1, schemaVersion=schemaVersion+1 WHERE databaseId = ? ").setParameter(1, cmd.getDatabaseId()).executeUpdate();
    em.createNativeQuery("UPDATE userpermission SET partnerId = ? where partnerId = ? and databaseId = ?").setParameter(1, newPartner.getId()).setParameter(2, sharedPartner.getId()).setParameter(3, cmd.getDatabaseId()).executeUpdate();
    em.createNativeQuery("UPDATE partnerindatabase SET partnerId = ? WHERE databaseId = ? AND partnerId = ?").setParameter(1, newPartner.getId()).setParameter(2, cmd.getDatabaseId()).setParameter(3, sharedPartner.getId()).executeUpdate();
    return new CreateResult(newPartner.getId());
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) DuplicateCreateResult(org.activityinfo.legacy.shared.command.result.DuplicateCreateResult) Partner(org.activityinfo.server.database.hibernate.entity.Partner) UpdatePartner(org.activityinfo.legacy.shared.command.UpdatePartner)

Example 3 with UpdatePartner

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

the class UpdatePartnerHandler method updatePartner.

private CreateResult updatePartner(Database db, UpdatePartner cmd) {
    Partner partner = em.find(Partner.class, cmd.getPartner().getId());
    if (partner == null) {
        LOGGER.severe("Partner " + cmd.getPartner() + " is not ");
        throw new IllegalArgumentException("No such partner");
    }
    boolean ownedByThisDatabase = false;
    boolean shared = false;
    LOGGER.info("Partner is shared between " + partner.getDatabases().size() + " database(s)");
    for (Database database : partner.getDatabases()) {
        if (database.getId() == cmd.getDatabaseId()) {
            ownedByThisDatabase = true;
        } else {
            shared = true;
        }
    }
    if (!ownedByThisDatabase) {
        LOGGER.info("Partner " + cmd.getPartner() + " is not associated with database " + cmd.getDatabaseId());
        throw new IllegalArgumentException("Partner not owned by the database");
    }
    // Are there any changes to actually make?
    if (Objects.equals(partner.getName(), cmd.getPartner().getName()) && Objects.equals(partner.getFullName(), cmd.getPartner().getFullName())) {
        LOGGER.info("No changes to make, stopping.");
        return new CreateResult(cmd.getPartner().getId());
    }
    // Increment the database's version number
    db.setLastSchemaUpdate(new Date());
    // If this partner is shared, then duplicate and update all references.
    if (shared) {
        return copyAndReplace(partner, cmd);
    } else {
        return simpleUpdate(partner, cmd);
    }
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) DuplicateCreateResult(org.activityinfo.legacy.shared.command.result.DuplicateCreateResult) Database(org.activityinfo.server.database.hibernate.entity.Database) Partner(org.activityinfo.server.database.hibernate.entity.Partner) UpdatePartner(org.activityinfo.legacy.shared.command.UpdatePartner) Date(java.util.Date)

Example 4 with UpdatePartner

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

the class UpdatePartnerHandler method addNewPartner.

private CreateResult addNewPartner(UpdatePartner cmd, Database db) {
    // first check to see if an organization by this name is already
    // a partner in the same database
    Set<Partner> dbPartners = db.getPartners();
    for (Partner partner : dbPartners) {
        if (partner.getName().equals(cmd.getPartner().getName())) {
            return new DuplicateCreateResult();
        }
    }
    // Add a new partner
    Partner partner = new Partner();
    partner.setName(cmd.getPartner().getName());
    partner.setFullName(cmd.getPartner().getFullName());
    em.persist(partner);
    db.setLastSchemaUpdate(new Date());
    em.persist(db);
    db.getPartners().add(partner);
    return new CreateResult(partner.getId());
}
Also used : DuplicateCreateResult(org.activityinfo.legacy.shared.command.result.DuplicateCreateResult) CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) DuplicateCreateResult(org.activityinfo.legacy.shared.command.result.DuplicateCreateResult) Partner(org.activityinfo.server.database.hibernate.entity.Partner) UpdatePartner(org.activityinfo.legacy.shared.command.UpdatePartner) Date(java.util.Date)

Example 5 with UpdatePartner

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

the class UpdatePartnerHandlerTest method modifyShared.

@Test
public void modifyShared() {
    SchemaDTO schema = execute(new GetSchema());
    UserDatabaseDTO nfiDatabase = schema.getDatabaseById(NFI_DATABASE);
    UserDatabaseDTO healthDatabase = schema.getDatabaseById(HEALTH_DATABASE);
    // The "Default" partner starts out being shared between the two database.
    PartnerDTO nfiDefaultPartner = nfiDatabase.getDefaultPartner().get();
    PartnerDTO healthDefaultPartner = healthDatabase.getDefaultPartner().get();
    assertThat(nfiDefaultPartner.getId(), equalTo(healthDefaultPartner.getId()));
    // If I update the name of "Default" partner for my database, a copy should
    // be created.
    PartnerDTO updatedPartner = new PartnerDTO(nfiDefaultPartner.getId(), "Solidarites");
    updatedPartner.setFullName("Solidarites International");
    execute(new UpdatePartner(1, updatedPartner));
    // Now we should have two distinct partner objects
    schema = execute(new GetSchema());
    nfiDatabase = schema.getDatabaseById(NFI_DATABASE);
    healthDatabase = schema.getDatabaseById(HEALTH_DATABASE);
    assertThat(nfiDatabase.getPartners(), containsInAnyOrder(hasProperty("name", equalTo("Solidarites")), hasProperty("name", equalTo("NRC"))));
    assertThat(healthDatabase.getPartners(), contains(hasProperty("name", equalTo("Default"))));
    // Users in the NFI database should be reassigned to the new NRC partner object
    UserResult nfiUsers = execute(new GetUsers(NFI_DATABASE));
    Optional<UserPermissionDTO> bavon = nfiUsers.getData().stream().filter(u -> u.getEmail().equals("bavon@nrc.org")).findAny();
    Optional<UserPermissionDTO> lisa = nfiUsers.getData().stream().filter(u -> u.getEmail().equals("lisa@solidarites")).findAny();
    // Bavon should stay as NRC
    assertThat(bavon.get().getPartner(), hasProperty("name", equalTo("NRC")));
    // Lisa should be moved to the new Solidarites partner
    assertThat(lisa.get().getPartner(), hasProperty("name", equalTo("Solidarites")));
    // Users in the Health database should be unaffected
    UserResult healthUsers = execute(new GetUsers(HEALTH_DATABASE));
    Optional<UserPermissionDTO> bavonInHealth = healthUsers.getData().stream().filter(u -> u.getEmail().equals("bavon@nrc.org")).findAny();
    assertThat(bavonInHealth.get().getPartner(), hasProperty("name", equalTo("Default")));
    // Sites in the NFI database should be update to point to the new partner
    SiteDTO nfiSite1 = execute(GetSites.byId(1)).getData().get(0);
    SiteDTO nfiSite3 = execute(GetSites.byId(3)).getData().get(0);
    assertThat(nfiSite1.getPartner(), hasProperty("name", equalTo("Solidarites")));
    assertThat(nfiSite3.getPartner(), hasProperty("name", equalTo("NRC")));
    // Sites in the Health database should be unaffected
    SiteDTO healthSite4 = execute(GetSites.byId(4)).getData().get(0);
    assertThat(healthSite4.getPartner(), hasProperty("name", equalTo("Default")));
}
Also used : CommandTestCase2(org.activityinfo.server.command.CommandTestCase2) RunWith(org.junit.runner.RunWith) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) org.activityinfo.legacy.shared.model(org.activityinfo.legacy.shared.model) Matchers(org.hamcrest.Matchers) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test) UserResult(org.activityinfo.legacy.shared.command.result.UserResult) UpdatePartner(org.activityinfo.legacy.shared.command.UpdatePartner) GetUsers(org.activityinfo.legacy.shared.command.GetUsers) Assert.assertThat(org.junit.Assert.assertThat) GetSites(org.activityinfo.legacy.shared.command.GetSites) InjectionSupport(org.activityinfo.fixtures.InjectionSupport) Optional(java.util.Optional) GetSchema(org.activityinfo.legacy.shared.command.GetSchema) UserResult(org.activityinfo.legacy.shared.command.result.UserResult) GetUsers(org.activityinfo.legacy.shared.command.GetUsers) GetSchema(org.activityinfo.legacy.shared.command.GetSchema) UpdatePartner(org.activityinfo.legacy.shared.command.UpdatePartner) Test(org.junit.Test)

Aggregations

UpdatePartner (org.activityinfo.legacy.shared.command.UpdatePartner)11 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)7 DuplicateCreateResult (org.activityinfo.legacy.shared.command.result.DuplicateCreateResult)7 PartnerDTO (org.activityinfo.legacy.shared.model.PartnerDTO)5 GetSchema (org.activityinfo.legacy.shared.command.GetSchema)4 Test (org.junit.Test)4 Partner (org.activityinfo.server.database.hibernate.entity.Partner)3 Date (java.util.Date)2 SchemaDTO (org.activityinfo.legacy.shared.model.SchemaDTO)2 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1 Optional (java.util.Optional)1 InjectionSupport (org.activityinfo.fixtures.InjectionSupport)1 GetSites (org.activityinfo.legacy.shared.command.GetSites)1 GetUsers (org.activityinfo.legacy.shared.command.GetUsers)1 UserResult (org.activityinfo.legacy.shared.command.result.UserResult)1 org.activityinfo.legacy.shared.model (org.activityinfo.legacy.shared.model)1 CommandTestCase2 (org.activityinfo.server.command.CommandTestCase2)1 UpdatePartnerHandler (org.activityinfo.server.command.handler.UpdatePartnerHandler)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 Database (org.activityinfo.server.database.hibernate.entity.Database)1