package org.dainst.gazetteer.helpers;

import java.math.BigDecimal;

import org.dainst.gazetteer.domain.Location;
import org.dainst.gazetteer.domain.Place;
import org.dainst.gazetteer.domain.User;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

public class ProtectLocationsService {

	private static int decimalPlaces = 1;
	
	public static void protectLocations(User user, Place place) {
		
		if (place.getType() != null && place.getType().equals("archaeological-site") &&
				(user == null || !user.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_USER")))) {			
			
			if (place.getPrefLocation() != null && !place.getPrefLocation().isPublicSite()) {
				double lng = BigDecimal.valueOf(place.getPrefLocation().getLng()).setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP).doubleValue();
				double lat = BigDecimal.valueOf(place.getPrefLocation().getLat()).setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP).doubleValue();				
				place.getPrefLocation().setCoordinates(new double[] {lng, lat});
				place.getPrefLocation().setShape(null);
			}
			
			for (Location location : place.getLocations()) {
				if (!location.isPublicSite()) {
					double lng = BigDecimal.valueOf(location.getLng()).setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP).doubleValue();
					double lat = BigDecimal.valueOf(location.getLat()).setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP).doubleValue();					
					location.setCoordinates(new double[] {lng, lat});
					location.setShape(null);
				}
			}
		}
	}
}
@@ -346,9 +346,9 @@
 s = 
-1
+3
 ;%0A%09%0A

set decimalPlaces in ProtectLocationsService back to 3