Announcement

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

    Faulty relations between datasources

    I'm a newbie that's been fooling around with Smart-Gwt for a couple of days. I've got 2 datasources up and running which are read from Hibernate/Gilead through GenericGwtRpcDatasource (thanks everyone involved for this!).



    Hibernate/Gilead DTO used as DataSources record
    Code:
    @Entity
    @Table(name="product")
    public class Product extends LightEntity implements Serializable{
    	@Id
    	@Column(name="product_nr", unique=true, nullable=false, length=20)
    	private String productNr;
    	@Column(name="status", unique=false, nullable=false, columnDefinition="bit")
    	private int status;
    	@Column(name="product_family_id", unique=false, nullable=false, length=13)
    	private String productFamilyId;
    	@Column(name="name", unique=false, nullable=true, length=32)
    	private String name;
    	@Column(name="short_description", unique=false, nullable=true, length=128)
    	private String shortDescription;
    	@Column(name="long_description", unique=false, nullable=true, columnDefinition="TEXT")
    	private String longDescription;
    	@Column(name="configurator_instruction", unique=false, nullable=true, columnDefinition="TEXT")
    	private String configuratorInstruction;
    	@Column(name="free_accessories_comment", unique=false, nullable=true, columnDefinition="TEXT")
    	private String freeAccessoriesComment;
    	@OneToOne(cascade = CascadeType.ALL)
    	private ConfiguratorSetting configuratorSetting;
    	
    	public ConfiguratorSetting getConfiguratorSetting() {
    		return configuratorSetting;
    	}
    	public void setConfiguratorSetting(ConfiguratorSetting configuratorSetting) {
    		this.configuratorSetting = configuratorSetting;
    	}
    	public String getProductNr() {
    		return productNr;
    	}
    	public void setProductNr(String productNr) {
    		this.productNr = productNr;
    	}
    	public int getStatus() {
    		return status;
    	}
    	public void setStatus(int status) {
    		this.status = status;
    	}
    	public String getProductFamilyId() {
    		return productFamilyId;
    	}
    	public void setProductFamilyId(String productFamilyId) {
    		this.productFamilyId = productFamilyId;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getShortDescription() {
    		return shortDescription;
    	}
    	public void setShortDescription(String shortDescription) {
    		this.shortDescription = shortDescription;
    	}
    	public String getLongDescription() {
    		return longDescription;
    	}
    	public void setLongDescription(String longDescription) {
    		this.longDescription = longDescription;
    	}
    	public String getConfiguratorInstruction() {
    		return configuratorInstruction;
    	}
    	public void setConfiguratorInstruction(String configuratorInstruction) {
    		this.configuratorInstruction = configuratorInstruction;
    	}
    	public String getFreeAccessoriesComment() {
    		return freeAccessoriesComment;
    	}
    	public void setFreeAccessoriesComment(String freeAccessoriesComment) {
    		this.freeAccessoriesComment = freeAccessoriesComment;
    	}
    	
    	public boolean equals(final Object other){
    		if(other.getClass() == Product.class){
    			Product otherProduct = (Product) other;
    			if(getProductNr() == otherProduct.getProductNr()){
    				return true;
    			}
    		}
    		return false;
    	}
    }
    
    @Entity
    @Table(name="configurator_setting")
    public class ConfiguratorSetting extends LightEntity implements Serializable{
    	@Id
    	@Column(name="id", unique=true, nullable=false)
    	private int id;
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	@Column(name="name", unique=false, nullable=false)
    	private String name;
    	//private ArrayList<PrintSurfaceGroup> possibleSurfaceGroups;
    }
    ProductDS
    Code:
    public class ProductDS extends GenericGwtRpcDataSource<Product, ListGridRecord, ProductDSServiceAsync>{
    	private static ProductDS instance;
    	
    	public static ProductDS getInstance() {
    		if (instance == null) {
    			instance = new ProductDS();
    			instance.setID("product");
    		}
    		return (instance);
    	}
        
        private ListGridRecord getEditedRecord (DSRequest request) {
            // Retrieving values before edit
            JavaScriptObject oldValues = request.getAttributeAsJavaScriptObject ("oldValues");
            // Creating new record for combining old values with changes
            ListGridRecord newRecord = new ListGridRecord ();
            // Copying properties from old record
            JSOHelper.apply (oldValues, newRecord.getJsObj ());
            // Retrieving changed values
            JavaScriptObject data = request.getData ();
            // Apply changes
            JSOHelper.apply (data, newRecord.getJsObj ());
            return newRecord;
        }
    
    	@Override
    	public List<DataSourceField> getDataSourceFields() {
    		setID("product");
    		List<DataSourceField> fields = new ArrayList<DataSourceField>();
    		
    	    DataSourceField field;
            field = new DataSourceTextField("productNr", "PRODUCTNR");
            field.setPrimaryKey (true);
            // AutoIncrement on server.
            field.setRequired (false);
            fields.add(field);
            
            field = new DataSourceTextField ("productFamilyId", "ProductFamiljId");
            field.setRequired (true);
            fields.add(field);
            
            DataSourceIntegerField field2 = new DataSourceIntegerField("configuratorSettingId", "configuratorSettingId");
            field2.setForeignKey("configuratorSetting.id");
            fields.add(field2);
            
            field = new DataSourceTextField ("name", "NAME");
            field.setRequired (true);
            fields.add(field);
            
    
            return fields;
    	}
    
    	@Override
    	public void copyValues(ListGridRecord from, Product to) {
    		  to.setProductNr(from.getAttributeAsString("productNr"));
    		  to.setName(from.getAttributeAsString("name"));
    		  to.setStatus(from.getAttributeAsInt("status"));
    		  to.setProductFamilyId(from.getAttribute("productFamilyId"));
    	}
    
    	@Override
    	public void copyValues(Product from, ListGridRecord to) {
    		to.setAttribute("productNr", from.getProductNr());
    		to.setAttribute("name", from.getName());
    		to.setAttribute("status", from.getStatus());
    		to.setAttribute("productFamilyId", from.getProductFamilyId());
    		if(from.getConfiguratorSetting() != null){
    			to.setAttribute("configuratorSettingId", from.getConfiguratorSetting().getId());
    		}
    	}
    
    	@Override
    	public ProductDSServiceAsync getServiceAsync() {
    		return GWT.create(ProductDSService.class);
    	}
    
    	@Override
    	public ListGridRecord getNewRecordInstance() {
    		return new ListGridRecord();
    	}
    
    	@Override
    	public Product getNewDataObjectInstance() {
    		return new Product();
    	}
    }
    ConfiguratorSettingDS:
    Code:
    public class ConfiguratorSettingDS extends GenericGwtRpcDataSource<ConfiguratorSetting, ListGridRecord, ConfiguratorSettingDSServiceAsync>{
    	private static ConfiguratorSettingDS instance;
    	public static ConfiguratorSettingDS getInstance() {
    		if (instance == null) {
    			instance = new ConfiguratorSettingDS();
    			instance.setID("configuratorSetting");
    		}
    		return (instance);
    	}
    
    	@Override
    	public List<DataSourceField> getDataSourceFields() {
    		setID("configuratorSetting");
    		List<DataSourceField> fields = new ArrayList<DataSourceField>();
    	    DataSourceIntegerField field;
    	    field = new DataSourceIntegerField("id", "ID");
    	    field.setPrimaryKey(true);
    	    // AutoIncrement on server.
    	    field.setRequired (false);
    	    fields.add(field);
    	    
    	    DataSourceTextField field2 = new DataSourceTextField ("name", "Namn");
    	    field2.setRequired (true);
    	    fields.add(field2);
    		return fields;
    	}
    
    	@Override
    	public void copyValues(ListGridRecord from, ConfiguratorSetting to) {
    		 to.setId(from.getAttributeAsInt("id"));
    		 to.setName(from.getAttributeAsString("name"));
    	}
    
    	@Override
    	public void copyValues(ConfiguratorSetting from, ListGridRecord to) {
          to.setAttribute("id", from.getId());
          to.setAttribute("name", from.getName());
    	}
    
    	@Override
    	public ConfiguratorSettingDSServiceAsync getServiceAsync() {
    		return GWT.create(ConfiguratorSettingDSService.class);
    	}
    
    	@Override
    	public ListGridRecord getNewRecordInstance() {
    		return new ListGridRecord();
    	}
    
    	@Override
    	public ConfiguratorSetting getNewDataObjectInstance() {
    		return new ConfiguratorSetting();
    	}
    }
    Both data sources are identified by their id field. However it seams thats their names fields are actually the one being chosen:
    Code:
    23:40:55.620:MUP5:WARN:DataSource:configuratorSetting:matched tree relationship field by name: name
    I' am able to display a ListGrid with http://www.smartclient.com/smartgwt/showcase/#featured_nested_grid as a foundation:
    Code:
    	public void onModuleLoad() {
    		ListGrid listGrid = new ListGrid() {  
    			public DataSource getRelatedDataSource(ListGridRecord record) {  
    				return ConfiguratorSettingDS.getInstance();  
    			}  
    			
    			@Override  
    			protected Canvas getExpansionComponent(final ListGridRecord record) {  
    				final ListGrid grid = this;  
                    VLayout layout = new VLayout(5);  
                    layout.setPadding(5);  
       
                    final ListGrid countryGrid = new ListGrid();  
                    countryGrid.setWidth(500);  
                    countryGrid.setHeight(224);  
                    countryGrid.setCellHeight(22);  
                    countryGrid.setDataSource(getRelatedDataSource(record));  
                    countryGrid.fetchRelatedData(record, ProductDS.getInstance());  
       
                    countryGrid.setCanEdit(true);  
                    countryGrid.setModalEditing(true);  
                    //countryGrid.setEditEvent(ListGridEditEvent.CLICK);  
                    //countryGrid.setListEndEditAction(RowEndEditAction.NEXT);  
                    countryGrid.setAutoSaveEdits(false);  
       
                    layout.addMember(countryGrid);  
       
                    HLayout hLayout = new HLayout(10);  
                    hLayout.setAlign(Alignment.CENTER);
                    IButton saveButton = new IButton("Save");  
                    saveButton.setTop(250);                                                  
                    layout.addMember(hLayout);  
                    return layout;  
    			}
    		};
    		listGrid.setDataSource(ProductDS.getInstance());
    		listGrid.setAutoFetchData(true);
    		listGrid.setWidth(500);
    		listGrid.setHeight(400);
    		listGrid.setCanExpandRecords(true);  
    		listGrid.setAutoFetchData(true);  		            
    		listGrid.draw();  
    	}
    }

    When I extend a node in the ListGrid I guess that it should send an request to fetch a ConfigurationSetttingDS record identified by the value in configuratorSettingId for the extended row.

    However what happens is that the value of ProductDS 'name'-field instead gets passed as the filter (verified at the server that the 'name' not the 'configuratorSettingId' -attribute of ProductDS gets passed as filter and reaches as far as to Hibernate DTO).

    I am quite convinced that I must have made some errors in my Data Sources but I can't see where.

    Any help would bee deeply appreciated.

    /Lukas

    Version: SmartGwt 2.2 (LGpl))

    #2
    Is there really nobody that can spot what I'm doing wrong? Please?
    Thanks /Lukas

    Comment

    Working...
    X