
com.podio.item.map.converter.CategoryConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api Show documentation
Show all versions of api Show documentation
The official Java wrapper for the Podio API
The newest version!
package com.podio.item.map.converter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.ConvertUtils;
import com.podio.app.CategoryOption;
public class CategoryConverter implements FieldConverter {
private final List options;
public CategoryConverter(List options) {
super();
this.options = options;
}
@Override
public Map fromModel(Object value) {
String stringValue;
if (value.getClass().isEnum()) {
stringValue = value.toString();
stringValue = stringValue.replace(' ', '_');
} else {
stringValue = (String) ConvertUtils.convert(value,
String.class);
}
CategoryOption option = getOptionByText(stringValue);
if (option != null) {
return Collections.singletonMap("value", option.getId());
}
throw new RuntimeException("No state with name " + stringValue
+ " found");
}
private CategoryOption getOptionByText(String text) {
for (CategoryOption option : options) {
if (option.getText().equalsIgnoreCase(text)) {
return option;
}
}
return null;
}
@Override
public Object toModel(Map map, Class modelClass) {
Map option = (Map) map.get("value");
String stringValue = (String) option.get("text");
if (modelClass.isEnum()) {
return Enum.valueOf(modelClass, stringValue.toUpperCase());
} else {
return ConvertUtils.convert(stringValue, modelClass);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy