Announcement

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

    ComboBoxItem on a hibernate datasource best way

    What is the best way to have a drop down comboboxitem or selectitem in Smartgwt?

    Should I create listgridfields (Causes errors) or create DataSourceTextFields?

    My usecase is to have an entity (e.g. for example city) that is categorized by type (e.g. country). This is a common use case for hibernate datasources where an object has a role or category.

    An Employee may be assigned to a Job_type. Employee table has a Long fk_job_type_id. I want to update the employee's foreign key not the job type.

    I have tried both (code is below). Neither seems to work....

    It's hard to determine if I should use includeFrom or displayField; setForeignKey or setValueField, etc.

    I would expect the foreignkey is ("country"). It's countryId.

    Code:
    package your.package.here;
     
    
    import com.smartgwt.client.core.KeyIdentifier;
    
    import com.smartgwt.client.data.DataSource;  
    import com.smartgwt.client.data.fields.DataSourceTextField;
    import com.smartgwt.client.types.ListGridEditEvent;  
    import com.smartgwt.client.util.KeyCallback;
    import com.smartgwt.client.util.Page;
    import com.smartgwt.client.util.SC;
    import com.smartgwt.client.widgets.Canvas;  
    import com.smartgwt.client.widgets.IButton;  
    import com.smartgwt.client.widgets.events.ClickEvent;  
    import com.smartgwt.client.widgets.events.ClickHandler;  
    import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
    import com.smartgwt.client.widgets.grid.ListGrid;  
    import com.smartgwt.client.widgets.grid.ListGridField;  
    import com.smartgwt.client.widgets.layout.VLayout;  
      
    import com.google.gwt.core.client.EntryPoint;  
    import com.google.gwt.user.client.Window;
    //import com.isomorphic.datasource.DataSource;
      
    public class MyDemo3b implements EntryPoint {  
    	DataSource hbdb;
        @Override  
        public void onModuleLoad() {  
    
    		KeyIdentifier debugKey = new KeyIdentifier();
    		debugKey.setCtrlKey(true);
    		debugKey.setKeyName("D");
    
    		Page.registerKey(debugKey, new KeyCallback() {
    			public void execute(String keyName) {
    				SC.showConsole();
    			}
    		});
    		//CityManyToOneSimple a;
    	//	a.setCountry(new CountryManyToOneSimple().setCountryId(new Long(3L));
    		
        
            final ListGrid listGrid = new ListGrid();  
       //     Window.alert("helloa");
        //  DataSource 
            hbdb = new DataSource();
          hbdb.setID("cityManyToOneSimpleHB");
        
        		  
            listGrid.setDataSource(hbdb);  
            listGrid.setWidth(700);  
          //   Window.alert("hellob");
            listGrid.setHeight(224);  
             listGrid.setShowFilterEditor(true);  
            listGrid.setAlternateRecordStyles(true);  
            listGrid.setAutoFetchData(true);  
            listGrid.setDataPageSize(50);  
            listGrid.setCanEdit(true);  
            listGrid.setEditEvent(ListGridEditEvent.CLICK);  
            
            listGrid.setCanRemoveRecords(true);  
            ComboBoxItem cbxitem = new ComboBoxItem("country");
            DataSource countryds = new DataSource();
            countryds.setID("countryManyToOneSimpleHB");
            
            cbxitem.setOptionDataSource(countryds);//countryds
        //   DataSourceTextField lgfCountry = new DataSourceTextField("country", "Country");
            ListGridField lgfCountry = new ListGridField("country", "Country");
            
            ListGridField lgfcityid = new ListGridField("cityId");
           lgfcityid.setHidden(true);
         //    cbxitem.setValueField("country");
            cbxitem.setDisplayField("countryName");
            cbxitem.setAddUnknownValues(false);
            //cbxitem.setVa
           lgfCountry.setDisplayField("countryName");
            lgfCountry.setEditorProperties(cbxitem);
        //    lgfCountry.setForeignKey(foreignKey);
            
         //   listGrid.setFields(new ListGridField("cityName", "City"), lgfCountry);  
            listGrid.setFields(lgfcityid, new ListGridField("cityName", "City"), lgfCountry);  
            
            IButton newButton = new IButton("Add New");  
            newButton.addClickHandler(new ClickHandler() {  
                public void onClick(ClickEvent event) {  
                    listGrid.startEditingNew();  
                }  
            });  
        //    Window.alert("helloc");
      
            VLayout layout = new VLayout();  
            layout.setWidth100();
            layout.addMember(listGrid);  
            layout.addMember(newButton);  
      
            layout.draw();  
        }  
      
    }

    Or something like
    Code:
     public void onModuleLoad() {  
    
    			KeyIdentifier debugKey = new KeyIdentifier();
    			debugKey.setCtrlKey(true);
    			debugKey.setKeyName("D");
    
    			Page.registerKey(debugKey, new KeyCallback() {
    				public void execute(String keyName) {
    					SC.showConsole();
    				}
    			});
    			//CityManyToOneSimple a;
    		//	a.setCountry(new CountryManyToOneSimple().setCountryId(new Long(3L));
    			
    	    
    	        final ListGrid listGrid = new ListGrid();  
    	   //     Window.alert("helloa");
    	    //  DataSource 
    	        hbdb = new DataSource();
    	      hbdb.setID("cityManyToOneSimpleHB");
    	    
    	    		  
    	        listGrid.setDataSource(hbdb);  
    	        listGrid.setWidth(700);  
    	      //   Window.alert("hellob");
    	        listGrid.setHeight(224);  
    	         listGrid.setShowFilterEditor(true);  
    	        listGrid.setAlternateRecordStyles(true);  
    	        listGrid.setAutoFetchData(true);  
    	        listGrid.setDataPageSize(50);  
    	        listGrid.setCanEdit(true);  
    	        listGrid.setEditEvent(ListGridEditEvent.CLICK);  
    	        
    	        listGrid.setCanRemoveRecords(true);  
    	        ComboBoxItem cbxitem = new ComboBoxItem("country");
    	        DataSource countryds = new DataSource();
    	        countryds.setID("countryManyToOneSimpleHB");
    	        
    	        cbxitem.setOptionDataSource(countryds);//countryds
    	    //   DataSourceTextField lgfCountry = new DataSourceTextField("country", "Country");
    	        DataSourceTextField dsCountry = new DataSourceTextField("country", "Country");
    	        
    	        //DataSourceIntegerField dscityid = new DataSourceIntegerField("cityId", "City Id");
    	        DataSourceTextField dsCityName = new DataSourceTextField("cityName", "City");
    	      // dscityid.setHidden(true);
    	     //    cbxitem.setValueField("country");
    	        cbxitem.setDisplayField("countryName");
    	        cbxitem.setAddUnknownValues(false);
    	        //cbxitem.setVa
    	       dsCountry.setDisplayField("countryName");
    	        dsCountry.setEditorProperties(cbxitem);
    	       dsCountry.setForeignKey("Country.country");
    	        
    	     //dscityid,
    	        		hbdb.setFields(		dsCityName, dsCountry);
    	     //   listGrid.setFields(new ListGridField("cityName", "City"), lgfCountry);  
    	     //   listGrid.setFields(dscityid, dsCityName, dsCountry);  
    	        
    	        IButton newButton = new IButton("Add New");  
    	        newButton.addClickHandler(new ClickHandler() {  
    	            public void onClick(ClickEvent event) {  
    	                listGrid.startEditingNew();  
    	            }  
    	        });  
    	    //    Window.alert("helloc");
    	  
    	        VLayout layout = new VLayout();  
    	        layout.setWidth100();
    	        layout.addMember(listGrid);  
    	        layout.addMember(newButton);  
    	  
    	        layout.draw();  
    	    }  
      
    }
    What is the best set of choices and what is the best example for code. The existing sample ones (databound comboboxes) do not cover this usecase well for the foreignkey.
    Last edited by jlhoward1; 16 Apr 2014, 09:07. Reason: Additional Use case.

    #2
    Best Practice for selectitem sought. But Duplicate rows i the Selectitem showup.

    /// Using this for the selectitem. But it duplicates the dropdown items in the selectitem list after edits.
    Code:
    final ListGrid listGrid = new ListGrid();
    		// Window.alert("helloa");
    		// DataSource
    		hbdb = new DataSource();
    		hbdb.setID("cityManyToOneSimpleHB");
    
    		// listGrid.setDataSource(hbdb);
    		listGrid.setWidth(700);
    		// Window.alert("hellob");
    		listGrid.setHeight(224);
    		listGrid.setShowFilterEditor(true);
    		listGrid.setAlternateRecordStyles(true);
    		listGrid.setAutoFetchData(true);
    		listGrid.setDataPageSize(50);
    		listGrid.setCanEdit(true);
    		listGrid.setEditEvent(ListGridEditEvent.CLICK);
    
    		listGrid.setCanRemoveRecords(true);
    		SelectItem cbxitem = new SelectItem("country");
    		DataSourceIntegerField cityidfield = new DataSourceIntegerField(
    				"cityId");
    		cityidfield.setPrimaryKey(true);
    
    		DataSource countryds = new DataSource();
    		countryds.setID("countryManyToOneSimpleHB");
    
    		cbxitem.setOptionDataSource(countryds);// countryds
    		ListGridField lgfCountry = new ListGridField("countryName", "Country");
    		DataSourceTextField dsCountry = new DataSourceTextField("country",
    				"Country");
    
    		DataSourceTextField dsCityName = new DataSourceTextField("cityName",
    				"City");
    
    		cbxitem.setDisplayField("countryName");
    		cbxitem.setPickListFields(lgfCountry);
    
    		cbxitem.setAddUnknownValues(false);
    
    		dsCountry.setDisplayField("countryName");
    
    		dsCountry.setEditorProperties(cbxitem);
    		dsCountry.setForeignKey("countryId");
    
    		hbdb.setFields(cityidfield, dsCityName, dsCountry);
    		listGrid.setDataSource(hbdb);
    
    		IButton newButton = new IButton("Add New");
    		newButton.addClickHandler(new ClickHandler() {
    			public void onClick(ClickEvent event) {
    				listGrid.startEditingNew();
    			}
    		});
    		// Window.alert("helloc");
    
    		VLayout layout = new VLayout();
    		layout.setWidth100();
    		layout.addMember(listGrid);
    		layout.addMember(newButton);
    
    		layout.draw();
    	}
    Attached Files
    Last edited by jlhoward1; 15 Apr 2014, 11:50. Reason: add code blocks

    Comment


      #3
      Results

      Da_n... I always wish people would post results after they solve or partly solve a problem. Anyway here is mine.

      The last posting seems to be more correct.

      1) Still getting duplicates in the selectitem after I edit each row. The edits work correctly but I am not sure why this is happening.

      2) Not sure If I should use setting on PickLists.

      3) I put displayFields on the dropdown. That seems to be ok.

      4) I setForeignKey(datasourceid.fieldname) to use countryId.

      For hibernate I am not sure if the field should textually match the
      - the database fieldname in the country table (countryId)
      - the join column name in the hibernate bean cityManyToOneSimple.java specifically this
      Code:
          @ManyToOne
          @JoinColumn(name=[b]"countryId[/b]", referencedColumnName="countryId")
          private CountryManyToOneSimple country;
      
          public CityManyToOneSimple ()
          {
          }
      - the field in CountryManyToOneSimple.java specifically

      Code:
        @Id
          @Column (nullable = false)
          @GeneratedValue (strategy = GenerationType.IDENTITY)
          private Long countryId;
      5. Set the datasource for the listGrid.setDataSource(hbdb); near the end. I am not sure why, but the program failed if I moved it up.

      6. Here is my current version. Hope it helps someone else. The duplicate problem in selectitem is not fixed.
      Code:
      	
      package your.package.here;
      
      /**
      view plainprint?
      /* 
       * Isomorphic SmartGWT web presentation layer 
       * Copyright 2000 and beyond Isomorphic Software, Inc. 
       * 
       * OWNERSHIP NOTICE 
       * Isomorphic Software owns and reserves all rights not expressly granted in this source code, 
       * including all intellectual property rights to the structure, sequence, and format of this code 
       * and to all designs, interfaces, algorithms, schema, protocols, and inventions expressed herein. 
       * 
       *  If you have any questions, please email <sourcecode@isomorphic.com>. 
       * 
       *  This entire comment must accompany any portion of Isomorphic Software source code that is 
       *  copied or moved from this file. 
       */
      
      import com.smartgwt.client.core.KeyIdentifier;
      import com.smartgwt.client.data.DataSource;
      import com.smartgwt.client.data.fields.DataSourceIntegerField;
      import com.smartgwt.client.data.fields.DataSourceSequenceField;
      import com.smartgwt.client.data.fields.DataSourceTextField;
      import com.smartgwt.client.types.ListGridEditEvent;
      import com.smartgwt.client.util.KeyCallback;
      import com.smartgwt.client.util.Page;
      import com.smartgwt.client.util.SC;
      import com.smartgwt.client.widgets.Canvas;
      import com.smartgwt.client.widgets.IButton;
      import com.smartgwt.client.widgets.events.ClickEvent;
      import com.smartgwt.client.widgets.events.ClickHandler;
      //import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
      import com.smartgwt.client.widgets.form.fields.SelectItem;
      import com.smartgwt.client.widgets.grid.ListGrid;
      import com.smartgwt.client.widgets.grid.ListGridField;
      import com.smartgwt.client.widgets.layout.VLayout;
      
      import com.google.gwt.core.client.EntryPoint;
      import com.google.gwt.user.client.Window;
      import com.isomorphic.datasource.IncludeFromInfo;
      
      //import com.isomorphic.datasource.DataSource;
      
      public class MyDemo3b implements EntryPoint {
      	DataSource hbdb;
      
      	public void onModuleLoad() {
      
      		KeyIdentifier debugKey = new KeyIdentifier();
      		debugKey.setCtrlKey(true);
      		debugKey.setKeyName("D");
      
      		Page.registerKey(debugKey, new KeyCallback() {
      			public void execute(String keyName) {
      				SC.showConsole();
      			}
      		});
      	
      		final ListGrid listGrid = new ListGrid();
      	
      		// DataSource.get in the sample only works for an existing datasource per isomorphic (in case you didn't catch the prior comment.) 
      		hbdb = new DataSource();
      		hbdb.setID("cityManyToOneSimpleHB");
      
      		// Don't do listGrid.setDataSource(hbdb); here.... 
      		// Do this at the end. 
      		listGrid.setWidth(700);
      
      		listGrid.setHeight(224);
      		listGrid.setShowFilterEditor(true);
      		listGrid.setAlternateRecordStyles(true);
      		listGrid.setAutoFetchData(true);
      		listGrid.setDataPageSize(50);
      		listGrid.setCanEdit(true);
      		listGrid.setEditEvent(ListGridEditEvent.CLICK);
      
      		listGrid.setCanRemoveRecords(true);
      		SelectItem cbxitem = new SelectItem("country");
      		DataSourceIntegerField cityidfield = new DataSourceIntegerField(
      				"cityId");
      		cityidfield.setPrimaryKey(true);
      
      		DataSource countryds = new DataSource();
      		countryds.setID("countryManyToOneSimpleHB");
      
      		cbxitem.setOptionDataSource(countryds);// countryds
      		ListGridField lgfCountry = new ListGridField("countryName", "Country");
      		DataSourceTextField dsCountry = new DataSourceTextField("country",
      				"Country");
      
      		DataSourceTextField dsCityName = new DataSourceTextField("cityName",
      				"City");
      
      		cbxitem.setDisplayField("countryName");
      		cbxitem.setPickListFields(lgfCountry);
      
      		cbxitem.setAddUnknownValues(false);
      
      		dsCountry.setDisplayField("countryName");
      
      		dsCountry.setEditorProperties(cbxitem);
      		dsCountry.setForeignKey("countryId");
      
      		hbdb.setFields(cityidfield, dsCityName, dsCountry);
      		listGrid.setDataSource(hbdb);
      
      		IButton newButton = new IButton("Add New");
      		newButton.addClickHandler(new ClickHandler() {
      			public void onClick(ClickEvent event) {
      				listGrid.startEditingNew();
      			}
      		});
      		// Don't use Window.alert("helloc"); 
                                // Instead Use the SC debugs that write to the log. 
      
      		VLayout layout = new VLayout();
      		layout.setWidth100();
      		layout.addMember(listGrid);
      		layout.addMember(newButton);
      
      		layout.draw();
      	}
      
      }
      8. The Window.alert("debug"); is not the preferred debugging method.

      Joe

      Comment


        #4
        For clarity, you'd be better declaring your DataSources as .ds.XML files, including them in your bootstrap HTML file and loading them up with DataSource.get().

        However - in this case, your sample code doesn't setValueField() on your select item. When you setOptionDataSource(), you should also setValueField(pkField) and setDisplayField(textField).

        If you still have problems getting this to work, please let us know - the specifics of what you're trying to achieve aren't clear from your descriptions coupled with the code, so if you can provide the rest of the code necessary to run the samples you've been posting, in particular the definitions of the DataSources, we'll have a deeper look.
        Last edited by Isomorphic; 16 Apr 2014, 22:05.

        Comment


          #5
          continued....

          I have corrected the setValueField. No luck.

          In hibernate, I generally expect the owning side (city) with the foreign key to update a child. Works as expected.

          --------------------------------------------------------

          If I want to reassign the country without updating the country table or causing a multiple batch update, I use wanted child (country) to update the city (foreign_key) .... however hibernate doesn't expose that foreign key. If I update city name and assign a new country, I want the foreign key to update (from the CountryManytoOne Hibernate Bean) and the owning record (from CityManyToOne Hibernate Bean) in one transaction.

          This is pretty common (Person has an immutable jobType). Hibernate/JPA (insertable=false,updatable=false) does not help here.

          There seems to be no way to do this in the logic.

          This is not true for SQLDataSources, which expose the foreignkey.

          I tried duplicating this in visualBuilder (results SQLDatasources Ok, Hibernate not working).

          ------------------------------------------------------

          Comment


            #6
            continued.

            In your hibernate sample should the foreign key be - the name or the referenced columnname
            @JoinColumn(name="fk_countryId", referencedColumnName="countryId") ??

            Comment


              #7
              Hi jlhoward1,

              I never worked with hibernate, but your general use case seems pretty usual. CRUD ListGrid with FKs. And obviously at no time you want to see IDs, but always some text from the child table.

              So here my observations: ListGrid has two states:
              1. Showing data
              2. Editing one row


              For state one everything can be handled with .ds.xml files.
              Code:
              <field name="RESELLER_ID" foreignKey="T_RESELLER.ID" joinType="outer" displayField="RESELLER_NAME"
              			type="integer" />
              <field name="RESELLER_NAME" includeFrom="T_RESELLER.NAME"></field>
              Once you doubleclick to edit a row (state two), SmartGWT uses the underlying (id-)value for that field and handles it over to the ListGridField's Editor (in that case by default a SelectItem, but you can control that behaviour with setEditorProperties(new xxx)).
              You'll want to use setOptionDataSource(ParentDS), setValueField("ID"), setDisplayField("NAME") in your editor item.

              Once you got that working you can play with the *PickList*-methods in your ListGridField's SelectItem.
              Code:
              lgf.setEditorProperties(new ComboBoxItemCountry (...));
              
              
              Some Editorclass of mine:
              
              public class ComboBoxItemCountry extends ComboBoxItem {
              	final private DataSource countryDS = DataSource.get(DatasourceEnum.T_COUNTRY.getValue());
              
              	public ComboBoxItemCountry(String name, boolean showTelephone) {
              		super(name);
              		
              		setOptionDataSource(countryDS);
              		setValueField(countryDS.getPrimaryKeyFieldName());
              		setFetchMissingValues(true);
              		
              		ListGridField shortnameDeLGF;
              		if (showTelephone) {
              			setOptionOperationId("fetchDropdowndataTelephone");
              			setDisplayField("SHORTNAME_DE_CCPLUS");
              			setSortField("SHORTNAME_DE_CCPLUS");
              			//setDefaultValue("Deutschland (+49)");
              			setDefaultValue(246); //Deutschland
              			shortnameDeLGF = new ListGridField("SHORTNAME_DE_CCPLUS");
              		} else {
              			setOptionOperationId("fetchDropdowndataCountry");
              			setDisplayField("SHORTNAME_DE");
              			setSortField("SHORTNAME_DE");
              			//setDefaultValue("Deutschland");
              			setDefaultValue(37); //Deutschland
              			shortnameDeLGF = new ListGridField("SHORTNAME_DE");
              		}
              
              		setValidateOnExit(true);
              		setDefaultToFirstOption(true);
              		setTextMatchStyle(TextMatchStyle.SUBSTRING);
              		setBrowserSpellCheck(false);
              
              		//in ds.xml without escapeHTML!
              		ListGridField countrycodeLGF = new ListGridField("ISO_3166_1_ALPHA_2", "Flag", 25) {
              			{
              				setAlign(Alignment.CENTER);
              				setType(ListGridFieldType.IMAGE);
              				setImageURLPrefix("flags/16/");
              				setImageURLSuffix(".png");
              			}
              		};
              		
              		setPickListFields(countrycodeLGF, shortnameDeLGF);
              		setPickListHeaderHeight(0);
              		setPickListWidth(200);
              		setPickListHeight(200);
              	}
              }
              I don't know how this works with hibernate, but as you could use .ds.xml-files with hibernate, I don't think there will be much difference.

              Best regards,
              Blama

              Comment

              Working...
              X