public void save() {

    // Sort list, add country to top, save types, put ids into type map
    List<RegionType> regionTypeList = new ArrayList(typeHashMap.values());
    Collections.sort(
        regionTypeList,
        (object1, object2) -> object1.getShortName().compareTo(object2.getShortName()));
    regionTypeList.add(0, new RegionType(COUNTRY_ID, COUNTRY_TYPE));
    // Add country to list after sort.
    typeHashMap.put(COUNTRY_TYPE, new RegionType(COUNTRY_ID, COUNTRY_TYPE));
    // Save
    for (RegionType regionType : regionTypeList) {
      regionType.identity = insertRegionType(regionType);
    }
    // Save countries put ids into country map
    List<Country> countryList = new ArrayList(countryHashMap.values());
    Collections.sort(
        countryList,
        (object1, object2) -> object1.getShortName().compareTo(object2.getShortName()));
    for (Country country : countryList) {
      country.identity = insertCountry(country);
    }
    // Save parent regions put ids into region map
    List<Region> regionList = new ArrayList(regionHashMap.values());
    Collections.sort(
        regionList, (object1, object2) -> object1.getShortName().compareTo(object2.getShortName()));
    for (Region region : regionList) {
      if (region.parentCode == null || region.parentCode.isEmpty()) {
        region.identity = insertRegion(region);
      }
    }
    // Save child regions
    for (Region region : regionList) {
      if (region.parentCode != null && !region.parentCode.isEmpty()) {
        region.identity = insertRegion(region);
      }
    }

    // Save Region path to root, TODO really brute force, this could be SO better.
    regionList = new ArrayList(regionHashMap.values());
    Collections.sort(
        regionList, (object1, object2) -> object1.getShortName().compareTo(object2.getShortName()));
    for (Region region : regionList) {
      if (!region.categoryName.equals(COUNTRY_TYPE)) {
        // region is not a country and so has a parent list. Country is the current top of the list.
        if (region.parentCode == null || region.parentCode.isEmpty()) {
          // if region has no parent, record country as parent node @ level 1 and leaf region as 2
          insertNodePath(region.identity, countryHashMap.get(region.countryCode).identity, 1);
          insertNodePath(region.identity, region.identity, 2);
        } else {
          // if region has a parent, record country as parent node @ level 1, parent region as 2 and
          // leaf region as 3
          insertNodePath(region.identity, countryHashMap.get(region.countryCode).identity, 1);
          insertNodePath(region.identity, regionHashMap.get(region.parentCode).identity, 2);
          insertNodePath(region.identity, region.identity, 3);
        }
      }
    }
  }
Beispiel #2
0
 public void generateRandomRegionAt(int x, int y) {
   if (doesRegionExistAt(x, y)) {
     return;
   }
   RegionType type = RegionType.values()[rand.nextInt(RegionType.values().length)];
   int sec = getSectorOf(x, y);
   x = Math.abs(x);
   y = Math.abs(y);
   regions[sec][x][y] = new Region(this, type);
 }