Skip to content

Loading…

play2-morphia-plugin blob related Error #1

Closed
bdeveloper01 opened this Issue · 4 comments

2 participants

@bdeveloper01

Hai!

Error:

com.google.code.morphia.mapping.MappingException: Error mapping field:models.Post.picture

add.scala.html

@(postForm: Form[Post])

@import helper._

@main("addPost"){

 <h1>Add Post</h1>
 @form(routes.Application.addPostSave(),'enctype -> "multipart/form-data"){

   @inputText(postForm("title"))
   @inputFile(postForm("picture"))       
   <input type="submit" value="Created">
 }

}

Routes File

Routes

This file defines all application routes (Higher priority routes first)

Home page

GET / controllers.Application.index()
GET /addPost controllers.Application.addPost()
POST /addPost controllers.Application.addPostSave()

Map static resources from the /public folder to the /assets URL path

GET /assets/*file controllers.Assets.at(path="/public", file)

controller

package controllers;

import java.io.File;

import com.google.code.morphia.mapping.MappingException;

import leodagdag.play2morphia.Blob;
import models.Post;
import play.;
//import play.api.data.Form;
import play.api.data.Form;
import play.api.libs.MimeTypes;
import play.mvc.
;

import views.html.*;

public class Application extends Controller {

public static Result index() {
return ok(index.render("Your new application is ready."));

}

static play.data.Form postForm = form(Post.class);
public static Result addPost(){
return ok(addPost.render(postForm));
}

public static Result addPostSave(){
try{
play.mvc.Http.MultipartFormData body = request().body().asMultipartFormData();

play.mvc.Http.MultipartFormData.FilePart picture = body.getFile("picture");
File file = new File("C:/Users/Kenla_15/Pictures/thumbbig-150464.JPG");
play.data.Form filledForm = postForm.bindFromRequest();
if(filledForm.hasErrors()){
return ok(addPost.render(postForm));
}else{
System.out.println();
Post post = new Post();
post.title = filledForm.get().title;
//String mimetype = MimeTypes.forFileName(file.getName()).get();
String mimetype = "JPG";//MimeTypes.forFileName(file.getName()).get();
Blob blob = new Blob(file, mimetype);
post.picture = blob;
post.insert();
return ok(index.render("The image is created"));
}

  }catch(MappingException e){
      System.out.println();
      return ok(index.render(e.toString()));
  }   

}

}

index.scala.html

@(message: String)

@main("Welcome to Play 2.0") {

@play20.welcome(message, style = "Java")

}

I hope I made mistake. If you know where then. please help me!

Thanking you!
T.Balamanikandan

@leodagdag
Owner

Hello T.Balamanikandan,
Sorry for the delay.
I release a new version (0.0.5) wicth allows to set and get a File in GridFS.
If you want an example, I include in sample a JUnit test class which shows how to fill a Model class with a File
https://github.com/leodagdag/play2-morphia-plugin/blob/master/samples/yabe/test/models/ModelTest.java

I hope it will works for you.

F.

@bdeveloper01

Hai!

Wow That is good news. I will try... And then i getting routes file error

when i declare id field in my model

...
@Id
public ObjectId id;
.....

routes file...

POST /client_admin/showClient controllers.Client_admin.editClient(id: ObjectId)

I am getting invalid datatype error...

is it possible to import this file in routes ? then please help me

"import org.bson.types.ObjectId;"

Thanking you
T.Balamanikandan

@leodagdag
Owner

Hello,

No, I don't think you can import org.bson.types.ObjectId in the routes files.
A solution is to use a java.lang.String in your routes file

POST /client_admin/showClient controllers.Client_admin.editClient(id: String)

and convert it to org.bson.types.ObjectId, in your controller method, using ObjectId.massageToObjectId()
http://api.mongodb.org/java/1.3/com/mongodb/ObjectId.html#massageToObjectId(java.lang.Object)

F.

@bdeveloper01

Hai!

Thanks for your guidance and support

Thanking you!
Balamanikandan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.