本文整理匯總了Java中com.google.appengine.api.datastore.DatastoreService.beginTransaction方法的典型用法代碼示例。如果您正苦於以下問題:Java DatastoreService.beginTransaction方法的具體用法?Java DatastoreService.beginTransaction怎麽用?Java DatastoreService.beginTransaction使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.appengine.api.datastore.DatastoreService
的用法示例。
在下文中一共展示了DatastoreService.beginTransaction方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: creatingAnEntityInASpecificEntityGroup
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Test
public void creatingAnEntityInASpecificEntityGroup() throws Exception {
String boardName = "my-message-board";
//CHECKSTYLE.OFF: VariableDeclarationUsageDistance - Increased clarity in sample
// [START creating_an_entity_in_a_specific_entity_group]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
String messageTitle = "Some Title";
String messageText = "Some message.";
Date postDate = new Date();
Transaction txn = datastore.beginTransaction();
Key messageBoardKey = KeyFactory.createKey("MessageBoard", boardName);
Entity message = new Entity("Message", messageBoardKey);
message.setProperty("message_title", messageTitle);
message.setProperty("message_text", messageText);
message.setProperty("post_date", postDate);
datastore.put(txn, message);
txn.commit();
// [END creating_an_entity_in_a_specific_entity_group]
//CHECKSTYLE.ON: VariableDeclarationUsageDistance
}
示例2: crossGroupTransactions
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Test
public void crossGroupTransactions() throws Exception {
// [START cross-group_XG_transactions_using_the_Java_low-level_API]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
TransactionOptions options = TransactionOptions.Builder.withXG(true);
Transaction txn = datastore.beginTransaction(options);
Entity a = new Entity("A");
a.setProperty("a", 22);
datastore.put(txn, a);
Entity b = new Entity("B");
b.setProperty("b", 11);
datastore.put(txn, b);
txn.commit();
// [END cross-group_XG_transactions_using_the_Java_low-level_API]
}
示例3: creatingAnEntityInASpecificEntityGroup
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Test
public void creatingAnEntityInASpecificEntityGroup() throws Exception {
String boardName = "my-message-board";
// [START creating_an_entity_in_a_specific_entity_group]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
String messageTitle = "Some Title";
String messageText = "Some message.";
Date postDate = new Date();
Key messageBoardKey = KeyFactory.createKey("MessageBoard", boardName);
Entity message = new Entity("Message", messageBoardKey);
message.setProperty("message_title", messageTitle);
message.setProperty("message_text", messageText);
message.setProperty("post_date", postDate);
Transaction txn = datastore.beginTransaction();
datastore.put(txn, message);
txn.commit();
// [END creating_an_entity_in_a_specific_entity_group]
}
示例4: doPost
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
UserPermissionsBean userPermissionsBean = (UserPermissionsBean)req.getAttribute(AuthRequestAttribute.USER_PERMISSIONS.getName());
if(userPermissionsBean.getChangePassword()) {
Error error = Error.NONE;
UserBean userBean = (UserBean)req.getAttribute(AuthRequestAttribute.USER.getName());
String currentPassword = req.getParameter(HtmlVariable.CURRENT_PASSWORD.getName());
String newPassword = req.getParameter(HtmlVariable.NEW_PASSWORD.getName());
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
User user = UserFactory.getByKey(datastore, txn, userBean.getKey());
if(user == null)
error = Error.NON_EXISTENT_USER;
else if(user.getPassword().compareTo(MiscFunctions.getEncryptedHash(currentPassword, HashAlgorithms.SHA_256)) != 0)
error = Error.INCORRECT_PASSWORD;
else
try {
user.setPassword(newPassword);
UserFactory.update(datastore, txn, user);
txn.commit();
}
catch(ConcurrentModificationException e) {
error = Error.ERROR_IN_CHANGE_PASSWORD;
}
if(error != Error.NONE && txn.isActive())
txn.rollback();
req.setAttribute(HtmlVariable.ERROR.getName(), error.toString());
req.getRequestDispatcher("/WEB-INF/auth/change-password.jsp").forward(req, resp);
}
else
resp.sendRedirect("/auth/settings");
}
示例5: transactionalTaskEnqueuing
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Test
public void transactionalTaskEnqueuing() throws Exception {
// [START transactional_task_enqueuing]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Queue queue = QueueFactory.getDefaultQueue();
Transaction txn = datastore.beginTransaction();
// ...
queue.add(TaskOptions.Builder.withUrl("/path/to/handler"));
// ...
txn.commit();
// [END transactional_task_enqueuing]
}
示例6: transactionalTaskEnqueuing
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Test
public void transactionalTaskEnqueuing() throws Exception {
// [START transactional_task_enqueuing]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Queue queue = QueueFactory.getDefaultQueue();
Transaction txn = datastore.beginTransaction();
// ...
queue.add(txn, TaskOptions.Builder.withUrl("/path/to/handler"));
// ...
txn.commit();
// [END transactional_task_enqueuing]
}
示例7: doGet
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Error error = Error.NONE;
CauseOfDisconnect causeOfDisconnect = CauseOfDisconnect.NONE;
boolean fromSessionLog = false;
String sessionId = null;
Cookie cookies[] = req.getCookies();
if(cookies != null) for(Cookie cookie : cookies)
if(cookie.getName().equalsIgnoreCase(Cookies.SESSION_ID.getName())) {
sessionId = cookie.getValue();
break;
}
if(sessionId == null || sessionId.length() == 0)
error = Error.NON_EXISTENT_SESSION;
else {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction(TransactionOptions.Builder.withXG(true));
Session session = SessionFactory.getBySessionId(datastore, txn, sessionId);
if(session != null) {
Calendar dateNow = Calendar.getInstance(java.util.TimeZone.getTimeZone(MiscFunctions.UTC_STRING));
long sessionTimeout = session.getSessionTimeout();
if(sessionTimeout > 0) {
Calendar lastSessionCheck = MiscFunctions.toCalendar(session.getLastSessionCheck());
lastSessionCheck.add(Calendar.SECOND, (int)sessionTimeout);
if(lastSessionCheck.compareTo(dateNow) < 0)
causeOfDisconnect = CauseOfDisconnect.TIMED_OUT_SESSION;
}
try {
SessionFactory.disconnect(datastore, txn, session, dateNow.getTime(), causeOfDisconnect);
txn.commit();
}
catch(ConcurrentModificationException e) {
error = Error.ERROR_IN_SIGN_OUT;
}
}
else {
SessionLog sessionLog = SessionLogFactory.getBySessionId(datastore, txn, sessionId);
if(sessionLog != null) {
causeOfDisconnect = sessionLog.getCauseOfDisconnect();
fromSessionLog = true;
}
else
error = Error.NON_EXISTENT_SESSION;
}
// delete session id cookie
Cookie sessionIdCookie = new Cookie(Cookies.SESSION_ID.getName(), "");
sessionIdCookie.setMaxAge(0);
sessionIdCookie.setPath(Cookies.SESSION_ID_PATH);
resp.addCookie(sessionIdCookie);
if((error != Error.NONE || fromSessionLog) && txn.isActive())
txn.rollback();
}
StringBuilder urlParams = new StringBuilder("/utils/sign-in?");
urlParams.append(HtmlVariable.ERROR.getName());
urlParams.append("=");
urlParams.append(error.toString());
urlParams.append("&");
urlParams.append(HtmlVariable.CAUSE_OF_DISCONNECT.getName());
urlParams.append("=");
urlParams.append(causeOfDisconnect.toString());
resp.sendRedirect(urlParams.toString());
}
示例8: doPost
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
UserPermissionsBean userPermissionsBean = (UserPermissionsBean)req.getAttribute(AuthRequestAttribute.USER_PERMISSIONS.getName());
if(userPermissionsBean.getViewSessions() && userPermissionsBean.getDisconnectSessions()) {
String[] sessionIds = req.getParameterValues(HtmlVariable.SESSION_ID.getName() + "[]");
Map<String, Boolean> disconnectResults = new HashMap<String, Boolean>();
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Calendar dateNow = Calendar.getInstance(java.util.TimeZone.getTimeZone(MiscFunctions.UTC_STRING));
for(int i = 0; i < sessionIds.length; ++i) {
Error error = Error.NONE;
CauseOfDisconnect causeOfDisconnect = CauseOfDisconnect.DISCONNECTED_SESSION;
Transaction txn = datastore.beginTransaction(TransactionOptions.Builder.withXG(true));
Session session = SessionFactory.getBySessionId(datastore, txn, sessionIds[i]);
if(session != null) {
long sessionTimeout = session.getSessionTimeout();
if(sessionTimeout > 0) {
Calendar lastSessionCheck = MiscFunctions.toCalendar(session.getLastSessionCheck());
lastSessionCheck.add(Calendar.SECOND, (int)sessionTimeout);
if(lastSessionCheck.compareTo(dateNow) < 0)
causeOfDisconnect = CauseOfDisconnect.TIMED_OUT_SESSION;
}
try {
SessionFactory.disconnect(datastore, txn, session, dateNow.getTime(), causeOfDisconnect);
txn.commit();
disconnectResults.put(sessionIds[i], true);
}
catch(ConcurrentModificationException e) {
error = Error.ERROR_IN_SESSIONS;
}
}
else {
error = Error.NON_EXISTENT_SESSION;
disconnectResults.put(sessionIds[i], false);
}
if(error != Error.NONE && txn.isActive())
txn.rollback();
}
StringBuilder responseJson = new StringBuilder("{ ");
for (Map.Entry<String, Boolean> entry : disconnectResults.entrySet()) {
responseJson.append("\"");
responseJson.append(entry.getKey());
responseJson.append("\" : ");
responseJson.append(entry.getValue().toString());
responseJson.append(", ");
}
if(disconnectResults.size() > 0)
responseJson.delete(responseJson.length() - 2, responseJson.length());
responseJson.append(" }");
resp.setContentType("application/json; charset=UTF-8");
resp.getWriter().print(responseJson.toString());
}
else
resp.sendRedirect("/auth/settings");
}
示例9: doPost
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
UserPermissionsBean userPermissionsBean = (UserPermissionsBean)req.getAttribute(AuthRequestAttribute.USER_PERMISSIONS.getName());
if(userPermissionsBean.getViewSessions() && userPermissionsBean.getViewSessionPermissions()) {
if(userPermissionsBean.getChangeSessionPermissions()) {
long sessionPermissionsCode = 0;
if(req.getParameter(HtmlVariable.VIEW_SESSIONS.getName()) != null)
sessionPermissionsCode = sessionPermissionsCode + SessionPermissions.VIEW_SESSIONS.getCode();
if(req.getParameter(HtmlVariable.DISCONNECT_SESSIONS.getName()) != null)
sessionPermissionsCode = sessionPermissionsCode + SessionPermissions.DISCONNECT_SESSIONS.getCode();
if(req.getParameter(HtmlVariable.VIEW_SESSION_PERMISSIONS.getName()) != null)
sessionPermissionsCode = sessionPermissionsCode + SessionPermissions.VIEW_SESSION_PERMISSIONS.getCode();
if(req.getParameter(HtmlVariable.CHANGE_SESSION_PERMISSIONS.getName()) != null)
sessionPermissionsCode = sessionPermissionsCode + SessionPermissions.CHANGE_SESSION_PERMISSIONS.getCode();
Error error = Error.NONE;
UserBean userBean = (UserBean)req.getAttribute(AuthRequestAttribute.USER.getName());
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction(TransactionOptions.Builder.withXG(true));
User user = UserFactory.getByKey(datastore, txn, userBean.getKey());
UserPermissions userPermissions = null;
if(user == null)
error = Error.NON_EXISTENT_USER;
else if((userPermissions = PermissionsFactory.getUserPermissionsByUserId(datastore, txn, user.getUserId())) == null)
error = Error.NO_PERMISSIONS;
else
try {
userPermissions.setSessionPermissions(sessionPermissionsCode);
PermissionsFactory.updateUserPermissions(datastore, txn, userPermissions);
txn.commit();
userPermissionsBean = new UserPermissionsBean(userPermissions);
req.setAttribute(AuthRequestAttribute.USER_PERMISSIONS.getName(), userPermissionsBean);
}
catch(ConcurrentModificationException e) {
error = Error.ERROR_IN_SESSION_PERMISSIONS;
}
if(error != Error.NONE && txn.isActive())
txn.rollback();
if(userPermissionsBean.getViewSessions() && userPermissionsBean.getViewSessionPermissions()) {
req.setAttribute(HtmlVariable.ERROR.getName(), error.toString());
req.getRequestDispatcher("/WEB-INF/auth/session-permissions.jsp").forward(req, resp);
}
else
resp.sendRedirect("/auth/settings");
}
else
req.getRequestDispatcher("/WEB-INF/auth/session-permissions.jsp").forward(req, resp);
}
else
resp.sendRedirect("/auth/settings");
}
示例10: doPost
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
UserPermissionsBean userPermissionsBean = (UserPermissionsBean)req.getAttribute(AuthRequestAttribute.USER_PERMISSIONS.getName());
if(userPermissionsBean.getViewSessionLogs() && userPermissionsBean.getRemoveSessionLogs()) {
String[] sessionIds = req.getParameterValues(HtmlVariable.SESSION_ID.getName() + "[]");
Map<String, Boolean> removeResults = new HashMap<String, Boolean>();
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
for(int i = 0; i < sessionIds.length; ++i) {
Error error = Error.NONE;
Transaction txn = datastore.beginTransaction();
SessionLog sessionLog = SessionLogFactory.getBySessionId(datastore, txn, sessionIds[i]);
if(sessionLog != null) {
try {
SessionLogFactory.remove(datastore, txn, sessionLog);
txn.commit();
removeResults.put(sessionIds[i], true);
}
catch(ConcurrentModificationException e) {
error = Error.ERROR_IN_SESSION_LOGS;
}
}
else {
error = Error.NON_EXISTENT_SESSION_LOG;
removeResults.put(sessionIds[i], false);
}
if(error != Error.NONE && txn.isActive())
txn.rollback();
}
StringBuilder responseJson = new StringBuilder("{ ");
for (Map.Entry<String, Boolean> entry : removeResults.entrySet()) {
responseJson.append("\"");
responseJson.append(entry.getKey());
responseJson.append("\" : ");
responseJson.append(entry.getValue().toString());
responseJson.append(", ");
}
if(removeResults.size() > 0)
responseJson.delete(responseJson.length() - 2, responseJson.length());
responseJson.append(" }");
resp.setContentType("application/json; charset=UTF-8");
resp.getWriter().print(responseJson.toString());
}
else
resp.sendRedirect("/auth/settings");
}
示例11: doPost
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
UserPermissionsBean userPermissionsBean = (UserPermissionsBean)req.getAttribute(AuthRequestAttribute.USER_PERMISSIONS.getName());
if(userPermissionsBean.getViewUserPermissions() && userPermissionsBean.getChangeUserPermissions()) {
long userPermissionsCode = 0;
if(req.getParameter(HtmlVariable.CHANGE_PASSWORD.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.CHANGE_PASSWORD.getCode();
if(req.getParameter(HtmlVariable.VIEW_EMAIL_ADDRESS.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.VIEW_EMAIL_ADDRESS.getCode();
if(req.getParameter(HtmlVariable.CHANGE_EMAIL_ADDRESS.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.CHANGE_EMAIL_ADDRESS.getCode();
if(req.getParameter(HtmlVariable.VIEW_TIME_ZONE.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.VIEW_TIME_ZONE.getCode();
if(req.getParameter(HtmlVariable.CHANGE_TIME_ZONE.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.CHANGE_TIME_ZONE.getCode();
if(req.getParameter(HtmlVariable.VIEW_SESSION_TIMEOUT.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.VIEW_SESSION_TIMEOUT.getCode();
if(req.getParameter(HtmlVariable.CHANGE_SESSION_TIMEOUT.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.CHANGE_SESSION_TIMEOUT.getCode();
if(req.getParameter(HtmlVariable.VIEW_MAX_SESSIONS.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.VIEW_MAX_SESSIONS.getCode();
if(req.getParameter(HtmlVariable.CHANGE_MAX_SESSIONS.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.CHANGE_MAX_SESSIONS.getCode();
if(req.getParameter(HtmlVariable.VIEW_EXCLUSIVE_SESSION.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.VIEW_EXCLUSIVE_SESSION.getCode();
if(req.getParameter(HtmlVariable.CHANGE_EXCLUSIVE_SESSION.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.CHANGE_EXCLUSIVE_SESSION.getCode();
if(req.getParameter(HtmlVariable.VIEW_MAX_FAILED_ATTEMPTS.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.VIEW_MAX_FAILED_ATTEMPTS.getCode();
if(req.getParameter(HtmlVariable.CHANGE_MAX_FAILED_ATTEMPTS.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.CHANGE_MAX_FAILED_ATTEMPTS.getCode();
if(req.getParameter(HtmlVariable.VIEW_DISABLED_STATUS.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.VIEW_DISABLED_STATUS.getCode();
if(req.getParameter(HtmlVariable.VIEW_ACTIVATED_STATUS.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.VIEW_ACTIVATED_STATUS.getCode();
if(req.getParameter(HtmlVariable.VIEW_USER_PERMISSIONS.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.VIEW_USER_PERMISSIONS.getCode();
if(req.getParameter(HtmlVariable.CHANGE_USER_PERMISSIONS.getName()) != null)
userPermissionsCode = userPermissionsCode + UserPermissions2.CHANGE_USER_PERMISSIONS.getCode();
Error error = Error.NONE;
UserBean userBean = (UserBean)req.getAttribute(AuthRequestAttribute.USER.getName());
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction(TransactionOptions.Builder.withXG(true));
User user = UserFactory.getByKey(datastore, txn, userBean.getKey());
UserPermissions userPermissions = null;
if(user == null)
error = Error.NON_EXISTENT_USER;
else if((userPermissions = PermissionsFactory.getUserPermissionsByUserId(datastore, txn, user.getUserId())) == null)
error = Error.NO_PERMISSIONS;
else
try {
userPermissions.setUserPermissions(userPermissionsCode);
PermissionsFactory.updateUserPermissions(datastore, txn, userPermissions);
txn.commit();
userPermissionsBean = new UserPermissionsBean(userPermissions);
req.setAttribute(AuthRequestAttribute.USER_PERMISSIONS.getName(), userPermissionsBean);
}
catch(ConcurrentModificationException e) {
error = Error.ERROR_IN_USER_PERMISSIONS;
}
if(error != Error.NONE && txn.isActive())
txn.rollback();
if(userPermissionsBean.getViewUserPermissions()) {
req.setAttribute(HtmlVariable.ERROR.getName(), error.toString());
req.getRequestDispatcher("/WEB-INF/auth/user-permissions.jsp").forward(req, resp);
}
else
resp.sendRedirect("/auth/settings");
}
else
resp.sendRedirect("/auth/settings");
}
示例12: doPost
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
UserPermissionsBean userPermissionsBean = (UserPermissionsBean)req.getAttribute(AuthRequestAttribute.USER_PERMISSIONS.getName());
if(userPermissionsBean.getViewSessionLogs() && userPermissionsBean.getViewSessionLogPermissions()) {
if(userPermissionsBean.getChangeSessionLogPermissions()) {
long sessionLogPermissionsCode = 0;
if(req.getParameter(HtmlVariable.VIEW_SESSION_LOGS.getName()) != null)
sessionLogPermissionsCode = sessionLogPermissionsCode + SessionLogPermissions.VIEW_SESSION_LOGS.getCode();
if(req.getParameter(HtmlVariable.REMOVE_SESSION_LOGS.getName()) != null)
sessionLogPermissionsCode = sessionLogPermissionsCode + SessionLogPermissions.REMOVE_SESSION_LOGS.getCode();
if(req.getParameter(HtmlVariable.VIEW_SESSION_LOG_PERMISSIONS.getName()) != null)
sessionLogPermissionsCode = sessionLogPermissionsCode + SessionLogPermissions.VIEW_SESSION_LOG_PERMISSIONS.getCode();
if(req.getParameter(HtmlVariable.CHANGE_SESSION_LOG_PERMISSIONS.getName()) != null)
sessionLogPermissionsCode = sessionLogPermissionsCode + SessionLogPermissions.CHANGE_SESSION_LOG_PERMISSIONS.getCode();
Error error = Error.NONE;
UserBean userBean = (UserBean)req.getAttribute(AuthRequestAttribute.USER.getName());
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction(TransactionOptions.Builder.withXG(true));
User user = UserFactory.getByKey(datastore, txn, userBean.getKey());
UserPermissions userPermissions = null;
if(user == null)
error = Error.NON_EXISTENT_USER;
else if((userPermissions = PermissionsFactory.getUserPermissionsByUserId(datastore, txn, user.getUserId())) == null)
error = Error.NO_PERMISSIONS;
else
try {
userPermissions.setSessionLogPermissions(sessionLogPermissionsCode);
PermissionsFactory.updateUserPermissions(datastore, txn, userPermissions);
txn.commit();
userPermissionsBean = new UserPermissionsBean(userPermissions);
req.setAttribute(AuthRequestAttribute.USER_PERMISSIONS.getName(), userPermissionsBean);
}
catch(ConcurrentModificationException e) {
error = Error.ERROR_IN_SESSION_LOG_PERMISSIONS;
}
if(error != Error.NONE && txn.isActive())
txn.rollback();
if(userPermissionsBean.getViewSessions() && userPermissionsBean.getViewSessionPermissions()) {
req.setAttribute(HtmlVariable.ERROR.getName(), error.toString());
req.getRequestDispatcher("/WEB-INF/auth/session-log-permissions.jsp").forward(req, resp);
}
else
resp.sendRedirect("/auth/settings");
}
else
req.getRequestDispatcher("/WEB-INF/auth/session-log-permissions.jsp").forward(req, resp);
}
else
resp.sendRedirect("/auth/settings");
}
示例13: doPost
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Error error = Error.NONE;
String userId = req.getParameter(HtmlVariable.USER_ID.getName());
if(userId == null || userId.length() == 0)
error = Error.REQUIRED_USER_ID;
else {
Date dateNow = new Date();
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Queue queue = QueueFactory.getQueue(SEND_MAIL_TASK_QUEUE);
Transaction txn = datastore.beginTransaction(TransactionOptions.Builder.withXG(true));
ResetPasswordRequest resetPasswordRequest = ResetPasswordRequestFactory.getByUserId(datastore, txn, userId);
if(resetPasswordRequest != null) {
if(dateNow.before(resetPasswordRequest.getDateOfExpiration()))
error = Error.ALREADY_EXISTS_REQUEST;
else
ResetPasswordRequestFactory.remove(datastore, txn, resetPasswordRequest);
}
if(error == Error.NONE) {
User user = UserFactory.getByUserId(datastore, txn, userId);
if(user == null)
error = Error.NON_EXISTENT_USER;
else if(!user.getActivated())
error = Error.NOT_ACTIVATED_USER;
else if(user.getDisabled())
error = Error.DISABLED_USER;
else
try {
String requestId = MiscFunctions.getEncryptedHash(MiscFunctions.toUTCDateString(dateNow) + userId, HashAlgorithms.MD5);
resetPasswordRequest = new ResetPasswordRequest(requestId, user.getKey(), dateNow);
ResetPasswordRequestFactory.add(datastore, txn, resetPasswordRequest);
queue.add(TaskOptions.Builder.withUrl(SEND_MAIL_TASK_URL).param(HtmlVariable.USER_ID.getName(), user.getUserId()).param(HtmlVariable.REQUEST_ID.getName(), requestId).param(Cookies.LANG.getName(), (String)req.getAttribute(Cookies.LANG.getName())));
txn.commit();
}
catch(ConcurrentModificationException e) {
error = Error.ERROR_IN_RESET_PASSWORD_REQUEST;
}
}
if(error != Error.NONE && txn.isActive())
txn.rollback();
}
if(error == Error.NONE)
req.getRequestDispatcher("/WEB-INF/utils/reset-password-notice.jsp").forward(req, resp);
else {
req.setAttribute(HtmlVariable.ERROR.getName(), error.toString());
req.setAttribute(HtmlVariable.USER_ID.getName(), userId);
req.getRequestDispatcher("/WEB-INF/utils/reset-password.jsp").forward(req, resp);
}
}
示例14: entityGroups
import com.google.appengine.api.datastore.DatastoreService; //導入方法依賴的package包/類
@Test
public void entityGroups() throws Exception {
try {
// [START entity_groups]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity person = new Entity("Person", "tom");
datastore.put(person);
// Transactions on root entities
Transaction txn = datastore.beginTransaction();
Entity tom = datastore.get(person.getKey());
tom.setProperty("age", 40);
datastore.put(txn, tom);
txn.commit();
// Transactions on child entities
txn = datastore.beginTransaction();
tom = datastore.get(person.getKey());
Entity photo = new Entity("Photo", tom.getKey());
// Create a Photo that is a child of the Person entity named "tom"
photo.setProperty("photoUrl", "http://domain.com/path/to/photo.jpg");
datastore.put(txn, photo);
txn.commit();
// Transactions on entities in different entity groups
txn = datastore.beginTransaction();
tom = datastore.get(person.getKey());
Entity photoNotAChild = new Entity("Photo");
photoNotAChild.setProperty("photoUrl", "http://domain.com/path/to/photo.jpg");
datastore.put(txn, photoNotAChild);
// Throws IllegalArgumentException because the Person entity
// and the Photo entity belong to different entity groups.
txn.commit();
// [END entity_groups]
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException expected) {
// We expect to get an exception that complains that we don't have a XG-transaction.
}
}