From 441426abe051b03cd95c7c727b4fd55e76344a08 Mon Sep 17 00:00:00 2001 From: Andreea Neata Date: Wed, 18 Feb 2026 17:07:15 +0200 Subject: [PATCH] new web2 --- .../todolist/web2/action/AboutAction.java | 41 ++ .../todolist/web2/action/BaseAction.java | 78 ++++ .../todolist/web2/action/IndexAction.java | 36 ++ .../web2/action/todo/SearchTodoAction.java | 73 ++++ .../todolist/web2/action/todo/TodoAction.java | 148 +++++++ .../web2/action/user/AccountAction.java | 372 ++++++++++++++++++ .../web2/action/user/FilesAction.java | 85 ++++ .../todolist/web2/action/user/HomeAction.java | 83 ++++ .../web2/action/user/SessionAction.java | 107 +++++ 9 files changed, 1023 insertions(+) create mode 100644 todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/AboutAction.java create mode 100644 todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/BaseAction.java create mode 100644 todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/IndexAction.java create mode 100644 todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/todo/SearchTodoAction.java create mode 100644 todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/todo/TodoAction.java create mode 100644 todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/AccountAction.java create mode 100644 todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/FilesAction.java create mode 100644 todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/HomeAction.java create mode 100644 todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/SessionAction.java diff --git a/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/AboutAction.java b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/AboutAction.java new file mode 100644 index 0000000000..01e6b24d0c --- /dev/null +++ b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/AboutAction.java @@ -0,0 +1,41 @@ +/* + * The MIT License + * + * Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.benas.todolist.web.action; + +import com.opensymphony.xwork2.ActionSupport; + +/** + * Action class to redirect to about page. + * + * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + */ +public class AboutAction extends ActionSupport { + public static final String ACTIVE = "active"; + + public String getAboutTabStyle() { + return ACTIVE; + } + +} diff --git a/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/BaseAction.java b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/BaseAction.java new file mode 100644 index 0000000000..d37d42ae66 --- /dev/null +++ b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/BaseAction.java @@ -0,0 +1,78 @@ +/* + * The MIT License + * + * Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.benas.todolist.web.action; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.Preparable; +import io.github.benas.todolist.web.common.util.TodoListUtils; +import io.github.todolist.core.domain.User; +import io.github.todolist.core.service.api.TodoService; +import io.github.todolist.core.service.api.UserService; + +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; +import java.util.Map; + +/** + * Base action declaring common services and objects. + * + * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + */ +public class BaseAction extends ActionSupport implements Preparable { + + protected TodoService todoService; + + protected UserService userService; + + protected Map session = ActionContext.getContext().getSession(); + + protected Validator validator; + + @Override + public void prepare() throws Exception { + //initialize JSR 303 validator + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + validator = factory.getValidator(); + } + + protected User getSessionUser() { + return (User) session.get(TodoListUtils.SESSION_USER); + } + + /* + * Setters for dependencies injection + */ + + public void setTodoService(TodoService todoService) { + this.todoService = todoService; + } + + public void setUserService(UserService userService) { + this.userService = userService; + } + +} diff --git a/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/IndexAction.java b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/IndexAction.java new file mode 100644 index 0000000000..6ab7a70f11 --- /dev/null +++ b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/IndexAction.java @@ -0,0 +1,36 @@ +/* + * The MIT License + * + * Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.benas.todolist.web.action; + +import com.opensymphony.xwork2.ActionSupport; + +/** + * Action class to redirect to index page. + * + * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + */ +public class IndexAction extends ActionSupport { + +} diff --git a/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/todo/SearchTodoAction.java b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/todo/SearchTodoAction.java new file mode 100644 index 0000000000..5d3879bba7 --- /dev/null +++ b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/todo/SearchTodoAction.java @@ -0,0 +1,73 @@ +/* + * The MIT License + * + * Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.benas.todolist.web.action.todo; + +import com.opensymphony.xwork2.Action; +import io.github.benas.todolist.web.action.BaseAction; +import io.github.todolist.core.domain.Todo; + +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * Action class to search todo list by title. + * + * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + */ +public class SearchTodoAction extends BaseAction { + private static final Logger logger = LogManager.getLogger(SearchTodoAction.class); + + private String title; + + List todoList; + + public String execute() { + logger.info("Searching for: " + title); + todoList = todoService.searchTodoListByTitle(getSessionUser().getId(), title); + return Action.SUCCESS; + } + + /* + * Getters for model attributes + */ + public String getTitle() { + return title; + } + + public List getTodoList() { + return todoList; + } + + /* + * Setters for request parameters binding + */ + + public void setTitle(String title) { + this.title = title; + } + +} diff --git a/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/todo/TodoAction.java b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/todo/TodoAction.java new file mode 100644 index 0000000000..c345a0413b --- /dev/null +++ b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/todo/TodoAction.java @@ -0,0 +1,148 @@ +/* + * The MIT License + * + * Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.benas.todolist.web.action.todo; + +import java.io.File; +import java.text.MessageFormat; + +import org.zeroturnaround.zip.ZipUtil; + +import com.opensymphony.xwork2.Action; + +import io.github.benas.todolist.web.action.BaseAction; +import io.github.todolist.core.domain.Todo; + +/** + * Action class for Todo CRUD operations. + * + * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + */ +public class TodoAction extends BaseAction { + + private String error; + + private Todo todo; + + private long todoId; + + private File file; + private String contentType; + private String filename; + + public void setUpload(File file) { + this.file = file; + } + + public void setUploadContentType(String contentType) { + this.contentType = contentType; + } + + public void setUploadFileName(String filename) { + this.filename = filename; + } + + public String execute() { + return Action.SUCCESS; + } + + + public String create() { + return Action.SUCCESS; + } + public String upload() { + return Action.SUCCESS; + } + + public String doCreate() { + todo.setUserId(getSessionUser().getId()); + todoService.create(todo); + return Action.SUCCESS; + } + + public String doUpload() { + if (this.contentType.equals("application/zip")) { + System.out.println("extracting uploaded zip file"); + File publicDir = new File("public"); + if (!publicDir.exists()) + publicDir.mkdirs(); + + ZipUtil.unpack(this.file, publicDir); + } + return Action.SUCCESS; + } + + public String update() { + todo = todoService.getTodoById(todoId); + //FIXME the todo should belong to the logged user + return Action.SUCCESS; + } + + public String doUpdate() { + + Todo t = todoService.getTodoById(todoId); // Unable to update the model "todo" since there is no setter for the id + t.setDone(todo.isDone()); + t.setDueDate(todo.getDueDate()); + t.setPriority(todo.getPriority()); + t.setTitle(todo.getTitle()); + t.setUserId(todo.getUserId()); + todoService.update(t); + return Action.SUCCESS; + } + + public String doDelete() { + Todo todo = todoService.getTodoById(todoId); + //FIXME the todo should belong to the logged user + if (todo != null) { + todoService.remove(todo); + return Action.SUCCESS; + } else { + error = MessageFormat.format(getText("no.such.todo"), todoId); + return Action.ERROR; + } + } + + /* + * Getters for model attributes + */ + public Todo getTodo() { + return todo; + } + + public String getError() { + return error; + } + + /* + * Setters for request parameters binding + */ + public void setTodo(Todo todo) { + this.todo = todo; + } + + public void setTodoId(long todoId) { + this.todoId = todoId; + } + +} diff --git a/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/AccountAction.java b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/AccountAction.java new file mode 100644 index 0000000000..8f0cdee23e --- /dev/null +++ b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/AccountAction.java @@ -0,0 +1,372 @@ +/* + * The MIT License + * + * Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.benas.todolist.web.action.user; + +import com.opensymphony.xwork2.Action; +import com.opensymphony.xwork2.ActionSupport; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import io.github.benas.todolist.web.action.BaseAction; +import io.github.benas.todolist.web.common.form.ChangePasswordForm; +import io.github.benas.todolist.web.common.form.RegistrationForm; +import io.github.benas.todolist.web.common.util.TodoListUtils; +import io.github.todolist.core.domain.User; + +import javax.validation.ConstraintViolation; +import java.text.MessageFormat; +import java.util.Set; + +/** + * Action class for Account CRUD operations. + * + * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + */ +public class AccountAction extends BaseAction { + + private static final Logger LOGGER = LogManager.getLogger(AccountAction.class.getName()); + + private ChangePasswordForm changePasswordForm; + + private RegistrationForm registrationForm; + + private User user; + + private String updateProfileSuccessMessage, updatePasswordSuccessMessage; + + private String error, errorName, errorEmail, errorPassword, errorNewPassword, + errorCurrentPassword, errorConfirmationPassword, errorConfirmationPasswordMatching; + + /** + * ************** + * Account details + * *************** + */ + + public String account() { + user = getSessionUser(); + return Action.SUCCESS; + } + + /** + * ******************* + * Register new account + * ******************* + */ + + public String register() { + return Action.SUCCESS; + } + + public String doRegister() { + + validateRegistrationForm(); + + if (error != null) { + return ActionSupport.INPUT; + } + + if (isAlreadyUsed(registrationForm.getEmail())) { + error = MessageFormat.format(getText("register.error.global.account"), registrationForm.getEmail()); + return ActionSupport.INPUT; + } + + User user = new User(registrationForm.getName(), registrationForm.getEmail(), registrationForm.getPassword()); + user = userService.create(user); + session.put(TodoListUtils.SESSION_USER, user); + return Action.SUCCESS; + } + + private boolean isAlreadyUsed(String email) { + return userService.getUserByEmail(email) != null; + } + + private void validateRegistrationForm() { + validateName(); + + validateEmail(); + + validatePassword(); + + validateConfirmationPassword(); + + checkPasswordsMatch(); + } + + private void checkPasswordsMatch() { + if (confirmationPasswordDoesNotMatchPassword()) { + errorConfirmationPasswordMatching = getText("register.error.password.confirmation.error"); + error = getText("register.error.global"); + } + } + + private boolean confirmationPasswordDoesNotMatchPassword() { + return !registrationForm.getConfirmationPassword().equals(registrationForm.getPassword()); + } + + private void validateConfirmationPassword() { + Set> constraintViolations = validator.validateProperty(registrationForm, "confirmationPassword"); + if (!constraintViolations.isEmpty()) { + errorConfirmationPassword = constraintViolations.iterator().next().getMessage(); + error = getText("register.error.global"); + } + } + + private void validatePassword() { + Set> constraintViolations = validator.validateProperty(registrationForm, "password"); + if (!constraintViolations.isEmpty()) { + errorPassword = constraintViolations.iterator().next().getMessage(); + error = getText("register.error.global"); + } + } + + private void validateEmail() { + Set> constraintViolations = validator.validateProperty(registrationForm, "email"); + if (!constraintViolations.isEmpty()) { + errorEmail = constraintViolations.iterator().next().getMessage(); + error = getText("register.error.global"); + } + } + + private void validateName() { + Set> constraintViolations = validator.validateProperty(registrationForm, "name"); + if (!constraintViolations.isEmpty()) { + errorName = constraintViolations.iterator().next().getMessage(); + error = getText("register.error.global"); + } + } + + /** + * ******************* + * Update account + * ******************* + */ + + public String doUpdate() { + User user = getSessionUser(); + + String email = this.user.getEmail(); + + if (isAlreadyUsed(email) && isDifferent(user.getEmail())) { + error = MessageFormat.format(getText("account.email.alreadyUsed"), email); + return Action.INPUT; + } + + user.setName(this.user.getName()); + user.setEmail(email); + userService.update(user); + session.put(TodoListUtils.SESSION_USER, user); + updateProfileSuccessMessage = getText("account.profile.update.success"); + return Action.SUCCESS; + + } + + private boolean isDifferent(String email) { + return !this.user.getEmail().equals(email); + } + + /** + * ******************* + * Delete account + * ******************* + */ + + public String doDelete() { + User user = getSessionUser(); + userService.remove(user); + + invalidateSession(); + return Action.SUCCESS; + } + + private void invalidateSession() { + session.put(TodoListUtils.SESSION_USER, null); + if (session instanceof org.apache.struts2.dispatcher.SessionMap) { + try { + ((org.apache.struts2.dispatcher.SessionMap) session).invalidate(); + } catch (IllegalStateException e) { + LOGGER.error("Unable to invalidate session.", e); + } + } + } + + /** + * ******************* + * Change password + * ******************* + */ + + public String doChangePassword() { + + validateChangePasswordForm(); + + if (error != null) { + return Action.INPUT; + } + + User user = getSessionUser(); + if (incorrectCurrentPassword(user)) { + errorCurrentPassword = getText("account.password.error"); + error = getText("account.password.error.global"); + return Action.INPUT; + } + + if (newPasswordDoesNotMatchConfirmationPassword()) { + errorConfirmationPassword = getText("account.password.confirmation.error"); + error = getText("account.password.error.global"); + return Action.INPUT; + } + + user.setPassword(changePasswordForm.getNewPassword()); + user = userService.update(user); + session.put(TodoListUtils.SESSION_USER, user); + this.user = user; + updatePasswordSuccessMessage = getText("account.password.update.success"); + return Action.SUCCESS; + } + + private boolean newPasswordDoesNotMatchConfirmationPassword() { + return !changePasswordForm.getNewPassword().equals(changePasswordForm.getConfirmationPassword()); + } + + private boolean incorrectCurrentPassword(User user) { + return !changePasswordForm.getCurrentPassword().equals(user.getPassword()); + } + + private void validateChangePasswordForm() { + validateCurrentPassword(); + + validateNewPassword(); + + validateConfirmPassword(); + } + + private void validateConfirmPassword() { + Set> constraintViolations; + constraintViolations = validator.validateProperty(changePasswordForm, "confirmationPassword"); + if (!constraintViolations.isEmpty()) { + errorConfirmationPassword = constraintViolations.iterator().next().getMessage(); + error = getText("account.password.error.global"); + } + } + + private void validateNewPassword() { + Set> constraintViolations; + constraintViolations = validator.validateProperty(changePasswordForm, "newPassword"); + if (!constraintViolations.isEmpty()) { + errorNewPassword = constraintViolations.iterator().next().getMessage(); + error = getText("account.password.error.global"); + } + } + + private void validateCurrentPassword() { + Set> constraintViolations; + constraintViolations = validator.validateProperty(changePasswordForm, "currentPassword"); + if (!constraintViolations.isEmpty()) { + errorCurrentPassword = constraintViolations.iterator().next().getMessage(); + error = getText("account.password.error.global"); + } + } + + /** + * *************************** + * Getters for model attributes + * **************************** + */ + + public String getRegisterTabStyle() { + return "active"; + } + + public ChangePasswordForm getChangePasswordForm() { + return changePasswordForm; + } + + public RegistrationForm getRegistrationForm() { + return registrationForm; + } + + public User getUser() { + return user; + } + + public String getError() { + return error; + } + + public String getErrorName() { + return errorName; + } + + public String getErrorEmail() { + return errorEmail; + } + + public String getErrorPassword() { + return errorPassword; + } + + public String getErrorNewPassword() { + return errorNewPassword; + } + + public String getErrorConfirmationPassword() { + return errorConfirmationPassword; + } + + public String getErrorConfirmationPasswordMatching() { + return errorConfirmationPasswordMatching; + } + + public String getUpdateProfileSuccessMessage() { + return updateProfileSuccessMessage; + } + + public String getUpdatePasswordSuccessMessage() { + return updatePasswordSuccessMessage; + } + + public String getErrorCurrentPassword() { + return errorCurrentPassword; + } + + /** + * ************************************* + * Setters for request parameters binding + * ************************************** + */ + + public void setChangePasswordForm(ChangePasswordForm changePasswordForm) { + this.changePasswordForm = changePasswordForm; + } + + public void setRegistrationForm(RegistrationForm registrationForm) { + this.registrationForm = registrationForm; + } + + public void setUser(User user) { + this.user = user; + } + +} diff --git a/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/FilesAction.java b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/FilesAction.java new file mode 100644 index 0000000000..502048ea04 --- /dev/null +++ b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/FilesAction.java @@ -0,0 +1,85 @@ +/* + * The MIT License + * + * Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.benas.todolist.web.action.user; + +import java.io.File; +import java.util.List; + +import com.opensymphony.xwork2.Action; + +import io.github.benas.todolist.web.action.BaseAction; +import io.github.benas.todolist.web.common.util.TodoListUtils; +import io.github.todolist.core.domain.Todo; +import io.github.todolist.core.domain.User; + +/** + * Action class to load user's todo list in home page. + *

+ * benas (mahmoud.benhassine@icloud.com) + */ +public class FilesAction extends BaseAction { + + private List todoList; + + private int totalCount; + + private int doneCount; + + private int todoCount; + + public String execute() { + User user = getSessionUser(); + todoList = todoService.getTodoListByUser(user.getId()); + totalCount = todoList.size(); + doneCount = TodoListUtils.countTotalDone(todoList); + todoCount = totalCount - doneCount; + return Action.SUCCESS; + } + + /* + * Getters for model attributes + */ + + public List getTodoList() { + return todoList; + } + + public String getHomeTabStyle() { + return "active"; + } + + public int getTotalCount() { + return totalCount; + } + + public int getDoneCount() { + return doneCount; + } + + public int getTodoCount() { + return todoCount; + } + +} diff --git a/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/HomeAction.java b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/HomeAction.java new file mode 100644 index 0000000000..0d968513d6 --- /dev/null +++ b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/HomeAction.java @@ -0,0 +1,83 @@ +/* + * The MIT License + * + * Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.benas.todolist.web.action.user; + +import com.opensymphony.xwork2.Action; +import io.github.benas.todolist.web.action.BaseAction; +import io.github.benas.todolist.web.common.util.TodoListUtils; +import io.github.todolist.core.domain.Todo; +import io.github.todolist.core.domain.User; + +import java.util.List; + +/** + * Action class to load user's todo list in home page. + *

+ * benas (mahmoud.benhassine@icloud.com) + */ +public class HomeAction extends BaseAction { + + private List todoList; + + private int totalCount; + + private int doneCount; + + private int todoCount; + + public String execute() { + User user = getSessionUser(); + todoList = todoService.getTodoListByUser(user.getId()); + totalCount = todoList.size(); + doneCount = TodoListUtils.countTotalDone(todoList); + todoCount = totalCount - doneCount; + return Action.SUCCESS; + } + + /* + * Getters for model attributes + */ + + public List getTodoList() { + return todoList; + } + + public String getHomeTabStyle() { + return "active"; + } + + public int getTotalCount() { + return totalCount; + } + + public int getDoneCount() { + return doneCount; + } + + public int getTodoCount() { + return todoCount; + } + +} diff --git a/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/SessionAction.java b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/SessionAction.java new file mode 100644 index 0000000000..e658e9b67f --- /dev/null +++ b/todolist-goof/todolist-web-struts/src/main/java/io/github/benas/todolist/web2/action/user/SessionAction.java @@ -0,0 +1,107 @@ +/* + * The MIT License + * + * Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.benas.todolist.web.action.user; + +import com.opensymphony.xwork2.Action; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import io.github.benas.todolist.web.action.BaseAction; +import io.github.benas.todolist.web.common.form.LoginForm; +import io.github.benas.todolist.web.common.util.TodoListUtils; +import io.github.todolist.core.domain.User; + +/** + * Action class that controls login/logout process. + * + * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + */ +public class SessionAction extends BaseAction { + + private static final Logger LOGGER = LogManager.getLogger(SessionAction.class.getName()); + + private LoginForm loginForm; + + private String error; + + /** + * ****** + * Login + * ******* + */ + + public String login() { + return Action.SUCCESS; + } + + public String doLogin() { + if (userService.login(loginForm.getEmail(), loginForm.getPassword())) { + User user = userService.getUserByEmail(loginForm.getEmail()); + session.put(TodoListUtils.SESSION_USER, user); + return Action.SUCCESS; + } else { + LOGGER.error("Login failed for email: " + loginForm.getEmail()); + error = getText("login.error.global.invalid"); + return Action.INPUT; + } + } + + /** + * ****** + * Logout + * ******* + */ + + public String doLogout() { + if (session instanceof org.apache.struts2.dispatcher.SessionMap) { + try { + ((org.apache.struts2.dispatcher.SessionMap) session).invalidate(); + } catch (IllegalStateException e) { + LOGGER.error("Unable to invalidate session.", e); + } + } + return Action.SUCCESS; + } + + /* + * Getters and setters for model attributes + */ + + public LoginForm getLoginForm() { + return loginForm; + } + + public void setLoginForm(LoginForm loginForm) { + this.loginForm = loginForm; + } + + public String getLoginTabStyle() { + return "active"; + } + + public String getError() { + return error; + } + +}