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

pl.edu.icm.unity.home.iddetails.UserIdentitiesPanel Maven / Gradle / Ivy

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import com.vaadin.ui.Label;

import pl.edu.icm.unity.MessageSource;
import pl.edu.icm.unity.engine.api.EntityManagement;
import pl.edu.icm.unity.engine.api.identity.IdentityTypeDefinition;
import pl.edu.icm.unity.engine.api.identity.IdentityTypeSupport;
import pl.edu.icm.unity.exceptions.EngineException;
import pl.edu.icm.unity.types.basic.Entity;
import pl.edu.icm.unity.types.basic.EntityParam;
import pl.edu.icm.unity.types.basic.Identity;
import pl.edu.icm.unity.types.basic.IdentityParam;
import pl.edu.icm.unity.types.basic.IdentityType;
import pl.edu.icm.unity.webui.common.FormValidationException;
import pl.edu.icm.unity.webui.common.composite.ComponentsGroup;
import pl.edu.icm.unity.webui.common.composite.CompositeLayoutAdapter.ComposableComponents;
import pl.edu.icm.unity.webui.common.composite.GroupOfGroups;
import pl.edu.icm.unity.webui.common.identities.IdentityEditorRegistry;
import pl.edu.icm.unity.webui.common.identities.SingleTypeIdentityEditor;

/**
 * Shows (optionally in edit mode) all configured identities.
 * 
 * @author K. Benedyczak
 */
public class UserIdentitiesPanel
{
	private MessageSource msg;
	protected IdentityEditorRegistry identityEditorReg;
	protected EntityManagement idsManagement;
	private long entityId;
	private List identityEditors;
	private Map> editableIdsByType;
	private Map> roIdsByType;
	private List roLabels;
	private IdentityTypeSupport idTypeSupport;
	private GroupOfGroups componentsGroup;
	
	public UserIdentitiesPanel(MessageSource msg, IdentityEditorRegistry identityEditorReg,
			EntityManagement idsManagement, long entityId, IdentityTypeSupport idTypeSupport) 
					throws EngineException
	{
		this.msg = msg;
		this.identityEditorReg = identityEditorReg;
		this.idsManagement = idsManagement;
		this.entityId = entityId;
		this.idTypeSupport = idTypeSupport;
		
		identityEditors = new ArrayList<>();
		roLabels = new ArrayList<>();
		componentsGroup = new GroupOfGroups();
		
		initIdentities();
		initUI();
	}

	private void initIdentities() throws EngineException
	{
		Entity entity = idsManagement.getEntity(new EntityParam(entityId));
		List identities = entity.getIdentities();
		editableIdsByType = new HashMap<>();
		roIdsByType = new HashMap<>();
		Collection identityTypes = idTypeSupport.getIdentityTypes();
		for (IdentityType idType: identityTypes)
		{
			if (idType.isSelfModificable())
				editableIdsByType.put(idType, new ArrayList());
		}
		for (Identity id: identities)
		{
			IdentityType type = idTypeSupport.getType(id.getTypeId());
			boolean editable = type.isSelfModificable(); 
			List list = (editable ? editableIdsByType : roIdsByType).get(type);
			if (list == null)
			{
				list = new ArrayList();
				(editable ? editableIdsByType : roIdsByType).put(type, list);
			}
			list.add(id);
		}
	}
	
	public ComposableComponents getContents()
	{
		return componentsGroup;
	}
	
	private void initUI() throws EngineException
	{
		identityEditors.clear();
		roLabels.clear();
		for (IdentityType typeKey: roIdsByType.keySet())
			addRoIdentity(typeKey);
		for (IdentityType typeKey: editableIdsByType.keySet())
			addEditableIdentity(typeKey);
	}
	
	private void addRoIdentity(IdentityType idType)
	{		
		List idList = roIdsByType.get(idType);
		for (int i = 0; i < idList.size(); i++)
		{
			Identity id = idList.get(i);
			IdentityTypeDefinition typeDef = idTypeSupport.getTypeDefinition(id.getTypeId());
			Label label = new Label(typeDef.toPrettyStringNoPrefix(id));
			String caption = typeDef.getHumanFriendlyName(msg);
			caption += (i > 0) ? " (" + (i+1) + "):" : ":";
			label.setCaption(caption);
			ComponentsGroup wrappedLabel = new ComponentsGroup(label);
			roLabels.add(wrappedLabel);
			componentsGroup.addComposableComponents(wrappedLabel);
		}
	}

	private void addEditableIdentity(IdentityType idType)
	{		
		List idList = editableIdsByType.get(idType);
		SingleTypeIdentityEditor singleTypeIdentityEditor = new SingleTypeIdentityEditor(idType, 
				idList, identityEditorReg, msg, idTypeSupport);
		identityEditors.add(singleTypeIdentityEditor);
		componentsGroup.addComposableComponents(singleTypeIdentityEditor.getComponentsGroup());
	}
	
	private void clear()
	{
		for (ComponentsGroup l: roLabels)
			l.removeAll();
		for (SingleTypeIdentityEditor e: identityEditors)
			e.removeAll();
	}

	public void refresh() throws EngineException
	{
		clear();
		initIdentities();
		initUI();
	}
	
	public void validate() throws FormValidationException
	{
		for (SingleTypeIdentityEditor editor: identityEditors)
			editor.getIdentities();
	}
	
	public void saveChanges() throws Exception
	{
		Collection newIdentities = new HashSet<>();
		Collection types = new HashSet<>();
		for (SingleTypeIdentityEditor editor: identityEditors)
		{
			try
			{
				newIdentities.addAll(editor.getIdentities());
			} catch (FormValidationException e)
			{
				throw new IllegalStateException("validation error on the idneities, "
						+ "i.e. validation was not performed earlier");
			}
			types.add(editor.getType().getIdentityTypeProvider());
		}
		
		idsManagement.setIdentities(new EntityParam(entityId), types, newIdentities);
	}

	public boolean hasEditable()
	{
		return identityEditors.size() > 0;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy