Author: tchemit Date: 2014-04-06 11:55:25 +0200 (Sun, 06 Apr 2014) New Revision: 1843 Url: http://forge.codelutin.com/projects/wao/repository/revisions/1843 Log: add BooleanSet converter + introduce converter package Added: trunk/wao-web/src/main/java/fr/ifremer/wao/web/converter/ trunk/wao-web/src/main/java/fr/ifremer/wao/web/converter/BooleanSetConverter.java trunk/wao-web/src/main/java/fr/ifremer/wao/web/converter/MonthConverter.java trunk/wao-web/src/main/resources/fr/ifremer/wao/ContactsFilter-conversion.properties Removed: trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/MonthConverter.java Modified: trunk/wao-web/src/main/resources/fr/ifremer/wao/SampleRowsFilter-conversion.properties trunk/wao-web/src/main/resources/fr/ifremer/wao/entity/SampleRow-conversion.properties Deleted: trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/MonthConverter.java =================================================================== --- trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/MonthConverter.java 2014-04-06 09:06:57 UTC (rev 1842) +++ trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/MonthConverter.java 2014-04-06 09:55:25 UTC (rev 1843) @@ -1,126 +0,0 @@ -package fr.ifremer.wao.web.action; - -/* - * #%L - * Wao :: Web - * %% - * Copyright (C) 2009 - 2014 Ifremer - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ - -import com.google.common.base.Preconditions; -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.conversion.TypeConversionException; -import fr.ifremer.wao.WaoUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.struts2.util.StrutsTypeConverter; - -import java.text.ParseException; -import java.util.Arrays; -import java.util.Date; -import java.util.Locale; -import java.util.Map; - -public class MonthConverter extends StrutsTypeConverter { - - /** Logger. */ - private static final Log log = LogFactory.getLog(MonthConverter.class); - - @Override - public Date convertFromString(Map map, String[] strings, Class aClass) { - - if (strings.length == 1) { - - String string = strings[0]; - - Date parsedValue; - - if (StringUtils.isEmpty(string)) { - - parsedValue = null; - - } else { - - Locale locale = (Locale) map.get(ActionContext.LOCALE); - Preconditions.checkNotNull(locale, "No locale found in ActionContext"); - - parsedValue = parseDate(locale, string, strings, true); - - if (parsedValue == null) { - - //FIXME If action is changing language, then date are not in good format - //FIXME Let's try why other locale... - if (Locale.FRENCH.equals(locale)) { - locale = Locale.ENGLISH; - } else { - locale = Locale.FRENCH; - } - parsedValue = parseDate(locale, string, strings, false); - } - } - - return parsedValue; - - } else { - throw new TypeConversionException("strings=" + Arrays.toString(strings)); - } - } - - protected Date parseDate(Locale locale, String string, String[] strings, boolean swallonError) { - Preconditions.checkNotNull(locale); - try { - return WaoUtils.parseMonth(locale, string); - } catch (ParseException e) { - if (swallonError) { - return null; - } - //FIXME See why we don't see the error - if (log.isErrorEnabled()) { - log.error("Could not format parse date " + string, e); - } - throw new TypeConversionException("strings=" + Arrays.toString(strings)); - } - } - - @Override - public String convertToString(Map map, Object o) { - - String str; - - if (o == null) { - - str = StringUtils.EMPTY; - - } else { - - if (o instanceof Date) { - - Date date = (Date) o; - - Locale locale = (Locale) map.get(ActionContext.LOCALE); - Preconditions.checkNotNull(locale, "No locale found in ActionContext"); - str = WaoUtils.formatMonth(locale, date); - } else { - throw new UnsupportedOperationException("cannot convert to month " + o); - } - } - - return str; - - } -} Added: trunk/wao-web/src/main/java/fr/ifremer/wao/web/converter/BooleanSetConverter.java =================================================================== --- trunk/wao-web/src/main/java/fr/ifremer/wao/web/converter/BooleanSetConverter.java (rev 0) +++ trunk/wao-web/src/main/java/fr/ifremer/wao/web/converter/BooleanSetConverter.java 2014-04-06 09:55:25 UTC (rev 1843) @@ -0,0 +1,97 @@ +package fr.ifremer.wao.web.converter; + +/* + * #%L + * Wao :: Web + * %% + * Copyright (C) 2009 - 2014 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import com.google.common.base.Joiner; +import org.apache.commons.lang3.StringUtils; +import org.apache.struts2.util.StrutsTypeConverter; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * To convert a set of Boolean to string (string empty value means {@code null} value). + * <p/> + * Created on 4/6/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 4.0 + */ +public class BooleanSetConverter extends StrutsTypeConverter { + + @Override + public Set<Boolean> convertFromString(Map map, String[] strings, Class aClass) { + + Set<Boolean> result = new HashSet<>(); + + if (strings.length > 0) { + + for (String string : strings) { + Boolean aBoolean; + + if (StringUtils.isEmpty(string)) { + aBoolean = null; + } else { + aBoolean = Boolean.valueOf(string); + } + result.add(aBoolean); + } + } + return result; + } + + @Override + public String convertToString(Map map, Object o) { + + String str; + + if (o == null) { + + str = StringUtils.EMPTY; + + } else { + + if (o instanceof Set) { + + Set<Boolean> booleans = (Set<Boolean>) o; + Set<String> strings = new HashSet<>(); + for (Boolean aBoolean : booleans) { + String string; + if (aBoolean == null) { + string = ""; + } else { + string = aBoolean.toString(); + } + strings.add(string); + } + + str = Joiner.on(',').join(strings); + } else { + throw new UnsupportedOperationException("cannot convert to set of Boolean " + o); + } + } + + return str; + + } +} \ No newline at end of file Property changes on: trunk/wao-web/src/main/java/fr/ifremer/wao/web/converter/BooleanSetConverter.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Copied: trunk/wao-web/src/main/java/fr/ifremer/wao/web/converter/MonthConverter.java (from rev 1839, trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/MonthConverter.java) =================================================================== --- trunk/wao-web/src/main/java/fr/ifremer/wao/web/converter/MonthConverter.java (rev 0) +++ trunk/wao-web/src/main/java/fr/ifremer/wao/web/converter/MonthConverter.java 2014-04-06 09:55:25 UTC (rev 1843) @@ -0,0 +1,135 @@ +package fr.ifremer.wao.web.converter; + +/* + * #%L + * Wao :: Web + * %% + * Copyright (C) 2009 - 2014 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import com.google.common.base.Preconditions; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.conversion.TypeConversionException; +import fr.ifremer.wao.WaoUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.struts2.util.StrutsTypeConverter; + +import java.text.ParseException; +import java.util.Arrays; +import java.util.Date; +import java.util.Locale; +import java.util.Map; + + +/** + * To convert a date as a month. + * + * FIXME-tchemit 2014-04-06 When user change his locale, we could have in parameters some date formatted with the previous + * FIXME-tchemit 2014-04-06 locale, we need then to try with previous locale date pattern. + * + * @since 4.0 + */ +public class MonthConverter extends StrutsTypeConverter { + + /** Logger. */ + private static final Log log = LogFactory.getLog(MonthConverter.class); + + @Override + public Date convertFromString(Map map, String[] strings, Class aClass) { + + if (strings.length == 1) { + + String string = strings[0]; + + Date parsedValue; + + if (StringUtils.isEmpty(string)) { + + parsedValue = null; + + } else { + + Locale locale = (Locale) map.get(ActionContext.LOCALE); + Preconditions.checkNotNull(locale, "No locale found in ActionContext"); + + parsedValue = parseDate(locale, string, strings, true); + + if (parsedValue == null) { + + //FIXME If action is changing language, then date are not in good format + //FIXME Let's try why other locale... + if (Locale.FRENCH.equals(locale)) { + locale = Locale.ENGLISH; + } else { + locale = Locale.FRENCH; + } + parsedValue = parseDate(locale, string, strings, false); + } + } + + return parsedValue; + + } else { + throw new TypeConversionException("strings=" + Arrays.toString(strings)); + } + } + + protected Date parseDate(Locale locale, String string, String[] strings, boolean swallonError) { + Preconditions.checkNotNull(locale); + try { + return WaoUtils.parseMonth(locale, string); + } catch (ParseException e) { + if (swallonError) { + return null; + } + //FIXME See why we don't see the error + if (log.isErrorEnabled()) { + log.error("Could not format parse date " + string, e); + } + throw new TypeConversionException("strings=" + Arrays.toString(strings)); + } + } + + @Override + public String convertToString(Map map, Object o) { + + String str; + + if (o == null) { + + str = StringUtils.EMPTY; + + } else { + + if (o instanceof Date) { + + Date date = (Date) o; + + Locale locale = (Locale) map.get(ActionContext.LOCALE); + Preconditions.checkNotNull(locale, "No locale found in ActionContext"); + str = WaoUtils.formatMonth(locale, date); + } else { + throw new UnsupportedOperationException("cannot convert to month " + o); + } + } + + return str; + + } +} Added: trunk/wao-web/src/main/resources/fr/ifremer/wao/ContactsFilter-conversion.properties =================================================================== --- trunk/wao-web/src/main/resources/fr/ifremer/wao/ContactsFilter-conversion.properties (rev 0) +++ trunk/wao-web/src/main/resources/fr/ifremer/wao/ContactsFilter-conversion.properties 2014-04-06 09:55:25 UTC (rev 1843) @@ -0,0 +1,22 @@ +### +# #%L +# Wao :: Web +# %% +# Copyright (C) 2009 - 2014 Ifremer +# %% +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# #L% +### +companyAcceptations=fr.ifremer.wao.web.converter.BooleanSetConverter +programAcceptations=fr.ifremer.wao.web.converter.BooleanSetConverter \ No newline at end of file Property changes on: trunk/wao-web/src/main/resources/fr/ifremer/wao/ContactsFilter-conversion.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/wao-web/src/main/resources/fr/ifremer/wao/SampleRowsFilter-conversion.properties =================================================================== --- trunk/wao-web/src/main/resources/fr/ifremer/wao/SampleRowsFilter-conversion.properties 2014-04-06 09:06:57 UTC (rev 1842) +++ trunk/wao-web/src/main/resources/fr/ifremer/wao/SampleRowsFilter-conversion.properties 2014-04-06 09:55:25 UTC (rev 1843) @@ -18,5 +18,5 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # #L% ### -periodFrom=fr.ifremer.wao.web.action.MonthConverter -periodTo=fr.ifremer.wao.web.action.MonthConverter +periodFrom=fr.ifremer.wao.web.converter.MonthConverter +periodTo=fr.ifremer.wao.web.converter.MonthConverter Modified: trunk/wao-web/src/main/resources/fr/ifremer/wao/entity/SampleRow-conversion.properties =================================================================== --- trunk/wao-web/src/main/resources/fr/ifremer/wao/entity/SampleRow-conversion.properties 2014-04-06 09:06:57 UTC (rev 1842) +++ trunk/wao-web/src/main/resources/fr/ifremer/wao/entity/SampleRow-conversion.properties 2014-04-06 09:55:25 UTC (rev 1843) @@ -18,5 +18,5 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # #L% ### -periodBegin=fr.ifremer.wao.web.action.MonthConverter -periodEnd=fr.ifremer.wao.web.action.MonthConverter +periodBegin=fr.ifremer.wao.web.converter.MonthConverter +periodEnd=fr.ifremer.wao.web.converter.MonthConverter