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

pl.edu.icm.unity.engine.translation.form.RegistrationMVELContext Maven / Gradle / Ivy

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

import pl.edu.icm.unity.engine.api.attributes.AttributeValueSyntax;
import pl.edu.icm.unity.engine.api.registration.RequestSubmitStatus;
import pl.edu.icm.unity.engine.api.translation.form.RegistrationMVELContextKey;
import pl.edu.icm.unity.engine.attribute.AttributeTypeHelper;
import pl.edu.icm.unity.types.basic.Attribute;
import pl.edu.icm.unity.types.basic.IdentityParam;
import pl.edu.icm.unity.types.registration.AttributeRegistrationParam;
import pl.edu.icm.unity.types.registration.BaseForm;
import pl.edu.icm.unity.types.registration.BaseRegistrationInput;
import pl.edu.icm.unity.types.registration.EnquiryResponse;
import pl.edu.icm.unity.types.registration.GroupRegistrationParam;
import pl.edu.icm.unity.types.registration.GroupSelection;
import pl.edu.icm.unity.types.registration.IdentityRegistrationParam;
import pl.edu.icm.unity.types.registration.RegistrationContext.TriggeringMode;
import pl.edu.icm.unity.types.registration.RegistrationRequest;
import pl.edu.icm.unity.types.registration.Selection;

/**
 * MVEL context is a string keyed map. This class adds initialization for registration profile processing.
 * @author Krzysztof Benedyczak
 */
public class RegistrationMVELContext extends HashMap
{
	/**
	 * Setups a full context for {@link EnquiryResponse} or {@link RegistrationRequest} processing
	 */
	public RegistrationMVELContext(BaseForm form, BaseRegistrationInput response,
			RequestSubmitStatus status, TriggeringMode triggered, boolean idpEndpoint, String requestId, 
			AttributeTypeHelper atHelper)
	{
		initCommon(form, response, status, triggered, idpEndpoint, requestId, atHelper);
		put(RegistrationMVELContextKey.validCode.name(), response.getRegistrationCode() != null);
	}
	
	private void initCommon(BaseForm form, BaseRegistrationInput request,
			RequestSubmitStatus status, TriggeringMode triggered, boolean idpEndpoint, String requestId, 
			AttributeTypeHelper atHelper)
	{
		createBaseMvelContext(form, status, triggered, idpEndpoint);
		
		put(RegistrationMVELContextKey.userLocale.name(), request.getUserLocale());
		put(RegistrationMVELContextKey.requestId.name(), requestId);
		
		setupAttributes(form, request, atHelper);
		setupIdentities(form, request);
		setupGroups(form, request);
		setupAgreements(request);
	}
	
	private void setupAttributes(BaseForm form, BaseRegistrationInput request, AttributeTypeHelper atHelper)
	{
		Map attr = new HashMap<>();
		Map> attrs = new HashMap<>();
		Map rattr = new HashMap<>();
		Map> rattrs = new HashMap<>();
		
		for (int i=0; i values = attributeValuesAsDomainObjects(attribute.getName(), 
					attribute.getValues(), atHelper);
			Object v = values.isEmpty() ? "" : values.get(0);
			
			attr.put(attribute.getName(), v);
			attrs.put(attribute.getName(), values);

			if (attributeRegistrationParam.getRetrievalSettings().isAutomaticOnly())
			{
				rattr.put(attribute.getName(), v);
				rattrs.put(attribute.getName(), values);				
			}
		}
		put(RegistrationMVELContextKey.attr.name(), attr);
		put(RegistrationMVELContextKey.attrs.name(), attrs);
		put(RegistrationMVELContextKey.rattr.name(), rattr);
		put(RegistrationMVELContextKey.rattrs.name(), rattrs);
	}
	
	private List attributeValuesAsDomainObjects(String attributeName, List values, 
			AttributeTypeHelper atHelper)
	{
		AttributeValueSyntax syntax = atHelper.getUnconfiguredSyntaxForAttributeName(attributeName);
		List ret = new ArrayList<>(values.size());
		for (String value: values)
			ret.add(syntax.convertFromString(value));
		return ret;
	}
	
	private void setupIdentities(BaseForm form, BaseRegistrationInput request)
	{
		Map> idsByType = new HashMap<>();
		Map> ridsByType = new HashMap<>();
		Map> idsByTypeObj = new HashMap<>();
		Map> ridsByTypeObj = new HashMap<>();
		for (int i=0; i vals = idsByType.get(identityParam.getTypeId());
			if (vals == null)
			{
				vals = new ArrayList<>();
				idsByType.put(identityParam.getTypeId(), vals);
			}
			vals.add(identityParam.getValue());

			List valsObj = idsByTypeObj.get(identityParam.getTypeId());
			if (valsObj == null)
			{
				valsObj = new ArrayList<>();
				idsByTypeObj.put(identityParam.getTypeId(), valsObj);
			}
			valsObj.add(identityParam);
			
			
			if (identityRegistrationParam.getRetrievalSettings().isAutomaticOnly())
			{
				List rvals = ridsByType.get(identityParam.getTypeId());
				if (rvals == null)
				{
					rvals = new ArrayList<>();
					ridsByType.put(identityParam.getTypeId(), rvals);
				}
				rvals.add(identityParam.getValue());			

				List rvalsObj = ridsByTypeObj.get(identityParam.getTypeId());
				if (rvalsObj == null)
				{
					rvalsObj = new ArrayList<>();
					ridsByTypeObj.put(identityParam.getTypeId(), rvalsObj);
				}
				rvalsObj.add(identityParam);			
			}
		}

		put(RegistrationMVELContextKey.idsByType.name(), idsByType);
		put(RegistrationMVELContextKey.ridsByType.name(), ridsByType);
		put(RegistrationMVELContextKey.idsByTypeObj.name(), idsByTypeObj);
		put(RegistrationMVELContextKey.ridsByTypeObj.name(), ridsByTypeObj);
	}
	
	private void setupGroups(BaseForm form, BaseRegistrationInput request)
	{
		List groups = new ArrayList<>();
		List rgroups = new ArrayList<>();
		for (int i=0; i agr = new ArrayList();
		for (Selection a : request.getAgreements())
		{
			agr.add(Boolean.toString(a.isSelected()));
		}
		put(RegistrationMVELContextKey.agrs.name(), agr);
	}
	
	private void createBaseMvelContext(BaseForm form,
			RequestSubmitStatus status, TriggeringMode triggered, boolean idpEndpoint)
	{
		put(RegistrationMVELContextKey.onIdpEndpoint.name(), idpEndpoint);
		put(RegistrationMVELContextKey.triggered.name(), triggered.toString());
		put(RegistrationMVELContextKey.status.name(), status.toString());
		put(RegistrationMVELContextKey.registrationForm.name(), form.getName());
		
		Map> empty = new HashMap<>();
		put(RegistrationMVELContextKey.attr.name(), empty);
		put(RegistrationMVELContextKey.attrs.name(), empty);
		put(RegistrationMVELContextKey.rattr.name(), empty);
		put(RegistrationMVELContextKey.rattrs.name(), empty);
		put(RegistrationMVELContextKey.idsByType.name(), empty);
		put(RegistrationMVELContextKey.ridsByType.name(), empty);
		put(RegistrationMVELContextKey.idsByTypeObj.name(), empty);
		put(RegistrationMVELContextKey.ridsByTypeObj.name(), empty);
		
		List emptyL = new ArrayList<>();
		put(RegistrationMVELContextKey.groups.name(), emptyL);
		put(RegistrationMVELContextKey.rgroups.name(), emptyL);
		put(RegistrationMVELContextKey.agrs.name(), emptyL);
	}
	
	@Override
	public String toString()
	{
		StringJoiner joiner = new StringJoiner("\n");
		forEach((key, value) -> joiner.add(key + " = " + value));
		return joiner.toString();
	}
}