Search in sources :

Example 1 with CreateSite

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

the class CreateSiteTest method testAllAttribsFalse.

@Test
public void testAllAttribsFalse() 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.setAttributeValue(1, false);
    newSite.setAttributeValue(2, false);
    newSite.setProject(new ProjectDTO(1, "SomeProject"));
    // create command
    CreateSite cmd = new CreateSite(newSite);
    assertThat((Integer) cmd.getProperties().get("locationId"), equalTo(1));
    // execute the command
    setUser(1);
    CreateResult result = execute(cmd);
    // let the client know the command has succeeded
    newSite.setId(result.getNewId());
    // try to retrieve what we've created
    SiteDTO secondRead = readSite(newSite.getId());
    // confirm that the changes are there
    Assert.assertEquals("site.attribute[2]", false, secondRead.getAttributeValue(1));
    Assert.assertEquals("site.attribute[2]", false, secondRead.getAttributeValue(2));
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) GregorianCalendar(java.util.GregorianCalendar) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) CreateSite(org.activityinfo.legacy.shared.command.CreateSite) Test(org.junit.Test)

Example 2 with CreateSite

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

the class CreateSiteTest method test.

@Test
public void test() throws CommandException {
    LocationDTO location = LocationDTOs.newLocation();
    execute(new CreateLocation(location));
    SiteDTO newSite = SiteDTOs.newSite();
    newSite.setLocation(location);
    CreateSite cmd = new CreateSite(newSite);
    setUser(1);
    CreateResult result = execute(cmd);
    newSite.setId(result.getNewId());
    assertThat(result.getNewId(), not(equalTo(0)));
    SiteDTO secondRead = readSite(newSite.getId());
    SiteDTOs.validateNewSite(secondRead);
}
Also used : CreateLocation(org.activityinfo.legacy.shared.command.CreateLocation) CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) CreateSite(org.activityinfo.legacy.shared.command.CreateSite) Test(org.junit.Test)

Example 3 with CreateSite

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

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

the class CommandQueueTest method testCreateSite.

@Test
public void testCreateSite() {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("anInt", 34);
    properties.put("aString", "testing");
    properties.put("aDouble", 3.0);
    properties.put("aBoolean", true);
    properties.put("anotherBoolean", false);
    properties.put("aTime", new Date());
    properties.put("aDate", new LocalDate(2011, 3, 15));
    final CreateSite cmd = new CreateSite(properties);
    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()));
    Collector<Void> deleted = Collector.newCollector();
    queue.remove(reread.getResult(), deleted);
    Collector<CommandQueue.QueueEntry> entry2 = Collector.newCollector();
    queue.peek(entry2);
    assertThat(entry2.getResult(), is(nullValue()));
}
Also used : SqlTransactionCallback(com.bedatadriven.rebar.sql.client.SqlTransactionCallback) HashMap(java.util.HashMap) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) LocalDate(com.bedatadriven.rebar.time.calendar.LocalDate) Date(java.util.Date) LocalDate(com.bedatadriven.rebar.time.calendar.LocalDate) CreateSite(org.activityinfo.legacy.shared.command.CreateSite) Test(org.junit.Test)

Example 5 with CreateSite

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

the class SiteDialog method saveNewSite.

private void saveNewSite() {
    final SiteDTO newSite = new SiteDTO();
    keyGenerator = new KeyGenerator();
    newSite.setId(keyGenerator.generateInt());
    newSite.setActivityId(activity.getId());
    if (activity.getReportingFrequency() == ActivityFormDTO.REPORT_ONCE) {
        newSite.setReportingPeriodId(new KeyGenerator().generateInt());
    }
    updateModel(newSite);
    dispatcher.execute(new CreateSite(newSite), new AsyncCallback<CreateResult>() {

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

        @Override
        public void onSuccess(CreateResult result) {
            hide();
            callback.onSaved();
        }
    });
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) SiteDTO(org.activityinfo.legacy.shared.model.SiteDTO) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) CreateSite(org.activityinfo.legacy.shared.command.CreateSite)

Aggregations

CreateSite (org.activityinfo.legacy.shared.command.CreateSite)7 Test (org.junit.Test)6 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)5 GregorianCalendar (java.util.GregorianCalendar)3 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)3 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)1 SqlTransactionCallback (com.bedatadriven.rebar.sql.client.SqlTransactionCallback)1 LocalDate (com.bedatadriven.rebar.time.calendar.LocalDate)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 CreateLocation (org.activityinfo.legacy.shared.command.CreateLocation)1 UpdateSite (org.activityinfo.legacy.shared.command.UpdateSite)1 SiteDTO (org.activityinfo.legacy.shared.model.SiteDTO)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 Ignore (org.junit.Ignore)1