Search in sources :

Example 6 with Cat

use of com.opensymphony.xwork2.util.Cat in project dhis2-core by dhis2.

the class LoadFormAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    User currentUser = currentUserService.getCurrentUser();
    Locale dbLocale = getLocaleWithDefault(new TranslateParams(true));
    CurrentUserUtil.setUserSetting(UserSettingKey.DB_LOCALE, dbLocale);
    dataSet = dataSetService.getDataSet(dataSetId);
    if (dataSet == null) {
        return INPUT;
    }
    FormType formType = dataSet.getFormType();
    if (formType.isCustom() && dataSet.hasDataEntryForm()) {
        dataEntryForm = dataSet.getDataEntryForm();
        customDataEntryFormCode = dataEntryFormService.prepareDataEntryFormForEntry(dataSet);
        return formType.toString();
    }
    // ---------------------------------------------------------------------
    // Section / default form
    // ---------------------------------------------------------------------
    List<DataElement> dataElements = new ArrayList<>(dataSet.getDataElements());
    if (dataElements.isEmpty()) {
        return INPUT;
    }
    Collections.sort(dataElements);
    orderedDataElements = ListMap.getListMap(dataElements, de -> de.getDataElementCategoryCombo(dataSet));
    orderedCategoryCombos = getCategoryCombos(dataElements, dataSet);
    for (CategoryCombo categoryCombo : orderedCategoryCombos) {
        List<CategoryOptionCombo> optionCombos = categoryCombo.getSortedOptionCombos();
        orderedCategoryOptionCombos.put(categoryCombo.getId(), optionCombos);
        addOptionAccess(currentUser, optionComboAccessMap, optionCombos);
        // -----------------------------------------------------------------
        // Perform ordering of categories and their options so that they
        // could be displayed as in the paper form. Note that the total
        // number of entry cells to be generated are the multiple of options
        // from each category.
        // -----------------------------------------------------------------
        numberOfTotalColumns.put(categoryCombo.getId(), optionCombos.size());
        orderedCategories.put(categoryCombo.getId(), categoryCombo.getCategories());
        Map<Long, List<CategoryOption>> optionsMap = new HashMap<>();
        for (Category category : categoryCombo.getCategories()) {
            optionsMap.put(category.getId(), category.getCategoryOptions());
        }
        orderedOptionsMap.put(categoryCombo.getId(), optionsMap);
        // -----------------------------------------------------------------
        // Calculating the number of times each category should be repeated
        // -----------------------------------------------------------------
        Map<Long, Integer> catRepeat = new HashMap<>();
        Map<Long, Collection<Integer>> colRepeat = new HashMap<>();
        int catColSpan = optionCombos.size();
        for (Category cat : categoryCombo.getCategories()) {
            int categoryOptionSize = cat.getCategoryOptions().size();
            if (categoryOptionSize > 0 && catColSpan >= categoryOptionSize) {
                catColSpan = catColSpan / categoryOptionSize;
                int total = optionCombos.size() / (catColSpan * categoryOptionSize);
                Collection<Integer> cols = new ArrayList<>(total);
                for (int i = 0; i < total; i++) {
                    cols.add(i);
                }
                colRepeat.put(cat.getId(), cols);
                catRepeat.put(cat.getId(), catColSpan);
            }
        }
        catColRepeat.put(categoryCombo.getId(), colRepeat);
    }
    // ---------------------------------------------------------------------
    // Get data entry form
    // ---------------------------------------------------------------------
    DataSet dsOriginal = dataSet;
    if (dataSet.getFormType().isDefault()) {
        DataSet dataSetCopy = new DataSet();
        dataSetCopy.setUid(dataSet.getUid());
        dataSetCopy.setCode(dataSet.getCode());
        dataSetCopy.setName(dataSet.getName());
        dataSetCopy.setShortName(dataSet.getShortName());
        dataSetCopy.setRenderAsTabs(dataSet.isRenderAsTabs());
        dataSetCopy.setRenderHorizontally(dataSet.isRenderHorizontally());
        dataSetCopy.setDataElementDecoration(dataSet.isDataElementDecoration());
        dataSetCopy.setCompulsoryDataElementOperands(dataSet.getCompulsoryDataElementOperands());
        for (int i = 0; i < orderedCategoryCombos.size(); i++) {
            CategoryCombo categoryCombo = orderedCategoryCombos.get(i);
            String name = !categoryCombo.isDefault() ? categoryCombo.getName() : dataSetCopy.getName();
            Section section = new Section();
            section.setUid(CodeGenerator.generateUid());
            section.setId(i);
            section.setName(name);
            section.setSortOrder(i);
            section.setDataSet(dataSetCopy);
            dataSetCopy.getSections().add(section);
            section.getDataElements().addAll(orderedDataElements.get(categoryCombo));
            if (i == 0) {
                section.setIndicators(new ArrayList<>(dataSet.getIndicators()));
            }
        }
        dataSet = dataSetCopy;
        formType = FormType.SECTION;
    }
    if (CodeGenerator.isValidUid(multiOrganisationUnit)) {
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(multiOrganisationUnit);
        List<OrganisationUnit> organisationUnitChildren = new ArrayList<>();
        for (OrganisationUnit child : organisationUnit.getChildren()) {
            if (child.getDataSets().contains(dsOriginal)) {
                organisationUnitChildren.add(child);
            }
        }
        Collections.sort(organisationUnitChildren);
        organisationUnits.addAll(organisationUnitChildren);
        getSectionForm(dataSet);
        formType = FormType.SECTION_MULTIORG;
    }
    getSectionForm(dataSet);
    return formType.toString();
}
Also used : Locale(java.util.Locale) CurrentUserUtil(org.hisp.dhis.user.CurrentUserUtil) ListMap(org.hisp.dhis.common.ListMap) AggregateAccessManager(org.hisp.dhis.datavalue.AggregateAccessManager) SectionUtils(org.hisp.dhis.dxf2.util.SectionUtils) SectionOrderComparator(org.hisp.dhis.dataset.comparator.SectionOrderComparator) CategoryOption(org.hisp.dhis.category.CategoryOption) DataSet(org.hisp.dhis.dataset.DataSet) UserSettingKey(org.hisp.dhis.user.UserSettingKey) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) DataElement(org.hisp.dhis.dataelement.DataElement) CategoryComboSizeNameComparator(org.hisp.dhis.category.comparator.CategoryComboSizeNameComparator) UserSettingService(org.hisp.dhis.user.UserSettingService) Locale(java.util.Locale) Map(java.util.Map) User(org.hisp.dhis.user.User) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DataEntryForm(org.hisp.dhis.dataentryform.DataEntryForm) FormType(org.hisp.dhis.dataset.FormType) Collection(java.util.Collection) Set(java.util.Set) Category(org.hisp.dhis.category.Category) DataEntryFormService(org.hisp.dhis.dataentryform.DataEntryFormService) Collectors(java.util.stream.Collectors) Section(org.hisp.dhis.dataset.Section) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CategoryCombo(org.hisp.dhis.category.CategoryCombo) CodeGenerator(org.hisp.dhis.common.CodeGenerator) DataSetService(org.hisp.dhis.dataset.DataSetService) Action(com.opensymphony.xwork2.Action) TranslateParams(org.hisp.dhis.dxf2.common.TranslateParams) Collections(java.util.Collections) User(org.hisp.dhis.user.User) Category(org.hisp.dhis.category.Category) TranslateParams(org.hisp.dhis.dxf2.common.TranslateParams) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DataSet(org.hisp.dhis.dataset.DataSet) ArrayList(java.util.ArrayList) DataElement(org.hisp.dhis.dataelement.DataElement) CategoryCombo(org.hisp.dhis.category.CategoryCombo) ArrayList(java.util.ArrayList) List(java.util.List) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) FormType(org.hisp.dhis.dataset.FormType) Section(org.hisp.dhis.dataset.Section) Collection(java.util.Collection) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 7 with Cat

use of com.opensymphony.xwork2.util.Cat in project struts by apache.

the class OgnlValueStackTest method testSetNullList.

public void testSetNullList() {
    Foo foo = new Foo();
    OgnlValueStack vs = createValueStack();
    vs.getContext().put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    vs.push(foo);
    vs.setValue("cats[0].name", "Cat One");
    vs.setValue("cats[1].name", "Cat Two");
    assertNotNull(foo.getCats());
    assertEquals(2, foo.getCats().size());
    assertEquals("Cat One", ((Cat) foo.getCats().get(0)).getName());
    assertEquals("Cat Two", ((Cat) foo.getCats().get(1)).getName());
    // test when both Key and Value types of Map are interfaces but concrete classes are defined in .properties file
    vs.setValue("animalMap[3].name", "Cat Three by interface");
    vs.setValue("animalMap[6].name", "Cat Six by interface");
    assertNotNull(foo.getAnimalMap());
    assertEquals(2, foo.getAnimalMap().size());
    assertEquals("Cat Three by interface", foo.getAnimalMap().get(3L).getName());
    assertEquals("Cat Six by interface", foo.getAnimalMap().get(6L).getName());
    vs.setValue("annotatedCats[0].name", "Cat One By Annotation");
    vs.setValue("annotatedCats[1].name", "Cat Two By Annotation");
    assertNotNull(foo.getAnnotatedCats());
    assertEquals(2, foo.getAnnotatedCats().size());
    assertEquals("Cat One By Annotation", ((Cat) foo.getAnnotatedCats().get(0)).getName());
    assertEquals("Cat Two By Annotation", ((Cat) foo.getAnnotatedCats().get(1)).getName());
    vs.setValue("cats[0].foo.cats[1].name", "Deep null cat");
    assertNotNull(((Cat) foo.getCats().get(0)).getFoo());
    assertNotNull(((Cat) foo.getCats().get(0)).getFoo().getCats());
    assertNotNull(((Cat) foo.getCats().get(0)).getFoo().getCats().get(1));
    assertEquals("Deep null cat", ((Cat) ((Cat) foo.getCats().get(0)).getFoo().getCats().get(1)).getName());
    vs.setValue("annotatedCats[0].foo.annotatedCats[1].name", "Deep null cat by annotation");
    assertNotNull(((Cat) foo.getAnnotatedCats().get(0)).getFoo());
    assertNotNull(((Cat) foo.getAnnotatedCats().get(0)).getFoo().getAnnotatedCats());
    assertNotNull(((Cat) foo.getAnnotatedCats().get(0)).getFoo().getAnnotatedCats().get(1));
    assertEquals("Deep null cat by annotation", ((Cat) ((Cat) foo.getAnnotatedCats().get(0)).getFoo().getAnnotatedCats().get(1)).getName());
}
Also used : Foo(com.opensymphony.xwork2.util.Foo)

Example 8 with Cat

use of com.opensymphony.xwork2.util.Cat in project struts by apache.

the class SetPropertiesTest method doTestAddingToListsWithObjects.

public void doTestAddingToListsWithObjects(final boolean allowAdditions) {
    loadConfigurationProviders(new StubConfigurationProvider() {

        @Override
        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            builder.factory(ObjectTypeDeterminer.class, new Factory() {

                public Object create(Context context) throws Exception {
                    return new MockObjectTypeDeterminer(null, Cat.class, null, allowAdditions);
                }

                @Override
                public Class type() {
                    return Cat.class;
                }
            });
        }
    });
    Foo foo = new Foo();
    foo.setMoreCats(new ArrayList());
    String spielname = "Spielen";
    ValueStack vs = ActionContext.getContext().getValueStack();
    vs.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    vs.getContext().put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    vs.push(foo);
    try {
        vs.setValue("moreCats[2].name", spielname);
    } catch (IndexOutOfBoundsException e) {
        if (allowAdditions) {
            throw e;
        }
    }
    Object setCat = null;
    if (allowAdditions) {
        setCat = foo.getMoreCats().get(2);
        assertNotNull(setCat);
        assertTrue(setCat instanceof Cat);
        assertTrue(((Cat) setCat).getName().equals(spielname));
    } else {
        assertTrue(foo.getMoreCats() == null || foo.getMoreCats().size() == 0);
    }
    // has been created
    if (allowAdditions) {
        spielname = "paws";
        vs.setValue("moreCats[0].name", spielname);
        setCat = foo.getMoreCats().get(0);
        assertNotNull(setCat);
        assertTrue(setCat instanceof Cat);
        assertTrue(((Cat) setCat).getName().equals(spielname));
    }
}
Also used : Context(com.opensymphony.xwork2.inject.Context) ActionContext(com.opensymphony.xwork2.ActionContext) ValueStack(com.opensymphony.xwork2.util.ValueStack) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) Foo(com.opensymphony.xwork2.util.Foo) Factory(com.opensymphony.xwork2.inject.Factory) MockObjectTypeDeterminer(com.opensymphony.xwork2.mock.MockObjectTypeDeterminer) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) ObjectTypeDeterminer(com.opensymphony.xwork2.conversion.ObjectTypeDeterminer) MockObjectTypeDeterminer(com.opensymphony.xwork2.mock.MockObjectTypeDeterminer) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) Cat(com.opensymphony.xwork2.util.Cat)

Example 9 with Cat

use of com.opensymphony.xwork2.util.Cat in project struts by apache.

the class ListAction method testNestedConverters.

public void testNestedConverters() {
    Cat cat = new Cat();
    cat.setFoo(new Foo());
    stack.push(cat);
    stack.setValue("foo.number", "123");
    assertEquals(321, cat.getFoo().getNumber());
}
Also used : Cat(com.opensymphony.xwork2.util.Cat) Foo(com.opensymphony.xwork2.util.Foo)

Aggregations

Foo (com.opensymphony.xwork2.util.Foo)6 Cat (com.opensymphony.xwork2.util.Cat)4 MockObjectTypeDeterminer (com.opensymphony.xwork2.mock.MockObjectTypeDeterminer)2 ValueStack (com.opensymphony.xwork2.util.ValueStack)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Action (com.opensymphony.xwork2.Action)1 ActionContext (com.opensymphony.xwork2.ActionContext)1 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 ObjectTypeDeterminer (com.opensymphony.xwork2.conversion.ObjectTypeDeterminer)1 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)1 Context (com.opensymphony.xwork2.inject.Context)1 Factory (com.opensymphony.xwork2.inject.Factory)1 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)1 TestBean2 (com.opensymphony.xwork2.test.TestBean2)1 AnnotatedCat (com.opensymphony.xwork2.util.AnnotatedCat)1 Bar (com.opensymphony.xwork2.util.Bar)1 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)1 ArrayList (java.util.ArrayList)1