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

io.imunity.console.tprofile.ActionParameterComponentProvider Maven / Gradle / Ivy

/*
 * Copyright (c) 2021 Bixbit - Krzysztof Benedyczak. All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package io.imunity.console.tprofile;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;

import com.google.common.base.Supplier;

import io.imunity.vaadin.endpoint.common.api.HtmlTooltipFactory;
import pl.edu.icm.unity.base.attribute.AttributeType;
import pl.edu.icm.unity.base.authn.CredentialRequirements;
import pl.edu.icm.unity.base.exceptions.EngineException;
import pl.edu.icm.unity.base.identity.IdentityType;
import pl.edu.icm.unity.base.message.MessageSource;
import pl.edu.icm.unity.base.msg_template.UserNotificationTemplateDef;
import pl.edu.icm.unity.base.registration.RegistrationForm;
import pl.edu.icm.unity.base.translation.ActionParameterDefinition;
import pl.edu.icm.unity.base.translation.TranslationProfile;
import pl.edu.icm.unity.engine.api.AttributeTypeManagement;
import pl.edu.icm.unity.engine.api.CredentialRequirementManagement;
import pl.edu.icm.unity.engine.api.GroupsManagement;
import pl.edu.icm.unity.engine.api.MessageTemplateManagement;
import pl.edu.icm.unity.engine.api.RegistrationsManagement;
import pl.edu.icm.unity.engine.api.TranslationProfileManagement;
import pl.edu.icm.unity.engine.api.identity.IdentityTypeSupport;
import pl.edu.icm.unity.engine.api.translation.form.DynamicGroupParam;
import pl.edu.icm.unity.engine.api.utils.PrototypeComponent;

/**
 * Responsible for creating {@link ActionParameterComponent}s.
 * @implNote prototype scope to allow to create a reusable provider per user.
 */
@PrototypeComponent
public class ActionParameterComponentProvider
{
	private final MessageSource msg;
	
	private List groups;
	private Collection credReqs;
	private Collection idTypes;
	private List atTypes;
	private List inputProfiles;
	private List outputProfiles;
	private List userMessageTemplates;
	private List registrationForm;

	private final AttributeTypeManagement attrsMan;
	private final IdentityTypeSupport idTypeSupport;
	private final CredentialRequirementManagement credReqMan;
	private final GroupsManagement groupsMan;
	private final TranslationProfileManagement profileMan;
	private final MessageTemplateManagement msgTemplateMan;
	private final RegistrationsManagement registrationMan;
	private final HtmlTooltipFactory htmlTooltipFactory;
	private final List>> dynamicGroupProviders;
	
	public ActionParameterComponentProvider(MessageSource msg,
			AttributeTypeManagement attrsMan, IdentityTypeSupport idTypeSupport,
			CredentialRequirementManagement credReqMan, GroupsManagement groupsMan,
			TranslationProfileManagement profileMan,
			MessageTemplateManagement msgTemplateMan,
			HtmlTooltipFactory htmlTooltipFactory,
			RegistrationsManagement registrationMan)
	{
		this.msg = msg;
		this.attrsMan = attrsMan;
		this.idTypeSupport = idTypeSupport;
		this.credReqMan = credReqMan;
		this.groupsMan = groupsMan;
		this.profileMan = profileMan;
		this.msgTemplateMan = msgTemplateMan;
		this.registrationMan = registrationMan;
		this.htmlTooltipFactory = htmlTooltipFactory;
		this.dynamicGroupProviders = new ArrayList<>();
	}

	public void init(Supplier> dynamicGroupProvider) throws EngineException
	{
		this.init();
		this.dynamicGroupProviders.add(dynamicGroupProvider);
	}
	
	public void init() throws EngineException
	{
		this.atTypes = new ArrayList<>(attrsMan.getAttributeTypes());
		Collections.sort(atTypes, (a1,a2) -> a1.getName().compareTo(a2.getName()));
		this.groups = new ArrayList<>(groupsMan.getChildGroups("/"));
		Collections.sort(groups);
		Collection crs = credReqMan.getCredentialRequirements();
		credReqs = new TreeSet<>();
		for (CredentialRequirements cr: crs)
			credReqs.add(cr.getName());
		Collection idTypesF = idTypeSupport.getIdentityTypes();
		idTypes = new TreeSet<>();
		for (IdentityType it: idTypesF)
			idTypes.add(it.getIdentityTypeProvider());
		inputProfiles = new ArrayList<>(profileMan.listInputProfiles().keySet());
		Collections.sort(inputProfiles);
		outputProfiles = new ArrayList<>(profileMan.listOutputProfiles().keySet());
		Collections.sort(outputProfiles);
		userMessageTemplates = new ArrayList<>(msgTemplateMan.getCompatibleTemplates(
				UserNotificationTemplateDef.NAME).keySet());
		Collections.sort(userMessageTemplates);
		registrationForm = registrationMan.getForms().stream()
				.map(RegistrationForm::getName)
				.collect(Collectors.toList());
	}

	TranslationProfile getInputProfile(String profile) throws EngineException
	{
		return profileMan.getInputProfile(profile);
	}

	TranslationProfile getOutputProfile(String profile) throws EngineException
	{
		return profileMan.getOutputProfile(profile);
	}
	
	
	public ActionParameterComponent getParameterComponent(ActionParameterDefinition param, EditorContext context)
	{
		switch (param.getType())
		{
		case ENUM:
			return new EnumActionParameterComponent(param, msg);
		case UNITY_ATTRIBUTE:
			return new AttributeActionParameterComponent(param, msg, atTypes);
		case UNITY_GROUP:
			return new BaseEnumActionParameterComponent(param, msg, groups);
		case UNITY_DYNAMIC_GROUP:
			return getUnityGroupActionParameterComponent(param);
		case UNITY_CRED_REQ:
			return new BaseEnumActionParameterComponent(param, msg, credReqs);
		case UNITY_ID_TYPE:
			return new BaseEnumActionParameterComponent(param, msg, idTypes);
		case EXPRESSION:
			return new ExpressionActionParameterComponent(param, msg, htmlTooltipFactory).applyContext(context);
		case DAYS:
			return new DaysActionParameterComponent(param, msg);
		case LARGE_TEXT:
			return new TextAreaActionParameterComponent(param, msg);
		case I18N_TEXT:
			return new LocalizedTextActionParameterComponent(param, msg);
		case BOOLEAN:
			return new BooleanActionParameterComponent(param, msg);
		case INTEGER:
			return new IntegerActionParameterComponent(param, msg);
		case UNITY_INPUT_TRANSLATION_PROFILE:
			return new BaseEnumActionParameterComponent(param, msg, inputProfiles);
		case UNITY_OUTPUT_TRANSLATION_PROFILE:
			return new BaseEnumActionParameterComponent(param, msg, outputProfiles);
		case USER_MESSAGE_TEMPLATE:
			return new BaseEnumActionParameterComponent(param, msg, userMessageTemplates);
		case REGISTRATION_FORM:
			return new BaseEnumActionParameterComponent(param, msg, registrationForm);
		default: 
			return new DefaultActionParameterComponent(param, msg);
		}
	}
	
	private BaseEnumActionParameterComponent getUnityGroupActionParameterComponent(ActionParameterDefinition param)
	{
		ArrayList groupsWithDynamic = new ArrayList<>(groups);
		Map dynamicGroups = new HashMap<>();
		dynamicGroupProviders.forEach(p -> p.get().stream().forEach(dg -> dynamicGroups.put(dg.toSelectionRepresentation(),  dg.getLabel(msg))));
		groupsWithDynamic.addAll(dynamicGroups.keySet());
		BaseEnumActionParameterComponent groupsCombo = new BaseEnumActionParameterComponent(param, msg,
				groupsWithDynamic);
		groupsCombo.setItemLabelGenerator(i -> DynamicGroupParam.isDynamicGroup(i) ? dynamicGroups.get(i) : i);
		return groupsCombo;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy