CayenneForm and FileField

6 messages Options
Embed this post
Permalink
Steve Wells-3

CayenneForm and FileField

Reply Threaded More More options
Print post
Permalink
Hi all,
 
I've only just begun using Click and so far it looks like a winner, well done guys on a simple and effective framework.  Finally things are starting to look they way they should for the end-user developer!  It takes me way back to my Delphi days.
 
As a Cayenne user one of the most appealing facets was the slick integration between the 2 frameworks.
 
I have hit my first snag trying to use a FileField that links automagically to a Cayenne object field of type byte[] (db is BLOB).  The field displays fine but as it is a Cayenne mandatory the commit fails with a Cayenne validation exception; it looks like the screen field is not wired to the Cayenne field.
 
Is my approach wrong?
 
Thank you,
 
Steve
 
 
 
Malcolm Edgar-2

Re: CayenneForm and FileField

Reply Threaded More More options
Print post
Permalink
Hi Steve,

I glad you are enjoying Click, interesting observation about Delphi.
Click actually takes some inspiration from Delphi, in terms of
components being named Controls and the control callback mechanism
(closures in modern parlance).

FileFields <input type="file"/> are a little tricky as browsers have
tight security restrictions around them to prevent them from being
used to pick up files off a users computer.

When I use FileFields I don't bind them directly to the Cayenne model
but handle this manually in the on submit event handler. You will need
to get the FileItem from the control and get the file data from that
to add to your Cayenne entity.

http://www.avoka.com/click-examples/control/file-upload.htm

regards Malcolm Edgar

On Fri, Jul 31, 2009 at 10:45 AM, Steve Wells<[hidden email]> wrote:

> Hi all,
>
> I've only just begun using Click and so far it looks like a winner, well
> done guys on a simple and effective framework.  Finally things are starting
> to look they way they should for the end-user developer!  It takes me way
> back to my Delphi days.
>
> As a Cayenne user one of the most appealing facets was the slick integration
> between the 2 frameworks.
>
> I have hit my first snag trying to use a FileField that links automagically
> to a Cayenne object field of type byte[] (db is BLOB).  The field displays
> fine but as it is a Cayenne mandatory the commit fails with a Cayenne
> validation exception; it looks like the screen field is not wired to the
> Cayenne field.
>
> Is my approach wrong?
>
> Thank you,
>
> Steve
>
>
>
Bob Schellink-2

Re: CayenneForm and FileField

Reply Threaded More More options
Print post
Permalink
In reply to this post by Steve Wells-3
Hi Steve,

Could you try the following and see if it works:

FileField myfield = new FileField("myfield") {

   public Object getValueObject() {
     if (getFileItem() != null) {
       return getFileItem().get();
     } else {
       return super.getValueObject();
     }
   }
}

Click uses the method getValueObject to retrieve the Field value which is bound
to the domain object. By default FileField.getValueObject returns a String, not
byte[] array and thus the mapping between the Field and the Domain object fails.

I think we should enhance FileField to better handle this mapping. One issue
might be that blobs uses byte[] or InputStream while clobs uses char[] or
Reader. Perhaps we should change FileField so one can specify the type of the
data, ala HiddenField.

kind regards

bob


Steve Wells wrote:

> Hi all,
>  
> I've only just begun using Click and so far it looks like a winner, well
> done guys on a simple and effective framework.  Finally things are
> starting to look they way they should for the end-user developer!  It
> takes me way back to my Delphi days.
>  
> As a Cayenne user one of the most appealing facets was the slick
> integration between the 2 frameworks.
>  
> I have hit my first snag trying to use a FileField that links
> automagically to a Cayenne object field of type byte[] (db is BLOB).  
> The field displays fine but as it is a Cayenne mandatory the commit
> fails with a Cayenne validation exception; it looks like the screen
> field is not wired to the Cayenne field.
>  
> Is my approach wrong?
>  
> Thank you,
>  
> Steve
>  
>  
>  

Bob Schellink-2

Re: CayenneForm and FileField

Reply Threaded More More options
Print post
Permalink
sabob wrote:
I think we should enhance FileField to better handle this mapping. One issue
might be that blobs uses byte[] or InputStream while clobs uses char[] or
Reader. Perhaps we should change FileField so one can specify the type of the
data, ala HiddenField.
Then again such a change might create confusion because by default
FileField.getValueObject returns the value of the field, in this case the name
of the file that was uploaded, not the *content* of the file.

bob


Steve Wells wrote:
> Hi all,
>  
> I've only just begun using Click and so far it looks like a winner, well
> done guys on a simple and effective framework.  Finally things are
> starting to look they way they should for the end-user developer!  It
> takes me way back to my Delphi days.
>  
> As a Cayenne user one of the most appealing facets was the slick
> integration between the 2 frameworks.
>  
> I have hit my first snag trying to use a FileField that links
> automagically to a Cayenne object field of type byte[] (db is BLOB).  
> The field displays fine but as it is a Cayenne mandatory the commit
> fails with a Cayenne validation exception; it looks like the screen
> field is not wired to the Cayenne field.
>  
> Is my approach wrong?
>  
> Thank you,
>  
> Steve
>  
>  
>  



Steve Wells-3

Re: CayenneForm and FileField

Reply Threaded More More options
Print post
Permalink
Thank you both for your responses.
 
Malcolm, my case is a rapid dev intranet application (quick and dirty for now) so I can trust the users (hey what trust a user!?#@).  I think it would be ideal to let the developer decide the fate of things...can the user upload certain mime tyes, file sizes etc.  At the end of it the lazy programmers dream would be to have file uploads link in to Cayenne or Hibernate et al. without too much work.  (Too much to ask? :) )
Bob, thanks.  My first test using your code worked, debugger tripped over "getFileItem().get();" and the values were inserted into DB.  So from that I could sure apply the mime type, file size logic validations.  When would "getFileItem()" not be null?
 
Oh a lot to learn for me as usual, I'll get more into this next week.
 
As an aside...before I had done too much in Click I easily implemented a SearchFormTablePage using a custom CayenneSearchForm extending the FormTable work already there.  Both still under development but quick and easy to extend your work to provide even more power with a search form.  I hope to submit something on this when it has been a little more developed. 
 
Cheers,
 
Steve

 
2009/7/31 sabob <[hidden email]>


sabob wrote:
>
>
> I think we should enhance FileField to better handle this mapping. One
> issue
> might be that blobs uses byte[] or InputStream while clobs uses char[] or
> Reader. Perhaps we should change FileField so one can specify the type of
> the
> data, ala HiddenField.
>
>

Then again such a change might create confusion because by default
FileField.getValueObject returns the value of the field, in this case the
name
of the file that was uploaded, not the *content* of the file.

bob


Steve Wells wrote:
> Hi all,
>
> I've only just begun using Click and so far it looks like a winner, well
> done guys on a simple and effective framework.  Finally things are
> starting to look they way they should for the end-user developer!  It
> takes me way back to my Delphi days.
>
> As a Cayenne user one of the most appealing facets was the slick
> integration between the 2 frameworks.
>
> I have hit my first snag trying to use a FileField that links
> automagically to a Cayenne object field of type byte[] (db is BLOB).
> The field displays fine but as it is a Cayenne mandatory the commit
> fails with a Cayenne validation exception; it looks like the screen
> field is not wired to the Cayenne field.
>
> Is my approach wrong?
>
> Thank you,
>
> Steve
>
>
>




--
View this message in context: http://n2.nabble.com/CayenneForm-and-FileField-tp3359910p3361110.html
Sent from the click-user mailing list archive at Nabble.com.

Bob Schellink-2

Re: CayenneForm and FileField

Reply Threaded More More options
Print post
Permalink
On 7/31/09, Steve Wells <[hidden email]> wrote:

> Bob, thanks.  My first test using your code worked, debugger tripped over
> "getFileItem().get();" and the values were inserted into DB.  So from that I
> could sure apply the mime type, file size logic validations.  When would
> "getFileItem()" not be null?

Field values are bound from the Field onProcess event. onProcess
delegates to bindRequestValue to set the Field value from the request
parameter.

So the fileItem will be set depending on whether a file was uploaded
or not. If no file
was uploaded during the Form submit, getFileItem() would return null.

kind regards

bob