Announcement

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

    Populating ValueMap from Java enum

    Hi,

    I have a datasource (via Spring DMI), that allows me to access JavaBean-objects of a certain class. At that class a field is defined. It's type is an enum.
    Now my question: Is it possible to populate the picklist of the SelectItem that is generated for this field in a dynamic form connected to that datasource with the available constants from the enum definition? Right now I have to define the values at two different places: within the enum and within the datasource-definition (as the valueMap).

    Thank your very much for your help.

    Disclaimer: I am a SmartGWT newbie and I have searched the forum, the documentation, the examples and also Google for long hours...)


    Your's

    John

    #2
    One approach is to use the schemaBean property in your .ds.xml file. This will cause the fields to be automatically derived from a target bean, and if one of those fields is of enum type, a valueMap will be automatically generated based on the values of the enum.

    More information in the QuickStart Guide, SmartGWT Framework Chapter, right at the beginning.

    Comment


      #3
      Thank you for answering so soon.

      I have a schemaBean attribute as well as an autoDeriveSchema, but the DataSource I get is always empty:

      My Definition looks like:
      Code:
      <DataSource ID="Firma" schemaBean="de.test.TestEntity" autoDeriveSchema="true">
      </DataSource>
      The JavaBean is:
      Code:
      package de.test;
      
      import de.test.Gesellschaftsform;
      
      public class TestEntity
      {
      	private String name;
      	private String vorname;
      	private Gesellschaftsform gform;
      
      	public String getName()
      	{
      		return name;
      	}
      
      	public void setName(String name)
      	{
      		this.name = name;
      	}
      
      	public String getVorname()
      	{
      		return vorname;
      	}
      
      	public void setVorname(String vorname)
      	{
      		this.vorname = vorname;
      	}
      
      	public Gesellschaftsform getGform()
      	{
      		return gform;
      	}
      
      	public void setGform(Gesellschaftsform gform)
      	{
      		this.gform = gform;
      	}
      
      }
      And the DataSourceLoader returns:
      Code:
      isc.DataSource.create({
          schemaBean:"de.test.TestEntity",
          ID:"Firma",
          autoDeriveSchema:true,
          inheritsFrom:null,
          fields:[
          ]
      })

      The Dynamic Form bound to this DataSource is empty.

      Can you tell me what I am doing wrong? I have just ordered a Pro-license and heavily depend on this to work to be able to use it for our next project...


      Your's

      John

      Comment


        #4
        I do not know if this is relevant in any way, but in the DataSource.ds.xml from my smartgwtee.jar there is no attribute mentioned that is named "schemaBean"...

        From the logs I get the following:

        Code:
        /DataSource[@ID=Firma]/ID as DataSource.ID type: string
        === 2010-11-11 11:12:56,932 [p--3] DEBUG Validation - Value provided for unknown field: DataSource.schemaBean: value is: de.test.TestEntity
        Do I use a wrong version? Since I am waiting for my ordered Pro-license, I am working on the trial-version right now...


        Your's

        John
        Last edited by stegemannj; 11 Nov 2010, 10:44.

        Comment


          #5
          You never mentioned your version (always post this). This feature was added after 2.3, so you'd need to use a nightly build (smartclient.com/builds).

          Comment


            #6
            I use Smartgwt Power 3.0 (some recent patched version) and I can't make Java enum values to be populated in valueMap.

            Here is my ds file:

            Code:
            <!-- 
            <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
            -->
              
            <DataSource 
            	xmlns:fmt="urn:jsptld:http://java.sun.com/jsp/jstl/fmt"
            	
                ID="orderBatch"  
                serverConstructor="com.isomorphic.jpa.JPA2DataSource"  
                autoDeriveSchema="true"  
                schemaBean="net.esteh.esdoc.domain.OrderBatch"
                dropExtraFields="true">
                
                
                <fields>
                	<field primaryKey="true" name="id" type="sequence" hidden="true"></field>
            		...
            		
            		<field name="status" type="enum" />
            		...
                       
                </fields>
                
            </DataSource>
            Java class:

            Code:
            
            @Entity
            @Table(name="es_order_batch")
            
            public class OrderBatch implements Serializable {
                
                @Id
                @GeneratedValue(strategy = GenerationType.AUTO)
                @Column(name="id")
                private Long id;
            	....    
                
                @Column(name="status_id")
                @Enumerated
                private OrderStatus status;
            
                ...
                
                public OrderStatus getStatus() {
                    return status;
                }
            
                public void setStatus(OrderStatus status) {
                    this.status = status;
                }
            
               	...
            
            
            
                
            }
            
            
            public enum OrderStatus {
                NEW, 
                READY_FOR_OCR,
                SENT_TO_OCR,
                OCR_COMPLETED,
                READY_FOR_OPERATOR,
                READY_FOR_VALIDATION,
                VALID,
                READY_FOR_EXPORT,
                EXPORTED,    
                NOT_VALID,
                EXPORT_ERROR,
                NEED_SUPERVISION
            }
            And here is what I get in when load data source

            Code:
            isc.DataSource.create({
                ID:"orderBatch",
                autoDeriveSchema:true,
                dropExtraFields:true,
                schemaBean:"net.esteh.esdoc.domain.OrderBatch",
                serverConstructor:"com.isomorphic.jpa.JPA2DataSource",
                fmt:"urn:jsptld:http://java.sun.com/jsp/jstl/fmt",
                fields:[
                    {
                        hidden:true,
                        name:"id",
                        primaryKey:true,
                        type:"sequence"
                    },
                  	...
                    {
                        name:"status",
                        type:"enum"
                    },
                   ...
                ]
            })
            Am I doing something wrong? Shouldn't valueMap property for status field be automatically populated with possible enum values?

            Comment


              #7
              This is happening because your DataSource is i18n'd - we didn't make this feature work in this mode since a valueMap derived from an enum is necessarily going to have non-i18n text. You can add a valueMap definition with JSTL tags to load the appropriate locale-specific strings.

              Comment


                #8
                You mean something like this? Or is there some more elegant solution?
                Code:
                <!-- 
                <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
                <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
                -->
                  
                <DataSource 
                	xmlns:fmt="urn:jsptld:http://java.sun.com/jsp/jstl/fmt"
                	xmlns:c="urn:jsptld:http://java.sun.com/jsp/jstl/core"
                    ID="orderBatch"  
                    serverConstructor="com.isomorphic.jpa.JPA2DataSource"  
                    autoDeriveSchema="true"  
                    schemaBean="net.esteh.esdoc.domain.OrderBatch"
                    dropExtraFields="true">
                    
                    
                    <fields>
                    	<field primaryKey="true" name="id" type="sequence" hidden="true"></field>
                 ...
                		<field name="status" type="enum" >
                		<valueMap>
                			<c:forEach var="entry" items="<%=net.esteh.esdoc.domain.OrderStatus.values() %>">
                				<value id="${entry}"><fmt:message key="label_${entry}"/></value>
                			</c:forEach>  
                		</valueMap>
                                </field>
                ... 
                    </fields> 
                    
                </DataSource>

                Comment


                  #9
                  Hi

                  You should skip this field definition. Then it will be auto generated.
                  Field definition in .ds.xml file overrides auto generated field.

                  i18n can be implemented in toString() of your enum class.

                  Next nightly build will take into account enum's toString().

                  Regards,
                  Alius

                  Comment

                  Working...
                  X