Search in sources :

Example 1 with CreateLocation

use of org.activityinfo.shared.command.CreateLocation 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 CreateLocation

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

the class BoundLocationSection method save.

@Override
public void save(final AsyncCallback<Void> callback) {
    if (isDirty()) {
        newLocation();
        dispatcher.execute(new CreateLocation(location), new AsyncCallback<VoidResult>() {

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

            @Override
            public void onSuccess(VoidResult result) {
                callback.onSuccess(null);
            }
        });
    } else {
        callback.onSuccess(null);
    }
}
Also used : CreateLocation(org.activityinfo.shared.command.CreateLocation) VoidResult(org.activityinfo.shared.command.result.VoidResult)

Example 3 with CreateLocation

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

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

the class FormSubmissionResource method createCreateLocationCommand.

private CreateLocation createCreateLocationCommand(SiteFormData data, SchemaDTO schemaDTO, ActivityDTO activity) {
    // create the dto
    LocationDTO loc = new LocationDTO();
    loc.setId(new KeyGenerator().generateInt());
    loc.setLocationTypeId(activity.getLocationTypeId());
    loc.setName(data.getLocationname());
    loc.setLatitude(data.getLatitude());
    loc.setLongitude(data.getLongitude());
    CreateLocation cmd = new CreateLocation(loc);
    // get adminentities that contain the specified coordinates
    List<AdminEntity> adminentities = geocoder.geocode(data.getLatitude(), data.getLongitude());
    if (adminentities.isEmpty()) {
        AdminEntity adminEntity = createDebugAdminEntity();
        if (adminEntity != null) {
            adminentities.add(adminEntity);
        }
    }
    if (!adminentities.isEmpty()) {
        RpcMap map = cmd.getProperties();
        for (AdminEntity entity : adminentities) {
            map.put(AdminLevelDTO.getPropertyName(entity.getLevel().getId()), entity.getId());
        }
    }
    return cmd;
}
Also used : CreateLocation(org.activityinfo.shared.command.CreateLocation) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) RpcMap(com.extjs.gxt.ui.client.data.RpcMap) KeyGenerator(org.activityinfo.client.local.command.handler.KeyGenerator) LocationDTO(org.activityinfo.shared.dto.LocationDTO)

Example 5 with CreateLocation

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

the class LocalSiteCreateTest method createNew.

@Test
@OnDataSet("/dbunit/sites-simple1.db.xml")
public void createNew() throws CommandException {
    synchronizeFirstTime();
    // create a new detached, client model
    SiteDTO newSite = SiteDTOs.newSite();
    LocationDTO location = LocationDTOs.newLocation();
    executeLocally(new CreateLocation(location));
    newSite.setLocation(location);
    // create command
    CreateSite cmd = new CreateSite(newSite);
    // execute the command
    CreateResult result = executeLocally(cmd);
    // let the client know the command has succeeded
    newSite.setId(result.getNewId());
    // try to retrieve what we've created FROM OUR CLIENT SIDE DATABASE
    SiteResult loadResult = executeLocally(GetSites.byId(newSite.getId()));
    Assert.assertEquals(1, loadResult.getData().size());
    SiteDTO secondRead = loadResult.getData().get(0);
    // confirm that the changes are there
    SiteDTOs.validateNewSite(secondRead);
    newRequest();
    // now Sync with the server
    synchronize();
    // Confirm that paging works client side
    GetSites pagingRequest = new GetSites();
    pagingRequest.setLimit(1);
    loadResult = executeLocally(pagingRequest);
}
Also used : CreateLocation(org.activityinfo.shared.command.CreateLocation) CreateResult(org.activityinfo.shared.command.result.CreateResult) SiteResult(org.activityinfo.shared.command.result.SiteResult) GetSites(org.activityinfo.shared.command.GetSites) SiteDTO(org.activityinfo.shared.dto.SiteDTO) LocationDTO(org.activityinfo.shared.dto.LocationDTO) CreateSite(org.activityinfo.shared.command.CreateSite) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Aggregations

CreateLocation (org.activityinfo.shared.command.CreateLocation)5 LocationDTO (org.activityinfo.shared.dto.LocationDTO)4 CreateSite (org.activityinfo.shared.command.CreateSite)3 SiteDTO (org.activityinfo.shared.dto.SiteDTO)3 KeyGenerator (org.activityinfo.client.local.command.handler.KeyGenerator)2 CreateResult (org.activityinfo.shared.command.result.CreateResult)2 Test (org.junit.Test)2 RpcMap (com.extjs.gxt.ui.client.data.RpcMap)1 ColumnBinding (org.activityinfo.client.importer.column.ColumnBinding)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)1 BatchCommand (org.activityinfo.shared.command.BatchCommand)1 Command (org.activityinfo.shared.command.Command)1 GetSites (org.activityinfo.shared.command.GetSites)1 BatchResult (org.activityinfo.shared.command.result.BatchResult)1 SiteResult (org.activityinfo.shared.command.result.SiteResult)1 VoidResult (org.activityinfo.shared.command.result.VoidResult)1