package cz.metacentrum.perun.core.impl.modules.attributes;

import cz.metacentrum.perun.core.api.Attribute;
import cz.metacentrum.perun.core.api.AttributeDefinition;
import cz.metacentrum.perun.core.api.AttributesManager;
import cz.metacentrum.perun.core.api.User;
import cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException;
import cz.metacentrum.perun.core.api.exceptions.InternalErrorException;
import cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException;
import cz.metacentrum.perun.core.impl.PerunSessionImpl;
import cz.metacentrum.perun.core.impl.Utils;
import cz.metacentrum.perun.core.implApi.modules.attributes.UserVirtualAttributesModuleAbstract;

/**
 * Attribute value depends on login-namespace:mu attribute
 *
 * @author Simona Kruppova, Michal Stava
 */
public class urn_perun_user_attribute_def_virt_optionalLogin_namespace_mu extends UserVirtualAttributesModuleAbstract {

	@Override
	public Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
		Attribute attribute = new Attribute(attributeDefinition);

		try {
			Attribute loginInMU = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":login-namespace:mu");
			attribute = Utils.copyAttributeToVirtualAttributeWithValue(loginInMU, attribute);
		} catch (AttributeNotExistsException ex) {
			//That means that egi-ui attribute not exists at all, return empty attribute
			return attribute;
		} catch (WrongAttributeAssignmentException ex) {
			throw new InternalErrorException(ex);
		}
		return attribute;
	}

	public AttributeDefinition getAttributeDefinition() {
		AttributeDefinition attr = new AttributeDefinition();
		attr.setNamespace(AttributesManager.NS_USER_ATTR_VIRT);
		attr.setFriendlyName("optionalLogin-namespace:mu");
		attr.setDisplayName("MU login (if available)");
		attr.setType(String.class.getName());
		attr.setDescription("Masaryk University login (if available)");
		return attr;
	}
}

@@ -252,20 +252,176 @@
 ore.api.
-User
+ExtSource;%0Aimport cz.metacentrum.perun.core.api.ExtSourcesManager;%0Aimport cz.metacentrum.perun.core.api.User;%0Aimport cz.metacentrum.perun.core.api.UserExtSource
 ;%0Aimport
@@ -843,16 +843,103 @@
 bstract;
+%0Aimport java.util.List;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0A/**%0A *
@@ -1164,16 +1164,189 @@
 ract %7B%0A%0A
+%09private final String EXTSOURCE_MUNI_IDP2 = %22https://idp2.ics.muni.cz/idp/shibboleth%22;%0A%09private final Pattern loginMUPattern = Pattern.compile(%22%5E(%5B0-9%5D+)%5B@%5Dmuni%5B.%5Dcz$%22);%0A%09%0A%0A
 %09@Overri
@@ -1869,14 +1869,16 @@
 hat 
-egi-ui
+mu login
  att
@@ -1905,53 +1905,8 @@
  all
-, return empty attribute%0A%09%09%09return attribute;
 %0A%09%09%7D
@@ -1997,16 +1997,1101 @@
 ex);%0A%09%09%7D
+%0A%0A%09%09//if attribute is still null (empty login in mu or not existing attribute), try to find uco in user ext sources%0A%09%09if(attribute.getValue() == null) %7B%0A%09%09%09List%3CUserExtSource%3E userExtSources = sess.getPerunBl().getUsersManagerBl().getUserExtSources(sess, user);%0A%09%09%09for(UserExtSource userExtSource : userExtSources) %7B%0A%09%09%09%09ExtSource extSource = userExtSource.getExtSource();%0A%0A%09%09%09%09//Skip if extSource is not the one we are looking for%0A%09%09%09%09if(userExtSource.getLogin() == null %7C%7C extSource == null) continue;%0A%09%09%09%09if(!ExtSourcesManager.EXTSOURCE_IDP.equals(extSource.getType())) continue;%0A%09%09%09%09if(!EXTSOURCE_MUNI_IDP2.equals(extSource.getName())) continue;%0A%0A%09%09%09%09//Get login from this extSource and get only UCO from it%0A%09%09%09%09String login = userExtSource.getLogin();%0A%09%09%09%09Matcher loginMUMatcher = loginMUPattern.matcher(login);%0A%09%09%09%09//This user has login in mu, but in weird format so skip this one%0A%09%09%09%09if(!loginMUMatcher.find()) continue;%0A%09%09%09%09//It is ok, take UCO from login and set it to attribute value%0A%09%09%09%09String UCO = loginMUMatcher.group(1);%0A%09%09%09%09attribute.setValue(UCO);%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A
 %0A%09%09retur

Extend module for attribute optional Login for mu