[Logo] Enterprise Client Community
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Regex and AdapterBinding  XML
Forum Index -> Development
Author Message
cvieira

Power User

Joined: 08/07/2010 21:46:40
Messages: 59
Offline

Hi, i'm using adapter binding on my forms, and i'm trying to use regex defined on fields to be also validated on the WorkpageDispatchedBean, llike it happens with mandatory fields, etc...

My base class is based on the adapter binding form example on the demo application. All the code is working Ok, except for the regex thing... i define regex expression on the necessary fields, and then i try to get that value to make the validation... but the value is always null... it's missing something on the code, but can't figure it out... can anyone help? Here's the code:

Code:
 public class DefaultFormAdapterBinding extends WorkpageDispatchedBean implements Serializable {
 
     static final Logger logger = Logger.getLogger(DefaultFormAdapterBinding.class.getName());
     // ------------------------------------------------------------------------
     // inner classes
     // ------------------------------------------------------------------------
     static Set<String> FIELDFIXATTRIBUTES = new HashSet<String>();
     static Set<String> FIELDDYNATTRIBUTES = new HashSet<String>();
 
     static {
         FIELDDYNATTRIBUTES.add("text");
         FIELDDYNATTRIBUTES.add("bgpaint");
         FIELDDYNATTRIBUTES.add("enabled");
         FIELDDYNATTRIBUTES.add("requestfocus");
         FIELDDYNATTRIBUTES.add("regex");
     }
 
     /**
      * Adapter binding that is behind one form field.
      */
     public class FIELDAdapterBinding implements IComponentAdapterBinding {
 
         public String m_text;
         public String m_propertyName;
         public boolean m_enabled = true;
         long m_requestfocus = -1;
         public boolean m_hasError = false;
         public boolean m_isMandatory = false;
         public String m_regex;
 
         public FIELDAdapterBinding(boolean isMandatory, String propertyName) {
             m_isMandatory = isMandatory;
             m_propertyName = propertyName;
         }
 
         @Override
         public Class getAttibuteType(String attributeName) {
             return String.class;
         }
 
         @Override
         public Object getAttributeValue(String attributeName) {
             if ("text".equals(attributeName)) {
                 return m_text;
             }
             if ("bgpaint".equals(attributeName)) {
                 return getBgpaint();
             }
             if ("enabled".equals(attributeName)) {
                 return m_enabled;
             }
             if ("requestfocus".equals(attributeName)) {
                 return m_requestfocus;
             }
             if ("regex".equals(attributeName)) {
                 return m_regex;
             }
             throw new Error("Should never happen!");
         }
 
         @Override
         public Set<String> getDynamicAttributeNames() {
             return FIELDDYNATTRIBUTES;
         }
 
         @Override
         public Set<String> getFixAttributeNames() {
             return FIELDFIXATTRIBUTES;
         }
 
         @Override
         public void onAction(ActionEvent event) {
             if (event instanceof BaseActionEventValueHelp) {
                 openValidValueHelp(this);
             }
         }
 
         @Override
         public void setAttributeValue(String attributeName, Object value) {
             if ("text".equals(attributeName)) {
                 m_text = (String) value;
             }
         }
 
         private String getBgpaint() {
             if (m_hasError == true) {
                 return "error()";
             }
             if (m_isMandatory == true) {
                 return "mandatory()";
             }
             return null;
         }
     }
     // ------------------------------------------------------------------------
     // members
     // ------------------------------------------------------------------------
     Map<String, FIELDAdapterBinding> m_fields = new HashMap<String, FIELDAdapterBinding>();
 
     // ------------------------------------------------------------------------
     // constructors
     // ------------------------------------------------------------------------
     public DefaultFormAdapterBinding(IWorkpageDispatcher dispatcher) {
         super(dispatcher);
     }
 
     // ------------------------------------------------------------------------
     // public usage
     // ------------------------------------------------------------------------
     public Map<String, FIELDAdapterBinding> getFields() {
         return m_fields;
     }
 
     // ------------------------------------------------------------------------
     // private usage
     // ------------------------------------------------------------------------
     public void registerField(String propertyName, boolean isMandatory) {
         m_fields.put(propertyName, new FIELDAdapterBinding(isMandatory, propertyName));
     }
 
     public boolean isGenericFieldValidationError(Dispatcher dispatcher) {
         boolean foundError = false;
         
         // reset error information
         for (FIELDAdapterBinding field : getFields().values()) {
             field.m_hasError = false;
         }
                
         // check for mandatory and regex
         for (FIELDAdapterBinding field : getFields().values()) {
             if (field.m_isMandatory == true && (field.m_text == null || field.m_text.length() == 0) ) {
                 foundError = true;
                 dispatcher.getStatusbar().writeAlert("Mandatory input field " + field.m_propertyName + " is missing");
             }
             
             logger.log(Level.INFO, "Field {0} regex pattern is {1}", new Object[]{field.m_propertyName, field.m_regex});
             
             if (field.m_regex != null && field.m_regex.length() > 0){
                 logger.log(Level.INFO, "Field {0} regex pattern is {1}", new Object[]{field.m_propertyName, field.m_regex});
                 Pattern p = Pattern.compile(field.m_regex, Pattern.CASE_INSENSITIVE);
                 Matcher m = p.matcher(field.m_text);
                 if (!m.matches()){
                     foundError = true;
                     dispatcher.getStatusbar().writeAlert("Field " + field.m_propertyName + " has not a valid value");
                 }
             }
         }
         
         return foundError;
     }
 
     public void openValidValueHelp(final FIELDAdapterBinding field) {
         throw new UnsupportedOperationException("Not supported yet.");
     }
 }
 

--
Carlos Vieira
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5521
Offline

Hi,

yes, this all looks OK, after taking a quick look.

Could you please double check in the folloing way:

Code:
 public Object getAttributeValue(String attributeName) {
              if ("text".equals(attributeName)) {
                  return m_regex; // instead of m_text
              }
 


Does your expected regex show up as text? If yes, please tell us... then we have to take a longer-than-quick look...

Regards, Björn

Björn Müller, CaptainCasa GmbH
cvieira

Power User

Joined: 08/07/2010 21:46:40
Messages: 59
Offline

Hi Bjorn, thanks for your quick reply!

Well, i've made the test, and it seems that the value is always null... i can't get the regex string pattern that is defined on the fields

Thanks!

--
Carlos Vieira
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5521
Offline

Hi,

so, the very strong assumption is that the problem is "on your side" - because otherwise you would have seen the regex values as text values.

It this, what you said?

Regards, Björn

(Sorry, I was not 100% sure how to undestand your response, so I better check...)

Björn Müller, CaptainCasa GmbH
cvieira

Power User

Joined: 08/07/2010 21:46:40
Messages: 59
Offline

Hi Bjor, well i'm not sure

I can't figure it out why regex value is null, when i have regex defined on the rich editor (jsp). It maybe my fault somehwere in the code, but i can't see any error... and you look at it also and it look like it was ok right?

Any further ideas?

Thanks,

--
Carlos Vieira
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5521
Offline

Hi,

ah, you defined REGEX in the Editor!
Please remove it from FIELDDYNATTRIBUTES then it should work.

Regards, Björn

Björn Müller, CaptainCasa GmbH
cvieira

Power User

Joined: 08/07/2010 21:46:40
Messages: 59
Offline

Hi Bjorn,

In fact that does not solve the issue... even removing it from FIELDDYNATTRIBUTES did not work. I've tried with other attributes, such as regexhint and others with no success also...

Any chance to be a CC bug?

Regards,

--
Carlos Vieira
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5521
Offline

Yes, OK, we will check! Thanks for your info so far!
Björn

Björn Müller, CaptainCasa GmbH
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5521
Offline

Hi Carlos,

everything works 100% fine...
In the example below I both did define the expression in the layout (firstName) and did define the expression in the Binding (lastName).

If you buy a flight to Portugal for me, I come over immediately. ;-)

Regards, Björn

Code:
 LAYOUT:
 
 <t:rowbodypane id="g_3">
 	<t:row id="g_4">
 		<t:field id="g_5" adapterbinding="#{wp.Test47.fields.firstName}"
 			regex="[01]{5}" width="100" />
 	</t:row>
 	<t:row id="g_6">
 		<t:field id="g_7" adapterbinding="#{wp.Test47.fields.lastName}"
 			width="100" />
 	</t:row>
 </t:rowbodypane>
 
 
 CODE:
 
 package test;
 
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
 import javax.faces.event.ActionEvent;
 
 import org.eclnt.jsfserver.elements.adapter.IComponentAdapterBinding;
 
 public class Test47
 {
     public class FIELDAdapterBinding implements IComponentAdapterBinding 
     {
         public String m_text;
         public String m_propertyName;
         public boolean m_enabled = true;
         long m_requestfocus = -1;
         public boolean m_hasError = false;
         public boolean m_isMandatory = false;
         public String m_regex;
         public FIELDAdapterBinding(String text, boolean isMandatory) 
         {
             m_text = text;
             m_isMandatory = isMandatory;
         }
         public Class getAttibuteType(String attributeName) { return String.class; }
         public Object getAttributeValue(String attributeName) 
         {
             if ("text".equals(attributeName)) { return m_text; }
             if ("bgpaint".equals(attributeName)) { return getBgpaint(); }
             if ("enabled".equals(attributeName)) { return m_enabled; }
             if ("requestfocus".equals(attributeName)) { return m_requestfocus; }
             if ("regex".equals(attributeName)) { return m_regex; }
             throw new Error("Should never happen!");
         }
         public Set<String> getDynamicAttributeNames() { return FIELDDYNATTRIBUTES; }
         public Set<String> getFixAttributeNames() { return FIELDFIXATTRIBUTES; }
         public void onAction(ActionEvent event) { }
         public void setAttributeValue(String attributeName, Object value) 
         {
             if ("text".equals(attributeName)) { m_text = (String) value; }
         }
         private String getBgpaint() {
             if (m_hasError == true) { return "error()"; }
             if (m_isMandatory == true) { return "mandatory()"; }
             return null;
         }
     }
     
     static Set<String> FIELDFIXATTRIBUTES = new HashSet<String>();
     static Set<String> FIELDDYNATTRIBUTES = new HashSet<String>();
     Map<String, FIELDAdapterBinding> m_fields = new HashMap<String, FIELDAdapterBinding>();
     
     static 
     {
         FIELDDYNATTRIBUTES.add("text");
         FIELDDYNATTRIBUTES.add("bgpaint");
         FIELDDYNATTRIBUTES.add("enabled");
         FIELDDYNATTRIBUTES.add("requestfocus");
         FIELDDYNATTRIBUTES.add("regex");
     }
 
     public Test47()
     {
         FIELDAdapterBinding firstName = new FIELDAdapterBinding("Captain",true);
         FIELDAdapterBinding lastName = new FIELDAdapterBinding("Casa",true); 
         lastName.m_regex = "[01]{5}";
         m_fields.put("firstName",firstName);
         m_fields.put("lastName",lastName);
     }
 
     public Map<String, FIELDAdapterBinding> getFields() { return m_fields; }
 }
 

Björn Müller, CaptainCasa GmbH
cvieira

Power User

Joined: 08/07/2010 21:46:40
Messages: 59
Offline

Hi Bjorn, thanks! Well, you're very welcome to visit Portugal

Taking your example:

Code:
  <t:rowbodypane id="g_3">
  	<t:row id="g_4">
  		<t:field id="g_5" adapterbinding="#{wp.Test47.fields.firstName}"
  			regex="[01]{5}" width="100" />
  	</t:row>
  	<t:row id="g_6">
  		<t:field id="g_7" adapterbinding="#{wp.Test47.fields.lastName}"
  			width="100" />
  	</t:row>
  </t:rowbodypane>
 


What i want is to get in code, the firstName regex pattern that was defined in layout. And i can't get that value... maybe there is some misunderstanding here...

My intention is to get regex pattern (from layout... in your example is "[01]{5}" for first name) so i can validate it on server side and do some action (show errors, etc...), or avoid submit action, etc...

Thanks,

--
Carlos Vieira
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5521
Offline

Hi,

yes, this is the misunderstanding.
The ADAPTERBINDING can NOT be used for transferring JSP parameters into the code.

Regards, Björn

Björn Müller, CaptainCasa GmbH
cvieira

Power User

Joined: 08/07/2010 21:46:40
Messages: 59
Offline

Hi Bjorn,

I thought that it was possible to do it in someway... but Ok, i will change my ADAPTERBINDING class to accomodate my requirement, without "looking" into JSP parameters.

Many thanks for your time!

Regards,

--
Carlos Vieira
 
Forum Index -> Development
Go to:   
Powered by JForum 2.1.6 © JForum Team