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