Search in sources :

Example 6 with KeyGenerator

use of org.activityinfo.client.local.command.handler.KeyGenerator 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 7 with KeyGenerator

use of org.activityinfo.client.local.command.handler.KeyGenerator 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)

Example 8 with KeyGenerator

use of org.activityinfo.client.local.command.handler.KeyGenerator in project activityinfo by bedatadriven.

the class SiteDTOs method newSite.

public static SiteDTO newSite() {
    SiteDTO newSite = new SiteDTO();
    newSite.setId(new KeyGenerator().generateInt());
    newSite.setActivityId(1);
    newSite.setLocationId(1);
    newSite.setPartner(new PartnerDTO(1, "Foobar"));
    newSite.setReportingPeriodId(new KeyGenerator().generateInt());
    newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
    newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
    newSite.setIndicatorValue(1, 996.0);
    newSite.setIndicatorValue(2, null);
    newSite.setAttributeValue(1, true);
    newSite.setAttributeValue(2, false);
    newSite.setComments("huba huba");
    newSite.setProject(new ProjectDTO(1, "WoeiProject"));
    return newSite;
}
Also used : ProjectDTO(org.activityinfo.shared.dto.ProjectDTO) PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) GregorianCalendar(java.util.GregorianCalendar) SiteDTO(org.activityinfo.shared.dto.SiteDTO) KeyGenerator(org.activityinfo.client.local.command.handler.KeyGenerator)

Example 9 with KeyGenerator

use of org.activityinfo.client.local.command.handler.KeyGenerator in project activityinfo by bedatadriven.

the class ImporterWizard method finish.

@Override
public void finish(final AsyncCallback<Void> callback) {
    final KeyGenerator keyGenerator = new KeyGenerator();
    int numColums = model.getData().getNumColumns();
    ColumnBinding[] bindings = bindingsArray();
    // do a first pass to match the location
    List<Command> matchBatch = Lists.newArrayList();
    for (ImportRowModel row : model.getData().getRowStore().getModels()) {
        MatchLocation location = new MatchLocation();
        location.setLocationType(model.getActivity().getLocationTypeId());
        for (int i = 0; i != numColums; ++i) {
            bindings[i].bindLocation(row.get(i), location);
        }
        matchBatch.add(location);
    }
    dispatcher.execute(new BatchCommand(matchBatch), new AsyncCallback<BatchResult>() {

        @Override
        public void onFailure(Throwable caught) {
            MessageBox.alert("Match locations failed", "Exception", null);
        }

        @Override
        public void onSuccess(BatchResult result) {
            submitSites((List) result.getResults(), callback);
        }
    });
}
Also used : ColumnBinding(org.activityinfo.client.importer.column.ColumnBinding) BatchResult(org.activityinfo.shared.command.result.BatchResult) MatchLocation(org.activityinfo.shared.command.MatchLocation) Command(org.activityinfo.shared.command.Command) BatchCommand(org.activityinfo.shared.command.BatchCommand) BatchCommand(org.activityinfo.shared.command.BatchCommand) List(java.util.List) KeyGenerator(org.activityinfo.client.local.command.handler.KeyGenerator)

Example 10 with KeyGenerator

use of org.activityinfo.client.local.command.handler.KeyGenerator in project activityinfo by bedatadriven.

the class SiteDialog method saveNewSite.

private void saveNewSite() {
    final SiteDTO newSite = new SiteDTO();
    newSite.setId(new KeyGenerator().generateInt());
    newSite.setActivityId(activity.getId());
    if (activity.getReportingFrequency() == ActivityDTO.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(newSite);
        }
    });
}
Also used : 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)

Aggregations

KeyGenerator (org.activityinfo.client.local.command.handler.KeyGenerator)12 LocationDTO (org.activityinfo.shared.dto.LocationDTO)6 SiteDTO (org.activityinfo.shared.dto.SiteDTO)6 CreateSite (org.activityinfo.shared.command.CreateSite)4 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)3 CreateResult (org.activityinfo.shared.command.result.CreateResult)3 GregorianCalendar (java.util.GregorianCalendar)2 ColumnBinding (org.activityinfo.client.importer.column.ColumnBinding)2 Location (org.activityinfo.server.database.hibernate.entity.Location)2 BatchCommand (org.activityinfo.shared.command.BatchCommand)2 Command (org.activityinfo.shared.command.Command)2 CreateLocation (org.activityinfo.shared.command.CreateLocation)2 MatchLocation (org.activityinfo.shared.command.MatchLocation)2 BatchResult (org.activityinfo.shared.command.result.BatchResult)2 AdminLevelDTO (org.activityinfo.shared.dto.AdminLevelDTO)2 PartnerDTO (org.activityinfo.shared.dto.PartnerDTO)2 ProjectDTO (org.activityinfo.shared.dto.ProjectDTO)2 RpcMap (com.extjs.gxt.ui.client.data.RpcMap)1 Date (java.util.Date)1 List (java.util.List)1