objectify, javascript and google endpoint - java

I am trying to figure out how integration between google endpoint, objectify and javscripts works. I made a real simple class named 'User'. All I am trying is to fetch a record from a Data Store and return this object back and use this in javascript.
However, the object does not seem to flow back properly. I do not see any details around this object using Chrome's developer tools... Any ideas? FYI, the record exists in the data store as I can see it while using the development console. I also can see the information as I've logged this info to the console as well. Thanks.
** JAVA CLASS **
package com.Backend;
import com.googlecode.objectify.annotation.*;
#Entity
public class User {
String firstName;
String lastName;
#Id String email;
public User(){
super();
}
public User(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
** GOOGLE ENDPOINT **
public class UserEndpoint {
private static final Logger LOG = Logger.getLogger(UserEndpoint.class.getName());
static {
ObjectifyService.register(User.class);
}
#ApiMethod(name = "getUser")
public User getUser() {
User u = ofy().load().type(User.class).id("johndoe#domain.com").now();
return u;
}
}

Finally found out what the issue is. It has to do with how the object was returned... It did not have any return value.

Related

Java Spring password encoding fail

I don't know why my UserEntity.java fail to encode the password when I test it in debug mode. The password appear without MD5 encoded. Else is when I click on org.springframework.security.authentication it's say:
this element has no attached Javadoc and the Javadoc could not be found in the attached source
I have springframework-core in Maven dependencies which contain the both classes of Md5PasswordEncoder.java and PasswordEncoder.java.
If this two needs a bean definition how do I create it?
package com.example.j2eeapp.domain;
import java.io.Serializable;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.authentication.encoding.PasswordEncoder;
/**
* Entity to hold application user data - first name, last name, etc.
*/
public class UserEntity implements Serializable {
private static final long serialVersionUID = 9014169812363387062L;
private String firstName;
private String lastName;
private String userName;
private String password;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
PasswordEncoder crypto = new Md5PasswordEncoder();
this.password = crypto.encodePassword(password, null);
}
}

How to return custom object in a MBean interface

I have create a custom JavaBean I want to return from my MBean method. The following is the custom JavaBean:
package org.text.jmx;
public class Person {
private firstName;
private lastName;
public Person(){
}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
The MBean interface is the following:
package org.text.jmx;
public interface TestJmxMBean {
public Person getPerson();
public void setPerson(String firstName, String lastName);
}
The class the implements the MBean:
package org.text.jmx;
public class TestJmx implements TestJmxMBean {
private Person person = new Person();
public Person getPerson() {
return person;
}
public void setPerson(String firstName, String lastName) {
person.setFirstName(firstName);
person.setLastName(lastName);
}
}
I create a server application that registers the above MBean, which is successful. I create a client application which successfully connects to the server application via JMX, but when I call the testJmx.getPerson() method from the client application is receive an error that it can't return the Person object. What am I doing wrong? It works fine is I just define the return type as as String or String[] from the TestJmx.getPerson().
In order to expose a custom object as a JMX attribute, or the return value or an operation, it must be defined as an OpenType. The usual way of doing this is to define an MXBean. I answered a similar question which should give you an idea on how to proceed.

JPA ManytoMany Relationship "JoinColumn cannot be resolved to a type" error

I am using Spring boot and trying to implement many to many relationship between User and Skill. I have a table users_skills with columns user_id and skill_id. I keep getting "JoinColumn cannot be resolved to a type" error in #JoinColumn annotations in STS when trying to implement the relationship. Below is my User class
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String email;
private String firstName;
private String lastName;
private List<Skill> skills = new ArrayList<Skill>();
protected User() {}
public User(String email,String firstName, String lastName) {
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id ;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email ;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName ;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName ;
}
#ManyToMany
#JoinTable(name="users_skills",
joinColumns={#JoinColumn(name="user_id")},
inverseJoinColumns={#JoinColumn(name="skill_id")})
public List<Skill> getSkills(){
return skills;
}
public void setSkills(List<Skill> skills) {
this.skills = skills ;
}
}
Just write this at the head of your class
import javax.persistence.JoinColumn;
Sometime eclipse doesn't show the link to import it in context menu, but it's there. Hope it will help someone.

Let user add custom fields to object "class"

I've built a Java application for, globally, mange computers database at my job. At first, I've been told that we needed a tab called 'Users', which would contain first name, last name and email. But now, the technician wants to add other fields such as address, phone, etc. He asked me if he could add himself these fields. The problem is, he's not a programmer. He wants to add these fields with a GUI. I have built the application with static fields, and here is my User Class.
public class User {
private String firstname;
private String lastname;
private String email;
private int id;
public User(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public User(int id, String firstname, String lastname, String email) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
public User(String firstname, String lastname, String email) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public String toString() {
return firstname + " " + lastname;
}
}
Now, I wonder if there's a way to modify this class using the GUI or if I have to rebuild entirely the software and stop using object classes like that. There are also classes like "Software" and "Operating Systems" that have static fields but need to be modified if necessary.
I don't know what options exactly I have to get the job done:
Let the tech modify the database and do something like "for each column in database, add this column in the GUI". (Which would cause to rebuild the entire software.)
others?
Any reads/tutorials on that kind of issues?
Thanks.
If the requirement is that a normal user should be able to add additonal fields to existing objects, propably the best way would be to store the information in a map.
So instead of:
public class User {
private String firstname;
private String lastname;
private String email;
private int id;
}
you would have:
public class User {
private int id;
private Map<String,String> properties;
public User(String firstname, String lastname) {
properties = new HashMap<String,String>();
properties.put("firstname",firstname);
properties.put("lastname",lastname);
}
etc.

Binding between Java Objects and Records

I have an embedded document based OrientDB database with records and I can't read back the saved entities. A get the right number of elements, but the attributes aren't mapped to my pojo's fields.
What am I doing wrong? Any tips?
Thanks!
OObjectDatabaseTx db = new OObjectDatabaseTx("local:db");
if (db.exists()) {
db = new OObjectDatabaseTx("local:db").open("admin", "admin");
} else {
db.create();
}
db.getEntityManager().registerEntityClass(User.class);
long cnt = db.countClass(User.class);
System.out.println(cnt); // OK
User user = db.newInstance(User.class, "Firstname", "Lastname", "email#example.com");
db.begin();
db.save(user); // it's OK, the DB contains this document, I can select it with the servers console
db.commit();
for (User usr : db.browseClass(User.class)) {
System.out.println(usr); // User object with null fields, why?
}
The User class:
public class User {
private String firstname;
private String lastname;
private String email;
public User() {
}
public User(String firstname, String lastname, String email) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Override
public String toString() {
return firstname + " " + lastname + " " + email;
}
}
Java objects retrieved directly from OrientDB (as you're doing in this example) are proxied transparently to their underlying records, allowing lazy loading of their fields. In your scenario, you'll have to use the User object get methods in order to trigger the population of the corresponding fields from the DB. The fields have default values (all null in your case) until loaded.
See also: "How It Works" in OrientDB Object2RecordJavaBinding
As referenced in the Binding section of the Orientdb docs, you must provide an empty constructor for each java object you wish to bind. Source : http://orientdb.com/docs/2.0/orientdb.wiki/Object-2-Record-Java-Binding.html

Categories

Resources