Announcement

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

    Datasource .ds.xml and java class

    Hi,
    Is there any way to use .ds.xml file AND Datasource java defined (related both to same data source) together?
    To be more clear, I'd like to define an .xml.ds like this:
    Code:
    <DataSource ID="kiosk" schemaBean="it.onetech.dtproviding.server.entity.Kiosk">
    	<serverObject lookupStyle="spring" bean="kioskDao" />
    </DataSource>
    and a client-side java class:
    Code:
    public class KioskDS {
    
    	private DataSource datasource = null;
    
    	/**
    	 * Default constructor
    	 */
    	public KioskDS () {
    		datasource = DataSource.get("kiosk");
    
    		DataSourceField id = new DataSourceField("id", FieldType.SEQUENCE);
    		id.setPrimaryKey(true);
    		id.setHidden(true);
    		id.setRequired(true);
    
    		DataSourceField alias = new DataSourceField(KioskDSFields.NAME.getAlias(), FieldType.TEXT, I18n.CST.kioskDSAliasTitle());
    		DataSourceField iNetAddress = new DataSourceField(KioskDSFields.INETADDRESS.getAlias(), FieldType.TEXT, I18n.CST.kioskDSINetAddressTitle());
    		DataSourceField regDate = new DataSourceField(KioskDSFields.REGDATE.getAlias(), FieldType.DATETIME, I18n.CST.kioskDSRegDateTitle());
    		DataSourceField addInfos = new DataSourceField(KioskDSFields.ADDINFO.getAlias(), FieldType.TEXT, I18n.CST.kioskDSAddInfoTitle(), 1000);
    		DataSourceField group = new DataSourceField(KioskDSFields.GROUP.getAlias(), FieldType.INTEGER, I18n.CST.kioskDSGroupTitle());
    		DataSourceField lastHB = new DataSourceField(KioskDSFields.LASTHB.getAlias(), FieldType.DATETIME, I18n.CST.kioskDSLastHBTitle());
    		
    		datasource.setFields(id, alias, iNetAddress, regDate, addInfos, group, lastHB);
    	}
    
    	public DataSource getDataSource() {
    		return datasource;
    	}
    }
    which work like a "proxy" for real DataSource. In this way a could define DMI via xml and optionally customize the DataSource elements (field-name, field-title, especially names that are re-used spreadingly in a lot of classes which contains data bind objects with ad-hoc field declaration) in located and easily maintenable classes.

    The code in this example isn't working, I obtain :
    Code:
    java.lang.IllegalStateException: Fields cannot be added to a DataSource after the underlying component has been created
    and I'll report only for explantion pourposes.
    I'm wonderin' if exist a method to successfully "mix" these two strategy (or if I could define "schemaBean" and "serverObject" attributes directly into DataSource java class).

    Thanks,

    Luca.

    EDIT:
    May I load .ds.xml DataSource and then inherit it and "override" fields into my java defined DataSource?
    Last edited by l.arcangeli; 15 May 2013, 07:59.

    #2
    Yes, you can do this - but you need to not load the server DataSource definition at all. Just create a client-side DataSource with the same ID.

    In this situation, the server-side framework knows only about the definitions in the .ds.xml and the client-side frameworks knows only about the definitions you supply in Java.

    There are rare cases where this makes sense, but from your description, you probably don't fall into one of those cases. If you have a range of domain objects on the server, you just want one DataSource per such domain object. You can use autoDeriveSchema to avoid any hand-creation of field definitions for those domain objects.

    Comment


      #3
      Thanks Isomorphic,
      I've changed my client-side java datasource classes:

      Code:
      public class KioskDS extends DataSource {
      
      	/**
      	 * Default constructor
      	 */
      	public KioskDS(DTPDataSourceType dsEnum) {
      		setID(dsEnum.getDsAlias());
      
      		DataSourceField id = new DataSourceField("id", FieldType.SEQUENCE);
      		id.setPrimaryKey(true);
      		id.setHidden(true);
      		id.setRequired(true);
      
      		DataSourceField alias = new DataSourceField(KioskDSFields.NAME.getAlias(), FieldType.TEXT, I18n.CST.kioskDSAliasTitle());
      		DataSourceField iNetAddress = new DataSourceField(KioskDSFields.INETADDRESS.getAlias(), FieldType.TEXT, I18n.CST.kioskDSINetAddressTitle());
      		DataSourceField regDate = new DataSourceField(KioskDSFields.REGDATE.getAlias(), FieldType.DATETIME, I18n.CST.kioskDSRegDateTitle());
      		DataSourceField addInfos = new DataSourceField(KioskDSFields.ADDINFO.getAlias(), FieldType.TEXT, I18n.CST.kioskDSAddInfoTitle(), 1000);
      		DataSourceField group = new DataSourceField(KioskDSFields.GROUP.getAlias(), FieldType.INTEGER, I18n.CST.kioskDSGroupTitle());
      		DataSourceField lastHB = new DataSourceField(KioskDSFields.LASTHB.getAlias(), FieldType.DATETIME, I18n.CST.kioskDSLastHBTitle());
      
      		setFields(id, alias, iNetAddress, regDate, addInfos, group, lastHB);
      	}
      }
      where dsEnum.getDsAlias() (in this case enum is KIOS_DS) return "kiosk" (same string ID defined in .ds.xml seen in my first post).
      I've got a "manager" that provide correct instance of DataSource to application objects (modified consequently just to manage only KIOSK_DS new mappings):

      Code:
      public class DTPDataSourceManager {
      
      	private static Map<DTPDataSourceType, DataSource> dsMap = new HashMap<DTPDataSourceType, DataSource>();
      
      	/**
      	 * @return Instance (as singleton) of associated {@link DataSource} 
      	 */
      	public static DataSource getDatasource(DTPDataSourceType datasourceEnum) {
      		if (dsMap.get(datasourceEnum) == null) {
      			switch (datasourceEnum) {
      			case KIOSK_DS:
      				DataSource dataSource = new KioskDS(datasourceEnum);
      				dsMap.put(datasourceEnum, dataSource);
      				break;
      			default:
      				DataSource dSource = DataSource.get(datasourceEnum.getDsAlias());
      				dsMap.put(datasourceEnum, dSource);
      			}
      		}
      		return dsMap.get(datasourceEnum);
      	}
      }
      but when I run application, still obtain:

      Code:
      08:43:36.986 [ERROR] [dtproviding] Unable to load module entry point class it.onetech.dtproviding.client.DTProviding (see associated exception for details)
      java.lang.IllegalStateException: Fields cannot be added to a DataSource after the underlying component has been created.
          at com.smartgwt.client.core.BaseClass.error(BaseClass.java:165)
          at com.smartgwt.client.data.DataSource.addField(DataSource.java:4009)
          at com.smartgwt.client.data.DataSource.setFields(DataSource.java:3997)
          at it.onetech.dtproviding.client.datasource.KioskDS.<init>(KioskDS.java:35)
          at it.onetech.dtproviding.client.datasource.DTPDataSourceManager.getDatasource(DTPDataSourceManager.java:25)
          at it.onetech.dtproviding.client.gui.kiosk.KioskPane.initWidgets(KioskPane.java:69)
          at it.onetech.dtproviding.client.gui.kiosk.KioskPane.<init>(KioskPane.java:44)
          at it.onetech.dtproviding.client.gui.MainRightSectionStack.initWidgetss(MainRightSectionStack.java:39)
          at it.onetech.dtproviding.client.gui.MainRightSectionStack.<init>(MainRightSectionStack.java:29)
          at it.onetech.dtproviding.client.DTProviding.onModuleLoad(DTProviding.java:44)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:601)
          at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:406)
          at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
          at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:526)
          at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
          at java.lang.Thread.run(Thread.java:722)
      and if I change DataSource istantiation (also fork KIOSK_DS) with:

      Code:
      DataSource dSource = DataSource.get(datasourceEnum.getDsAlias());
      client-side data source is not referred in any way.

      Miss I somenthing else?

      Thanks, Luca.



      EDIT:
      Latest attempt. I've modified client-side DataSource class:

      Code:
      public KioskDS(DTPDataSourceType dsEnum) {
      	setInheritsFrom(dsEnum.getDsAlias());
      	setID(dsEnum.getDsAlias());
      
      	DataSourceField id = getField(KioskDSFields.ID.getAlias());
      	id.setType(FieldType.SEQUENCE);
      	id.setPrimaryKey(true);
      	id.setHidden(true);
      	id.setRequired(true);
      
      	DataSourceField alias = getField(KioskDSFields.NAME.getAlias());
      	alias.setTitle(I18n.CST.kioskDSAliasTitle());
      	alias.setType(FieldType.TEXT);
      
      	DataSourceField iNetAddress = getField(KioskDSFields.INETADDRESS.getAlias());
      	iNetAddress.setTitle(I18n.CST.kioskDSINetAddressTitle());
      	iNetAddress.setType(FieldType.TEXT);
      
      	DataSourceField regDate = getField(KioskDSFields.REGDATE.getAlias());
      	regDate.setTitle(I18n.CST.kioskDSRegDateTitle());
      	regDate.setType(FieldType.DATETIME);
      
      	DataSourceField addInfos = getField(KioskDSFields.ADDINFO.getAlias());
      	addInfos.setTitle(I18n.CST.kioskDSAddInfoTitle());
      	addInfos.setType(FieldType.TEXT);
      	addInfos.setLength(1000);
      
      	DataSourceField group = getField(KioskDSFields.GROUP.getAlias());
      	group.setTitle(I18n.CST.kioskDSGroupTitle());
      	group.setType(FieldType.INTEGER);
      
      	DataSourceField lastHB = getField(KioskDSFields.LASTHB.getAlias());
      	lastHB.setTitle(I18n.CST.kioskDSLastHBTitle());
      	lastHB.setType(FieldType.DATETIME);
      }
      not re-instantiating and adding, but only redefining (if necessary) the fields. This seems to match my target.

      I see this messages on Developer console:
      Code:
      [ERROR] [dtproviding] - 11:40:00.279:WARN:Log:Specified ID:kioskGroup collides with the ID of an existing object '[DataSource ID:kioskGroup]'.
      [ERROR] [dtproviding] - 11:40:03.820:WARN:Log:Specified ID:kiosk collides with the ID of an existing object '[DataSource ID:kiosk]'.
      Any controindication?
      Last edited by l.arcangeli; 16 May 2013, 01:47.

      Comment


        #4
        Once again - you should not load the server-side DataSource at all if you plan to define fields on the client.

        Comment


          #5
          Originally posted by Isomorphic View Post
          Once again - you should not load the server-side DataSource at all if you plan to define fields on the client.
          Sorry Isomorphic, I was completely misunderstood the "at all" clause. I tought ti was about the "fields" specifications of server-side DataSource definitions... and not really "at all" (...ok, yes, this sounds weird...)

          I've changed what necessary (no inheritance on specific identified client-side datasources but istantiating for themselves with same ID of server-side, removed them from loading <script> in .html file) and now all works like a charm, no more warnings.

          Thanks again!

          Luca.

          Comment

          Working...
          X