package org.osiam.storage.entities.extension;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.osiam.resources.scim.Extension;
import org.osiam.storage.entities.UserEntity; //NOSONAR - Needed due to bidirectional relation

/**
 * Defines a SCIM-Extension.
 */
@Entity(name = "scim_extension")
public class ExtensionEntity implements Serializable {

    private static final long serialVersionUID = 2192284482983111198L;

    @Id
    @GeneratedValue
    @Column(name = "internal_id")
    private long internalId;

    @Column(name = "urn", nullable = false, unique = true)
    private String urn;

    @OneToMany(mappedBy = "extension")
    private Set<ExtensionFieldEntity> fields;

    public long getInternalId() {
        return internalId;
    }

    public void setInternalId(long internalId) {
        this.internalId = internalId;
    }

    public String getUrn() {
        return urn;
    }

    public void setUrn(String extensionUrn) {
        this.urn = extensionUrn;
    }

    public Set<ExtensionFieldEntity> getFields() {
        if (fields == null) {
            fields = new HashSet<>();
        }
        return fields;
    }

    public void setFields(Set<ExtensionFieldEntity> extensionFields) {
        if (extensionFields != null) {
            for (ExtensionFieldEntity extensionFieldEntity : extensionFields) {
                extensionFieldEntity.setExtension(this);
            }
        }
        this.fields = extensionFields;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((urn == null) ? 0 : urn.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        ExtensionEntity other = (ExtensionEntity) obj;
        if (urn == null) {
            if (other.urn != null) {
                return false;
            }
        } else if (!urn.equals(other.urn)) {
            return false;
        }
        return true;
    }


    /**
     * Converting all ExtensionFields of the User into a Scim-Extensions representation.
     *
     * @param userEntity
     *          getting the extensions for conversion
     * @return A Map of extensions with corresponding urn's. Never null
     */
    public static Map<String, Extension> toScim(UserEntity userEntity) {
        // Sorting all fields by extension
        Map<String, Map<String, String>> sortedValues = new HashMap<>();
        for (ExtensionFieldValueEntity extFieldValue : userEntity.getUserExtensions()) {
            String urn = extFieldValue.getExtensionField().getExtension().getUrn();
            String fieldName = extFieldValue.getExtensionField().getName();
            String value = extFieldValue.getValue();

            Map<String, String> extMap = sortedValues.get(urn);
            if (extMap == null) {
                extMap = new HashMap<String, String>();
                sortedValues.put(urn, extMap);
            }
            extMap.put(fieldName, value);
        }

        // Convert Extensions to scim Extensions
        Map<String, Extension> res = new HashMap<>();
        for(Map.Entry<String, Map<String, String>> extensionEntry : sortedValues.entrySet()) {
            Extension ext = new Extension(extensionEntry.getKey());
            for (Entry<String, String> fieldData : extensionEntry.getValue().entrySet()) {
                ext.addOrUpdateField(fieldData.getKey(), fieldData.getValue());
            }
            String urn = extensionEntry.getKey();
            Extension extension = new Extension(urn);
            Map<String, String> extensionEntries = extensionEntry.getValue();
            for (String field:extensionEntries.keySet()){
                extension.addOrUpdateField(field, extensionEntries.get(field));
            }
            res.put(urn, extension);
        }
        return res;
    }
}
@@ -44,162 +44,8 @@
 n;%0A%0A
-import java.io.Serializable;%0Aimport java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;%0Aimport java.util.Set;%0A%0A
 impo
@@ -65,22 +65,17 @@
 istence.
-Column
+*
 ;%0Aimport
@@ -83,274 +83,73 @@
 java
-x.persistence.Entity;%0Aimport javax.persistence.GeneratedValue;%0Aimport javax.persistence.Id;%0Aimport javax.persistence.OneToMany;%0A%0Aimport org.osiam.resources.scim.Extension;%0Aimport org.osiam.storage.entities.UserEntity; //NOSONAR - Needed due to bidirectional relation
+.io.Serializable;%0Aimport java.util.HashSet;%0Aimport java.util.Set;
 %0A%0A/*
@@ -2119,1887 +2119,5 @@
  %7D%0A%0A
-%0A    /**%0A     * Converting all ExtensionFields of the User into a Scim-Extensions representation.%0A     *%0A     * @param userEntity%0A     *          getting the extensions for conversion%0A     * @return A Map of extensions with corresponding urn's. Never null%0A     */%0A    public static Map%3CString, Extension%3E toScim(UserEntity userEntity) %7B%0A        // Sorting all fields by extension%0A        Map%3CString, Map%3CString, String%3E%3E sortedValues = new HashMap%3C%3E();%0A        for (ExtensionFieldValueEntity extFieldValue : userEntity.getUserExtensions()) %7B%0A            String urn = extFieldValue.getExtensionField().getExtension().getUrn();%0A            String fieldName = extFieldValue.getExtensionField().getName();%0A            String value = extFieldValue.getValue();%0A%0A            Map%3CString, String%3E extMap = sortedValues.get(urn);%0A            if (extMap == null) %7B%0A                extMap = new HashMap%3CString, String%3E();%0A                sortedValues.put(urn, extMap);%0A            %7D%0A            extMap.put(fieldName, value);%0A        %7D%0A%0A        // Convert Extensions to scim Extensions%0A        Map%3CString, Extension%3E res = new HashMap%3C%3E();%0A        for(Map.Entry%3CString, Map%3CString, String%3E%3E extensionEntry : sortedValues.entrySet()) %7B%0A            Extension ext = new Extension(extensionEntry.getKey());%0A            for (Entry%3CString, String%3E fieldData : extensionEntry.getValue().entrySet()) %7B%0A                ext.addOrUpdateField(fieldData.getKey(), fieldData.getValue());%0A            %7D%0A            String urn = extensionEntry.getKey();%0A            Extension extension = new Extension(urn);%0A            Map%3CString, String%3E extensionEntries = extensionEntry.getValue();%0A            for (String field:extensionEntries.keySet())%7B%0A                extension.addOrUpdateField(field, extensionEntries.get(field));%0A            %7D%0A            res.put(urn, extension);%0A        %7D%0A        return res;%0A    %7D%0A
 %7D

Remove toScim()