Search in sources :

Example 1 with CreateSite

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

the class ImporterWizard method submitSites.

private void submitSites(List<LocationDTO> results, final AsyncCallback<Void> callback) {
    int numColums = model.getData().getNumColumns();
    ColumnBinding[] bindings = bindingsArray();
    KeyGenerator keyGenerator = new KeyGenerator();
    // do a first pass to match the location
    List<Command> siteBatch = Lists.newArrayList();
    int rowIndex = 0;
    for (ImportRowModel row : model.getData().getRowStore().getModels()) {
        LocationDTO location = results.get(rowIndex);
        if (location.isNew()) {
            siteBatch.add(new CreateLocation(location));
        }
        SiteDTO site = new SiteDTO();
        site.setId(keyGenerator.generateInt());
        site.setReportingPeriodId(keyGenerator.generateInt());
        site.setActivityId(model.getActivity().getId());
        site.setLocationId(location.getId());
        site.setPartner(model.getActivity().getDatabase().getPartners().get(0));
        for (int i = 0; i != numColums; ++i) {
            bindings[i].bindSite(row.get(i), site);
        }
        siteBatch.add(new CreateSite(site));
        rowIndex++;
    }
    dispatcher.execute(new BatchCommand(siteBatch), new AsyncCallback<BatchResult>() {

        @Override
        public void onFailure(Throwable caught) {
            MessageBox.alert("Import failed", "Exception: " + caught.getMessage(), null);
            callback.onFailure(null);
        }

        @Override
        public void onSuccess(BatchResult result) {
            MessageBox.alert("Import succeeded!", "Refresh the data grid to see your new sites", null);
            callback.onSuccess(null);
        }
    });
}
Also used : CreateLocation(org.activityinfo.shared.command.CreateLocation) ColumnBinding(org.activityinfo.client.importer.column.ColumnBinding) BatchResult(org.activityinfo.shared.command.result.BatchResult) CreateSite(org.activityinfo.shared.command.CreateSite) Command(org.activityinfo.shared.command.Command) BatchCommand(org.activityinfo.shared.command.BatchCommand) BatchCommand(org.activityinfo.shared.command.BatchCommand) SiteDTO(org.activityinfo.shared.dto.SiteDTO) KeyGenerator(org.activityinfo.client.local.command.handler.KeyGenerator) LocationDTO(org.activityinfo.shared.dto.LocationDTO)

Example 2 with CreateSite

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

the class FormSubmissionResource method createSite.

private void createSite(SiteFormData data, SchemaDTO schemaDTO, ActivityDTO activity) {
    final SiteDTO site = new SiteDTO();
    site.setId(new KeyGenerator().generateInt());
    site.setActivityId(data.getActivity());
    if (activity.getReportingFrequency() == ActivityDTO.REPORT_ONCE) {
        site.setReportingPeriodId(new KeyGenerator().generateInt());
    }
    // set activitymodel
    if (activity.getReportingFrequency() == ActivityDTO.REPORT_ONCE) {
        site.setDate1(data.getDate1());
        site.setDate2(data.getDate2());
    }
    site.setPartner(schemaDTO.getPartnerById(data.getPartner()));
    // set comments
    site.setComments(data.getComments());
    // set attributes
    for (FormAttributeGroup formAttributeGroup : data.getAttributegroups()) {
        AttributeGroupDTO attributeGroup = activity.getAttributeGroupById(formAttributeGroup.getId());
        for (Integer attributeId : attributeGroup.getAttributeIds()) {
            site.setAttributeValue(attributeId, formAttributeGroup.isSelected(attributeId));
        }
    }
    // set indicators
    if (activity.getReportingFrequency() == ActivityDTO.REPORT_ONCE) {
        for (FormIndicator formIndicator : data.getIndicators()) {
            site.setIndicatorValue(formIndicator.getId(), formIndicator.getDoubleValue());
        }
    }
    // create command(s)
    CreateSite cmd = new CreateSite(site);
    cmd.setNestedCommand(createCreateLocationCommand(data, schemaDTO, activity));
    // save
    CreateResult createResult = dispatcher.execute(cmd);
    // create sitehistory entry
    siteHistoryProcessor.process(cmd, getUser().getId(), createResult.getNewId());
}
Also used : FormAttributeGroup(org.activityinfo.server.endpoint.odk.SiteFormData.FormAttributeGroup) AttributeGroupDTO(org.activityinfo.shared.dto.AttributeGroupDTO) FormIndicator(org.activityinfo.server.endpoint.odk.SiteFormData.FormIndicator) CreateResult(org.activityinfo.shared.command.result.CreateResult) SiteDTO(org.activityinfo.shared.dto.SiteDTO) KeyGenerator(org.activityinfo.client.local.command.handler.KeyGenerator) CreateSite(org.activityinfo.shared.command.CreateSite)

Example 3 with CreateSite

use of org.activityinfo.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.shared.command.CreateSite) Test(org.junit.Test)

Example 4 with CreateSite

use of org.activityinfo.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)));
    PagingLoadResult<SiteDTO> loadResult = execute(GetSites.byId(newSite.getId()));
    Assert.assertEquals(1, loadResult.getData().size());
    SiteDTO secondRead = loadResult.getData().get(0);
    SiteDTOs.validateNewSite(secondRead);
}
Also used : CreateLocation(org.activityinfo.shared.command.CreateLocation) CreateResult(org.activityinfo.shared.command.result.CreateResult) SiteDTO(org.activityinfo.shared.dto.SiteDTO) LocationDTO(org.activityinfo.shared.dto.LocationDTO) CreateSite(org.activityinfo.shared.command.CreateSite) Test(org.junit.Test)

Example 5 with CreateSite

use of org.activityinfo.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());
    // cmd.onCompleted(result);
    // try to retrieve what we've created
    PagingLoadResult<SiteDTO> loadResult = execute(GetSites.byId(newSite.getId()));
    Assert.assertEquals(1, loadResult.getData().size());
    SiteDTO secondRead = loadResult.getData().get(0);
    // 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 : ProjectDTO(org.activityinfo.shared.dto.ProjectDTO) PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) CreateResult(org.activityinfo.shared.command.result.CreateResult) GregorianCalendar(java.util.GregorianCalendar) SiteDTO(org.activityinfo.shared.dto.SiteDTO) KeyGenerator(org.activityinfo.client.local.command.handler.KeyGenerator) CreateSite(org.activityinfo.shared.command.CreateSite) Test(org.junit.Test)

Aggregations

CreateSite (org.activityinfo.shared.command.CreateSite)10 SiteDTO (org.activityinfo.shared.dto.SiteDTO)9 CreateResult (org.activityinfo.shared.command.result.CreateResult)7 Test (org.junit.Test)7 KeyGenerator (org.activityinfo.client.local.command.handler.KeyGenerator)4 PartnerDTO (org.activityinfo.shared.dto.PartnerDTO)4 CreateLocation (org.activityinfo.shared.command.CreateLocation)3 LocationDTO (org.activityinfo.shared.dto.LocationDTO)3 Date (java.util.Date)2 GregorianCalendar (java.util.GregorianCalendar)2 OnDataSet (org.activityinfo.server.database.OnDataSet)2 SiteResult (org.activityinfo.shared.command.result.SiteResult)2 ProjectDTO (org.activityinfo.shared.dto.ProjectDTO)2 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)1 SqlTransactionCallback (com.bedatadriven.rebar.sql.client.SqlTransactionCallback)1 LocalDate (com.bedatadriven.rebar.time.calendar.LocalDate)1 HashMap (java.util.HashMap)1 ColumnBinding (org.activityinfo.client.importer.column.ColumnBinding)1 FormAttributeGroup (org.activityinfo.server.endpoint.odk.SiteFormData.FormAttributeGroup)1 FormIndicator (org.activityinfo.server.endpoint.odk.SiteFormData.FormIndicator)1