Advertisement
Guest User

Convert flat table to tree

a guest
Sep 16th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. package tr2;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Map;
  5. import java.util.concurrent.ConcurrentHashMap;
  6.  
  7. import com.google.gson.Gson;
  8. import com.google.gson.GsonBuilder;
  9.  
  10. public class Alta {
  11.  
  12.     static Map<Long, Linie> m = new ConcurrentHashMap<Long, Linie>();
  13.  
  14.     public static void main(String[] args) {
  15.         Response response = new Response();
  16.         populateMap();
  17.         // afiseazaMap();
  18.         Long actual = getFirstAncestor(m);
  19.         response.setId(actual);
  20.         response.setName(m.get(actual).getName());
  21.         response.setChildren(takeTree(actual, m));
  22.         System.out.println(response);
  23.        
  24.         Gson gs = new GsonBuilder().setPrettyPrinting().create();
  25.         String jso = gs.toJson(response);
  26.         System.out.println(jso);
  27.     }
  28.  
  29.     private static ArrayList<Response> takeTree(Long actual,
  30.             Map<Long, Linie> map) {
  31.         System.out.println("[takeTree] actual = " + actual);
  32.         ArrayList<Response> al = new ArrayList<Response>();
  33.         for (Long key : map.keySet()) {
  34.             Linie l = map.get(key);
  35.             if (l != null && l.getParentId() != null && l.getParentId().equals(actual)) {
  36.                 Response r = new Response();
  37.                 r.setId(l.getId());
  38.                 r.setName(l.getName());
  39.                 r.setChildren(takeTree(l.getId(), map));
  40.                 al.add(r);
  41.             }
  42.         }
  43.         return al;
  44.     }
  45.  
  46.     private static Long getFirstAncestor(Map<Long, Linie> m) {
  47.         for (Long i : m.keySet()) {
  48.             return m.get(i).getUltimateId();
  49.         }
  50.         return null;
  51.     }
  52.  
  53.     public static void afiseazaMap(Map<Long, Linie> m) {
  54.         for (Long key : m.keySet()) {
  55.             Linie l = m.get(key);
  56.             System.out.println(l.getId() + " --- " + l.getParentId() + " --- "
  57.                     + l.getUltimateId() + " --- " + l.getName());
  58.         }
  59.     }
  60.  
  61.     public static void populateMap() {
  62.         add(1, 3, 5);
  63.         add(2, 3, 5);
  64.         add(3, 5, 5);
  65.         add(4, 5, 5);
  66.         m.put((long) 5, new Linie(new Long(5), null, new Long(5)));
  67.     }
  68.  
  69.     public static void add(long i, long j, long k) {
  70.         m.put((long) i, new Linie(i, j, k));
  71.     }
  72. }
  73.  
  74. class Linie {
  75.  
  76.     Long id;
  77.     Long parentId;
  78.     Long ultimateId;
  79.     String name = "unNume";
  80.  
  81.     public Linie(long id, long parentId, long ultimateId) {
  82.         this.id = id;
  83.         this.parentId = parentId;
  84.         this.ultimateId = ultimateId;
  85.         this.name = this.name + id;
  86.     }
  87.  
  88.     public Linie(Long id, Long parentId, Long ultimateId) {
  89.         this.id = id;
  90.         this.parentId = parentId;
  91.         this.ultimateId = ultimateId;
  92.         this.name = this.name + id;
  93.     }
  94.  
  95.     public Long getId() {
  96.         return id;
  97.     }
  98.  
  99.     public void setId(Long id) {
  100.         this.id = id;
  101.     }
  102.  
  103.     public Long getParentId() {
  104.         return parentId;
  105.     }
  106.  
  107.     public void setParentId(Long parentId) {
  108.         this.parentId = parentId;
  109.     }
  110.  
  111.     public Long getUltimateId() {
  112.         return ultimateId;
  113.     }
  114.  
  115.     public void setUltimateId(Long ultimateId) {
  116.         this.ultimateId = ultimateId;
  117.     }
  118.  
  119.     public String getName() {
  120.         return name;
  121.     }
  122.  
  123.     public void setName(String name) {
  124.         this.name = name;
  125.     }
  126.  
  127. }
  128.  
  129. ---------------------
  130.  
  131.  
  132. package tr2;
  133.  
  134. import java.io.Serializable;
  135. import java.util.ArrayList;
  136.  
  137. public class Response implements Serializable {
  138.     /**
  139.      *
  140.      */
  141.     private static final long serialVersionUID = 1L;
  142.    
  143.     Long id;
  144.     String name;
  145.     ArrayList<Response> children;
  146.            
  147.     @Override
  148.     public String toString(){
  149.         return "id = " + id + "; name = " + name + " children = \n " + children;
  150.     }
  151.    
  152.     public Long getId() {
  153.         return id;
  154.     }
  155.     public void setId(Long id) {
  156.         this.id = id;
  157.     }
  158.     public String getName() {
  159.         return name;
  160.     }
  161.     public void setName(String name) {
  162.         this.name = name;
  163.     }
  164.     public ArrayList<Response> getChildren() {
  165.         return children;
  166.     }
  167.     public void setChildren(ArrayList<Response> children) {
  168.         this.children = children;//new ArrayList(children);
  169.     }  
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement