Author: bleny Date: 2014-03-27 14:34:21 +0100 (Thu, 27 Mar 2014) New Revision: 1767 Url: http://forge.codelutin.com/projects/wao/repository/revisions/1767 Log: refs #4483 sort option by labels in select in filter form, introduce FilterOption Added: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/FilterOption.java Modified: trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SamplingStrategy.java trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/SampleRowsFilterValues.java trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/LocalizedSampleRowsFilterValues.java trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/sampling-plan.jsp trunk/wao-web/src/main/webapp/js/wao.js Modified: trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SamplingStrategy.java =================================================================== --- trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SamplingStrategy.java 2014-03-27 12:39:43 UTC (rev 1766) +++ trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SamplingStrategy.java 2014-03-27 13:34:21 UTC (rev 1767) @@ -22,7 +22,7 @@ import static org.nuiton.i18n.I18n.n; -public enum SamplingStrategy { +public enum SamplingStrategy implements I18nAble { SIMULTANEOUS_G1_SPECIES(n("SamplingStrategy.SIMULTANEOUS_G1_SPECIES")), SIMULTANEOUS_G1_G2_SPECIES(n("SamplingStrategy.SIMULTANEOUS_G1_G2_SPECIES")), @@ -45,9 +45,8 @@ } @Override - public String toString() { - // return WaoUtils.t(i18nKey); - throw new UnsupportedOperationException(); + public String getI18nKey() { + return i18nKey; } public boolean isSpecificStock() { Added: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/FilterOption.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/FilterOption.java (rev 0) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/FilterOption.java 2014-03-27 13:34:21 UTC (rev 1767) @@ -0,0 +1,50 @@ +package fr.ifremer.wao.services.service; + +import com.google.common.collect.Ordering; +import fr.ifremer.wao.entity.I18nAble; + +public class FilterOption implements Comparable<FilterOption> { + + protected String value; + + protected String label; + + /** + * Both label and value is the given str. + */ + public static FilterOption forString(String str) { + FilterOption filterOption = new FilterOption(str, str); + return filterOption; + } + + /** + * Both label and value is the given str. + */ + public static <T extends Enum & I18nAble> FilterOption forEnum(T enumValue) { + FilterOption filterOption = new FilterOption(enumValue.name(), enumValue.getI18nKey()); + return filterOption; + } + + public static FilterOption forValueAndLabel(String value, String label) { + FilterOption filterOption = new FilterOption(value, label); + return filterOption; + } + + public FilterOption(String value, String label) { + this.value = value; + this.label = label; + } + + public String getValue() { + return value; + } + + public String getLabel() { + return label; + } + + @Override + public int compareTo(FilterOption other) { + return Ordering.natural().compare(label, other.label); + } +} Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/SampleRowsFilterValues.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/SampleRowsFilterValues.java 2014-03-27 12:39:43 UTC (rev 1766) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/SampleRowsFilterValues.java 2014-03-27 13:34:21 UTC (rev 1767) @@ -3,15 +3,11 @@ import fr.ifremer.wao.entity.DCF5Code; import fr.ifremer.wao.entity.FishingZone; import fr.ifremer.wao.entity.SampleRow; -import fr.ifremer.wao.entity.SamplingStrategy; import fr.ifremer.wao.entity.TerrestrialLocation; -import org.apache.commons.lang3.tuple.Pair; import java.io.Serializable; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; /** * Bean to hold all values that user can select to fill {@link fr.ifremer.wao.SampleRowsFilter}. @@ -20,86 +16,89 @@ private static final long serialVersionUID = 1L; - protected Set<String> fishingZoneFacadeNames = new HashSet<>(); + protected SortedSet<FilterOption> fishingZoneFacadeNames = new TreeSet<>(); - protected Set<String> fishingZoneSectorNames = new HashSet<>(); + protected SortedSet<FilterOption> fishingZoneSectorNames = new TreeSet<>(); - protected Set<String> sampleRowCodes = new HashSet<>(); + protected SortedSet<FilterOption> sampleRowCodes = new TreeSet<>(); - protected Set<String> programNames = new HashSet<>(); + protected SortedSet<FilterOption> programNames = new TreeSet<>(); - protected Map<String, String> companies = new HashMap<>(); + protected SortedSet<FilterOption> companies = new TreeSet<>(); - protected Set<SamplingStrategy> samplingStrategies = new HashSet<>(); + protected SortedSet<FilterOption> samplingStrategies = new TreeSet<>(); - protected Map<String, String> terrestrialDistricts = new HashMap<>(); + protected SortedSet<FilterOption> terrestrialDistricts = new TreeSet<>(); /** Values are the code and the label i18n key. */ - protected Map<String, Pair<String, String>> fishingGearDcfs = new HashMap<>(); + protected SortedSet<FilterOption> fishingGearDcfs = new TreeSet<>(); /** Values are the code and the label i18n key. */ - protected Map<String, Pair<String, String>> targetSpeciesDcfs = new HashMap<>(); + protected SortedSet<FilterOption> targetSpeciesDcfs = new TreeSet<>(); public void addSampleRow(SampleRow sampleRow) { for (FishingZone fishingZone : sampleRow.getFishingZone()) { - fishingZoneFacadeNames.add(fishingZone.getFacadeName()); - fishingZoneSectorNames.add(fishingZone.getSectorName()); + fishingZoneFacadeNames.add(FilterOption.forString(fishingZone.getFacadeName())); + fishingZoneSectorNames.add(FilterOption.forString(fishingZone.getSectorName())); } - sampleRowCodes.add(sampleRow.getCode()); - programNames.add(sampleRow.getProgramName()); - companies.put(sampleRow.getCompany().getTopiaId(), sampleRow.getCompany().getName()); - samplingStrategies.add(sampleRow.getSamplingStrategy()); + sampleRowCodes.add(FilterOption.forString(sampleRow.getCode())); + programNames.add(FilterOption.forString(sampleRow.getProgramName())); + companies.add(FilterOption.forValueAndLabel(sampleRow.getCompany().getTopiaId(), sampleRow.getCompany().getName())); + samplingStrategies.add(FilterOption.forEnum(sampleRow.getSamplingStrategy())); if (sampleRow.getObsProgram().isObsVente()) { TerrestrialLocation terrestrialDistrict = sampleRow.getTerrestrialLocation(); - terrestrialDistricts.put(terrestrialDistrict.getTopiaId(), terrestrialDistrict.getDescription()); + terrestrialDistricts.add( + FilterOption.forValueAndLabel( + terrestrialDistrict.getTopiaId(), + terrestrialDistrict.getDescription())); } for (DCF5Code dcf5Code : sampleRow.getdCF5Code()) { - fishingGearDcfs.put( - dcf5Code.getFishingGearDCF().getTopiaId(), - Pair.of(dcf5Code.getFishingGearDCF().getCode(), + fishingGearDcfs.add( + FilterOption.forValueAndLabel( + dcf5Code.getFishingGearDCF().getTopiaId(), dcf5Code.getFishingGearDCF().getI18nKey())); if (dcf5Code.getTargetSpeciesDCF() != null) { - targetSpeciesDcfs.put( - dcf5Code.getTargetSpeciesDCF().getTopiaId(), - Pair.of(dcf5Code.getTargetSpeciesDCF().getCode(), + targetSpeciesDcfs.add( + FilterOption.forValueAndLabel( + dcf5Code.getTargetSpeciesDCF().getTopiaId(), dcf5Code.getTargetSpeciesDCF().getI18nKey())); } } } - public Set<String> getFishingZoneFacadeNames() { + public SortedSet<FilterOption> getFishingZoneFacadeNames() { return fishingZoneFacadeNames; } - public Set<String> getFishingZoneSectorNames() { + public SortedSet<FilterOption> getFishingZoneSectorNames() { return fishingZoneSectorNames; } - public Set<String> getSampleRowCodes() { + public SortedSet<FilterOption> getSampleRowCodes() { return sampleRowCodes; } - public Set<String> getProgramNames() { + public SortedSet<FilterOption> getProgramNames() { return programNames; } - public Map<String, String> getCompanies() { + public SortedSet<FilterOption> getCompanies() { return companies; } - public Set<SamplingStrategy> getSamplingStrategies() { + public SortedSet<FilterOption> getSamplingStrategies() { return samplingStrategies; } - public Map<String, String> getTerrestrialDistricts() { + public SortedSet<FilterOption> getTerrestrialDistricts() { return terrestrialDistricts; } - public Map<String, Pair<String, String>> getFishingGearDcfs() { + public SortedSet<FilterOption> getFishingGearDcfs() { return fishingGearDcfs; } - public Map<String, Pair<String, String>> getTargetSpeciesDcfs() { + public SortedSet<FilterOption> getTargetSpeciesDcfs() { return targetSpeciesDcfs; } } Modified: trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/LocalizedSampleRowsFilterValues.java =================================================================== --- trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/LocalizedSampleRowsFilterValues.java 2014-03-27 12:39:43 UTC (rev 1766) +++ trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/LocalizedSampleRowsFilterValues.java 2014-03-27 13:34:21 UTC (rev 1767) @@ -1,16 +1,12 @@ package fr.ifremer.wao.web.action.obsmer; import fr.ifremer.wao.entity.SampleRow; -import fr.ifremer.wao.entity.SamplingStrategy; import fr.ifremer.wao.services.WaoCacheElement; +import fr.ifremer.wao.services.service.FilterOption; import fr.ifremer.wao.services.service.SampleRowsFilterValues; -import org.apache.commons.lang3.tuple.Pair; -import org.nuiton.i18n.I18n; -import java.util.HashMap; import java.util.Locale; -import java.util.Map; -import java.util.Set; +import java.util.SortedSet; public class LocalizedSampleRowsFilterValues implements WaoCacheElement { @@ -27,56 +23,39 @@ decorated.addSampleRow(sampleRow); } - public Set<String> getSampleRowCodes() { - return decorated.getSampleRowCodes(); + public SortedSet<FilterOption> getTargetSpeciesDcfs() { + return decorated.getTargetSpeciesDcfs(); } - public Set<SamplingStrategy> getSamplingStrategies() { - return decorated.getSamplingStrategies(); + public SortedSet<FilterOption> getTerrestrialDistricts() { + return decorated.getTerrestrialDistricts(); } - public Set<String> getFishingZoneSectorNames() { - return decorated.getFishingZoneSectorNames(); + public SortedSet<FilterOption> getSampleRowCodes() { + return decorated.getSampleRowCodes(); } - public Set<String> getProgramNames() { - return decorated.getProgramNames(); + public SortedSet<FilterOption> getFishingZoneSectorNames() { + return decorated.getFishingZoneSectorNames(); } - public Set<String> getFishingZoneFacadeNames() { - return decorated.getFishingZoneFacadeNames(); - } - - public Map<String, String> getCompanies() { + public SortedSet<FilterOption> getCompanies() { return decorated.getCompanies(); } - public Map<String, String> getTerrestrialDistricts() { - return decorated.getTerrestrialDistricts(); + public SortedSet<FilterOption> getProgramNames() { + return decorated.getProgramNames(); } - public Map<String, String> getFishingGearDcfs() { - Map<String, String> translated = new HashMap<>(); - for (Map.Entry<String, Pair<String, String>> entry : - decorated.getFishingGearDcfs().entrySet()) { - String topiaId = entry.getKey(); - String code = entry.getValue().getLeft(); - String i18nKey = entry.getValue().getRight(); - translated.put(topiaId, code + " " + I18n.l(locale, i18nKey)); - } - return translated; + public SortedSet<FilterOption> getSamplingStrategies() { + return decorated.getSamplingStrategies(); } - public Map<String, String> getTargetSpeciesDcfs() { - Map<String, String> translated = new HashMap<>(); - for (Map.Entry<String, Pair<String, String>> entry : - decorated.getTargetSpeciesDcfs().entrySet()) { - String topiaId = entry.getKey(); - String code = entry.getValue().getLeft(); - String i18nKey = entry.getValue().getRight(); - translated.put(topiaId, code + " " + I18n.l(locale, i18nKey)); - } - return translated; + public SortedSet<FilterOption> getFishingZoneFacadeNames() { + return decorated.getFishingZoneFacadeNames(); } + public SortedSet<FilterOption> getFishingGearDcfs() { + return decorated.getFishingGearDcfs(); + } } Modified: trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/sampling-plan.jsp =================================================================== --- trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/sampling-plan.jsp 2014-03-27 12:39:43 UTC (rev 1766) +++ trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/sampling-plan.jsp 2014-03-27 13:34:21 UTC (rev 1767) @@ -77,36 +77,48 @@ <s:select name="filter.companyIds" label="%{getText('wao.ui.entity.Company')}" list="filterValues.companies" + listKey="value" + listValue="label" multiple="true" dataBinding="companies" /> <s:select name="filter.programNames" label="%{getText('wao.ui.field.SampleRow.programName')}" list="filterValues.programNames" + listKey="value" + listValue="label" multiple="true" dataBinding="programNames" /> <s:select name="filter.fishingZoneFacadeNames" label="%{getText('wao.ui.field.FishingZone.facadeName')}" list="filterValues.fishingZoneFacadeNames" + listKey="value" + listValue="label" multiple="true" dataBinding="fishingZoneFacadeNames" /> <s:select name="filter.fishingZoneSectorNames" label="%{getText('wao.ui.field.FishingZone.sectorName')}" list="filterValues.fishingZoneSectorNames" + listKey="value" + listValue="label" multiple="true" dataBinding="fishingZoneSectorNames" /> <s:select name="filter.sampleRowCodes" label="%{getText('wao.ui.field.SampleRow.code')}" list="filterValues.sampleRowCodes" + listKey="value" + listValue="label" multiple="true" dataBinding="sampleRowCodes" /> <s:select name="filter.fishingGearDcfIds" label="%{getText('wao.ui.entity.fishingGearDCF')}" list="filterValues.fishingGearDcfs" + listKey="value" + listValue="label" value="%getText(#value)" multiple="true" dataBinding="fishingGearDcfs" /> @@ -114,6 +126,8 @@ <s:select name="filter.targetSpeciesDcfIds" label="%{getText('wao.ui.entity.targetSpeciesDCF')}" list="filterValues.targetSpeciesDcfs" + listKey="value" + listValue="label" multiple="true" dataBinding="targetSpeciesDcfs" /> Modified: trunk/wao-web/src/main/webapp/js/wao.js =================================================================== --- trunk/wao-web/src/main/webapp/js/wao.js 2014-03-27 12:39:43 UTC (rev 1766) +++ trunk/wao-web/src/main/webapp/js/wao.js 2014-03-27 13:34:21 UTC (rev 1767) @@ -79,11 +79,8 @@ // FIXME brendan 26/03/14 should be data-binding but freemarker break the template :-( var binding = $select.attr('databinding'); var options = filterValues[binding]; - $.each(options, function (value, label) { - if (typeof value == "number") { - value = label; - } - var optionHtml = '<option value="' + value + '">' + label + '</option>'; + $.each(options, function (index, option) { + var optionHtml = '<option value="' + option.value + '">' + option.label + '</option>'; $select.append(optionHtml); }); var selectedOptions = filter[name];