CCM NG: Delete from groups

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3991 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2016-04-14 16:38:03 +00:00
parent 403fa93dfa
commit bbfdf4f39c
11 changed files with 117 additions and 65 deletions

View File

@ -47,6 +47,7 @@ import com.arsdigita.bebop.event.ParameterListener;
* *
* *
*/ */
@Deprecated //Does not work. Do not use
public class CancellableValidationListener implements ParameterListener { public class CancellableValidationListener implements ParameterListener {
private RequestLocal m_isCancel; private RequestLocal m_isCancel;
@ -65,6 +66,7 @@ public class CancellableValidationListener implements ParameterListener {
m_listener = l; m_listener = l;
} }
@Override
public void validate(ParameterEvent evt) throws FormProcessException { public void validate(ParameterEvent evt) throws FormProcessException {
PageState ps = evt.getPageState(); PageState ps = evt.getPageState();
Boolean b = (Boolean) m_isCancel.get(ps); Boolean b = (Boolean) m_isCancel.get(ps);

View File

@ -28,7 +28,7 @@ import com.arsdigita.bebop.form.Submit;
import com.arsdigita.bebop.form.TextField; import com.arsdigita.bebop.form.TextField;
import com.arsdigita.bebop.parameters.StringParameter; import com.arsdigita.bebop.parameters.StringParameter;
import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.ui.login.UserForm;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;

View File

@ -23,13 +23,10 @@ import com.arsdigita.bebop.FormData;
import com.arsdigita.bebop.Label; import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.ParameterSingleSelectionModel; import com.arsdigita.bebop.ParameterSingleSelectionModel;
import com.arsdigita.bebop.RequestLocal;
import com.arsdigita.bebop.SaveCancelSection; import com.arsdigita.bebop.SaveCancelSection;
import com.arsdigita.bebop.form.TextField; import com.arsdigita.bebop.form.TextField;
import com.arsdigita.bebop.parameters.CancellableValidationListener;
import com.arsdigita.bebop.parameters.NotEmptyValidationListener;
import com.arsdigita.bebop.parameters.StringInRangeValidationListener;
import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.globalization.GlobalizedMessage;
import org.libreccm.cdi.utils.CdiUtil; import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.security.Group; import org.libreccm.security.Group;
import org.libreccm.security.GroupRepository; import org.libreccm.security.GroupRepository;
@ -50,8 +47,9 @@ public class GroupForm extends Form {
private final SaveCancelSection saveCancelSection; private final SaveCancelSection saveCancelSection;
public GroupForm( public GroupForm(
final GroupAdmin groupAdmin, final GroupAdmin groupAdmin,
final ParameterSingleSelectionModel<String> selectedGroupId) { final ParameterSingleSelectionModel<String> selectedGroupId) {
super("groupform"); super("groupform");
this.groupAdmin = groupAdmin; this.groupAdmin = groupAdmin;
@ -63,42 +61,20 @@ public class GroupForm extends Form {
final Label target = (Label) e.getTarget(); final Label target = (Label) e.getTarget();
final String selectedGroupIdStr = selectedGroupId.getSelectedKey( final String selectedGroupIdStr = selectedGroupId.getSelectedKey(
state); state);
if (selectedGroupIdStr == null || selectedGroupIdStr.isEmpty()) { if (selectedGroupIdStr == null || selectedGroupIdStr.isEmpty()) {
target.setLabel(new GlobalizedMessage( target.setLabel(new GlobalizedMessage(
"ui.admin.group.create_new", "ui.admin.group.create_new",
ADMIN_BUNDLE)); ADMIN_BUNDLE));
} else { } else {
target.setLabel(new GlobalizedMessage("ui.admin.group.edit", target.setLabel(new GlobalizedMessage("ui.admin.group.edit",
ADMIN_BUNDLE)); ADMIN_BUNDLE));
} }
})); }));
final RequestLocal cancelled = new RequestLocal() {
@Override
public Object initialValue(final PageState state) {
return !saveCancelSection.getSaveButton().isSelected(state);
}
};
groupName = new TextField(GROUP_NAME); groupName = new TextField(GROUP_NAME);
groupName.setLabel(new GlobalizedMessage("ui.admin.group.name", groupName.setLabel(new GlobalizedMessage("ui.admin.group.name",
ADMIN_BUNDLE)); ADMIN_BUNDLE));
groupName.addValidationListener(new CancellableValidationListener(
new NotEmptyValidationListener(
new GlobalizedMessage(
"ui.admin.group.name.error.notempty",
ADMIN_BUNDLE)),
cancelled));
groupName.addValidationListener(
new CancellableValidationListener(
new StringInRangeValidationListener(
1,
256,
new GlobalizedMessage(
"ui.admin.group.name.error.length",
ADMIN_BUNDLE)),
cancelled));
add(groupName); add(groupName);
@ -113,14 +89,27 @@ public class GroupForm extends Form {
final String groupNameData = data.getString(GROUP_NAME); final String groupNameData = data.getString(GROUP_NAME);
if (groupNameData == null || groupNameData.isEmpty()) {
data.addError(GROUP_NAME, new GlobalizedMessage(
"ui.admin.group.name.error.notempty",
ADMIN_BUNDLE));
return;
}
if (groupNameData.length() > 256) {
data.addError(GROUP_NAME, new GlobalizedMessage(
"ui.admin.group.name.error.length",
ADMIN_BUNDLE));
}
final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final GroupRepository groupRepository = cdiUtil.findBean( final GroupRepository groupRepository = cdiUtil.findBean(
GroupRepository.class); GroupRepository.class);
if (groupRepository.findByName(groupNameData) != null) { if (groupRepository.findByName(groupNameData) != null) {
data.addError(new GlobalizedMessage( data.addError(GROUP_NAME, new GlobalizedMessage(
"ui.admin.group.error.name_already_in_use", "ui.admin.group.error.name_already_in_use",
ADMIN_BUNDLE)); ADMIN_BUNDLE));
} }
} }
}); });
@ -129,15 +118,15 @@ public class GroupForm extends Form {
final PageState state = e.getPageState(); final PageState state = e.getPageState();
final String selectedGroupIdStr = selectedGroupId.getSelectedKey( final String selectedGroupIdStr = selectedGroupId.getSelectedKey(
state); state);
if (selectedGroupIdStr != null && !selectedGroupIdStr.isEmpty()) { if (selectedGroupIdStr != null && !selectedGroupIdStr.isEmpty()) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final GroupRepository groupRepository = cdiUtil.findBean( final GroupRepository groupRepository = cdiUtil.findBean(
GroupRepository.class); GroupRepository.class);
final Group group = groupRepository.findById(Long.parseLong( final Group group = groupRepository.findById(Long.parseLong(
selectedGroupIdStr)); selectedGroupIdStr));
groupName.setValue(state, group.getName()); groupName.setValue(state, group.getName());
} }
}); });
@ -151,19 +140,19 @@ public class GroupForm extends Form {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final GroupRepository groupRepository = cdiUtil.findBean( final GroupRepository groupRepository = cdiUtil.findBean(
GroupRepository.class); GroupRepository.class);
final String selectedGroupIdStr = selectedGroupId. final String selectedGroupIdStr = selectedGroupId.
getSelectedKey(state); getSelectedKey(state);
if (selectedGroupIdStr == null if (selectedGroupIdStr == null
|| selectedGroupIdStr.isEmpty()) { || selectedGroupIdStr.isEmpty()) {
final Group group = new Group(); final Group group = new Group();
group.setName(groupNameData); group.setName(groupNameData);
groupRepository.save(group); groupRepository.save(group);
} else { } else {
final Group group = groupRepository.findById(Long.parseLong( final Group group = groupRepository.findById(Long.parseLong(
selectedGroupIdStr)); selectedGroupIdStr));
group.setName(groupNameData); group.setName(groupNameData);
groupRepository.save(group); groupRepository.save(group);

View File

@ -58,9 +58,9 @@ public class GroupsTable extends Table {
private final ParameterSingleSelectionModel<String> selectedGroupId; private final ParameterSingleSelectionModel<String> selectedGroupId;
public GroupsTable( public GroupsTable(
final GroupAdmin parent, final GroupAdmin parent,
final TextField groupsTableFilter, final TextField groupsTableFilter,
final ParameterSingleSelectionModel<String> selectedGroupId) { final ParameterSingleSelectionModel<String> selectedGroupId) {
super(); super();
setIdAttr("groupsTable"); setIdAttr("groupsTable");
@ -69,20 +69,21 @@ public class GroupsTable extends Table {
this.selectedGroupId = selectedGroupId; this.selectedGroupId = selectedGroupId;
setEmptyView(new Label(new GlobalizedMessage( setEmptyView(new Label(new GlobalizedMessage(
"ui.admin.groups.table.no_groups", ADMIN_BUNDLE))); "ui.admin.groups.table.no_groups", ADMIN_BUNDLE)));
final TableColumnModel columnModel = getColumnModel(); final TableColumnModel columnModel = getColumnModel();
columnModel.add(new TableColumn( columnModel.add(new TableColumn(
COL_GROUP_NAME, COL_GROUP_NAME,
new Label(new GlobalizedMessage("ui.admin.groups.table.name", new Label(new GlobalizedMessage("ui.admin.groups.table.name",
ADMIN_BUNDLE)))); ADMIN_BUNDLE))));
columnModel.add(new TableColumn( columnModel.add(new TableColumn(
COL_DELETE, COL_DELETE,
new Label(new GlobalizedMessage("ui.admin.groups.table.delete", new Label(new GlobalizedMessage("ui.admin.groups.table.delete",
ADMIN_BUNDLE)))); ADMIN_BUNDLE))));
columnModel.get(COL_GROUP_NAME).setCellRenderer( columnModel.get(COL_GROUP_NAME).setCellRenderer(
new TableCellRenderer() { new TableCellRenderer() {
@Override @Override
public Component getComponent(final Table table, public Component getComponent(final Table table,
final PageState state, final PageState state,
@ -93,28 +94,64 @@ public class GroupsTable extends Table {
final int column) { final int column) {
return new ControlLink((String) value); return new ControlLink((String) value);
} }
});
columnModel.get(COL_DELETE).setCellRenderer(new TableCellRenderer() {
@Override
public Component getComponent(final Table table,
final PageState state,
final Object value,
final boolean isSelected,
final Object key,
final int row,
final int column) {
final ControlLink link = new ControlLink((Component) value);
link.setConfirmation(new GlobalizedMessage(
"ui.admin.group.delete.confirm", ADMIN_BUNDLE));
return link;
}
}); });
addTableActionListener(new TableActionListener() { addTableActionListener(new TableActionListener() {
@Override @Override
public void cellSelected(final TableActionEvent event) { public void cellSelected(final TableActionEvent event) {
final PageState state = event.getPageState(); final PageState state = event.getPageState();
final String key = (String) event.getRowKey(); final String key = (String) event.getRowKey();
selectedGroupId.setSelectedKey(state, key);
parent.showGroupDetails(state); switch (event.getColumn()) {
case COL_GROUP_NAME:
selectedGroupId.setSelectedKey(state, key);
parent.showGroupDetails(state);
break;
case COL_DELETE:
final GroupRepository groupRepository = CdiUtil
.createCdiUtil().findBean(GroupRepository.class);
final Group group = groupRepository.findById(
Long.parseLong(key));
groupRepository.delete(group);
break;
default:
throw new IllegalArgumentException(
"Invalid value for column.");
}
} }
@Override @Override
public void headSelected(final TableActionEvent event) { public void headSelected(final TableActionEvent event) {
//Nothing //Nothing
} }
}); });
setModelBuilder(new GroupsTableModelBuilder()); setModelBuilder(new GroupsTableModelBuilder());
} }
private class GroupsTableModelBuilder extends LockableImpl private class GroupsTableModelBuilder extends LockableImpl
implements TableModelBuilder { implements TableModelBuilder {
@Override @Override
public TableModel makeModel(final Table table, public TableModel makeModel(final Table table,
@ -123,6 +160,7 @@ public class GroupsTable extends Table {
return new GroupsTableModel(state); return new GroupsTableModel(state);
} }
} }
private class GroupsTableModel implements TableModel { private class GroupsTableModel implements TableModel {
@ -135,14 +173,14 @@ public class GroupsTable extends Table {
final String filterTerm = (String) groupsTableFilter.getValue(state); final String filterTerm = (String) groupsTableFilter.getValue(state);
LOGGER.debug("Value of filter is: \"{}\"", filterTerm); LOGGER.debug("Value of filter is: \"{}\"", filterTerm);
final GroupRepository groupRepository = CdiUtil.createCdiUtil(). final GroupRepository groupRepository = CdiUtil.createCdiUtil().
findBean(GroupRepository.class); findBean(GroupRepository.class);
if (filterTerm == null || filterTerm.isEmpty()) { if (filterTerm == null || filterTerm.isEmpty()) {
groups = groupRepository.findAllOrderedByGroupName(); groups = groupRepository.findAllOrderedByGroupName();
LOGGER.debug("Found {} groups in database.", groups.size()); LOGGER.debug("Found {} groups in database.", groups.size());
} else { } else {
groups = groupRepository.searchGroupByName(filterTerm); groups = groupRepository.searchGroupByName(filterTerm);
LOGGER.debug("Found {} groups in database which match the " LOGGER.debug("Found {} groups in database which match the "
+ "filter \"{}\".", + "filter \"{}\".",
groups.size(), groups.size(),
filterTerm); filterTerm);
} }
@ -171,10 +209,10 @@ public class GroupsTable extends Table {
return group.getName(); return group.getName();
case COL_DELETE: case COL_DELETE:
return new Label(new GlobalizedMessage( return new Label(new GlobalizedMessage(
"ui.admin.groups.table.delete", ADMIN_BUNDLE)); "ui.admin.groups.table.delete", ADMIN_BUNDLE));
default: default:
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Not a valid column index"); "Not a valid column index");
} }
} }

View File

@ -301,12 +301,14 @@ public abstract class AbstractEntityRepository<K, E> {
* *
* @param entity The entity to delete. * @param entity The entity to delete.
*/ */
@Transactional(Transactional.TxType.REQUIRED)
public void delete(final E entity) { public void delete(final E entity) {
if (entity == null) { if (entity == null) {
throw new IllegalArgumentException("Can't delete a null entity."); throw new IllegalArgumentException("Can't delete a null entity.");
} }
entityManager.remove(entity); //We need to make sure we use a none detached entity, therefore the merge
entityManager.remove(entityManager.merge(entity));
} }
protected boolean hasDefaultEntityGraph() { protected boolean hasDefaultEntityGraph() {

View File

@ -19,10 +19,14 @@
package org.libreccm.security; package org.libreccm.security;
import java.util.List; import java.util.List;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.persistence.TypedQuery; import javax.persistence.TypedQuery;
import org.libreccm.core.AbstractEntityRepository; import org.libreccm.core.AbstractEntityRepository;
import javax.transaction.Transactional;
/** /**
* Repository for groups. * Repository for groups.
* *
@ -88,4 +92,17 @@ public class GroupRepository extends AbstractEntityRepository<Long, Group> {
"Group.findAllOrderedByGroupName", Group.class); "Group.findAllOrderedByGroupName", Group.class);
return query.getResultList(); return query.getResultList();
} }
@Override
@Transactional(Transactional.TxType.REQUIRED)
public void delete(final Group entity) {
final Group delete = getEntityManager().find(Group.class,
entity.getPartyId());
delete.getMemberships().forEach(m -> {
getEntityManager().remove(m);
});
getEntityManager().remove(getEntityManager().merge(entity));
}
} }

View File

@ -252,3 +252,4 @@ ui.admin.group.error.name_already_in_use=The provided group name is already in u
ui.admin.group.create_new=Create new group ui.admin.group.create_new=Create new group
ui.admin.group.edit=Edit group ui.admin.group.edit=Edit group
ui.admin.group.name=Name of group ui.admin.group.name=Name of group
ui.admin.group.delete.confirm=Are you sure to delete this group?

View File

@ -252,3 +252,4 @@ ui.admin.group.error.name_already_in_use=Der angegebene Gruppen-Name wird bereit
ui.admin.group.create_new=Neue Gruppe anlegen ui.admin.group.create_new=Neue Gruppe anlegen
ui.admin.group.edit=Gruppe bearbeiten ui.admin.group.edit=Gruppe bearbeiten
ui.admin.group.name=Name der Gruppe ui.admin.group.name=Name der Gruppe
ui.admin.group.delete.confirm=Sind Sie sicher das Sie diese Gruppe l\u00f6schen wollen?

View File

@ -225,3 +225,4 @@ ui.admin.group.error.name_already_in_use=The provided group name is already in u
ui.admin.group.create_new=Create new group ui.admin.group.create_new=Create new group
ui.admin.group.edit=Edit group ui.admin.group.edit=Edit group
ui.admin.group.name=Name of group ui.admin.group.name=Name of group
ui.admin.group.delete.confirm=Are you sure to delete this group?

View File

@ -216,3 +216,4 @@ ui.admin.group.error.name_already_in_use=The provided group name is already in u
ui.admin.group.create_new=Create new group ui.admin.group.create_new=Create new group
ui.admin.group.edit=Edit group ui.admin.group.edit=Edit group
ui.admin.group.name=Name of group ui.admin.group.name=Name of group
ui.admin.group.delete.confirm=Are you sure to delete this group?

View File

@ -660,7 +660,7 @@ table.dataTable{
table * table ,form table { table * table ,form table {
border-style: none; border-style: none;
width: auto; /*width: auto;*/
margin: 0; margin: 0;
} }