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

pl.edu.icm.unity.engine.groupMember.GroupMemberService Maven / Gradle / Ivy

/*
 * Copyright (c) 2018 Bixbit - Krzysztof Benedyczak. All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package pl.edu.icm.unity.engine.groupMember;

import com.google.common.base.Stopwatch;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import pl.edu.icm.unity.base.utils.Log;
import pl.edu.icm.unity.engine.api.groupMember.GroupMemberWithAttributes;
import pl.edu.icm.unity.store.api.*;
import pl.edu.icm.unity.store.types.StoredAttribute;
import pl.edu.icm.unity.store.types.StoredIdentity;
import pl.edu.icm.unity.types.basic.*;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.*;

@Component
class GroupMemberService
{
	private static final Logger log = Log.getLogger(Log.U_SERVER_CORE, GroupMemberService.class);

	private final EntityDAO entityDAO;
	private final AttributeTypeDAO attributeTypeDAO;
	private final MembershipDAO membershipDAO;
	private final AttributeDAO attributeDAO;
	private final IdentityDAO identityDAO;

	GroupMemberService(EntityDAO entityDAO, AttributeTypeDAO attributeTypeDAO,
	                   MembershipDAO membershipDAO, AttributeDAO attributeDAO, IdentityDAO identityDAO) {
		this.entityDAO = entityDAO;
		this.attributeTypeDAO = attributeTypeDAO;
		this.membershipDAO = membershipDAO;
		this.attributeDAO = attributeDAO;
		this.identityDAO = identityDAO;
	}

	List getGroupMembersWithAttributes(String group, List attributes) {
		Set globalAttr = getGlobalAttributes(attributes);

		Stopwatch stopwatch = Stopwatch.createStarted();
		Map>> groupedAttributes = getAttributes(List.of(group), attributes, globalAttr);
		log.debug("Attributes in groups retrieval: {}", stopwatch.toString());

		return getGroupMembersWithAttributes(group, globalAttr, groupedAttributes);
	}

	Map> getGroupMembersWithAttributes(List groups, List attributes)
	{
		Map> groupMembers = new HashMap<>();

		Set globalAttr = getGlobalAttributes(attributes);

		Map>> groupedAttributes = getAttributes(groups, attributes, globalAttr);

		for (String grp: groups)
		{
			groupMembers.put(grp, getGroupMembersWithAttributes(grp, globalAttr, groupedAttributes));
		}

		return groupMembers;
	}

	private Set getGlobalAttributes(List attributes)
	{
		Map allAsMap = attributeTypeDAO.getAllAsMap();
		if(attributes.isEmpty())
			return getGlobalAttributes(allAsMap.values().stream());

		return getGlobalAttributes(attributes.stream()
				.map(allAsMap::get)
				.filter(Objects::nonNull)
		);
	}

	private Set getGlobalAttributes(Stream allAsMap)
	{
		return allAsMap
				.filter(AttributeType::isGlobal)
				.map(AttributeType::getName)
				.collect(Collectors.toSet());
	}

	private List getGroupMembersWithAttributes(String group, Set globalAttr, Map>> groupedAttributes)
	{
		Stopwatch stopwatch = Stopwatch.createStarted();
		Map entityInfo = getEntityInfo(group);
		log.debug("Entities data retrieval: {}", stopwatch.toString());

		stopwatch.reset();
		stopwatch.start();
		Map> memberships = getMemberships(group);
		log.debug("Group membership data retrieval: {}", stopwatch.toString());

		stopwatch.reset();
		stopwatch.start();
		Map> identities = getIdentities(group);
		log.debug("Identities data retrieval: {}", stopwatch.toString());

		List ret = new ArrayList<>();
		for (Long memberId: memberships.keySet())
		{
			Map> memberGroupsWithAttr = groupedAttributes.getOrDefault(memberId, Map.of());
			Collection groupAttributes = memberGroupsWithAttr.getOrDefault(group, Map.of()).values();
			List globalAttributes = memberGroupsWithAttr.getOrDefault("/", Map.of()).values().stream()
					.filter(attributeExt -> globalAttr.contains(attributeExt.getName()))
					.collect(toList());
			Collection values = Stream.concat(groupAttributes.stream(), globalAttributes.stream())
					.collect(toMap(Attribute::getName, identity(), (attr1, attr2) -> attr1.getGroupPath().equals("/") ? attr2 : attr1))
					.values();
			ret.add(new GroupMemberWithAttributes(entityInfo.get(memberId), identities.get(memberId), values));
		}

		return ret;
	}

	private Map getEntityInfo(String group)
	{
		return entityDAO.getByGroup(group).stream()
			.collect(toMap(EntityInformation::getId, identity()));
	}

	private Map> getMemberships(String group)
	{
		Stopwatch w = Stopwatch.createStarted();
		List all = membershipDAO.getMembers(group);
		log.debug("getMemberships {}", w.toString());

		return all.stream()
				.collect(groupingBy(GroupMembership::getEntityId, mapping(GroupMembership::getGroup, toSet())));
	}

	private Map> getIdentities(String group)
	{
		Stopwatch w = Stopwatch.createStarted();
		List all = identityDAO.getByGroup(group);
		log.debug("getIdentities {}", w.toString());

		return all.stream()
				.collect(groupingBy(StoredIdentity::getEntityId, mapping(StoredIdentity::getIdentity, toList())));
	}

	private Map>> getAttributes(List groups,
	                                                                        List attributes,
	                                                                        Set globalAttributes)
	{
		List groupAttr;
		List globalAttr = new ArrayList<>();
		if(attributes != null && !attributes.isEmpty())
			groupAttr = attributeDAO.getAttributesOfGroupMembers(attributes, groups);
		else
			groupAttr = attributeDAO.getAttributesOfGroupMembers(groups);
		if(!globalAttributes.isEmpty())
			globalAttr = attributeDAO.getAttributesOfGroupMembers(new ArrayList<>(globalAttributes), List.of("/"));

		return Stream.concat(groupAttr.stream(), globalAttr.stream())
				.collect(
						groupingBy(StoredAttribute::getEntityId,
								groupingBy(attribute -> attribute.getAttribute().getGroupPath(),
										toMap(
												attribute -> attribute.getAttribute().getName(),
												StoredAttribute::getAttribute,
												(attr1, attr2) -> attr1.getGroupPath().equals("/") ? attr2 : attr1))
						)
				);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy