syncope-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ilgro...@apache.org
Subject [06/10] syncope git commit: [SYNCOPE-717] Fixing locale issue with conversion pattern on core, replacing SpinnerFieldPanel with new AjaxDoubleFieldPanel on console
Date Thu, 29 Oct 2015 09:40:07 GMT
[SYNCOPE-717] Fixing locale issue with conversion pattern on core, replacing SpinnerFieldPanel
with new AjaxDoubleFieldPanel on console


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/5954e2e7
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/5954e2e7
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/5954e2e7

Branch: refs/heads/master
Commit: 5954e2e7d44878c0f87d594140f6cc42146ad5c3
Parents: 893d889
Author: Francesco Chicchiriccò <ilgrosso@apache.org>
Authored: Wed Oct 28 14:13:40 2015 +0100
Committer: Francesco Chicchiriccò <ilgrosso@apache.org>
Committed: Wed Oct 28 14:13:40 2015 +0100

----------------------------------------------------------------------
 .../console/pages/panels/AttributesPanel.java   |   7 +-
 .../markup/html/form/AjaxDoubleFieldPanel.java  | 196 +++++++++++++++++++
 .../wicket/markup/html/form/FieldPanel.java     |   2 +-
 .../console/SyncopeApplication.properties       |   1 +
 .../console/SyncopeApplication_it.properties    |   1 +
 .../console/SyncopeApplication_pt_BR.properties |   1 +
 .../markup/html/form/AjaxDoubleFieldPanel.html  |  23 +++
 .../apache/syncope/core/util/DataFormat.java    |   6 +-
 .../syncope/core/rest/RoleTestITCase.java       |  43 ++++
 pom.xml                                         |   1 +
 10 files changed, 275 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/5954e2e7/console/src/main/java/org/apache/syncope/console/pages/panels/AttributesPanel.java
----------------------------------------------------------------------
diff --git a/console/src/main/java/org/apache/syncope/console/pages/panels/AttributesPanel.java
b/console/src/main/java/org/apache/syncope/console/pages/panels/AttributesPanel.java
index da97a45..8e44f85 100644
--- a/console/src/main/java/org/apache/syncope/console/pages/panels/AttributesPanel.java
+++ b/console/src/main/java/org/apache/syncope/console/pages/panels/AttributesPanel.java
@@ -19,7 +19,6 @@
 package org.apache.syncope.console.pages.panels;
 
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
@@ -28,7 +27,6 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
-import java.util.ListIterator;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
@@ -52,6 +50,7 @@ import org.apache.syncope.console.rest.ConfigurationRestClient;
 import org.apache.syncope.console.rest.RoleRestClient;
 import org.apache.syncope.console.rest.SchemaRestClient;
 import org.apache.syncope.console.wicket.markup.html.form.AjaxCheckBoxPanel;
+import org.apache.syncope.console.wicket.markup.html.form.AjaxDoubleFieldPanel;
 import org.apache.syncope.console.wicket.markup.html.form.AjaxDropDownChoicePanel;
 import org.apache.syncope.console.wicket.markup.html.form.AjaxTextFieldPanel;
 import org.apache.syncope.console.wicket.markup.html.form.BinaryFieldPanel;
@@ -318,8 +317,8 @@ public class AttributesPanel extends Panel {
                 break;
 
             case Double:
-                panel = new SpinnerFieldPanel<Double>("panel", schemaTO.getName(),
-                        Double.class, new Model<Double>(), null, null);
+                panel = new AjaxDoubleFieldPanel("panel", schemaTO.getName(), schemaTO.getConversionPattern(),
+                        new Model<Double>());
 
                 if (required) {
                     panel.addRequiredLabel();

http://git-wip-us.apache.org/repos/asf/syncope/blob/5954e2e7/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/AjaxDoubleFieldPanel.java
----------------------------------------------------------------------
diff --git a/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/AjaxDoubleFieldPanel.java
b/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/AjaxDoubleFieldPanel.java
new file mode 100644
index 0000000..a5f8129
--- /dev/null
+++ b/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/AjaxDoubleFieldPanel.java
@@ -0,0 +1,196 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.console.wicket.markup.html.form;
+
+import java.io.Serializable;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.ParseException;
+import java.util.List;
+import java.util.Locale;
+import java.util.regex.Pattern;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.syncope.console.SyncopeSession;
+import org.apache.syncope.console.commons.Constants;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
+import org.apache.wicket.markup.html.form.TextField;
+import org.apache.wicket.markup.html.list.ListItem;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.Model;
+
+public class AjaxDoubleFieldPanel extends FieldPanel<Double> {
+
+    private static final long serialVersionUID = 935151916638207380L;
+
+    private static final Pattern ENGLISH_DOUBLE_PATTERN = Pattern.compile("\\d+\\.\\d+");
+
+    private static final Pattern OTHER_DOUBLE_PATTERN = Pattern.compile("\\d+,\\d+");
+
+    private final String name;
+
+    private final Pattern pattern;
+
+    private final DecimalFormat englishDf;
+
+    private final DecimalFormat localeDf;
+
+    public AjaxDoubleFieldPanel(
+            final String id, final String name, final String conversionPattern, final IModel<Double>
model) {
+
+        super(id, model);
+
+        this.name = name;
+
+        this.pattern = SyncopeSession.get().getLocale().equals(Locale.ENGLISH)
+                ? ENGLISH_DOUBLE_PATTERN
+                : OTHER_DOUBLE_PATTERN;
+
+        englishDf = new DecimalFormat();
+        englishDf.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH));
+        if (StringUtils.isNotBlank(conversionPattern)) {
+            englishDf.applyPattern(conversionPattern);
+        }
+        localeDf = new DecimalFormat();
+        localeDf.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(SyncopeSession.get().getLocale()));
+        if (StringUtils.isNotBlank(conversionPattern)) {
+            localeDf.applyPattern(conversionPattern);
+        }
+
+        field = new TextField<Double>("doubleField", model) {
+
+            private static final long serialVersionUID = -378877047108711669L;
+
+            @Override
+            protected void convertInput() {
+                if (StringUtils.isNotBlank(getInput())) {
+                    if (pattern.matcher(getInput()).matches()) {
+                        Double value;
+                        try {
+                            value = localeDf.parse(getInput()).doubleValue();
+                            setConvertedInput(value);
+                        } catch (ParseException e) {
+                            error(name + ": " + getString("textField.DoubleValidator"));
+                        }
+                    } else {
+                        error(name + ": " + getString("textField.DoubleValidator"));
+                    }
+                }
+            }
+        };
+        add(field.setLabel(new Model<String>(name)).setOutputMarkupId(true));
+
+        if (!isReadOnly()) {
+            field.add(new AjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
+
+                private static final long serialVersionUID = -1107858522700306810L;
+
+                @Override
+                protected void onUpdate(final AjaxRequestTarget target) {
+                    // nothing to do
+                }
+            });
+        }
+    }
+
+    @Override
+    public FieldPanel<Double> addRequiredLabel() {
+        if (!isRequired()) {
+            setRequired(true);
+        }
+
+        this.isRequiredLabelAdded = true;
+
+        return this;
+    }
+
+    @Override
+    public FieldPanel<Double> setNewModel(final List<Serializable> list) {
+        setNewModel(new Model<Double>() {
+
+            private static final long serialVersionUID = 527651414610325237L;
+
+            @Override
+            public Double getObject() {
+                Double value = null;
+
+                if (list != null && !list.isEmpty()) {
+                    try {
+                        value = englishDf.parse(list.get(0).toString()).doubleValue();
+                    } catch (ParseException e) {
+                        error(name + ": " + getString("textField.DoubleValidator"));
+                    }
+                }
+
+                return value;
+            }
+
+            @Override
+            public void setObject(final Double object) {
+                list.clear();
+                if (object != null) {
+                    list.add(englishDf.format(object));
+                }
+            }
+        });
+
+        return this;
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Override
+    public FieldPanel<Double> setNewModel(final ListItem item) {
+        IModel<Double> model = new Model<Double>() {
+
+            private static final long serialVersionUID = 6799404673615637845L;
+
+            @Override
+            public Double getObject() {
+                Double value = null;
+
+                final Object obj = item.getModelObject();
+
+                if (obj != null && !obj.toString().isEmpty()) {
+                    if (obj instanceof String) {
+                        try {
+                            value = englishDf.parse(obj.toString()).doubleValue();
+                        } catch (ParseException e) {
+                            error(name + ": " + getString("textField.DoubleValidator"));
+                        }
+                    } else if (obj instanceof Double) {
+                        // Don't parse anything
+                        value = (Double) obj;
+                    }
+                }
+
+                return value;
+            }
+
+            @Override
+            @SuppressWarnings("unchecked")
+            public void setObject(final Double object) {
+                item.setModelObject(englishDf.format(object));
+            }
+        };
+
+        field.setModel(model);
+        return this;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/5954e2e7/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/FieldPanel.java
----------------------------------------------------------------------
diff --git a/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/FieldPanel.java
b/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/FieldPanel.java
index 967f369..ae02f87 100644
--- a/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/FieldPanel.java
+++ b/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/FieldPanel.java
@@ -126,7 +126,7 @@ public abstract class FieldPanel<T> extends AbstractFieldPanel<T>
implements Clo
     }
 
     public T getModelObject() {
-        return (T) field.getModelObject();
+        return field.getModelObject();
     }
 
     public FieldPanel<T> setNewModel(final IModel<T> model) {

http://git-wip-us.apache.org/repos/asf/syncope/blob/5954e2e7/console/src/main/resources/org/apache/syncope/console/SyncopeApplication.properties
----------------------------------------------------------------------
diff --git a/console/src/main/resources/org/apache/syncope/console/SyncopeApplication.properties
b/console/src/main/resources/org/apache/syncope/console/SyncopeApplication.properties
index 223b27a..c733b09 100644
--- a/console/src/main/resources/org/apache/syncope/console/SyncopeApplication.properties
+++ b/console/src/main/resources/org/apache/syncope/console/SyncopeApplication.properties
@@ -52,3 +52,4 @@ jexl_ex1=surname + ',' + firstname
 jexl_ex2='new.' + surname
 jexl_syntax_url=Full JEXL reference
 create=Create
+textField.DoubleValidator=Double value not valid

http://git-wip-us.apache.org/repos/asf/syncope/blob/5954e2e7/console/src/main/resources/org/apache/syncope/console/SyncopeApplication_it.properties
----------------------------------------------------------------------
diff --git a/console/src/main/resources/org/apache/syncope/console/SyncopeApplication_it.properties
b/console/src/main/resources/org/apache/syncope/console/SyncopeApplication_it.properties
index 3a54be5..9f3d00c 100644
--- a/console/src/main/resources/org/apache/syncope/console/SyncopeApplication_it.properties
+++ b/console/src/main/resources/org/apache/syncope/console/SyncopeApplication_it.properties
@@ -51,3 +51,4 @@ jexl_ex1=surname + ',' + firstname
 jexl_ex2='new.' + surname
 jexl_syntax_url=Sintassi JEXL completa
 create=Crea
+textField.DoubleValidator=Valore double non valido

http://git-wip-us.apache.org/repos/asf/syncope/blob/5954e2e7/console/src/main/resources/org/apache/syncope/console/SyncopeApplication_pt_BR.properties
----------------------------------------------------------------------
diff --git a/console/src/main/resources/org/apache/syncope/console/SyncopeApplication_pt_BR.properties
b/console/src/main/resources/org/apache/syncope/console/SyncopeApplication_pt_BR.properties
index dd9ecd7..653983d 100644
--- a/console/src/main/resources/org/apache/syncope/console/SyncopeApplication_pt_BR.properties
+++ b/console/src/main/resources/org/apache/syncope/console/SyncopeApplication_pt_BR.properties
@@ -51,3 +51,4 @@ jexl_ex1=sobrenome + ',' + nome
 jexl_ex2='novo.' + sobrenome
 jexl_syntax_url=Refer\u00eancia JEXL completa
 create=Criar
+textField.DoubleValidator=Double valor n\u00e3o \u00e9 v\u00e1lido

http://git-wip-us.apache.org/repos/asf/syncope/blob/5954e2e7/console/src/main/resources/org/apache/syncope/console/wicket/markup/html/form/AjaxDoubleFieldPanel.html
----------------------------------------------------------------------
diff --git a/console/src/main/resources/org/apache/syncope/console/wicket/markup/html/form/AjaxDoubleFieldPanel.html
b/console/src/main/resources/org/apache/syncope/console/wicket/markup/html/form/AjaxDoubleFieldPanel.html
new file mode 100644
index 0000000..050ff2d
--- /dev/null
+++ b/console/src/main/resources/org/apache/syncope/console/wicket/markup/html/form/AjaxDoubleFieldPanel.html
@@ -0,0 +1,23 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org">
+  <wicket:extend>
+    <input type="text" class="ui-widget-content ui-corner-all long_dynamicsize" wicket:id="doubleField"/>
+  </wicket:extend>
+</html>

http://git-wip-us.apache.org/repos/asf/syncope/blob/5954e2e7/core/src/main/java/org/apache/syncope/core/util/DataFormat.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/syncope/core/util/DataFormat.java b/core/src/main/java/org/apache/syncope/core/util/DataFormat.java
index 58b719a..4296422 100644
--- a/core/src/main/java/org/apache/syncope/core/util/DataFormat.java
+++ b/core/src/main/java/org/apache/syncope/core/util/DataFormat.java
@@ -19,9 +19,11 @@
 package org.apache.syncope.core.util;
 
 import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Locale;
 import org.apache.commons.lang3.time.DateUtils;
 import org.apache.syncope.common.SyncopeConstants;
 
@@ -44,7 +46,9 @@ public final class DataFormat {
 
         @Override
         protected DecimalFormat initialValue() {
-            return new DecimalFormat();
+            DecimalFormat df = new DecimalFormat();
+            df.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ENGLISH));
+            return df;
         }
     };
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/5954e2e7/core/src/test/java/org/apache/syncope/core/rest/RoleTestITCase.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/syncope/core/rest/RoleTestITCase.java b/core/src/test/java/org/apache/syncope/core/rest/RoleTestITCase.java
index 5f0feb0..ce553cc 100644
--- a/core/src/test/java/org/apache/syncope/core/rest/RoleTestITCase.java
+++ b/core/src/test/java/org/apache/syncope/core/rest/RoleTestITCase.java
@@ -51,6 +51,7 @@ import org.apache.syncope.common.to.RoleTO;
 import org.apache.syncope.common.to.SchemaTO;
 import org.apache.syncope.common.to.UserTO;
 import org.apache.syncope.common.types.AttributableType;
+import org.apache.syncope.common.types.AttributeSchemaType;
 import org.apache.syncope.common.types.ClientExceptionType;
 import org.apache.syncope.common.types.IntMappingType;
 import org.apache.syncope.common.types.MappingPurpose;
@@ -886,4 +887,46 @@ public class RoleTestITCase extends AbstractTest {
             resourceService.delete("new-ldap");
         }
     }
+
+    @Test
+    public void issueSYNCOPE717() {
+        String doubleSchemaName = "double" + getUUIDString();
+
+        // 1. create double schema without conversion pattern
+        SchemaTO schema = new SchemaTO();
+        schema.setName(doubleSchemaName);
+        schema.setType(AttributeSchemaType.Double);
+
+        schema = createSchema(AttributableType.ROLE, SchemaType.NORMAL, schema);
+        assertNotNull(schema);
+        assertNull(schema.getConversionPattern());
+
+        // 2. create role, provide valid input value
+        RoleTO roleTO = buildBasicRoleTO("syncope717");
+        roleTO.getRAttrTemplates().add(doubleSchemaName);
+        roleTO.getAttrs().add(attributeTO(doubleSchemaName, "11.23"));
+
+        roleTO = createRole(roleTO);
+        assertNotNull(roleTO);
+        assertEquals("11.23", roleTO.getAttrMap().get(doubleSchemaName).getValues().get(0));
+
+        // 3. update schema, set conversion pattern
+        schema.setConversionPattern("0.000");
+        schemaService.update(AttributableType.ROLE, SchemaType.NORMAL, schema.getName(),
schema);
+
+        // 4. re-read role, verify that pattern was applied
+        roleTO = roleService.read(roleTO.getId());
+        assertNotNull(roleTO);
+        assertEquals("11.230", roleTO.getAttrMap().get(doubleSchemaName).getValues().get(0));
+
+        // 5. modify role with new double value
+        RoleMod roleMod = new RoleMod();
+        roleMod.setId(roleTO.getId());
+        roleMod.getAttrsToRemove().add(doubleSchemaName);
+        roleMod.getAttrsToUpdate().add(attributeMod(doubleSchemaName, "11.257"));
+
+        roleTO = updateRole(roleMod);
+        assertNotNull(roleTO);
+        assertEquals("11.257", roleTO.getAttrMap().get(doubleSchemaName).getValues().get(0));
+    }
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/5954e2e7/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 3b74d7d..65dd922 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1026,6 +1026,7 @@ under the License.
           <configuration>
             <source>${targetJdk}</source>
             <target>${targetJdk}</target>
+            <useIncrementalCompilation>false</useIncrementalCompilation>
             <showWarnings>true</showWarnings>
             <showDeprecation>true</showDeprecation>
             <!--<compilerArgument>-Xlint:unchecked</compilerArgument>-->


Mime
View raw message