Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search channel UI labels #953

Merged
merged 4 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fi.nls.oskari.control.ActionHandler;
import fi.nls.oskari.control.ActionParameters;
import fi.nls.oskari.domain.User;
import fi.nls.oskari.search.channel.SearchAutocomplete;
import fi.nls.oskari.search.channel.SearchableChannel;
import fi.nls.oskari.service.OskariComponentManager;
import fi.nls.oskari.util.JSONHelper;
Expand Down Expand Up @@ -52,7 +53,13 @@ public void handleAction(final ActionParameters params) throws ActionException {
JSONObject json = new JSONObject();
JSONHelper.putValue(json, "id", channel.getId());
JSONHelper.putValue(json, "isDefault", channel.isDefaultChannel());
JSONHelper.putValue(json, "locale", channel.getUILabels().optJSONObject(params.getLocale().getLanguage()));
JSONHelper.putValue(json, "isSuggestions", channel instanceof SearchAutocomplete);
JSONObject labels = channel.getUILabels();
if (labels.has(params.getLocale().getLanguage())) {
JSONHelper.putValue(json, "locale", channel.getUILabels().optJSONObject(params.getLocale().getLanguage()));
} else {
JSONHelper.putValue(json, "locale", channel.getUILabels().optJSONObject(PropertyUtil.getDefaultLanguage()));
}
channelsJSONArray.put(json);
}
JSONObject response = new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public abstract class SearchChannel extends OskariComponent implements Searchabl
// store encountered types here to only log about possible configs for new types
private Set<String> types = new HashSet<String>();
private int maxCount = 100;
private JSONObject uiLabels = null;

public String getId() {
return getName();
Expand Down Expand Up @@ -69,8 +70,46 @@ public ChannelSearchResult reverseGeocode(SearchCriteria searchCriteria) throws
}

public JSONObject getUILabels() {
if (uiLabels != null) {
return uiLabels;
}
String defaultLang = PropertyUtil.getDefaultLanguage();
uiLabels = new JSONObject();
final Object labelObj = PropertyUtil.getLocalizableProperty("search.channel." + getName() + ".label", getId());
if (labelObj instanceof String) {
// single value configured
JSONHelper.putValue(uiLabels, defaultLang, JSONHelper.createJSONObject("name", labelObj));
} else if (labelObj instanceof Map) {
// localized values configured
Map<String, String> values = (Map<String, String>) labelObj;
values.keySet().forEach(lang ->
JSONHelper.putValue(uiLabels, lang, JSONHelper.createJSONObject("name", values.get(lang))));
}
// inject descriptions
final Object descObj = PropertyUtil.getLocalizableProperty("search.channel." + getName() + ".desc");
if (descObj instanceof String) {
// single value configured
JSONObject langBlock = uiLabels.optJSONObject(defaultLang);
if (langBlock != null) {
JSONHelper.putValue(langBlock, "desc", descObj);
}
} else if (descObj instanceof Map) {
// localized values configured
Map<String, String> values = (Map<String, String>) descObj;
values.keySet().forEach(lang -> {
JSONObject langBlock = uiLabels.optJSONObject(lang);
if (langBlock != null) {
JSONHelper.putValue(langBlock, "desc", values.get(lang));
}
});
}
if (uiLabels.keys().hasNext()) {
return uiLabels;
}
// fallback to channel id
JSONObject name = JSONHelper.createJSONObject("name", getId());
return JSONHelper.createJSONObject(PropertyUtil.getDefaultLanguage(), name);
JSONHelper.putValue(uiLabels, defaultLang, name);
return uiLabels;
}

public boolean isValidSearchTerm(SearchCriteria criteria) {
Expand Down