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);
}
});
}
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());
}
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()));
}
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);
}
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));
}
Aggregations