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 {
private RequestLocal m_isCancel;
@ -65,6 +66,7 @@ public class CancellableValidationListener implements ParameterListener {
m_listener = l;
}
@Override
public void validate(ParameterEvent evt) throws FormProcessException {
PageState ps = evt.getPageState();
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.parameters.StringParameter;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.ui.login.UserForm;
import org.apache.logging.log4j.LogManager;
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.PageState;
import com.arsdigita.bebop.ParameterSingleSelectionModel;
import com.arsdigita.bebop.RequestLocal;
import com.arsdigita.bebop.SaveCancelSection;
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 org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.security.Group;
import org.libreccm.security.GroupRepository;
@ -50,8 +47,9 @@ public class GroupForm extends Form {
private final SaveCancelSection saveCancelSection;
public GroupForm(
final GroupAdmin groupAdmin,
final ParameterSingleSelectionModel<String> selectedGroupId) {
final GroupAdmin groupAdmin,
final ParameterSingleSelectionModel<String> selectedGroupId) {
super("groupform");
this.groupAdmin = groupAdmin;
@ -63,42 +61,20 @@ public class GroupForm extends Form {
final Label target = (Label) e.getTarget();
final String selectedGroupIdStr = selectedGroupId.getSelectedKey(
state);
state);
if (selectedGroupIdStr == null || selectedGroupIdStr.isEmpty()) {
target.setLabel(new GlobalizedMessage(
"ui.admin.group.create_new",
ADMIN_BUNDLE));
"ui.admin.group.create_new",
ADMIN_BUNDLE));
} else {
target.setLabel(new GlobalizedMessage("ui.admin.group.edit",
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.setLabel(new GlobalizedMessage("ui.admin.group.name",
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);
@ -113,14 +89,27 @@ public class GroupForm extends Form {
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 GroupRepository groupRepository = cdiUtil.findBean(
GroupRepository.class);
GroupRepository.class);
if (groupRepository.findByName(groupNameData) != null) {
data.addError(new GlobalizedMessage(
"ui.admin.group.error.name_already_in_use",
ADMIN_BUNDLE));
data.addError(GROUP_NAME, new GlobalizedMessage(
"ui.admin.group.error.name_already_in_use",
ADMIN_BUNDLE));
}
}
});
@ -129,15 +118,15 @@ public class GroupForm extends Form {
final PageState state = e.getPageState();
final String selectedGroupIdStr = selectedGroupId.getSelectedKey(
state);
state);
if (selectedGroupIdStr != null && !selectedGroupIdStr.isEmpty()) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final GroupRepository groupRepository = cdiUtil.findBean(
GroupRepository.class);
GroupRepository.class);
final Group group = groupRepository.findById(Long.parseLong(
selectedGroupIdStr));
selectedGroupIdStr));
groupName.setValue(state, group.getName());
}
});
@ -151,19 +140,19 @@ public class GroupForm extends Form {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final GroupRepository groupRepository = cdiUtil.findBean(
GroupRepository.class);
GroupRepository.class);
final String selectedGroupIdStr = selectedGroupId.
getSelectedKey(state);
getSelectedKey(state);
if (selectedGroupIdStr == null
|| selectedGroupIdStr.isEmpty()) {
|| selectedGroupIdStr.isEmpty()) {
final Group group = new Group();
group.setName(groupNameData);
groupRepository.save(group);
} else {
final Group group = groupRepository.findById(Long.parseLong(
selectedGroupIdStr));
selectedGroupIdStr));
group.setName(groupNameData);
groupRepository.save(group);

View File

@ -58,9 +58,9 @@ public class GroupsTable extends Table {
private final ParameterSingleSelectionModel<String> selectedGroupId;
public GroupsTable(
final GroupAdmin parent,
final TextField groupsTableFilter,
final ParameterSingleSelectionModel<String> selectedGroupId) {
final GroupAdmin parent,
final TextField groupsTableFilter,
final ParameterSingleSelectionModel<String> selectedGroupId) {
super();
setIdAttr("groupsTable");
@ -69,20 +69,21 @@ public class GroupsTable extends Table {
this.selectedGroupId = selectedGroupId;
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();
columnModel.add(new TableColumn(
COL_GROUP_NAME,
new Label(new GlobalizedMessage("ui.admin.groups.table.name",
ADMIN_BUNDLE))));
COL_GROUP_NAME,
new Label(new GlobalizedMessage("ui.admin.groups.table.name",
ADMIN_BUNDLE))));
columnModel.add(new TableColumn(
COL_DELETE,
new Label(new GlobalizedMessage("ui.admin.groups.table.delete",
ADMIN_BUNDLE))));
COL_DELETE,
new Label(new GlobalizedMessage("ui.admin.groups.table.delete",
ADMIN_BUNDLE))));
columnModel.get(COL_GROUP_NAME).setCellRenderer(
new TableCellRenderer() {
new TableCellRenderer() {
@Override
public Component getComponent(final Table table,
final PageState state,
@ -93,28 +94,64 @@ public class GroupsTable extends Table {
final int column) {
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() {
@Override
public void cellSelected(final TableActionEvent event) {
final PageState state = event.getPageState();
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
public void headSelected(final TableActionEvent event) {
//Nothing
}
});
setModelBuilder(new GroupsTableModelBuilder());
}
private class GroupsTableModelBuilder extends LockableImpl
implements TableModelBuilder {
implements TableModelBuilder {
@Override
public TableModel makeModel(final Table table,
@ -123,6 +160,7 @@ public class GroupsTable extends Table {
return new GroupsTableModel(state);
}
}
private class GroupsTableModel implements TableModel {
@ -135,14 +173,14 @@ public class GroupsTable extends Table {
final String filterTerm = (String) groupsTableFilter.getValue(state);
LOGGER.debug("Value of filter is: \"{}\"", filterTerm);
final GroupRepository groupRepository = CdiUtil.createCdiUtil().
findBean(GroupRepository.class);
findBean(GroupRepository.class);
if (filterTerm == null || filterTerm.isEmpty()) {
groups = groupRepository.findAllOrderedByGroupName();
LOGGER.debug("Found {} groups in database.", groups.size());
} else {
groups = groupRepository.searchGroupByName(filterTerm);
LOGGER.debug("Found {} groups in database which match the "
+ "filter \"{}\".",
+ "filter \"{}\".",
groups.size(),
filterTerm);
}
@ -171,10 +209,10 @@ public class GroupsTable extends Table {
return group.getName();
case COL_DELETE:
return new Label(new GlobalizedMessage(
"ui.admin.groups.table.delete", ADMIN_BUNDLE));
"ui.admin.groups.table.delete", ADMIN_BUNDLE));
default:
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.
*/
@Transactional(Transactional.TxType.REQUIRED)
public void delete(final E entity) {
if (entity == null) {
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() {

View File

@ -19,10 +19,14 @@
package org.libreccm.security;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.persistence.TypedQuery;
import org.libreccm.core.AbstractEntityRepository;
import javax.transaction.Transactional;
/**
* Repository for groups.
*
@ -88,4 +92,17 @@ public class GroupRepository extends AbstractEntityRepository<Long, Group> {
"Group.findAllOrderedByGroupName", Group.class);
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.edit=Edit 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.edit=Gruppe bearbeiten
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.edit=Edit 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.edit=Edit 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 {
border-style: none;
width: auto;
/*width: auto;*/
margin: 0;
}