execute(), executeFetch() etc are all documented in the superclass (as they should be - they apply across all DataSource types, not just SQLDataSource).
We'll see if we can reproduce the issue you're claiming.
Announcement
Collapse
No announcement yet.
X
-
Hi Isomorphic,
I changed the code of MySQLDataSource.java:
Code:package com.smartgwt.sample.server.listener; import com.isomorphic.datasource.DSRequest; import com.isomorphic.datasource.DSResponse; import com.isomorphic.sql.SQLDataSource; public class MySQLDataSource extends SQLDataSource { private static final long serialVersionUID = -2103887355803373948L; @Override public DSResponse execute(DSRequest request) throws Exception { request.addToTemplateContext("t_config", T_CONFIG.getInstance()); // breakpoint hit, if set return super.execute(request); } }
Can you have a look and also perhaps document what to do in this case somewhere, my suggestion being in viewRequires? IMHO this attribute does not make sense without own velocity variables, so it should be clear how to inject them.
In this docs, a link to SQLDataSource would also help. Additionally, in SQLDataSource itself there are not many methods documented (like executeFetch(), which is mentioned in the forums very often or any executeX()).
Thank you & Best regards
Blama
Leave a comment:
-
executeFetch() is too late. Use addToTemplateContext() in an override of execute().
Leave a comment:
-
Hi Isomorphic,
OK, one step closer now, but I still have a problem with executeFetch(), which does not seem to use the Velocity expression and silently defaults to false. Attached the full changes to BuiltInDS (v11.1p_2018-03-17):
animals.ds.xml:
Code:<DataSource ID="animals" serverType="sql" tableName="animals" testFileName="animals.data.xml" [B]serverConstructor="com.smartgwt.sample.server.listener.MySQLDataSource"[/B]> <fields> <field name="commonName" title="Animal" type="text"/> <field name="scientificName" title="Scientific Name" type="text" primaryKey="true" required="true"/> <field name="lifeSpan" title="Life Span" type="integer" viewRequires="true" /> <field name="status" title="Endangered Status" type="text"> <valueMap> <value>Threatened</value> <value>Endangered</value> <value>Not Endangered</value> <value>Not currently listed</value> <value>May become threatened</value> <value>Protected</value> </valueMap> </field> [B] <!-- visible --> <field name="diet" title="Diet" type="text" viewRequires="$servletRequest.getAttribute("t_config").getVisible("diet")" /> <!-- not visible --> <field name="information" title="Interesting Facts" type="text" viewRequires="$servletRequest.getAttribute("t_config").getVisible("information")" length="1000" />[/B] <field name="picture" title="Picture" type="image" detail="true" imageURLPrefix="/isomorphic/system/reference/inlineExamples/tiles/images/"/> </fields> </DataSource>
Code:package com.smartgwt.sample.server.listener; public class T_CONFIG { private static T_CONFIG instance = new T_CONFIG(); public static T_CONFIG getInstance() { return instance; } public boolean getVisible(String fieldName) { [B]return "diet".equals(fieldName);[/B] } }
Code:package com.smartgwt.sample.server.listener; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.isomorphic.servlet.DataSourceLoader; public class MyDataSourceLoader extends DataSourceLoader { private static final long serialVersionUID = 616663945156810788L; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("t_config", T_CONFIG.getInstance()); // breakpoint hit, if set super.doGet(request, response); } }
Code:package com.smartgwt.sample.server.listener; import com.isomorphic.datasource.DSRequest; import com.isomorphic.datasource.DSResponse; import com.isomorphic.sql.SQLDataSource; public class MySQLDataSource extends SQLDataSource { private static final long serialVersionUID = -2103887355803373948L; @Override public DSResponse executeFetch(DSRequest request) throws Exception { request.addToTemplateContext("t_config", T_CONFIG.getInstance()); // breakpoint hit, if set return super.executeFetch(request); } }
Code:<servlet> <servlet-name>DataSourceLoader</servlet-name> <!-- <servlet-class>com.isomorphic.servlet.DataSourceLoader</servlet-class> --> <servlet-class>com.smartgwt.sample.server.listener.MyDataSourceLoader</servlet-class> </servlet>
Code:{name:"diet",columnCode:"8cc164d47468f433251653c8ab7971f1",title:"Diet",type:"text",validators:[]}, {[B]canView:false[/B],[B]name:"information"[/B],columnCode:"bb3ccd5881d651448ded1dac904054ac"}
Code:SELECT LIMIT 0 75 animals.commonName, animals.scientificName, animals.lifeSpan, animals.status, animals.picture FROM animals WHERE ('1'='1')
As a breakpoint in T_CONFIG.getVisible() is not hit for a fetch (only on startup for DataSourceLoader twice, as expected), I assume there is some issue here.
A breakpoint in MySQLDataSource.executeFetch() is hit, so the setup is correct in general.
Best regards
Blama
Leave a comment:
-
Yes, when contacting the DataSourceLoader, there is no dsRequest. However, other standard Velocity variables are available, including servletRequest, so you can make your object available via setAttribute().
Leave a comment:
-
Hi Isomorphic,
sorry, still unclear. What to add here where? The addToTemplateContext() I use in my SQLDataSource-subclass' overridden executeFetch() is defined on DSRequest. I do not have a DSRequest in DataSourceLoader.
Code:public class MyDataSourceLoader extends DataSourceLoader { private static final long serialVersionUID = 3994104605640773331L; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.doGet(request, response); } }
Blama
Leave a comment:
-
It's a servlet, so any place in the normal servlet lifecycle is fine, eg, doGet(), doPost().
Leave a comment:
-
Your placement is correct for a fetch, but your need a subclass of DataSourceLoader that injects the variable there as well.
Leave a comment:
-
6.1p: viewRequires + velocity problem with empty template variable and therefore broken viewRequires
Hi Isomorphic,
I'm trying to use viewRequires as you describe it in the docs:
Indicates that the specified VelocityExpression must evaluate to true if values for the field are to be fetched. If the specified expression does not evaluate to true, the field will be dropped as described for viewRequiresAuthentication.
In addition to the normal context variables available to Velocity expressions in Smart GWT, expressions you write for field-level requires clauses - viewRequires, editRequires, initRequires and updateRequires - can reference two additional variables: $fieldName and $dsName. These are the names of the dataSource and field currently undergoing requires checks. They are helpful because they allow you to write a generic checker function that can be used to handle requires checks for multiple fields and dataSources.
animals.ds.xml:
Code:<DataSource ID="animals" serverType="sql" tableName="animals" testFileName="animals.data.xml" serverConstructor="com.smartgwt.sample.server.listener.MySQLDataSource"> <fields> <field name="commonName" title="Animal" type="text"/> <field name="scientificName" title="Scientific Name" type="text" primaryKey="true" required="true"/> <field name="lifeSpan" title="Life Span" type="integer"/> <field name="status" title="Endangered Status" type="text"> <valueMap> <value>Threatened</value> <value>Endangered</value> <value>Not Endangered</value> <value>Not currently listed</value> <value>May become threatened</value> <value>Protected</value> </valueMap> </field> [B] <field name="diet" title="Diet" type="text" viewRequires="$t_config.getVisible("diet", $servletRequest)" /> <field name="information" title="Interesting Facts" type="text" length="1000" viewRequires="$t_config.getVisible("diet")" /> [/B] <!-- <field name="diet" title="Diet" type="text" /> <field name="information" title="Interesting Facts" type="text" length="1000" /> --> <field name="picture" title="Picture" type="image" detail="true" imageURLPrefix="/isomorphic/system/reference/inlineExamples/tiles/images/"/> </fields> </DataSource>
Code:package com.smartgwt.sample.server.listener; import com.isomorphic.datasource.DSRequest; import com.isomorphic.datasource.DSResponse; import com.isomorphic.sql.SQLDataSource; public class MySQLDataSource extends SQLDataSource { private static final long serialVersionUID = 1L; [B] public static DSRequest addDefaultTemplateContext(DSRequest request) throws Exception { request.addToTemplateContext("t_config", T_CONFIG.getInstance()); return request; }[/B] @Override public DSResponse executeFetch(DSRequest request) throws Exception { // Not hit during DataSourceLoader - most likely OK [B] addDefaultTemplateContext(request);[/B] DSResponse fetchResponse = super.executeFetch(request); return fetchResponse; } }
Code:package com.smartgwt.sample.server.listener; import javax.servlet.http.HttpServletRequest; public class T_CONFIG { private static T_CONFIG instance = new T_CONFIG(); public static T_CONFIG getInstance() { return instance; } public boolean getVisible(String fieldName, HttpServletRequest servletRequest) { return true; } public boolean getVisible(String fieldName) { return true; } public String getVisible2(String fieldName, HttpServletRequest servletRequest) { return "true"; } public String getVisible2(String fieldName) { return "true"; } }
It is OK that the function then will rely on user grants and on database data to return either true or false, correct?
So differing results for different users are OK, like it is the case with viewRequires, correct? (This basically means, viewRequires is an boilerplate-free version of canView?)
This is an important one for me, as a customer of mine wants to see extra fields, but I want to show those fields only there and not for other customers.
Best regards
BlamaTags: None
Leave a comment: