Announcement

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

    SmartGWT EE - Upload with FileItem + Hibernate + Postgres

    Hi there,
    I'm on SmartGWT EE 4.0d, nightly 2013-05-20.

    This is mi issue: I've got a databound ListGrid and a DynamicForm (bind to same datasource as ListGrid) which perform the file upload to database. At the end of DynamicForm.saveData(...) execution (upload process), I obtain the exceptions in attach (marked with _UPLOAD) and after that, when ListGrid attempt to perform a "fetch" operation, no data were loading. In attachment the exceptions (markerd with _FETCH) I got.
    Database record are inserted succesfully (checked by pgAdminIII).
    Here some of my code:

    DocumentDAO.java (DMI implementation based on Spring):
    Code:
    @Repository("documentDao")
    public class DocumentDao {
    	Logger log = new Logger(DocumentDao.class.getName());
    
    	@Autowired
    	private SessionFactory sessionFactory;
    
    	@SuppressWarnings("unchecked")
    	@Transactional(readOnly = true)
    	public DSResponse fetch(DSRequest dsRequest) throws Exception {
    		DSResponse dsResponse = new DSResponse();
    		Session hibernateSession = sessionFactory.getCurrentSession();
    		List<Document> docList = hibernateSession.getNamedQuery("Document.fetchAll").list();
    		log.info("FETCH " + docList.size() + " kiosk record");
    		dsResponse.setData(docList);
    		return dsResponse;
    	}
    
    	@Transactional(propagation = Propagation.REQUIRED)
    	public DSResponse add(DSRequest dsRequest, Document item) throws Exception {
    		DSResponse dsResponse = new DSResponse();
    		Session session = sessionFactory.getCurrentSession();
    		item.setFileName(item.getFile().getName());
    		item.setUploadDate(new Date(System.currentTimeMillis()));
    		System.out.println("TEST_SIZE:" + item.getFile().length());
    		session.saveOrUpdate(item);
    		log.info("ADD new document " + item.getFileName() + " record");
    		dsResponse.setData(item);
    		return dsResponse;
    	}
    
            [...other CRUD metod...]
    Form source:
    Code:
    public class DocumentEditForm extends DynamicForm {
    
    	private ListGrid referredGrid = null;
    	private Criteria referredGridCurrCriteria = null;
    	private final int WIDTH = 400;
    	private final int HEIGHT = 300;
    	private FileItem fileItem = null;
    	private TextAreaItem notesItem = null;
    
    	public DocumentEditForm(ListGrid referredGrid) {
    		this.referredGrid = referredGrid;
    		this.referredGridCurrCriteria = referredGrid.getCriteria();
    		setDataSource(this.referredGrid.getDataSource());
    		setWidth(WIDTH);
    		setHeight(HEIGHT);
    		setMargin(5);
    		setCellPadding(5);
    		setAutoFocus(true);
    		setUseAllDataSourceFields(false);
    		initComponents();
    	}
    
    	private void initComponents() {
    		fileItem = new FileItem(DocumentDSFields.FILE.getAlias());
    		fileItem.setColSpan(2);
    		fileItem.setWidth("100%");
    		fileItem.setTitleOrientation(TitleOrientation.TOP);
    
    		notesItem = new TextAreaItem(DocumentDSFields.NOTES.getAlias());
    		notesItem.setColSpan(2);
    		notesItem.setWidth("100%");
    		notesItem.setTitleOrientation(TitleOrientation.TOP);
    		notesItem.setLength(2048);
    
    		ButtonItem saveBtn = new ButtonItem("saveBtn", "Add");
    		saveBtn.setWidth(60);
    		saveBtn.setColSpan(2);
    		saveBtn.setIcon("[SKIN]/actions/add.png");
    		saveBtn.setAlign(Alignment.CENTER);
    		saveBtn.addClickHandler(new ClickHandler() {
    			@Override
    			public void onClick(ClickEvent event) {
    				if (validate()) {
    					saveData(new DSCallback() {
    						@Override
    						public void execute(DSResponse dsResponse, Object data, DSRequest dsRequest) {
    							resetValues();
    							referredGrid.filterData(referredGridCurrCriteria);
    						}
    					});
    				}
    			}
    		});
    		this.setFields(fileItem, notesItem, new SpacerItem(), saveBtn);
    	}
    }
    Client datasource definition:
    Code:
    public class DocumentDS extends DataSource {
    
    	private DataSourceField id = null;
    	private DataSourceField filename = null;
    	private DataSourceField uploadDate = null;
    	private DataSourceField notes = null;
    	private DataSourceField file = null;
    
    	/**
    	 * Default constructor
    	 */
    	public DocumentDS(String dsAlias) {
    		setID(dsAlias);
    
    		id = new DataSourceField(DocumentDSFields.ID.getAlias(), FieldType.SEQUENCE);
    		id.setPrimaryKey(true);
    		id.setRequired(true);
    		id.setHidden(true);
    
    		filename = new DataSourceField(DocumentDSFields.FILENAME.getAlias(), FieldType.TEXT);
    		filename.setTitle(I18n.CST.documentFilenameLbl());
    
    		uploadDate = new DataSourceField(DocumentDSFields.UPLOAD_DATE.getAlias(), FieldType.DATETIME);
    		uploadDate.setTitle(I18n.CST.docuemtnUploadDateLbl());
    
    		notes = new DataSourceField(DocumentDSFields.NOTES.getAlias(), FieldType.TEXT);
    		notes.setTitle(I18n.CST.docuemtnNotesLbl());
    		notes.setLength(2048);
    
    		file = new DataSourceField(DocumentDSFields.FILE.getAlias(), FieldType.BINARY);
    
    		setFields(id, filename, uploadDate, notes, file);
    	}
    }
    Server (.ds.xml) datasource definition:
    Code:
    <DataSource ID="dpDocument" schemaBean="it.onetech.dtproviding.server.entity.Document">
    	<serverObject lookupStyle="spring" bean="documentDao" />
    </DataSource>
    Hibernate entity:
    Code:
    @Entity
    @Table(name = "document")
    @NamedQueries({
    		@NamedQuery(name = "Document.fetchAll", query = "from Document order by fileName")
    })
    public class Document implements Serializable {
    
    	private static final long serialVersionUID = 1182313769904090209L;
    
    	@Id
    	@SequenceGenerator(name = "idSeq", sequenceName = "document_id_seq")
    	@GeneratedValue(generator = "idSeq")
    	protected Long id = null;
    
    	@Column(name = "filename")
    	protected String fileName = null;
    
    	@Column(name = "upload_gmt")
    	protected Date uploadDate = null;
    
    	@Column(name = "add_info")
    	protected String notes = null;
    
    	@Column(name = "file")
    	protected File file = null;
    
            [...getter and setter...]
    and the Postgresql table definition (binary file going into bytea column):
    Code:
    CREATE TABLE dtproviding.document
    (
      id bigserial NOT NULL,
      filename character varying(255),
      upload_gmt timestamp without time zone,
      add_info character varying(2048),
      file bytea,
      CONSTRAINT document_pkey PRIMARY KEY (id)
    )
    Any suggestion?

    Thanks, Luca.
    Attached Files
    Last edited by l.arcangeli; 31 May 2013, 07:41. Reason: Typo correction

    #2
    Hello Luca,

    This indicates that you're getting runaway serialization of your Document bean, probably due to some subobject that it is holding onto, possibly the File object.

    You can avoid this by fixing your DataSource definition - and you are currently doing work that you don't need to. Instead of using new DataSource() to create a parallel client-side DataSource, just declare <fields> in your .ds.xml file, then load the DataSource via the DataSourceLoader servlet or via DataSource.load() (this is covered in the QuickStart Guide where DataSources are introduced).

    Once you've declared the file field as type "binary" you'll avoid this problem.

    Comment


      #3
      Originally posted by Isomorphic View Post
      Hello Luca,

      This indicates that you're getting runaway serialization of your Document bean, probably due to some subobject that it is holding onto, possibly the File object.

      You can avoid this by fixing your DataSource definition - and you are currently doing work that you don't need to. Instead of using new DataSource() to create a parallel client-side DataSource, just declare <fields> in your .ds.xml file, then load the DataSource via the DataSourceLoader servlet or via DataSource.load() (this is covered in the QuickStart Guide where DataSources are introduced).

      Once you've declared the file field as type "binary" you'll avoid this problem.
      Hi Isomorphic,
      Thanks for so-fast answer.
      I worked on a refactoring that not involve Files object but byte array, tryin' to solve it without any other intervention. As of DataSource istantiation, I know I'm doing it in not-standard method, but due application architecture I need to follow this way (...we discussed this in another thread some times ago... :) )

      ATM I persist and retrieve data with no more exceptions. Now I'm trying to adjust my code (following binary field javadoc) to have a "showable" and "dowlodable" field into ListGrid (now is blank, but I was ignoring all metadata section and specification)

      Luca.

      Comment


        #4
        Hi again,
        After previously mentioned refactoring, all is ok. I've got a full-functionally file upload, download and ListGrid view.

        Only a question: I don't need to "show" files online, so I'm wondering if there is a way to hide (or disable) the "magnifying glass" icon showed by ListGrid binary field.

        I read some thread about this and seems impossible (at least in older release).
        Is something changed or workaround?

        Thanks, Luca.

        Comment


          #5
          At the moment, no, there is no way to suppress the built-in view icon while retaining the download icon.

          But by declaring your own field of type "icon" and calling DataSource.downloadFile() you can quickly replicate the built-in support for a donwload icon.

          Comment


            #6
            Originally posted by Isomorphic View Post
            At the moment, no, there is no way to suppress the built-in view icon while retaining the download icon.

            But by declaring your own field of type "icon" and calling DataSource.downloadFile() you can quickly replicate the built-in support for a donwload icon.
            Yes,
            So I (also) don't need to showing any "binary" Field into my ListGrid.

            Thanks again,

            Luca.

            Comment

            Working...
            X