package cn.momia.mapi.common.util;

import cn.momia.api.common.CommonServiceApi;
import cn.momia.api.common.city.City;
import cn.momia.api.common.region.Region;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class MetaUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(MetaUtil.class);

    private static Map<Integer, City> citiesMap = new HashMap<Integer, City>();
    private static Map<Integer, Region> regionsMap = new HashMap<Integer, Region>();

    public void init() {
        // TODO reloadable
        try {
            citiesMap = new HashMap<Integer, City>();
            for (City city : CommonServiceApi.CITY.getAll()) citiesMap.put(city.getId(), city);

            regionsMap = new HashMap<Integer, Region>();
            for (Region region : CommonServiceApi.REGION.getAll()) regionsMap.put(region.getId(), region);
        } catch (Exception e) {
            LOGGER.error("fail to init meta util");
        }
    }

    public static String getCityName(int cityId) {
        City city = citiesMap.get(cityId);
        return city == null ? "" : city.getName();
    }

    public static String getRegionName(int regionId) {
        Region region = regionsMap.get(regionId);
        return region == null ? "" : region.getName();
    }
}

@@ -212,16 +212,39 @@
 ctory;%0A%0A
+import java.util.Date;%0A
 import j
@@ -261,16 +261,16 @@
 ashMap;%0A
-
 import j
@@ -387,24 +387,72 @@
 il.class);%0A%0A
+    private static Date lastReloadTime = null;%0A%0A
     private 
@@ -642,26 +642,109 @@
     
-// TODO reloadable
+reload();%0A    %7D%0A%0A    private synchronized static void reload() %7B%0A        if (!isOutOfDate()) return;%0A
 %0A   
@@ -1068,16 +1068,58 @@
 region);
+%0A%0A            lastReloadTime = new Date();
 %0A       
@@ -1204,32 +1204,200 @@
        %7D%0A    %7D%0A%0A
+    private static boolean isOutOfDate() %7B%0A        return lastReloadTime == null %7C%7C lastReloadTime.before(new Date(new Date().getTime() - 24 * 60 * 60 * 1000));%0A    %7D%0A%0A
     public stati
@@ -1427,24 +1427,62 @@
 t cityId) %7B%0A
+        if (isOutOfDate()) reload();%0A%0A
         City
@@ -1562,32 +1562,32 @@
 tName();%0A    %7D%0A%0A
-
     public stati
@@ -1621,24 +1621,62 @@
 regionId) %7B%0A
+        if (isOutOfDate()) reload();%0A%0A
         Regi

refresh meta util