Search in sources :

Example 1 with UpdateSite

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

the class CreateSiteTest method testSiteWithCalculatedIndicators.

@OnDataSet("/dbunit/sites-calculated-indicators.db.xml")
@Test
public void testSiteWithCalculatedIndicators() throws CommandException {
    // create a new detached, client model
    SiteDTO newSite = new SiteDTO();
    newSite.setId(new KeyGenerator().generateInt());
    newSite.setActivityId(1);
    newSite.setLocationId(1);
    newSite.setPartner(new PartnerDTO(1, "Foobar"));
    newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
    newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
    newSite.setLocationName("Virunga");
    newSite.setProject(new ProjectDTO(1, "SomeProject"));
    newSite.setReportingPeriodId(11);
    newSite.setIndicatorValue(1, 1);
    newSite.setIndicatorValue(2, 2);
    // create command
    CreateSite cmd = new CreateSite(newSite);
    assertThat((Integer) cmd.getProperties().get("locationId"), equalTo(1));
    // execute the command
    setUser(1);
    CreateResult result = execute(cmd);
    newSite.setId(result.getNewId());
    // try to retrieve what we've created
    SiteDTO firstRead = readSite(newSite.getId());
    Assert.assertEquals(1d, firstRead.<Object>getIndicatorValue(1));
    Assert.assertEquals(2d, firstRead.<Object>getIndicatorValue(2));
    Assert.assertEquals(3d, firstRead.<Object>getIndicatorValue(11));
    Assert.assertEquals(0.5d, firstRead.<Object>getIndicatorValue(12));
    SiteDTO updateSite = new SiteDTO(newSite);
    updateSite.setIndicatorValue(1, null);
    updateSite.setIndicatorValue(2, null);
    // update site
    execute(new UpdateSite(newSite, updateSite));
    SiteDTO secondRead = readSite(newSite.getId());
    // BACHE
    Assert.assertEquals(null, secondRead.getIndicatorValue(1));
    // BENE
    Assert.assertEquals(null, secondRead.getIndicatorValue(2));
    Assert.assertEquals(null, secondRead.getIndicatorValue(11));
    Assert.assertEquals(null, secondRead.getIndicatorValue(12));
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) GregorianCalendar(java.util.GregorianCalendar) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) UpdateSite(org.activityinfo.legacy.shared.command.UpdateSite) CreateSite(org.activityinfo.legacy.shared.command.CreateSite) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 2 with UpdateSite

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

the class CommandQueueTest method testUpdateSite.

@Test
public void testUpdateSite() {
    Map<String, Object> changes = new HashMap<String, Object>();
    changes.put("anInt", 34);
    changes.put("aString", "testing");
    final UpdateSite cmd = new UpdateSite(99, changes);
    db.transaction(new SqlTransactionCallback() {

        @Override
        public void begin(SqlTransaction tx) {
            queue.queue(tx, cmd);
        }
    });
    Collector<CommandQueue.QueueEntry> reread = Collector.newCollector();
    queue.peek(reread);
    assertThat(reread.getResult(), not(nullValue()));
    assertThat(cmd, equalTo(reread.getResult().getCommand()));
}
Also used : SqlTransactionCallback(com.bedatadriven.rebar.sql.client.SqlTransactionCallback) HashMap(java.util.HashMap) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) UpdateSite(org.activityinfo.legacy.shared.command.UpdateSite) Test(org.junit.Test)

Example 3 with UpdateSite

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

the class UpdateSiteTest method charsets.

@Test
public void charsets() throws CommandException {
    // retrieve from the server
    ListResult<SiteDTO> result = execute(GetSites.byId(1));
    SiteDTO original = result.getData().get(0);
    SiteDTO modified = original.copy();
    assertThat(modified.getId(), equalTo(original.getId()));
    // modify and generate command
    // note that the character sequence below is two characters:
    // the first a simple unicode character and the second a code point
    // requiring 4-bytes.
    // http://www.charbase.com/20731-unicode-cjk-unified-ideograph
    // NOTE: for the moment, i'm rolling back utf8mb4 support becuase it
    // requires
    // Mysql-5.5 which is **PITA*** to get running on earlier versions of
    // ubuntu.
    // To be reapplied when suppport
    // modified.setComments("≥\ud841\udf31");
    modified.setComments("≥");
    System.out.println(modified.getComments());
    assertThat(modified.getComments().codePointCount(0, modified.getComments().length()), equalTo(1));
    UpdateSite cmd = new UpdateSite(original, modified);
    assertThat((String) cmd.getChanges().get("comments"), equalTo(modified.getComments()));
    execute(cmd);
    // retrieve the old one
    result = execute(GetSites.byId(1));
    SiteDTO secondRead = result.getData().get(0);
    // confirm that the changes are there
    assertThat(secondRead.getComments(), equalTo(modified.getComments()));
}
Also used : SiteDTO(org.activityinfo.legacy.shared.model.SiteDTO) UpdateSite(org.activityinfo.legacy.shared.command.UpdateSite) Test(org.junit.Test)

Example 4 with UpdateSite

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

the class UpdateSiteTest method testUpdatePartner.

@Test
public void testUpdatePartner() throws CommandException {
    // define changes for site id=2
    Map<String, Object> changes = new HashMap<String, Object>();
    changes.put("partnerId", 2);
    execute(new UpdateSite(2, changes));
    // assure that the change has been effected
    Site site = em.find(Site.class, 2);
    Assert.assertEquals("partnerId", 2, site.getPartner().getId());
}
Also used : UpdateSite(org.activityinfo.legacy.shared.command.UpdateSite) Site(org.activityinfo.server.database.hibernate.entity.Site) HashMap(java.util.HashMap) UpdateSite(org.activityinfo.legacy.shared.command.UpdateSite) Test(org.junit.Test)

Example 5 with UpdateSite

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

the class CreateSiteHandlerAsync method updateExistingSite.

private void updateExistingSite(final CreateSite cmd, ExecutionContext context, final AsyncCallback<CreateResult> callback) {
    UpdateSite updateSite = new UpdateSite(cmd.getSiteId(), cmd.getProperties());
    context.execute(updateSite, new AsyncCallback<VoidResult>() {

        @Override
        public void onFailure(Throwable caught) {
            callback.onFailure(caught);
        }

        @Override
        public void onSuccess(VoidResult result) {
            callback.onSuccess(new CreateResult(cmd.getSiteId()));
        }
    });
}
Also used : VoidResult(org.activityinfo.legacy.shared.command.result.VoidResult) CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) UpdateSite(org.activityinfo.legacy.shared.command.UpdateSite)

Aggregations

UpdateSite (org.activityinfo.legacy.shared.command.UpdateSite)8 Test (org.junit.Test)6 SiteDTO (org.activityinfo.legacy.shared.model.SiteDTO)4 HashMap (java.util.HashMap)2 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)2 VoidResult (org.activityinfo.legacy.shared.command.result.VoidResult)2 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)1 SqlTransactionCallback (com.bedatadriven.rebar.sql.client.SqlTransactionCallback)1 GregorianCalendar (java.util.GregorianCalendar)1 CreateSite (org.activityinfo.legacy.shared.command.CreateSite)1 SiteResult (org.activityinfo.legacy.shared.command.result.SiteResult)1 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 Site (org.activityinfo.server.database.hibernate.entity.Site)1