Is it possible to set Criteria on an Enum field in list grid ?
Announcement
Collapse
No announcement yet.
X
-
Sorry Isomorphic but it does not seem to work.
Code:... <field name="status" type="enum" required="true"> <valueMap> <value ID="ACTIVE" /> <value ID="INACTIVE" /> </valueMap> </field> ...
Code:... Criteria criteria = new Criteria(); criteria.addCriteria("status", Status.Active); //no such method in SmartGWT criteria.addCriteria("status", "ACTIVE"); produces exception: Parameter value [ACTIVE] was not matching type com.mycompany.Status] criteria.addCriteria("status", 1); produces exception: Parameter value [1] was not matching type com.mycompany.Status] criteria.addCriteria("status", "1"); produces exception: Parameter value [1] was not matching type com.mycompany.Status] listGrid.addCriteria(criteria) ...
Thanks!
Comment
-
Im not using any nightly builds. Im using smartgwt 2.3 downloaded from your site.
There is no stack trace, it only pops up a window with the error that i wrote for every case i've tried and shows empty listgrid.
It should be quite obvious from the code i've sent if i should do something some other way. So my only question is how should i filter a listGrid on an enum field ?
Comment
-
Here is a stack trace of what happens in the server when sending 0 as a criteria for an enum field in a JPA datasource:
java.lang.ClassCastException: Value '0' of type 'class java.lang.Integer' can not be casted to type 'class se.vipforce.field.StatusEnum'.
at com.isomorphic.jpa.JPADataSource.castValue(JPADataSource.java:1264)
at com.isomorphic.jpa.JPADataSource.executeFetch(JPADataSource.java:380)
at com.isomorphic.datasource.DataSource.execute(DataSource.java:1048)
at com.isomorphic.jpa.JPADataSource.execute(JPADataSource.java:218)
at com.isomorphic.application.AppBase.executeDefaultDSOperation(AppBase.java:721)
at com.isomorphic.application.AppBase.executeAppOperation(AppBase.java:658)
at com.isomorphic.application.AppBase.execute(AppBase.java:491)
at com.isomorphic.datasource.DSRequest.execute(DSRequest.java:1443)
at se.vipforce.server.RequestHandler.fetch(RequestHandler.java:275)
Comment
-
I'm having identical issues with the original poster.
Using Smart GWT 3.0 - nightly build, Firefox 5.whatever
Trying to set enum criteria on a hibernate-based DataSource. Since the addCriteria method will not take an "object" as a value, my only options are to pass the enum's ordinal or the enum's string.
Here is my datasource definition:
Code:<DataSource ID="snmpIP" serverType="hibernate" beanClassName="com.bpt.db.model.Server" dropExtraFields="true"> <fields> <field name="id" type="sequence" hidden="true" primaryKey="true" /> <field name="host" type="text" valueXPath="device/hostName" /> <field name="hostId" type="integer" hidden="true" valueXPath="device/id" /> <field name="ipAddress" type="text" /> <field name="type" type="enum" hidden="true"> <valueMap> <value ID="0">NTP</value> <value ID="1">TACACS</value> <value ID="2">SNMP</value> <value ID="3">LOGGING</value> <value ID="4">UNKNOWN</value> </valueMap> </field> </fields> </DataSource>
Code:public ServerTypeEnum getType() { return type; } public void setType(ServerTypeEnum type) { this.type = type; }
Code:public enum ServerTypeEnum { NTP, TACACS, SNMP, LOGGING, UNKNOWN; }
Code:Criteria c = new Criteria(); c.addCriteria("type", ServerTypeEnum.SNMP.toString()); datasource.fetch(c);
=== 2011-08-22 22:25:46,598 [l0-6] INFO HibernateDataSource - [builtinApplication.snmpIP_fetch] Performing fetch operation with
criteria: {type:"SNMP",hostId:[4]} values: {type:"SNMP",hostId:[4]}
=== 2011-08-22 22:25:46,598 [l0-6] DEBUG HibernateTransaction - [builtinApplication.snmpIP_fetch] Started new transaction "1974599536"
=== 2011-08-22 22:25:46,598 [l0-6] WARN HibernateDataSource - [builtinApplication.snmpIP_fetch] Failed to cast value for field 'type'.
Value 'SNMP' of type 'class java.lang.String' can not be casted to type 'class com.bpt.db.model.ServerTypeEnum'.
Skipping.
Here is my "ordinal" criteria version:
Code:Criteria c = new Criteria(); c.addCriteria("type", ServerTypeEnum.SNMP.ordinal()); datasource.fetch(c);
=== 2011-08-22 22:12:01,989 [l0-2] INFO HibernateDataSource - [builtinApplication.snmpIP_fetch] Performing fetch operation with
criteria: {type:2,hostId:[3]} values: {type:2,hostId:[3]}
=== 2011-08-22 22:12:01,990 [l0-2] DEBUG HibernateTransaction - [builtinApplication.snmpIP_fetch] Started new transaction "1389734155"
=== 2011-08-22 22:12:01,990 [l0-2] WARN HibernateDataSource - [builtinApplication.snmpIP_fetch] Failed to cast value for field 'type'.
Value '2' of type 'class java.lang.Long' can not be casted to type 'class com.bpt.db.model.ServerTypeEnum'.
Skipping.
Ideally, I would like to just pass a string or ordinal and have this work, but I'm guessing Smart GWT won't support this.
3 "workarounds" that I can attempt (though some advice as to preferred would be helpful):
1) Skeptical (meaning I don't think this is a valid workaround or solution) - look into hibernate and see if there are additional annotations I can put on my getType() method to get hibernate to treat "longs" as valid values for get or set (not sure if hibernate can do this)
2) Create a getter on my hibernate bean:
Code:public long getTypeOrdinal()
3) Add a DMI class for this particular object that takes the criteria ordinal or string and converts it to a ServerTypeEnum, adds the converted criteria value to the Criteria map, and removes the original criteria string so the HibernateDataSource doesn't get confused (or just let it ignore it/drop it like it does now).
Thoughts? Opinons? I can't be the only one who's run into this issue but this thread looks unresolved and I can't find anything else on the forums. If it's there, please let me know.
Thanks in advance.
Comment
-
I agree. It would be nice if Smart GWT could handle this automatically...either by passing in an ordinal value that matches the IDs of enum types in the datasource definition file or by passing in a string that matches the enum types and their mappings to numbers used by my backend Hibernate bean.
Keep in mind that Hibernate by default stores the ordinal value of an enum (makes sense for queries and storage purposes).
I successfully implemented a DMI "workaround" whereby I handle the fetch request, convert the string into the correct enum type and replace the string criteria with the object and then execute the default HibernateDataSource fetch request.
It works and wasn't that much of a hassle to implement. I've used the same "DMI Converter" on several of my datasources so it wasn't bad but right now it's the only reason I need to use DMI.
If you could keep me updated on this issue, I would greatly appreciate it. Thanks!
Comment
Comment