Search in sources :

Example 6 with CreateResult

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());
}
Also used : CreateResult(org.activityinfo.shared.command.result.CreateResult) Activity(org.activityinfo.server.database.hibernate.entity.Activity) Indicator(org.activityinfo.server.database.hibernate.entity.Indicator) Date(java.util.Date)

Example 7 with CreateResult

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());
    }
}
Also used : ActivityPolicy(org.activityinfo.server.command.handler.crud.ActivityPolicy) PropertyMap(org.activityinfo.server.command.handler.crud.PropertyMap) CreateResult(org.activityinfo.shared.command.result.CreateResult) UserDatabasePolicy(org.activityinfo.server.command.handler.crud.UserDatabasePolicy) IllegalAccessCommandException(org.activityinfo.shared.exception.IllegalAccessCommandException) CommandException(org.activityinfo.shared.exception.CommandException)

Example 8 with CreateResult

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());
}
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 9 with CreateResult

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));
}
Also used : ModelData(com.extjs.gxt.ui.client.data.ModelData) CreateResult(org.activityinfo.shared.command.result.CreateResult) MockEventBus(org.activityinfo.client.MockEventBus) DispatcherStub(org.activityinfo.client.dispatch.DispatcherStub) UIConstants(org.activityinfo.client.i18n.UIConstants) StateManagerStub(org.activityinfo.client.mock.StateManagerStub) ActivityDTO(org.activityinfo.shared.dto.ActivityDTO) SchemaDTO(org.activityinfo.shared.dto.SchemaDTO) Test(org.junit.Test)

Example 10 with CreateResult

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());
}
Also used : CreateResult(org.activityinfo.shared.command.result.CreateResult) UserDatabaseDTO(org.activityinfo.shared.dto.UserDatabaseDTO) LocationTypeDTO(org.activityinfo.shared.dto.LocationTypeDTO) ActivityDTO(org.activityinfo.shared.dto.ActivityDTO) SchemaDTO(org.activityinfo.shared.dto.SchemaDTO) GetSchema(org.activityinfo.shared.command.GetSchema) Test(org.junit.Test)

Aggregations

CreateResult (org.activityinfo.shared.command.result.CreateResult)36 Test (org.junit.Test)17 SchemaDTO (org.activityinfo.shared.dto.SchemaDTO)9 Date (java.util.Date)8 GetSchema (org.activityinfo.shared.command.GetSchema)8 PartnerDTO (org.activityinfo.shared.dto.PartnerDTO)8 CreateSite (org.activityinfo.shared.command.CreateSite)7 SiteDTO (org.activityinfo.shared.dto.SiteDTO)7 AddPartner (org.activityinfo.shared.command.AddPartner)6 ActivityDTO (org.activityinfo.shared.dto.ActivityDTO)6 ProjectDTO (org.activityinfo.shared.dto.ProjectDTO)6 OnDataSet (org.activityinfo.server.database.OnDataSet)5 UserDatabase (org.activityinfo.server.database.hibernate.entity.UserDatabase)5 CreateEntity (org.activityinfo.shared.command.CreateEntity)5 DuplicateCreateResult (org.activityinfo.shared.command.result.DuplicateCreateResult)5 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)4 FormDialogCallback (org.activityinfo.client.page.common.dialog.FormDialogCallback)4 FormDialogTether (org.activityinfo.client.page.common.dialog.FormDialogTether)4 Activity (org.activityinfo.server.database.hibernate.entity.Activity)4 AddTarget (org.activityinfo.shared.command.AddTarget)4