package eu.dnetlib.enabling.aas.is;

import org.springframework.context.ApplicationContext;

import eu.dnetlib.enabling.aas.utils.SpringUtils;
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;

/**
 * Class for removing profiles from IS.
 * @author mhorst
 *
 */
public class ProfilesRemover {

	ISRegistryService registryService = null;
	
	public ProfilesRemover() {
//		load spring configuration
		ApplicationContext ctx = SpringUtils.getSpringContext(SpringUtils.DEFAULT_TEST_RESOURCE);
		registryService = (ISRegistryService) ctx.getBean(SpringUtils.BEAN_REMOTE_IS_REGISTRY);
	}
	
	/**
	 * Removes profile for given profId as parameter.
	 * @param profId
	 * @return true if successfully removed, false - otherwise
	 */
	public boolean removeProfile(String profId) {
		if (profId==null)
			return false;
		try {
			return registryService.deleteProfile(profId);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static void main(String[] args) {
		if (args.length==0) {
			System.out.println("No profile identifier provided!");
			return;
		}
		
		ProfilesRemover remover = new ProfilesRemover();
		for (int i = 0; i < args.length; i++) {
			System.out.println("Removing profile: " + args[i] + "...");
			if (remover.removeProfile(args[i]))
				System.out.println("Profile "+ args[i] + "successfully removed.");
			else {
				System.out.println("Some problems occured when removing profile "+ args[i] + "!");
				System.exit(1);
			}
		}
		System.out.println("Profiles removing finished!");
		
	}
}
