package org.pdxfinder.admin.pojos;

import org.pdxfinder.rdbms.dao.MappingEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/*
 * Created by csaba on 18/06/2018.
 */
public class MappingContainer {

    private int size;

    /**
     * A container holding mapped and unmapped entities
     */
    private TreeMap<Long, MappingEntity> mappings;



    public MappingContainer(TreeMap<Long, MappingEntity> mappings) {
        this.mappings = new TreeMap<>();
        this.mappings = mappings;
        size = mappings.size();
    }

    public MappingContainer() {

        mappings = new TreeMap<>();
        size = 0;
    }

    public TreeMap<Long, MappingEntity> getMappings() {
        return mappings;
    }

    public void setMappings(TreeMap<Long, MappingEntity> mappings) {
        this.mappings = mappings;
    }



    public MappingEntity getEntityById(Long id){

        if(mappings.containsKey(id)) return mappings.get(id);

        return null;
    }

    public Long getNextAvailableId(){

        return (long) size +1;
    }


    public void add(MappingEntity me){

        mappings.put(me.getEntityId(), me);
        size += 1;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }


    //method to transform the container something more processable by the frontend
    public Map<String, List<MappingEntity>> getEntityList(){

        List<MappingEntity> list = mappings.values().stream().collect(Collectors.toList());

        Map<String, List<MappingEntity>> entityMap = new HashMap<>();
        entityMap.put("mappings", list);


        return entityMap;
    }
}

@@ -392,34 +392,36 @@
 private TreeMap%3C
-Lo
+Stri
 ng, MappingEntit
@@ -464,34 +464,36 @@
 ntainer(TreeMap%3C
-Lo
+Stri
 ng, MappingEntit
@@ -511,49 +511,8 @@
 ) %7B%0A
-        this.mappings = new TreeMap%3C%3E();%0A
     
@@ -685,34 +685,36 @@
  public TreeMap%3C
-Lo
+Stri
 ng, MappingEntit
@@ -800,18 +800,20 @@
 TreeMap%3C
-Lo
+Stri
 ng, Mapp
@@ -917,18 +917,20 @@
 ityById(
-Lo
+Stri
 ng id)%7B%0A
@@ -1135,24 +1135,86 @@
 ntity me)%7B%0A%0A
+        if(!mappings.containsKey(me.getContainerKey()))%7B%0A%0A    
         mapp
@@ -1232,16 +1232,20 @@
 .get
-EntityId
+ContainerKey
 (), 
@@ -1249,16 +1249,20 @@
 ), me);%0A
+    
         
@@ -1272,16 +1272,27 @@
 e += 1;%0A
+        %7D%0A%0A
     %7D%0A%0A 
@@ -1551,20 +1551,19 @@
 etEntity
-List
+Map
 ()%7B%0A%0A   
@@ -1797,10 +1797,265 @@
 ;%0A    %7D%0A
+%0A%0A    //method to transform the container something more processable by the frontend%0A    public List%3CMappingEntity%3E getEntityList()%7B%0A%0A        List%3CMappingEntity%3E list = mappings.values().stream().collect(Collectors.toList());%0A%0A%0A        return list;%0A    %7D%0A
 %7D%0A

Implement additional methods for container