All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.edu.icm.unity.webadmin.groupdetails.AttributeStatementsTable Maven / Gradle / Ivy

There is a newer version: 3.3.4
Show newest version
/*
 * Copyright (c) 2013 ICM Uniwersytet Warszawski All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package pl.edu.icm.unity.webadmin.groupdetails;

import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import com.vaadin.shared.ui.grid.DropMode;
import com.vaadin.ui.components.grid.GridDragSource;
import com.vaadin.ui.components.grid.GridDropTarget;

import io.imunity.webadmin.attributeStatment.AttrStatementWithId;
import io.imunity.webadmin.attributeStatment.AttributeStatementEditDialog;
import io.imunity.webadmin.directoryBrowser.GroupChangedEvent;
import pl.edu.icm.unity.MessageSource;
import pl.edu.icm.unity.engine.api.AttributeTypeManagement;
import pl.edu.icm.unity.engine.api.GroupsManagement;
import pl.edu.icm.unity.types.basic.AttributeStatement;
import pl.edu.icm.unity.types.basic.Group;
import pl.edu.icm.unity.webui.WebSession;
import pl.edu.icm.unity.webui.bus.EventsBus;
import pl.edu.icm.unity.webui.common.ConfirmDialog;
import pl.edu.icm.unity.webui.common.DnDGridUtils;
import pl.edu.icm.unity.webui.common.GenericElementsTable;
import pl.edu.icm.unity.webui.common.NotificationPopup;
import pl.edu.icm.unity.webui.common.SingleActionHandler;
import pl.edu.icm.unity.webui.common.attributes.AttributeHandlerRegistry;

/**
 * Table with attribute statements. Allows for management operations.
 * @author K. Benedyczak
 */
public class AttributeStatementsTable extends GenericElementsTable
{
	private final String DND_TYPE = "attribute_statement";
	private MessageSource msg;
	private GroupsManagement groupsMan;
	private AttributeTypeManagement attrsMan;
	private Group group;
	private EventsBus bus;
	private AttributeHandlerRegistry attributeHandlerRegistry;
	
	public AttributeStatementsTable(MessageSource msg, GroupsManagement groupsMan,
			AttributeTypeManagement attrsMan,
			AttributeHandlerRegistry attributeHandlerRegistry)
	{

		super(msg.getMessage("AttributeStatements.tableHdr"), a -> a.toString(), false);
		this.msg = msg;
		this.groupsMan = groupsMan;
		this.attrsMan = attrsMan;
		this.attributeHandlerRegistry = attributeHandlerRegistry;
		this.bus = WebSession.getCurrent().getEventBus();
		setMultiSelect(false);
		
		addActionHandler(getAddAction());
		addActionHandler(getEditAction());
		addActionHandler(getDeleteAction());

		
		GridDragSource source = new GridDragSource<>(this);
		source.setDragDataGenerator(DND_TYPE, as -> "{}");
		source.addGridDragStartListener(e -> source.setDragData(e.getDraggedItems().iterator().next()));
		source.addGridDragEndListener(e -> source.setDragData(null));

		GridDropTarget target = new GridDropTarget<>(this,
				DropMode.ON_TOP_OR_BETWEEN);
		target.setDropCriteriaScript(DnDGridUtils.getTypedCriteriaScript(DND_TYPE));
		target.addGridDropListener(e -> 
		{
			Optional dragData = e.getDragData();
			if (!dragData.isPresent() || e.getDropTargetRow() == null 
					|| e.getDropTargetRow().get() == null)
				return;
			int index = contents.indexOf(e.getDropTargetRow().get());
			AttrStatementWithId attrS = (AttrStatementWithId) dragData.get();
			contents.remove(attrS);
			contents.add(index, attrS);
			updateGroup();
		});
	}

	public void setInput(Group group)
	{
		this.group = group;
		super.setInput(Arrays.stream(group.getAttributeStatements())
				.map(AttrStatementWithId::new)
				.collect(Collectors.toList()));
	}

	private void updateGroup()
	{
		AttributeStatement[] attributeStatements = contents.stream()
				.map(withId -> withId.statement)
				.collect(Collectors.toList())
				.toArray(new AttributeStatement[contents.size()]);
		Group updated = group.clone();
		updated.setAttributeStatements(attributeStatements);
		try
		{
			groupsMan.updateGroup(updated.toString(), updated, "set group statement",  Arrays.asList(attributeStatements).toString());
			bus.fireEvent(new GroupChangedEvent(group.toString()));
		} catch (Exception e)
		{
			NotificationPopup.showError(msg,
					msg.getMessage("AttributeStatements.cantUpdateGroup"), e);
		}
	}

	private SingleActionHandler getAddAction()
	{
		return SingleActionHandler.builder4Add(msg, AttrStatementWithId.class)
				.withHandler(this::showAddDialog).build();
	}

	private void showAddDialog(Set target)
	{

		new AttributeStatementEditDialog(msg, null, attrsMan, group.toString(),
				attributeHandlerRegistry, groupsMan, st -> addStatement(st)).show();
	}

	private void addStatement(AttributeStatement newStatement)
	{
		addElement(new AttrStatementWithId(newStatement));
		updateGroup();
	}

	private SingleActionHandler getEditAction()
	{
		return SingleActionHandler.builder4Edit(msg, AttrStatementWithId.class)
				.withHandler(this::showEditDialog).build();
	}

	private void showEditDialog(Collection target)
	{
		AttrStatementWithId st = target.iterator().next();
		AttributeStatement old = st.statement.clone();
		new AttributeStatementEditDialog(msg, old, attrsMan, group.toString(),
				attributeHandlerRegistry, groupsMan,
				newSt -> updateStatement(st, newSt)).show();
	}

	private void updateStatement(AttrStatementWithId oldStatement,
			AttributeStatement newStatement)
	{
		int index = contents.indexOf(oldStatement);
		contents.set(index, new AttrStatementWithId(newStatement));
		updateGroup();
	}

	private SingleActionHandler getDeleteAction()
	{
		return SingleActionHandler.builder4Delete(msg, AttrStatementWithId.class)
				.withHandler(this::deleteHandler).build();
	}

	private void deleteHandler(Set items)
	{
		new ConfirmDialog(msg, msg.getMessage("AttributeStatements.confirmDelete"), () -> {
			removeStatements(items);
		}).show();
	}

	private void removeStatements(Collection removedStatements)
	{
		removedStatements.forEach(this::removeElement);
		updateGroup();
	}
}