Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    DynamicDSGenerator.getDataSource() - dsRequest is null

    Hi Isomorphic,

    second parameter of DynamicDSGenerator.getDataSource() is "DSRequest dsRequest" (Docs: ...This will be null if the DataSource is not needed in the context of a DSRequest).

    Please take a look at this test case (using v12.0p_2019-04-03/PowerEdition Deployment (built 2019-04-03)).

    web.xml
    Code:
        <!-- Rewrite default definiton with this one -->
        <servlet>
            <servlet-name>RESTHandler</servlet-name>
            <servlet-class>com.smartgwt.sample.server.listener.LMSRESTHandler</servlet-class>
           <init-param>
               <param-name>defaultDataFormat</param-name>
               <param-value>json</param-value>
           </init-param>
       </servlet>
    
        <servlet>
            <servlet-name>RegisterDS</servlet-name>
            <servlet-class>com.smartgwt.sample.server.listener.RegisterDS</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    LMSRESTHandler.java
    Code:
    package com.smartgwt.sample.server.listener;
    
    import com.isomorphic.datasource.DSRequest;
    import com.isomorphic.datasource.DSResponse;
    import com.isomorphic.datasource.DataSource;
    import com.isomorphic.rpc.RPCManager;
    import com.isomorphic.servlet.RESTHandler;
    import com.isomorphic.servlet.RequestContext;
    
    public class LMSRESTHandler extends RESTHandler {
        private static final long serialVersionUID = -1L;
    
        @Override
        public void processRestTransaction(RPCManager rpcManager, RequestContext requestContext) throws Exception {
            super.processRestTransaction(rpcManager, requestContext);
        }
    
        @Override
        public DSResponse handleDSRequest(DSRequest dsRequest, RPCManager rpcManager, RequestContext requestContext) throws Exception {
            DSRequest request = new DSRequest("animals", DataSource.OP_FETCH, rpcManager);
            DSResponse returnResponse = request.execute();
            return returnResponse;
        }
    }
    RegisterDS.java
    Code:
    package com.smartgwt.sample.server.listener;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    
    import com.isomorphic.datasource.DSRequest;
    import com.isomorphic.datasource.DataSource;
    import com.isomorphic.datasource.DynamicDSGenerator;
    
    public class RegisterDS extends HttpServlet {
        private static final long serialVersionUID = 7849924835392267735L;
    
        @Override
        public void init() throws ServletException {
            final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    
            DynamicDSGenerator animalsGenerator = new DynamicDSGenerator() {
                @Override
                public DataSource getDataSource(String id, DSRequest dsRequest) {
                    DataSource ds = null;
                    try {
                        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                        // Document doc = documentBuilder
                        // .parse(getServletContext().getResourceAsStream("war\\ds\\animals.ds.xml"));
                        Document doc = documentBuilder
                                .parse("C:\\Users\\Pavo\\eclipse-workspace\\lib\\smartgwtpower-12.0p\\samples\\built-in-ds\\war\\ds\\animals.ds.xml");
                        //Set a breakpoint on the next line of the code
                        ds = DataSource.fromXML(doc);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    return ds;
                }
            };
            DataSource.addDynamicDSGenerator(animalsGenerator, "animals");
        }
    }
    animals.ds.xml
    Code:
    <DataSource
        ID="animals"
        serverType="sql"
        tableName="animals"
        testFileName="animals.data.xml"
    >
        <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>
            <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>
    <serverObject lookupStyle="new" className="com.smartgwt.sample.server.listener.Animals" />
    </DataSource>
    Animals.java
    Code:
    package com.smartgwt.sample.server.listener;
    
    import javax.servlet.http.HttpServletRequest;
    
    import com.isomorphic.datasource.DSRequest;
    import com.isomorphic.datasource.DSResponse;
    import com.isomorphic.datasource.DataSource;
    import com.isomorphic.tools.DataImport;
    
    public class Animals {
        public DSResponse fetch(DSRequest dsRequest, HttpServletRequest servletRequest) throws Exception {
            DataImport dataImporter = new DataImport();
            dataImporter.setPopulateDisplayFields(true);
    
            DSRequest addRequest = new DSRequest("animals", DataSource.OP_ADD, dsRequest.getRPCManager());
            //Set a breakpoint on the next line of the code
            addRequest.setValues(dataImporter.importDataSourceRecord(dsRequest.getValues(), dsRequest.getDataSourceName()));
            DSResponse addResponse = addRequest.execute();
    
            return addResponse;
        }
    }
    Call:
    Code:
    http://127.0.0.1:8888/builtinds/sc/RESTHandler/employees
    If you set a breakpoint in both places where I wrote in the comments, after execution of
    Code:
    addRequest.setValues (dataImporter.importDataSourceRecord (dsRequest.getValues (), dsRequest.getDataSourceName ()));
    in Animals.java, "animalsGenerator" is called again but "dsRequest" is null. I don't see a reason why "dsRequest" is null at this point so this could be a bug.

    My actual case is a way more complicated but I hope you see the problem.

    Best regards
    Pavo
    Last edited by pavo123; 25 Oct 2019, 06:07.

    #2
    Your call to DataImport performs a DataSource operation, and since it is not passed a DataSource, it needs to retrieve one, and since it is not passed a DSRequest, there's no way it can provide a DSRequest when the DataSource is being generated.

    This isn't a bug per se, but it's true that right now there's no signature on DataImport that would get you a DSRequest in your DynamicDSGenerator. We'll look at adding one.

    Comment


      #3
      Hi Isomorphic,

      thank you very much! That will solve my problem.

      Best regards
      Pavo

      Comment


        #4
        We've just added a new signature of DataImport.importDataSourceRecord() in order to pass it an instance of DataSource, which should solve your issue. The new signature will be available as of tomorrow's builds (October 29th).

        So, your code should now be:
        Code:
        addRequest.setValues(dataImporter.importDataSourceRecord(dsRequest.getValues(), dsRequest.getDataSource()));
        Regards
        Isomorphic Software

        Comment


          #5
          Hi Isomorphic,

          that solved my problem, thank you very much!

          Best regards
          Pavo

          Comment

          Working...
          X