@Component
public class StyleConverterServiceImpl implements StyleConverterService {
public NamedStyleInfo convert(StyledLayerDescriptorInfo styledLayerDescriptorInfo, FeatureInfo featureInfo,
String layerName, String styleName) throws LayerException {
NamedLayerInfo namedLayerInfo = null;
// find first named layer or find by name
for (StyledLayerDescriptorInfo.ChoiceInfo choice : styledLayerDescriptorInfo.getChoiceList()) {
// we only support named layers, pick the right name or the first one
if (choice.ifNamedLayer()) {
if (layerName != null) {
if (layerName.equals(choice.getNamedLayer().getName())) {
namedLayerInfo = choice.getNamedLayer();
break;
}
}
if (namedLayerInfo == null) {
namedLayerInfo = choice.getNamedLayer();
}
}
}
if (namedLayerInfo == null) {
throw new LayerException(ExceptionCode.INVALID_SLD, styledLayerDescriptorInfo.getName(), layerName);
}
UserStyleInfo userStyleInfo = null;
for (NamedLayerInfo.ChoiceInfo choice : namedLayerInfo.getChoiceList()) {
// we only support user styles, pick the right name or the first
if (choice.ifUserStyle()) {
if (styleName != null) {
if (styleName.equals(choice.getUserStyle().getName())) {
userStyleInfo = choice.getUserStyle();
break;
}
}
if (userStyleInfo == null) {
userStyleInfo = choice.getUserStyle();
}
}
}
if (userStyleInfo == null) {
throw new LayerException(ExceptionCode.INVALID_SLD, styledLayerDescriptorInfo.getName(), layerName);
}
NamedStyleInfo namedStyleInfo = convert(userStyleInfo, featureInfo);
if (namedStyleInfo.getName() == null) {
namedStyleInfo.setName(styleName);
}
for (FeatureStyleInfo featureStyleInfo : namedStyleInfo.getFeatureStyles()) {
if (featureStyleInfo.getName() == null) {
featureStyleInfo.setName(namedLayerInfo.getName());
}
featureStyleInfo.setStyleId(namedStyleInfo.getName() + featureStyleInfo.getIndex());
}
return namedStyleInfo;
}