Skip to content

Commit

Permalink
Merge pull request #953 from ZakarFin/search-labels
Browse files Browse the repository at this point in the history
Search channel UI labels
  • Loading branch information
ZakarFin authored Apr 24, 2023
2 parents c1fbac7 + 0419885 commit a90b659
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
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

0 comments on commit a90b659

Please sign in to comment.