CRUD example doesnt work
holly_bony Jul 25, 2011 8:48 PMHi guys the following code used to work with seam 2.x, but with seam 3 doesnt:
Any idea?
tcaModalPanel.xhtml
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:rich="http://richfaces.org/rich"
xmlns:s="http://jboss.com/products/seam/taglib">
<rich:modalPanel id="#{id}" autosized="true" width="#{width}">
<f:facet name="header">
<h:panelGroup>
<h:outputText value="#{title}"></h:outputText>
</h:panelGroup>
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage value="/images/close.png" styleClass="hidelink" id="#{id.concat('closeLink')}"/>
<rich:componentControl for="#{id}" attachTo="#{id.concat('closeLink')}" operation="hide" event="onclick"/>
</h:panelGroup>
</f:facet>
<a4j:form id="#{id.concat('Data')}">
<ui:insert name="formContent" />
</a4j:form>
<rich:hotKey key="esc" selector="##{id.concat('Data')}" handler="#{rich:component(id)}.hide()" />
</rich:modalPanel>
</ui:composition>
usersManager.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:rich="http://richfaces.org/rich">
<ui:composition template="desktopLayout.xhtml">
<ui:define name="head">
<style type="text/css">
.tca-cell{
padding-left: 4px;
padding-top: 4px;
}
.user-role{
width: 100px;
}
.delete-role{
vertical-align: middle;
}
</style>
</ui:define>
<ui:define name="content">
<ui:decorate template="tcaModalPanel.xhtml">
<ui:param name="id" value="editUserPanel"/>
<ui:param name="width" value="350"/>
<ui:param name="title" value="#{messages['user.information']}"/>
<ui:define name="formContent">
<h:panelGrid columns="3">
<h:outputText value="#{messages['user.name']}" />
<h:inputText id="userName" readonly="#{user.name.equals('')?'false':'true'}"
value="#{user.name}" required="true" onfocus="this.select()">
<!-- <rich:ajaxValidator event="onblur"/>-->
</h:inputText>
<rich:message for="userName" />
<h:outputText value="#{messages['user.password']}" />
<h:inputSecret id="password" value="#{user.password}" redisplay="true"
required="true" onfocus="this.select()">
<!-- <rich:ajaxValidator event="onblur"/>-->
</h:inputSecret>
<rich:message for="password" />
<h:outputText value="#{messages['user.company']}" />
<h:selectOneMenu id="company" value="#{user.company}">
<s:selectItems value="#{companies}"
var="currentCompany"
label="#{currentCompany.description}">
</s:selectItems>
<s:convertEntity/>
</h:selectOneMenu>
<rich:message for="company" />
<f:facet name="footer">
<a4j:commandButton value="#{messages['global.ok']}"
data="#{facesContext.maximumSeverity}"
onclick="document.body.style.cursor='wait'"
oncomplete="document.body.style.cursor='default';
if(data==null)#{rich:component('editUserPanel')}.hide()"
action="#{securityManager.updateUser}"
reRender="allUsers">
<s:defaultAction/>
</a4j:commandButton>
</f:facet>
</h:panelGrid>
</ui:define>
</ui:decorate>
<a4j:form ajaxSubmit="true">
<a4j:commandLink id="addUserLink" ajaxSingle="true"
action="#{securityManager.clearUser}"
oncomplete="#{rich:component('editUserPanel')}.show();
document.body.style.cursor='default';#{rich:element('userName')}.focus();"
onclick="document.body.style.cursor='wait'"
reRender="editUserPanel">
<h:graphicImage value="/images/add_user.png" style="border:0" />
</a4j:commandLink>
<rich:scrollableDataTable id="allUsers" width="400px" height="300px"
value="#{users}" var="currentUser"
rendered="#{facesContext.maximumSeverity==null}"
columnClasses="tca-cell">
<a4j:support reRender="editUserPanel" event="onRowDblClick"
eventsQueue="eventUpdateUser"
onsubmit="document.body.style.cursor='wait'"
action="#{securityManager.findUser(currentUser.name)}"
oncomplete="#{rich:component('editUserPanel')}.show();
document.body.style.cursor='default';#{rich:element('password')}.focus();"/>
<rich:column sortable="false">
<f:facet name="header">#{messages['user.name']}</f:facet>
#{currentUser.name}
</rich:column>
<rich:column sortable="false" width="110px">
<f:facet name="header">#{messages['user.company']}</f:facet>
#{currentUser.company.description}
</rich:column>
<rich:column sortable="false" width="70px" style="text-align:center">
<f:facet name="header">#{messages['user.role']}</f:facet>
<a4j:commandLink ajaxSingle="true"
action="#{securityManager.findUser(currentUser.name)}"
oncomplete="#{rich:component('editUserRolesPanel')}.show()"
reRender="editUserRolesPanel">
<h:graphicImage value="/images/role.png" style="border:0" />
</a4j:commandLink>
</rich:column>
<rich:column sortable="false" width="70px" >
<f:facet name="header">#{messages['global.delete']}</f:facet>
<a4j:commandLink ajaxSingle="true"
onclick="document.body.style.cursor='wait'"
action="#{securityManager.findUser(currentUser.name)}"
oncomplete="#{rich:component('deleteUserPanel')}.show();
document.body.style.cursor='default'"
reRender="deleteUserPanel">
<h:graphicImage value="/images/delete.gif" style="border:0" />
</a4j:commandLink>
</rich:column>
</rich:scrollableDataTable>
</a4j:form>
<rich:modalPanel id="deleteUserPanel" autosized="true" width="250">
<f:facet name="header"><!--/*#*/{user.name}-->
<h:outputText value="#{messages['global.confirmDelete']}"/>
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage value="/images/close.png" styleClass="hidelink" id="deleteUserLink"/>
<rich:componentControl for="deleteUserPanel" attachTo="deleteUserLink" operation="hide" event="onclick"/>
</h:panelGroup>
</f:facet>
<h:form id="deleteUserForm" rendered="#{!user.name.equals('')}">
<h:panelGrid columns="1">
<h:outputText value="#{msgFormated.get('user.confirmDelete',user.name)}"/>
<a4j:commandButton ajaxSingle="true" value="#{messages['global.ok']}"
onclick="document.body.style.cursor='wait'"
oncomplete="#{rich:component('deleteUserPanel')}.hide();document.body.style.cursor='default'"
action="#{securityManager.deleteUser(user.name)}"
reRender="allUsers">
</a4j:commandButton>
</h:panelGrid>
</h:form>
<rich:hotKey key="esc" selector="#deleteUserForm" handler="#{rich:component('deleteUserPanel')}.hide()" />
</rich:modalPanel>
<rich:dragIndicator id="indicator" />
<rich:modalPanel id="editUserRolesPanel" width="280" height="400" resizeable="false">
<f:facet name="header">
<h:panelGroup>
<h:outputText value="#{messages['user.editRoles']}"></h:outputText>
</h:panelGroup>
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage id="editUserRolesLink" value="/images/close.png" styleClass="hidelink"/>
<rich:componentControl for="editUserRolesPanel" attachTo="editUserRolesLink"
operation="hide" event="onclick"/>
</h:panelGroup>
</f:facet>
<a4j:form>
<h:panelGrid columns="2" rendered="#{!user.name.equals('')}">
<rich:dataTable id="availableRoles" columns="1" value="#{roles}" var="currentRole">
<rich:column width="110">
<f:facet name="header">#{messages['roles.availables']}</f:facet>
<a4j:outputPanel style="width:100px;border:1px solid gray;padding:2px"
layout="block">
<rich:dragSupport dragIndicator=":indicator"
dragType="ROLES" dragValue="#{currentRole}">
<rich:dndParam name="label" value="#{currentRole.description}" />
</rich:dragSupport>
<h:outputText value="#{currentRole.description}"></h:outputText>
</a4j:outputPanel>
</rich:column>
</rich:dataTable>
<rich:panel style="border: none">
<rich:dropSupport acceptedTypes="ROLES" dropValue="ROLES"
dropListener="#{securityManager.addRole}"
reRender="availableRoles, userRoles" ajaxSingle="true">
</rich:dropSupport>
<rich:dataTable id="userRoles" columns="1" value="#{user.roles}" var="userRole">
<rich:column>
<f:facet name="header">#{messages['roles.from']} #{user.name}</f:facet>
<h:panelGrid columns="2" columnClasses="user-role, delete-role">
<h:outputText value="#{userRole.description}"></h:outputText>
<a4j:commandLink ajaxSingle="true"
onclick="document.body.style.cursor='wait'"
action="#{securityManager.removeRole(userRole.id)}"
oncomplete="document.body.style.cursor='default'"
reRender="availableRoles, userRoles">
<h:graphicImage value="/images/delete.gif" style="border:0" />
</a4j:commandLink>
</h:panelGrid>
</rich:column>
</rich:dataTable>
</rich:panel>
</h:panelGrid>
</a4j:form>
</rich:modalPanel>
</ui:define>
</ui:composition>
</html>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.tca.merksyst.cart.admin;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.log4j.Logger;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.security.Restrict;
import org.richfaces.event.DropEvent;
/**
* Performs processes about the users
*
* @author carlos juarez
*/
@Stateful
@Name("securityManager")
@Restrict("#{s:hasRole('admin')}")
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class SecurityManager implements SecurityManagerLocal {
public static final Logger log = Logger.getLogger(SecurityManager.class.getName());
@In(required = false, scope = ScopeType.PAGE)
@Out(required = false, scope = ScopeType.PAGE)
List<User> users;
@Out(required = false)
List<Role> roles;
@Out(required = false, scope = ScopeType.PAGE)
List<Company> companies;
@In(required = false)
//this line seem work from securituManager page and others
@Out(required = false)
User user;
@PersistenceContext(unitName = "TCADBWEB")
private EntityManager entityManager;
@Factory("users")
public void findAllUsers() {
filterUsers("");
}
@Factory("roles")
public void findAvailableRoles() {
log.debug("findAllRoles");
roles = entityManager.createNamedQuery("getRoles").getResultList();
roles.removeAll(user.getRoles());
}
@Factory("companies")
public void findAllCompanies() {
log.debug("getting companies");
companies = entityManager.createNamedQuery("getCompanies").getResultList();
}
public void clearUser() {
log.debug("clearing user...");
user.setName("");
user.setPassword("");
user.setRoles(new ArrayList<Role>());
}
public void filterUsers(String searchPattern) {
log.debug("Init filterUsers");
searchPattern = searchPattern.trim().equals("") ? "%" : searchPattern + "%";
Query query = entityManager.createNamedQuery("filterUsers");
query.setParameter("name", searchPattern);
users = query.getResultList();
log.debug("end filterUsers");
}
public void findUser(String name) {
log.debug("Init findUser name:" + name);
user = (User) entityManager.find(User.class, name);
log.debug("end findUser");
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void deleteUser(String name) {
log.debug("Init deleteUser with name: " + name);
User userToDelete = entityManager.find(User.class, name);
if (userToDelete != null) {
entityManager.remove(userToDelete);
findAllUsers();
}
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void updateUser() {
log.debug("User to update :" + user);
if (entityManager.find(User.class, user.getName()) == null) {
entityManager.persist(user);
} else {
entityManager.merge(user);
}
findAllUsers();
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void addRole(DropEvent dropEvent) {
log.debug("Init addRole ");
Role roleSelected = (Role) dropEvent.getDragValue();
log.debug("role roleSelected " + roleSelected);
log.debug("user before setting roles " + user);
user.getRoles().add(roleSelected);
roles.remove(roleSelected);
log.debug("user after setting roles " + user);
entityManager.merge(user);
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void removeRole(int roleId) {
log.debug("Init removeRole :" + roleId);
log.debug("user " + user);
log.debug("roles " + roles);
for (Role currentRole : user.getRoles()) {
if (currentRole.getId() == roleId) {
user.getRoles().remove(currentRole);
//roles.add(currentRole);
findAvailableRoles();
entityManager.merge(user);
//estas linea sale sobrando por obvias razones
break;
}
}
}
@Destroy
@Remove//remove es onvoked for ejbcontainer and destroy when ejb is timeout on the session
public void destroy() {
}
;
}
Any idea?
tcaModalPanel.xhtml
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:rich="http://richfaces.org/rich"
xmlns:s="http://jboss.com/products/seam/taglib">
<rich:modalPanel id="#{id}" autosized="true" width="#{width}">
<f:facet name="header">
<h:panelGroup>
<h:outputText value="#{title}"></h:outputText>
</h:panelGroup>
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage value="/images/close.png" styleClass="hidelink" id="#{id.concat('closeLink')}"/>
<rich:componentControl for="#{id}" attachTo="#{id.concat('closeLink')}" operation="hide" event="onclick"/>
</h:panelGroup>
</f:facet>
<a4j:form id="#{id.concat('Data')}">
<ui:insert name="formContent" />
</a4j:form>
<rich:hotKey key="esc" selector="##{id.concat('Data')}" handler="#{rich:component(id)}.hide()" />
</rich:modalPanel>
</ui:composition>
usersManager.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:rich="http://richfaces.org/rich">
<ui:composition template="desktopLayout.xhtml">
<ui:define name="head">
<style type="text/css">
.tca-cell{
padding-left: 4px;
padding-top: 4px;
}
.user-role{
width: 100px;
}
.delete-role{
vertical-align: middle;
}
</style>
</ui:define>
<ui:define name="content">
<ui:decorate template="tcaModalPanel.xhtml">
<ui:param name="id" value="editUserPanel"/>
<ui:param name="width" value="350"/>
<ui:param name="title" value="#{messages['user.information']}"/>
<ui:define name="formContent">
<h:panelGrid columns="3">
<h:outputText value="#{messages['user.name']}" />
<h:inputText id="userName" readonly="#{user.name.equals('')?'false':'true'}"
value="#{user.name}" required="true" onfocus="this.select()">
<!-- <rich:ajaxValidator event="onblur"/>-->
</h:inputText>
<rich:message for="userName" />
<h:outputText value="#{messages['user.password']}" />
<h:inputSecret id="password" value="#{user.password}" redisplay="true"
required="true" onfocus="this.select()">
<!-- <rich:ajaxValidator event="onblur"/>-->
</h:inputSecret>
<rich:message for="password" />
<h:outputText value="#{messages['user.company']}" />
<h:selectOneMenu id="company" value="#{user.company}">
<s:selectItems value="#{companies}"
var="currentCompany"
label="#{currentCompany.description}">
</s:selectItems>
<s:convertEntity/>
</h:selectOneMenu>
<rich:message for="company" />
<f:facet name="footer">
<a4j:commandButton value="#{messages['global.ok']}"
data="#{facesContext.maximumSeverity}"
onclick="document.body.style.cursor='wait'"
oncomplete="document.body.style.cursor='default';
if(data==null)#{rich:component('editUserPanel')}.hide()"
action="#{securityManager.updateUser}"
reRender="allUsers">
<s:defaultAction/>
</a4j:commandButton>
</f:facet>
</h:panelGrid>
</ui:define>
</ui:decorate>
<a4j:form ajaxSubmit="true">
<a4j:commandLink id="addUserLink" ajaxSingle="true"
action="#{securityManager.clearUser}"
oncomplete="#{rich:component('editUserPanel')}.show();
document.body.style.cursor='default';#{rich:element('userName')}.focus();"
onclick="document.body.style.cursor='wait'"
reRender="editUserPanel">
<h:graphicImage value="/images/add_user.png" style="border:0" />
</a4j:commandLink>
<rich:scrollableDataTable id="allUsers" width="400px" height="300px"
value="#{users}" var="currentUser"
rendered="#{facesContext.maximumSeverity==null}"
columnClasses="tca-cell">
<a4j:support reRender="editUserPanel" event="onRowDblClick"
eventsQueue="eventUpdateUser"
onsubmit="document.body.style.cursor='wait'"
action="#{securityManager.findUser(currentUser.name)}"
oncomplete="#{rich:component('editUserPanel')}.show();
document.body.style.cursor='default';#{rich:element('password')}.focus();"/>
<rich:column sortable="false">
<f:facet name="header">#{messages['user.name']}</f:facet>
#{currentUser.name}
</rich:column>
<rich:column sortable="false" width="110px">
<f:facet name="header">#{messages['user.company']}</f:facet>
#{currentUser.company.description}
</rich:column>
<rich:column sortable="false" width="70px" style="text-align:center">
<f:facet name="header">#{messages['user.role']}</f:facet>
<a4j:commandLink ajaxSingle="true"
action="#{securityManager.findUser(currentUser.name)}"
oncomplete="#{rich:component('editUserRolesPanel')}.show()"
reRender="editUserRolesPanel">
<h:graphicImage value="/images/role.png" style="border:0" />
</a4j:commandLink>
</rich:column>
<rich:column sortable="false" width="70px" >
<f:facet name="header">#{messages['global.delete']}</f:facet>
<a4j:commandLink ajaxSingle="true"
onclick="document.body.style.cursor='wait'"
action="#{securityManager.findUser(currentUser.name)}"
oncomplete="#{rich:component('deleteUserPanel')}.show();
document.body.style.cursor='default'"
reRender="deleteUserPanel">
<h:graphicImage value="/images/delete.gif" style="border:0" />
</a4j:commandLink>
</rich:column>
</rich:scrollableDataTable>
</a4j:form>
<rich:modalPanel id="deleteUserPanel" autosized="true" width="250">
<f:facet name="header"><!--/*#*/{user.name}-->
<h:outputText value="#{messages['global.confirmDelete']}"/>
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage value="/images/close.png" styleClass="hidelink" id="deleteUserLink"/>
<rich:componentControl for="deleteUserPanel" attachTo="deleteUserLink" operation="hide" event="onclick"/>
</h:panelGroup>
</f:facet>
<h:form id="deleteUserForm" rendered="#{!user.name.equals('')}">
<h:panelGrid columns="1">
<h:outputText value="#{msgFormated.get('user.confirmDelete',user.name)}"/>
<a4j:commandButton ajaxSingle="true" value="#{messages['global.ok']}"
onclick="document.body.style.cursor='wait'"
oncomplete="#{rich:component('deleteUserPanel')}.hide();document.body.style.cursor='default'"
action="#{securityManager.deleteUser(user.name)}"
reRender="allUsers">
</a4j:commandButton>
</h:panelGrid>
</h:form>
<rich:hotKey key="esc" selector="#deleteUserForm" handler="#{rich:component('deleteUserPanel')}.hide()" />
</rich:modalPanel>
<rich:dragIndicator id="indicator" />
<rich:modalPanel id="editUserRolesPanel" width="280" height="400" resizeable="false">
<f:facet name="header">
<h:panelGroup>
<h:outputText value="#{messages['user.editRoles']}"></h:outputText>
</h:panelGroup>
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage id="editUserRolesLink" value="/images/close.png" styleClass="hidelink"/>
<rich:componentControl for="editUserRolesPanel" attachTo="editUserRolesLink"
operation="hide" event="onclick"/>
</h:panelGroup>
</f:facet>
<a4j:form>
<h:panelGrid columns="2" rendered="#{!user.name.equals('')}">
<rich:dataTable id="availableRoles" columns="1" value="#{roles}" var="currentRole">
<rich:column width="110">
<f:facet name="header">#{messages['roles.availables']}</f:facet>
<a4j:outputPanel style="width:100px;border:1px solid gray;padding:2px"
layout="block">
<rich:dragSupport dragIndicator=":indicator"
dragType="ROLES" dragValue="#{currentRole}">
<rich:dndParam name="label" value="#{currentRole.description}" />
</rich:dragSupport>
<h:outputText value="#{currentRole.description}"></h:outputText>
</a4j:outputPanel>
</rich:column>
</rich:dataTable>
<rich:panel style="border: none">
<rich:dropSupport acceptedTypes="ROLES" dropValue="ROLES"
dropListener="#{securityManager.addRole}"
reRender="availableRoles, userRoles" ajaxSingle="true">
</rich:dropSupport>
<rich:dataTable id="userRoles" columns="1" value="#{user.roles}" var="userRole">
<rich:column>
<f:facet name="header">#{messages['roles.from']} #{user.name}</f:facet>
<h:panelGrid columns="2" columnClasses="user-role, delete-role">
<h:outputText value="#{userRole.description}"></h:outputText>
<a4j:commandLink ajaxSingle="true"
onclick="document.body.style.cursor='wait'"
action="#{securityManager.removeRole(userRole.id)}"
oncomplete="document.body.style.cursor='default'"
reRender="availableRoles, userRoles">
<h:graphicImage value="/images/delete.gif" style="border:0" />
</a4j:commandLink>
</h:panelGrid>
</rich:column>
</rich:dataTable>
</rich:panel>
</h:panelGrid>
</a4j:form>
</rich:modalPanel>
</ui:define>
</ui:composition>
</html>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.tca.merksyst.cart.admin;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.log4j.Logger;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.security.Restrict;
import org.richfaces.event.DropEvent;
/**
* Performs processes about the users
*
* @author carlos juarez
*/
@Stateful
@Name("securityManager")
@Restrict("#{s:hasRole('admin')}")
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class SecurityManager implements SecurityManagerLocal {
public static final Logger log = Logger.getLogger(SecurityManager.class.getName());
@In(required = false, scope = ScopeType.PAGE)
@Out(required = false, scope = ScopeType.PAGE)
List<User> users;
@Out(required = false)
List<Role> roles;
@Out(required = false, scope = ScopeType.PAGE)
List<Company> companies;
@In(required = false)
//this line seem work from securituManager page and others
@Out(required = false)
User user;
@PersistenceContext(unitName = "TCADBWEB")
private EntityManager entityManager;
@Factory("users")
public void findAllUsers() {
filterUsers("");
}
@Factory("roles")
public void findAvailableRoles() {
log.debug("findAllRoles");
roles = entityManager.createNamedQuery("getRoles").getResultList();
roles.removeAll(user.getRoles());
}
@Factory("companies")
public void findAllCompanies() {
log.debug("getting companies");
companies = entityManager.createNamedQuery("getCompanies").getResultList();
}
public void clearUser() {
log.debug("clearing user...");
user.setName("");
user.setPassword("");
user.setRoles(new ArrayList<Role>());
}
public void filterUsers(String searchPattern) {
log.debug("Init filterUsers");
searchPattern = searchPattern.trim().equals("") ? "%" : searchPattern + "%";
Query query = entityManager.createNamedQuery("filterUsers");
query.setParameter("name", searchPattern);
users = query.getResultList();
log.debug("end filterUsers");
}
public void findUser(String name) {
log.debug("Init findUser name:" + name);
user = (User) entityManager.find(User.class, name);
log.debug("end findUser");
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void deleteUser(String name) {
log.debug("Init deleteUser with name: " + name);
User userToDelete = entityManager.find(User.class, name);
if (userToDelete != null) {
entityManager.remove(userToDelete);
findAllUsers();
}
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void updateUser() {
log.debug("User to update :" + user);
if (entityManager.find(User.class, user.getName()) == null) {
entityManager.persist(user);
} else {
entityManager.merge(user);
}
findAllUsers();
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void addRole(DropEvent dropEvent) {
log.debug("Init addRole ");
Role roleSelected = (Role) dropEvent.getDragValue();
log.debug("role roleSelected " + roleSelected);
log.debug("user before setting roles " + user);
user.getRoles().add(roleSelected);
roles.remove(roleSelected);
log.debug("user after setting roles " + user);
entityManager.merge(user);
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void removeRole(int roleId) {
log.debug("Init removeRole :" + roleId);
log.debug("user " + user);
log.debug("roles " + roles);
for (Role currentRole : user.getRoles()) {
if (currentRole.getId() == roleId) {
user.getRoles().remove(currentRole);
//roles.add(currentRole);
findAvailableRoles();
entityManager.merge(user);
//estas linea sale sobrando por obvias razones
break;
}
}
}
@Destroy
@Remove//remove es onvoked for ejbcontainer and destroy when ejb is timeout on the session
public void destroy() {
}
;
}