Alter GORM database column - grails

I have a ImageController function that takes input data from a form.gsp file and adds it to a Profile class. The Profile class has a byte[] photo field, and using a MultiPart HTTP Request transferTo() method, the file is replicated in the database if size is < 200KB.
My issue is each time I try to upload, I get a SQLException stating that the input value is too large for a PHOTO_BINARY(255) column.
First, why is a byte[] array mapped to a column limiter? To move around this, would byte[] photo= new byte[200*1024] work?
Second, what is necessary to alter said column to a medium blob of 16777215 characters?

Use the maxSize constraint, e.g.
static constraints = {
photo maxSize: 16777215
}

Try setting the type of the column to blob in the mappings.
static mapping = {
photo type: "binary" // or "blob"
}
Hope that helps

Related

Attach file to data row

I am trying to attach a text file to a data row in my custom object. I must be missing something. I have the pointer to the current record (asn) and the Byte array (retLabels.Labels) but I can't figure out what the third parameter should be. Also, do I need to execute an update and save after attaching the file?
if (retLabels.Code == "OK" || ediDemo)
{
asnGraph.ASN.Current = asn;
PXNoteAttribute.AttachFile(asn, retLabels.Labels, ???? PX.SM.FileInfo );
}
Create the file in memory:
PX.SM.FileInfo file = new PX.SM.FileInfo("textfile.txt",
null,
Encoding.UTF8.GetBytes("Text file content."));
Upload the file in Acumatica:
UploadFileMaintenance upload = PXGraph.CreateInstance<UploadFileMaintenance>();
upload.SaveFile(file,
FileExistsAction.CreateVersion);
Attach the file to any DAC records by linking file UID (unique ID) to DAC NoteID field:
PXNoteAttribute.SetFileNotes(Base.Caches[typeof(DAC)], dacRecord, file.UID.Value);

MvvmCross ImageView Binding to Byte Array

I am currently trying to bind a Android ImageView to a Byte array that I have setup as my ViewModel.
private Byte[] _currentActivityImage;
public Byte[] CurrentActivityImage
{
get { return _currentActivityImage; }
set { _currentActivityImage = value; RaisePropertyChanged(() => CurrentActivityImage); }
}
I am doing it this way as I am downloading the image as part of the large data download and storing that image data into the database so it's tired directly to a specific record in the table.
I haven't been able to find any information on specifically how to bind the Android ImageView (Or the MvxImageView) to a Byte array or a raw bitmap image.
I saw in one of the posts that Stuart mentioned that capability exists (or maybe I read it wrong.), but was not able to find the correct bindings to use.
(Note: if need be, I can change the property to use Bitmap, but figured that the Byte array might be more cross-platform compatible.)
The picture choose sample shows one way to do this - https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/PictureTaking
The N+1 N=12,etc CollectABull sample shows another - going via a file https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/tree/master/N-12-CollectABull/

Display byte array in gsp

I want to loop through this class and display in a table. what is the easiest way to display the image?
class Crop {
static hasMany = [diseases: Disease]
int id
String commonName
String scientificName
byte[] image
}
static mapping = {
table: 'Crops'
commonName length : 100
scientificName length: 100
image sqlType: "longblob"
}
You can if you don't mind the lack of browser compatablity use data: uri encoding:
<img src="data:image/png;base64,${crop.image.encodeBase64()}"/>
see the Wikipedia page on Data URI encoding scheme
#Xeon suggest the more conventional approach, ie create a controller that returns an image response with the correct mime type and the byte array as the body.
You could do the same thing as in this answer.
Or you can create controller which provide image data stream from your entity. On GSP you could write:
<img src="${request.contextPath}/imageController/actionName?id=${entity.id}" ...
But this is unusual - storing images in entities as byte[]. You should consider changing it to String which would indicate a path/filename of the image.

Override Grails Error Messages to format Dates and Numbers

I have created a domain with a Double field. When the validation occurs it throws the error message with size value showing the number with commas. Following are the detials
Groovy Class
class Quote {
String content;
Double size;
static constraints = {
content(maxSize:1000, blank:false)
size(min: 0.00D, max:999.99D)
}
}
Value entered "11111", error obtained "Size 11,111 is exceeded the limit". I have added the property key/value pair in messages.properties.
Here, I would like to get the message back without the commas. My main aim is to take the key and format the message returned based on my requirements. I require this as I have other fields that need conversion. For example, a date is validated but when showing the error the Gregorian date needs to be converted to an Islamic date and shown to user.
Does anyone know if I can do something to make this work.
I have tried the solution provided in http://ishanf.tumblr.com/post/434379583/custom-property-editor-for-grails but this did not work.
I have also tried modifying the messages values, but this is not flexible in case of my date issue. Example, for a key value pair, instead of using {2} as a place holder I could use {2, date, mm/dd/yyyy}, but for Islamic dates I want to format and show things differently.
Also, please note I have created a separate key for default date formatting for my application.
Would appreciate the help.
In grails, the return of a constrain is an already translated string.
You can create a taglib to format that, or enhance the
Another option would be custom validators. A custom validator can return false or a key when failing.
For example in your domain class, to vaildate a field:
myDateField validator: {val, obj -> obj.myShinyDateValidatorMethod(val) }
private myShinyDateValidatorMethod() {
if (isNotValidDate(val) {
return [the_message_key, val.formatedAsYouWand]
}
}
and, in your properties file you have to have defined the key:
the_message_key=This date: {3} is not valid
The trick here is that in the return from the validator, first string is the key and the rest are parameters for that key, but grails already uses {0}, {1}, {2} placeholders for className, fieldName and value, and the first parameter that you pass will be used as {3} placeholder.
Hope this helps

Sending javaMail attachement of any type from database

I'm have domain class with property that represents files uploaded on my GSP. I've defined that file as byte array (byte [] file). When some specific action happens I'm sending mail with attachments from. This is part of my SendMail service:
int i = 1;
[requestInstance.picture1, requestInstance.picture2, requestInstance.picture3].each(){
if(it.length != 0){
DataSource image = new ByteArrayDataSource(it, "image/jpeg");
helper.addAttachment("image" + i + ".jpg", image);
i++;
}
}
This works fine with image files. But now I want to be able to work with all file types and I'm wondering how to implement this. Also, I want to save real file name in database. All help is welcomed.
You can see where the file name and MIME type are specified in your code. It should be straightforward to save and restore that information from your database along with the attachment data.
If you're trying to figure out from the byte array of data what the MIME type is and what a good filename would be, that's a harder problem. Try this.

Resources