Search in sources :

Example 6 with CreateResult

use of org.activityinfo.legacy.shared.command.result.CreateResult 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 7 with CreateResult

use of org.activityinfo.legacy.shared.command.result.CreateResult 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 8 with CreateResult

use of org.activityinfo.legacy.shared.command.result.CreateResult 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 9 with CreateResult

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

the class CustomerCalcIndicatorTest method calculationsWithMissingValues.

@Test
public void calculationsWithMissingValues() {
    formClass = createFormClass();
    int activityId = getLegacyIdFromCuid(formClass.getId());
    SiteDTO newSite = newSite(activityId);
    newSite.setIndicatorValue(fieldId("EXP"), 3);
    newSite.setIndicatorValue(fieldId("WATER_ALLOC"), 400);
    newSite.setIndicatorValue(fieldId("PCT_INITIAL"), 50);
    newSite.setIndicatorValue(fieldId("PCT_INITIAL_HARD"), 0);
    newSite.setIndicatorValue(fieldId("PCT_INITIAL_SOFT"), 30);
    CreateResult createSiteResult = execute(new CreateSite(newSite));
    // let the client know the command has succeeded
    newSite.setId(createSiteResult.getNewId());
    PagingLoadResult<SiteDTO> loadResult = execute(GetSites.byId(newSite.getId()));
    Assert.assertEquals(1, loadResult.getData().size());
    SiteDTO siteDTO = loadResult.getData().get(0);
    // WATER_EXP = EXP * (WATER_ALLOC / 100)
    assertThat(indicatorValue(siteDTO, "WATER_EXP"), closeTo(12, 0));
    // INITIAL = WATER_EXP * (PCT_INITIAL / 100)
    assertThat(indicatorValue(siteDTO, "INITIAL"), closeTo(6, 0));
    // INITIAL_HARD = WATER_EXP * (PCT_INITIAL_HARD / 100)
    assertThat(indicatorValue(siteDTO, "INITIAL_HARD"), equalTo(0d));
    // INITIAL_SOFT = WATER_EXP * (PCT_INITIAL_HARD / 100)
    assertThat(indicatorValue(siteDTO, "INITIAL_SOFT"), closeTo(3.6, 0.001));
    // INITIAL_TOTAL = INITIAL + INITIAL_HARD + INITIAL_SOFT
    assertThat(indicatorValue(siteDTO, "INITIAL_TOTAL"), closeTo(9.6, 0.001));
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) Test(org.junit.Test)

Example 10 with CreateResult

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

the class ActivityTest method testActivity.

@Test
public void testActivity() throws CommandException {
    /*
         * Initial data load
         */
    SchemaDTO schema = execute(new GetSchema());
    UserDatabaseDTO db = schema.getDatabaseById(1);
    /*
         * Create a new activity
         */
    LocationTypeDTO locType = schema.getCountryById(1).getLocationTypes().get(0);
    ActivityFormDTO act = new ActivityFormDTO();
    act.setName("Warshing the dishes");
    act.setLocationType(locType);
    act.setReportingFrequency(ActivityFormDTO.REPORT_MONTHLY);
    act.setClassicView(false);
    CreateResult cresult = execute(CreateEntity.Activity(db, act));
    int newId = cresult.getNewId();
    /*
         * Reload schema to verify the changes have stuck
         */
    act = execute(new GetActivityForm(newId));
    assertEquals("name", "Warshing the dishes", act.getName());
    assertEquals("locationType", locType.getName(), act.getLocationType().getName());
    assertEquals("reportingFrequency", ActivityFormDTO.REPORT_MONTHLY, act.getReportingFrequency());
    assertEquals("public", Published.NOT_PUBLISHED.getIndex(), act.getPublished());
    assertEquals("classicView", false, act.getClassicView());
    Extents countryBounds = act.getLocationType().getCountryBounds();
    assertThat(countryBounds.getMinLat(), Matchers.equalTo(-13.0));
    assertThat(countryBounds.getMaxLat(), Matchers.equalTo(5.0));
    assertThat(countryBounds.getMinLon(), Matchers.equalTo(12.0));
    assertThat(countryBounds.getMaxLon(), Matchers.equalTo(31.0));
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) Extents(org.activityinfo.model.type.geo.Extents) Test(org.junit.Test)

Aggregations

CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)46 Test (org.junit.Test)26 Date (java.util.Date)9 GetSchema (org.activityinfo.legacy.shared.command.GetSchema)7 UpdatePartner (org.activityinfo.legacy.shared.command.UpdatePartner)7 DuplicateCreateResult (org.activityinfo.legacy.shared.command.result.DuplicateCreateResult)7 OnDataSet (org.activityinfo.server.database.OnDataSet)6 CreateSite (org.activityinfo.legacy.shared.command.CreateSite)5 PartnerDTO (org.activityinfo.legacy.shared.model.PartnerDTO)5 CreateEntity (org.activityinfo.legacy.shared.command.CreateEntity)4 SchemaDTO (org.activityinfo.legacy.shared.model.SchemaDTO)4 TargetDTO (org.activityinfo.legacy.shared.model.TargetDTO)4 CuidAdapter.activityFormClass (org.activityinfo.model.legacy.CuidAdapter.activityFormClass)4 ResourceId (org.activityinfo.model.resource.ResourceId)4 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)3 GregorianCalendar (java.util.GregorianCalendar)3 CloneDatabase (org.activityinfo.legacy.shared.command.CloneDatabase)3 ProjectDTO (org.activityinfo.legacy.shared.model.ProjectDTO)3 SiteDTO (org.activityinfo.legacy.shared.model.SiteDTO)3 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)3