I am using vaadin 8, java ee7 and wildfly 12.
I have a class Account, where i save the Loggin-Account
@Entity
@Table(name = "ACCOUNT")
@NamedQueries(
{ @NamedQuery(name = Account.QUERY_FIND_ALL, query = "SELECT c FROM Account c"),
@NamedQuery(name = Account.QUERY_FIND_BY_USERNAME, query = "SELECT c FROM Account c WHERE c.username = :username")
})
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
public static final String QUERY_FIND_ALL = "Account.FindAll";
public static final String QUERY_FIND_BY_USERNAME = "Account.FindByUserName";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(unique = true, nullable = false, length = 45)
private String username;
@Column(nullable = false, length = 60)
private String password;
/**
* Einbinden: Enum AccountGroup über ComboBox
*/
@Enumerated(EnumType.STRING)
private AccountGroup accountGroup;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public AccountGroup getAccountGroup() {
return accountGroup;
}
public void setAccountGroup(AccountGroup accountGroup) {
this.accountGroup = accountGroup;
}
}
package org.app.model.dao;
import java.util.List;
import org.app.model.entity.Account;
public interface AccountDAO {
public Account create(Account account);
public Account update(Account account);
public void remove(int id);
public Account findByID(int id);
public Account findByUserName(String username);
public List<Account> findAll();
}
package org.app.model.beans;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.app.model.dao.AccountDAO;
import org.app.model.entity.Account;
@Stateless
@Remote(AccountDAO.class)
public class AccountBean implements AccountDAO {
@PersistenceContext
private EntityManager em;
@Override
public Account create(Account account) {
em.persist(account);
return account;
}
@Override
public Account update(Account account) {
return em.merge(account);
}
@Override
public void remove(int id) {
Account toBeDeleted = findByID(id);
em.remove(toBeDeleted);
}
@Override
public List<Account> findAll() {
return em.createNamedQuery(Account.QUERY_FIND_ALL, Account.class).getResultList();
}
@Override
public Account findByID(int id) {
return em.find(Account.class, id);
}
@Override
public Account findByUserName(String username) {
return em.createNamedQuery(Account.QUERY_FIND_BY_USERNAME, Account.class ).setParameter("username", username).getSingleResult();
}
}
I have then a MainUI with Login-Form:
MainUI:
@CDIUI("")
public class MainUI extends UI {
@Inject
CDIViewProvider viewProvider;
@Inject
LoginView loginView;
private Navigator navigator;
@Override
protected void init(VaadinRequest request) {
if (isLoggedIn()) {
setupMainLayout();
} else {
setContent(loginView);
}
}
private boolean isLoggedIn() {
return getSession().getAttribute(Account.class.getName()) != null;
}
public void loginSuccessful(Account account) {
// Store somewhere logged user state
// This is only an example, do it in a better way :D
getSession().setAttribute(Account.class.getName(), account);
setupMainLayout();
}
private void setupMainLayout() {
final VerticalLayout mainLayout = new VerticalLayout();
final CssLayout menuView = new CssLayout();
final CssLayout contentView = new CssLayout();
mainLayout.addComponent(menuView);
mainLayout.addComponent(contentView);
mainLayout.setMargin(true);
mainLayout.setSpacing(true);
setContent(mainLayout);
navigator = new Navigator(this, contentView);
navigator.addProvider(viewProvider);
navigator.setErrorView(loginView);
String initialState = Optional.ofNullable(navigator.getState()).filter(state -> !state.trim().isEmpty())
.orElse(PERSON_VIEW);
navigator.navigateTo(initialState);
}
}
LoginView:
@UIScoped
public class LoginView extends VerticalLayout implements View, Translatable {
@Inject
private AccountService accountService;
@Inject
private PasswordService passwordService;
private Account account;
private String welcomeMessageCaption = "Welcome, please Login - Test";
private String usernameCaption = "Username";
private String passwordCaption = "Password";
private String loginButtonCaption = "Log in";
private String rememberMeCaption = "Remember me";
private Label welcomeMessage;
private TextField username;
private TextField password;
private Button loginButton;
private LanguageSelector languageSelector;
public LoginView() {
setSpacing(true);
welcomeMessage = new Label(welcomeMessageCaption);
username = new TextField("Username");
password = new TextField("Password");
// password.addStyleName("v-password-textfield");
loginButton = new Button("Login", e -> {
if (validate(username.getValue(), password.getValue())) {
((MainUI) UI.getCurrent()).loginSuccessful(accountService.findByUserName(username.getValue()));
} else {
}
});
addComponent(welcomeMessage);
addComponent(username);
addComponent(password);
addComponent(loginButton);
addComponent(languageSelector);
}
private boolean validate(String username, String password) {
account = new Account();
try {
account = accountService.findByUserName(username);
} catch (Exception e) {
Notification.show("Username not exist: " + username + " -- " + account.getUsername());
return false;
}
if (passwordService.validatePassword(password, account.getPassword())) {
return true;
} else {
Notification.show("Password not correct");
return false;
}
}
....
Later i want to show my Loggged-In-Account with the following code:
Account account = (Account) getSession().getAttribute(Account.class.getName());
Label username = new Label(account.getUsername());
But i get an error?