?? toplinksubscriberdao.java
字號:
package jsfdb.model.dao;
import jsfdb.model.LoginInfo;
import jsfdb.model.Subscriber;
import jsfdb.model.ModelUtils;
import jsfdb.model.err.IncorrectPasswordException;
import jsfdb.model.err.LoginException;
import jsfdb.model.err.ProfileException;
import jsfdb.model.err.SubscribeException;
import jsfdb.model.err.UnknownSubscriberException;
import jsfdb.model.err.UnsubscribeException;
import oracle.toplink.expressions.ExpressionBuilder;
import oracle.toplink.queryframework.ReadObjectQuery;
import oracle.toplink.sessions.Session;
import oracle.toplink.sessions.UnitOfWork;
import oracle.toplink.threetier.ClientSession;
import oracle.toplink.threetier.Server;
import oracle.toplink.tools.sessionmanagement.SessionManager;
public class TopLinkSubscriberDAO implements SubscriberDAO {
private Server serverSession;
public TopLinkSubscriberDAO() {
SessionManager manager = SessionManager.getManager();
String id = ModelUtils.getResource("TopLinkSession");
ClassLoader loader = this.getClass().getClassLoader();
serverSession = (Server) manager.getSession(id, loader);
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
serverSession.logout();
SessionManager.getManager().getSessions().remove(
ModelUtils.getResource("TopLinkSession"));
}
});
}
private ClientSession acquireClientSession() {
return serverSession.acquireClientSession();
}
private UnitOfWork acquireUnitOfWork() {
return acquireClientSession().acquireUnitOfWork();
}
private Subscriber read(Session session, String email) {
ReadObjectQuery query
= new ReadObjectQuery(Subscriber.class);
ExpressionBuilder builder = new ExpressionBuilder();
query.setSelectionCriteria(
builder.get("email").equal(email));
return (Subscriber) session.executeQuery(query);
}
public Subscriber select(LoginInfo loginInfo)
throws LoginException,
UnknownSubscriberException,
IncorrectPasswordException {
Subscriber s = null;
try {
ClientSession session = acquireClientSession();
s = read(session, loginInfo.getEmail());
} catch (Exception x) {
ModelUtils.log(x);
throw new LoginException();
}
if (s == null)
throw new UnknownSubscriberException();
if (!s.getPassword().equals(loginInfo.getPassword()))
throw new IncorrectPasswordException();
return s;
}
public void insert(Subscriber subscriber)
throws SubscribeException {
try {
UnitOfWork uow = acquireUnitOfWork();
Subscriber s = new Subscriber();
ModelUtils.copy(subscriber, s);
uow.registerObject(s);
uow.commit();
} catch (Exception x) {
ModelUtils.log(x);
throw new SubscribeException();
}
}
public void update(Subscriber subscriber)
throws ProfileException {
try {
UnitOfWork uow = acquireUnitOfWork();
Subscriber s = read(uow, subscriber.getEmail());
ModelUtils.copy(subscriber, s);
uow.commit();
} catch (Exception x) {
ModelUtils.log(x);
throw new ProfileException();
}
}
public void delete(Subscriber subscriber)
throws UnsubscribeException {
try {
UnitOfWork uow = acquireUnitOfWork();
Subscriber s = read(uow, subscriber.getEmail());
uow.deleteObject(s);
uow.commit();
} catch (Exception x) {
ModelUtils.log(x);
throw new UnsubscribeException();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -