use of org.activityinfo.shared.command.result.CreateResult in project activityinfo by bedatadriven.
the class CreateEntityHandler method createIndicator.
private CommandResult createIndicator(User user, CreateEntity cmd, Map<String, Object> properties) throws IllegalAccessCommandException {
Indicator indicator = new Indicator();
Activity activity = entityManager().getReference(Activity.class, properties.get("activityId"));
indicator.setActivity(activity);
assertDesignPriviledges(user, indicator.getActivity().getDatabase());
updateIndicatorProperties(indicator, properties);
entityManager().persist(indicator);
activity.getDatabase().setLastSchemaUpdate(new Date());
return new CreateResult(indicator.getId());
}
use of org.activityinfo.shared.command.result.CreateResult in project activityinfo by bedatadriven.
the class CreateEntityHandler method execute.
@Override
public CommandResult execute(CreateEntity cmd, User user) throws CommandException {
Map<String, Object> properties = cmd.getProperties().getTransientMap();
PropertyMap propertyMap = new PropertyMap(cmd.getProperties().getTransientMap());
if ("UserDatabase".equals(cmd.getEntityName())) {
UserDatabasePolicy policy = injector.getInstance(UserDatabasePolicy.class);
return new CreateResult((Integer) policy.create(user, propertyMap));
} else if ("Activity".equals(cmd.getEntityName())) {
ActivityPolicy policy = injector.getInstance(ActivityPolicy.class);
return new CreateResult((Integer) policy.create(user, propertyMap));
} else if ("AttributeGroup".equals(cmd.getEntityName())) {
return createAttributeGroup(cmd, properties);
} else if ("Attribute".equals(cmd.getEntityName())) {
return createAttribute(cmd, properties);
} else if ("Indicator".equals(cmd.getEntityName())) {
return createIndicator(user, cmd, properties);
} else {
throw new CommandException("Invalid entity class " + cmd.getEntityName());
}
}
use of org.activityinfo.shared.command.result.CreateResult 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.result.CreateResult in project activityinfo by bedatadriven.
the class DesignTest method testNewActivityComesWithFolders.
@Test
public void testNewActivityComesWithFolders() {
// Test data
SchemaDTO schema = DTOs.pear();
// Collaborator : EventBus
MockEventBus eventBus = new MockEventBus();
// Collaborator : Command Service
DispatcherStub service = new DispatcherStub();
service.setResult(GetSchema.class, schema);
service.setResult(CreateEntity.class, new CreateResult(991));
// Collaborator : View
MockDesignTree view = new MockDesignTree();
// Constants
UIConstants constants = createNiceMock(UIConstants.class);
replay(constants);
// Class under test
DesignPresenter designer = new DesignPresenter(eventBus, service, new StateManagerStub(), view, constants);
// VERIFY that when an activity is added, it appears at the end of the
// list with two
// sub folders
designer.go(schema.getDatabaseById(1));
view.newEntityProperties.put("name", "Psychosocial support");
designer.onNew("Activity");
List<ModelData> rootItems = designer.getTreeStore().getRootItems();
ActivityDTO addedActivity = (ActivityDTO) rootItems.get(rootItems.size() - 1);
Assert.assertEquals("Psychosocial support", addedActivity.getName());
Assert.assertEquals("child nodes", 2, designer.getTreeStore().getChildCount(addedActivity));
}
use of org.activityinfo.shared.command.result.CreateResult in project activityinfo by bedatadriven.
the class ActivityTest method testActivity.
@Test
public void testActivity() throws CommandException {
/*
* Initial data load
*/
SchemaDTO schema = execute(new GetSchema());
UserDatabaseDTO db = schema.getDatabaseById(1);
/*
* Create a new activity
*/
LocationTypeDTO locType = schema.getCountryById(1).getLocationTypes().get(0);
ActivityDTO act = new ActivityDTO();
act.setName("Warshing the dishes");
act.setLocationTypeId(locType.getId());
act.setReportingFrequency(ActivityDTO.REPORT_MONTHLY);
CreateResult cresult = execute(CreateEntity.Activity(db, act));
int newId = cresult.getNewId();
/*
* Reload schema to verify the changes have stuck
*/
schema = execute(new GetSchema());
act = schema.getActivityById(newId);
Assert.assertEquals("name", "Warshing the dishes", act.getName());
Assert.assertEquals("locationType", locType.getName(), act.getLocationType().getName());
Assert.assertEquals("reportingFrequency", ActivityDTO.REPORT_MONTHLY, act.getReportingFrequency());
Assert.assertEquals("public", Published.NOT_PUBLISHED.getIndex(), act.getPublished());
}
Aggregations