package org.hive2hive.core.network.data.vdht;

import java.security.KeyPair;

import org.hive2hive.core.H2HConstants;
import org.hive2hive.core.exceptions.GetFailedException;
import org.hive2hive.core.exceptions.PutFailedException;
import org.hive2hive.core.model.versioned.Locations;
import org.hive2hive.core.network.data.DataManager;
import org.hive2hive.core.network.data.DataManager.H2HPutStatus;
import org.hive2hive.core.network.data.parameters.Parameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Wrapper for the {@link VersionManager} for the Locations
 * 
 * @author Nico
 *
 */
public class LocationsManager {
	private static final Logger logger = LoggerFactory.getLogger(VersionManager.class);

	private final KeyPair protectionKeys;
	private final VersionManager<Locations> versionManager;
	private final DataManager dataManager;
	private final String userId;

	public LocationsManager(DataManager dataManager, String userId, KeyPair protectionKeys) {
		this.dataManager = dataManager;
		this.userId = userId;
		this.protectionKeys = protectionKeys;
		versionManager = new VersionManager<Locations>(dataManager, H2HConstants.USER_LOCATIONS, userId);
	}

	public void put(Locations locations) throws PutFailedException {
		versionManager.put(locations, protectionKeys);
	}

	public Locations get() throws GetFailedException {
		return versionManager.get();
	}

	/**
	 * The locations file might have a conflict or is missing during the login. Here we try to repair it.
	 * Only call this if the locations are gone or ongoing version forks are detected. All entries of old
	 * Locations artifacts are deleted.
	 * 
	 * @return the new locations or <code>null</code> if it failed
	 */
	public Locations repairLocations() {
		Parameters removeParams = new Parameters().setContentKey(H2HConstants.USER_LOCATIONS).setLocationKey(userId)
				.setProtectionKeys(protectionKeys);
		if (dataManager.remove(removeParams)) {
			logger.debug("Removed old locations");
		} else {
			logger.warn("Failed to remove the old locations");
		}

		Locations locations = new Locations(userId);
		locations.generateVersionKey();
		Parameters addParams = new Parameters().setLocationKey(userId).setContentKey(H2HConstants.USER_LOCATIONS)
				.setVersionKey(locations.getVersionKey()).setBasedOnKey(locations.getBasedOnKey())
				.setNetworkContent(locations).setProtectionKeys(protectionKeys).setTTL(locations.getTimeToLive());
		if (dataManager.put(addParams) == H2HPutStatus.OK) {
			logger.debug("Successfully repaired the locations");
			return locations;
		} else {
			logger.warn("Could not put the repaired locations");
			return null;
		}
	}
}

@@ -700,23 +700,25 @@
 tLogger(
-Vers
+Locat
 ion
+s
 Manager.
@@ -1143,16 +1143,24 @@
 Manager,
+ userId,
  H2HCons
@@ -1179,24 +1179,16 @@
 OCATIONS
-, userId
 );%0A%09%7D%0A%0A%09
@@ -1903,16 +1903,83 @@
 nKeys);%0A
+%09%09logger.info(%22Start repairing the locations of user %7B%7D%22, userId);%0A
 %09%09if (da
@@ -2046,25 +2046,44 @@
 ld locations
-%22
+ of user %7B%7D%22, userId
 );%0A%09%09%7D else 
@@ -2130,25 +2130,44 @@
 ld locations
-%22
+ of user %7B%7D%22, userId
 );%0A%09%09%7D%0A%0A%09%09Lo

Fix LocationsManager (swapped location and content key)