/*
* Spadger - an open source discussion forum system.
*
* Copyright (C) 2009 The Spadger Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
*
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.ajiaojr.spadger.server.entity;
import java.io.Serializable;
import java.util.Date;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.persistence.Transient;
import net.ajiaojr.spadger.server.util.PMF;
import com.google.appengine.api.datastore.Text;
import com.google.gson.annotations.Expose;
/**
* Spadger user information. Spadger users are identified by a unique user ID.
* Users are allowed to change email address as they see fit, however, the
* unique user ID will always remain the same regardless of the email.
*
* @author The Spadger Team
*/
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class SpadgerUser implements Serializable,
net.ajiaojr.spadger.shared.entity.SpadgerUser {
private static final long serialVersionUID = 6120246852958797435L;
public static SpadgerUser findSpadgerUser(String id) {
SpadgerUser user = null;
PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
user = pm.getObjectById(SpadgerUser.class, id);
} catch (JDOObjectNotFoundException e) {
user = new SpadgerUser();
user.id = id;
user.email = id;
user = pm.makePersistent(user);
user = pm.detachCopy(user);
} finally {
tx.commit();
pm.close();
}
return user;
}
@Persistent
private Date birthday;
@Persistent
private boolean disabled = false;
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private String id;
@Expose
@Persistent
private String email;
@Expose
@Persistent
private String nickname;
@Expose
@Persistent
private Text signature;
/**
* Returns the birthday of the user.
*
* @return the birthday of the user.
*/
public Date getBirthday() {
return birthday;
}
/**
* Returns the email address of the user.
*
* @return the email address of the user.
*/
@Override
public String getEmail() {
return email;
}
/*
* (non-Javadoc)
*
* @see net.ajiaojr.spadger.shared.entity.SpadgerUser#getId()
*/
@Override
public String getId() {
return id;
}
/**
* Returns the nick name of the user.
*
* @return the nick name of the user.
*/
public String getNickname() {
return nickname;
}
/**
* Returns the user's signature.
*
* @return the user's signature.
*/
public String getSignature() {
return signature.getValue();
}
@Transient
public Integer getVersion() {
return 0;
}
/**
* Returns whether this user account is disabled.
*
* @return whether this user account is disabled.
*/
public boolean isDisabled() {
return disabled;
}
/**
* Sets the user's birthday.
*
* @param birthday
* the user's birthday.
*/
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
/**
* Sets whether this user account is disabled.
*
* @param disabled
* whether the user account is disabled.
*/
public void setDisabled(boolean disabled) {
this.disabled = disabled;
}
/**
* Sets the email address of the user.
*
* @param email
* the email address of the user.
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Sets the nick name of the user.
*
* @param nickname
* the nick name of the user.
*/
public void setNickname(String nickname) {
this.nickname = nickname;
}
/**
* Sets the signature of the user.
*
* @param signature
* the signature of the user.
*/
public void setSignature(String signature) {
this.signature = new Text(signature);
}
}