2015年1月20日火曜日

某ソース


import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.beanutils.PropertyUtils;

public class Test {

 static Map, Map> relationMap = new HashMap, Map>();
 static Map, Serializable>> seachResult = new HashMap, Serializable>>();
 static Map> endEntity = new HashMap>();

 static {
  List list = new ArrayList();
  Map map2 = new HashMap();
  relationMap.put(list, map2);
  list.add("led");
  list.add("m");
  list.add("r");
  map2.put("m", "m");
  map2.put("c", "c");
 }

 public static void main(String[] args) {
  try {

   List searchList = new ArrayList();
   for (Serializable serializable : searchList) {
    loadRoleEntity(serializable);
   }
  } catch (Exception e1) {
   e1.printStackTrace();
  }
 }

 private static void loadRoleEntity(Serializable serializable)
   throws Exception {
  Set idSet = endEntity.get(serializable.getClass().getCanonicalName());
  if (idSet == null) {
   idSet = new HashSet();
   endEntity.put(serializable.getClass().getCanonicalName(), idSet);
  }
  if (idSet.contains(serializable.getId())) {
   return;
  }
  Set targets = getNextTarget(serializable.getClass().getCanonicalName());
  if (targets.isEmpty()) {
   return;
  }
  for (String target : targets) {
   List> relation = getParentMap(serializable.getClass().getCanonicalName(), target);
   List> foreginKeyValuMapList = new ArrayList>();
   for (Set set : relation) {
    Map hashMap = new HashMap();
    for (String key : set) {
     Object property = PropertyUtils.getProperty(serializable, key);
     hashMap.put(key, (Serializable) property);
    }
    foreginKeyValuMapList.add(hashMap);
   }

   List searched = searchTarget(target, foreginKeyValuMapList);
   Map> keysMap = getChildMap(serializable.getClass().getCanonicalName(),
     target);
   if (searched != null) {
    Map, Serializable> hashMap2 = new HashMap, Serializable>();
    seachResult.put(target, hashMap2);

    Map hashMap = new HashMap();
    for (Serializable serializable2 : searched) {
     for (Entry> e : keysMap.entrySet()) {
      Map keys = e.getValue();
      for (Entry e2 : keys.entrySet()) {
       Object property = PropertyUtils.getProperty(serializable2, e2.getValue());
       hashMap.put(e2.getKey(), (Serializable) property);
      }
      hashMap2.put(hashMap, serializable2);
     }
    }
   }

   Set keySet = keysMap.keySet();
   for (String key : keySet) {
    List keyList = new ArrayList();
    keyList.add(serializable.getClass().getCanonicalName());
    keyList.add(key);
    keyList.add(target);
    Map map2 = relationMap.get(keyList);
    Map, Serializable> searchResultMap = seachResult.get(target);
    Serializable serializable2 = searchResultMap != null ? searchResultMap.get(map2) : null;
    Field declaredField = serializable.getClass().getDeclaredField(key);
    declaredField.setAccessible(true);
    if (Set.class.equals(declaredField)) {
     Set set = (Set) PropertyUtils.getProperty(serializable, key);
     if (set == null) {
      set = new HashSet();
      PropertyUtils.setProperty(serializable, key, set);
     }
     if (serializable2 != null) {
      set.add(serializable2);
     }
    }
    else {
     PropertyUtils.setProperty(serializable, key, serializable2);
    }
   }

   for (Serializable serializable2 : searched) {
    loadRoleEntity(serializable2);
   }

  }
 }

 private static Map> getChildMap(String canonicalName, String target) {
  Map> result = new HashMap>();
  for (Entry, Map> e : relationMap.entrySet()) {
   List list = e.getKey();
   if (list.get(0).equals(canonicalName) &&
     list.get(2).equals(canonicalName)) {
    Map set = result.get(list.get(1));
    if (set == null) {
     set = new HashMap();
     result.put(list.get(1), e.getValue());
    }
   }
  }

  return result;
 }

 private static List searchTarget(String target, List> foreginKeyValuMapList) {
  return null;

 }

 private static Set getNextTarget(String canonicalName) {
  Set result = new HashSet();
  Set> keySet = relationMap.keySet();
  for (List list : keySet) {
   if (list.get(0).equals(canonicalName)) {
    result.add(list.get(2));
   }
  }
  return result;
 }

 private static List> getParentMap(String canonicalName, String target) {
  List> result = new ArrayList>();
  for (Entry, Map> e : relationMap.entrySet()) {
   List list = e.getKey();
   if (list.get(0).equals(canonicalName) &&
     list.get(2).equals(canonicalName)) {
    Set keySet = e.getValue().keySet();
    result.add(keySet);
   }
  }

  return result;
 }
}

0 件のコメント:

コメントを投稿