"jsonify" a peristable entity?

78 views
Skip to first unread message

Elhanan

unread,
Jun 27, 2011, 11:14:51 AM6/27/11
to google-we...@googlegroups.com
hi..
assume i wanna be client agnostic, meaning the client for gwt app maybe gwt client,flex/sliverlight what not.
so i can't use requestFactory, and i need a standard wire protocol, so how about json?

can i use AutoBean in a servlet like so:?

        final EntityManagerFactory emf = Persistence.createEntityManagerFactory("hr");
        final EntityManager em = emf.createEntityManager();
        final String id = request.getParameter("id");
        final Employee e= em.find(Employee.class, Long.valueOf(id));
        final AutoBeanVisitor v=new AutoBeanVisitor() {
            @Override
            public boolean visitValueProperty(final String propertyName,final Object value, final PropertyContext ctx) {
                if(ctx.canSet()) {
                    try {
                        final Object field = ReflectionHelper.getField(Employee.class, e, propertyName);
                        ctx.set(field);
                    } catch (final Exception e2) {
                        System.out.println(e2);
                    }
                }
                return super.visitValueProperty(propertyName, value, ctx);
            }
        };
        final AutoBean<IEmployee> abEmployee = F.employee();
        abEmployee.accept(v);
        final Splittable encodedEmployee = AutoBeanCodex.encode(abEmployee);
        final String asString = encodedEmployee.toString();
        final PrintWriter writer = response.getWriter();
        writer.write(asString);
        writer.flush();
        writer.close();
        em.close();
        emf.close();

and then in a gwt client write like this:

final String url = "http://127.0.0.1:8888/EmployeeInfo?id=200";
        final RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));

        try {
            final Request request = builder.sendRequest(null, new RequestCallback() {
                @Override
                public void onError(final Request request, final Throwable exception) {
                    System.out.println(exception);
                }
                @Override
                public void onResponseReceived(final Request request, final Response response) {
                    if (response.getStatusCode()==Response.SC_OK) {
                        final String text = response.getText();
                        final AutoBean<IEmployee> decode = AutoBeanCodex.decode(F, IEmployee.class, text);
                        final IEmployee as = decode.as();
                        driver.edit(as);

                    } else {
                        System.out.println(response.getStatusCode());
                    }
                }
            });

        } catch (final RequestException e) {
            System.out.println(e.toString());
        }

thus i still get the benefits of a gwt editor, and not be bound to the wire protocol of requestFactory (yes i know it's also json, but it's an end-to-end thingy)

Thomas Broyer

unread,
Jun 27, 2011, 11:39:40 AM6/27/11
to google-we...@googlegroups.com
I guess you could do so yes; best to try it out, to make sure.

I just wonder why you aren't using a "wrapper bean" rather than a visitor, i.e. IEmployee abEmployee = F.employee(e); Unless Employee doesn't implement IEmployee? or you don't want to visit reference properties in this particular servlet? (in that case, you should probably change your visitor to also return false from visitReferenceProperty)

Oh, just one thing: with both AutoBean and RequestFactory, there's a memory leak on the "VM" code (not present in the compiled JavaScript) that'll only be fixed in 2.4: http://code.google.com/p/google-web-toolkit/issues/detail?id=6193

Elhanan Maayan

unread,
Jun 27, 2011, 1:08:34 PM6/27/11
to google-we...@googlegroups.com
 i allready did, and it works, i just wanted to make sure.

employee does implement IEmployee,i'm using the  visitor becouse i wanted to avoid all the boiler code for set=get stuff, this way i have a generic class. my original intent was to use jpa's meta model api's and then dynamically call autobean's setters, however i didn't find anyway to have setProperty(name) on a autobean (unlike dynabean).

i also wonder whenever i can further customize the visitation process by applying annotations on the getters, so in some cases the visitor could access other places aside from main bean. (like the dozer framework i guess).

i wonder i could also use validation framework with this auto bean..

btw on a completely different matter, what's the big idea  with GPE plugin which somehow manages to "looses" the reference to gwt sdk, each time i open eclipse , i get a huge a mount of compile errors all indicating it misses gwt libraries, even though it already references them on the build path, i then have to right click the properties, go to google node, re-select the default sdk , then it settles down.


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/vciODcchxFAJ.

To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Reply all
Reply to author
Forward
0 new messages