public OGRFeature save(OGRFeature item) { // If type == country then must update can't have 2 shapes for 1 country if (item.getFeatureType().equals(FeatureType.COUNTRY)) { OGRFeature existingItem = findByCountryAndType(item.getCountryCode(), FeatureType.COUNTRY); if (existingItem != null) { existingItem.setGeometry(item.getGeometry()); existingItem.setBoundingBox(item.getBoundingBox()); super.save(existingItem); item = existingItem; } else { super.save(item); } // Save to country table as well so that we have populated it CountryDao countryDao = new CountryDao(); Country country = countryDao.findByCode(item.getCountryCode()); if (country == null) { country = new Country(); country.setName(item.getName()); country.setDisplayName(item.getName()); country.setCentroidLat(item.getCentroidLat()); country.setCentroidLon(item.getCentroidLon()); country.setIsoAlpha2Code(item.getCountryCode()); countryDao.save(country); } else if (country.getName() == null || country.getDisplayName() == null) { country.setName(item.getName()); country.setDisplayName(item.getName()); countryDao.save(country); } return item; } else { ArrayList<String> subList = new ArrayList<String>(); if (item.getSub1() != null) { subList.add(item.getSub1()); } if (item.getSub2() != null) { subList.add(item.getSub2()); } if (item.getSub3() != null) { subList.add(item.getSub3()); } if (item.getSub4() != null) { subList.add(item.getSub4()); } if (item.getSub5() != null) { subList.add(item.getSub5()); } if (item.getSub6() != null) { subList.add(item.getSub6()); } OGRFeature existingItem = findByCountryTypeAndSub( item.getCountryCode(), item.getName(), FeatureType.SUB_COUNTRY_OTHER, subList); if (existingItem != null) { boolean isSame = true; if (item.getSub1() != null && existingItem.getSub1() != null && !existingItem.getSub1().equals(item.getSub1())) { isSame = false; } if ((item.getSub2() != null && existingItem.getSub2() != null && !existingItem.getSub2().equals(item.getSub2())) || (item.getSub2() == null && existingItem.getSub2() != null)) { isSame = false; } if (item.getSub3() != null && existingItem.getSub3() != null && !existingItem.getSub3().equals(item.getSub3())) { isSame = false; } if (item.getSub4() != null && existingItem.getSub4() != null && !existingItem.getSub4().equals(item.getSub4())) { isSame = false; } if (item.getSub5() != null && existingItem.getSub5() != null && !existingItem.getSub5().equals(item.getSub5())) { isSame = false; } if (item.getSub6() != null && existingItem.getSub6() != null && !existingItem.getSub6().equals(item.getSub6())) { isSame = false; } if (isSame) { existingItem.setGeometry(item.getGeometry()); existingItem.setBoundingBox(item.getBoundingBox()); existingItem.setCentroidLat(item.getCentroidLat()); existingItem.setCentroidLon(item.getCentroidLon()); super.save(existingItem); return existingItem; } } super.save(item); return item; } }