Hi Isomorphic,
I'm trying to use viewRequires as you describe it in the docs:
I do have a problem with a template variable I need in velocity to evaluate the value, though. To me it seems that this template variable is never set - the breakpoint for it is never hit for a fetch nor for DataSourceLoader (which might be correct, but then I'd need to know how to add those here). Please see this BuiltInDS-based sample (v11.1p_2018-03-17) and select "Animals":
animals.ds.xml:
MySQLDataSource.java:
T_CONFIG.java - I add all template classes like this as singleton with a timed data reload (no data here):
If you look at the DataSourceLoader-result, you'll see a canView:false for both edited fields in the .ds.xml. I assume that when I know where to inject the template variable here, everything will be good.
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
Blama
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.
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
Blama
Comment