package org.sagebionetworks.repo.manager.backup.migration;

import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.List;

import java.lang.Long;
import java.util.*;

import org.sagebionetworks.repo.model.Annotations;
import org.sagebionetworks.repo.model.registry.MigrationSpecData.FieldMigrationSpecData;

// TODO: Add type transformation
public class MigrationHelper {
	public static void migrateBucket(Annotations srcAnnots, Annotations dstAnnots, FieldMigrationSpecData fmsd) {
		if (null != srcAnnots.getAllValues(fmsd.getSrcFieldName())) {
			migrateType(srcAnnots, dstAnnots, fmsd);
			Object srcData = srcAnnots.deleteAnnotation(fmsd.getSrcFieldName());
			dstAnnots.addAnnotation(fmsd.getDestFieldName(), srcData);
		}
		
		return;
	}
	
	public static void deleteFromAnnotations(Annotations srcAnnots, FieldMigrationSpecData fmsd) {
		if (null != srcAnnots.getAllValues(fmsd.getSrcFieldName())) {
			Object srcData = srcAnnots.deleteAnnotation(fmsd.getSrcFieldName());
		}
		
		return;
	}
	
	// TODO: Move type migration to separate migration step
	private static void migrateType(Annotations srcAnnots, Annotations dstAnnots, FieldMigrationSpecData fmsd) {
		// Only support string to int for now
		if (fmsd.getSrcType().equals("string") && fmsd.getDestType().equals("integer")) {
				List<String> ls = srcAnnots.getStringAnnotations().remove(fmsd.getSrcFieldName());
				List<Long> ll = new ArrayList<Long>();
				Long l;
				for (String s: ls) {
					try {
						l = Long.valueOf(s);
					} catch (NumberFormatException e) {
						l = -1L;
					}
					ll.add(l);
					// TODO: Should also log something
				}
				// Put back in src annotations to be migrated to other bucket as needed
				srcAnnots.getLongAnnotations().put(fmsd.getSrcFieldName(), ll);
		}
	}
}

@@ -1316,24 +1316,103 @@
 nteger%22)) %7B%0A
+%09%09%09if (srcAnnots.getStringAnnotations().containsKey(fmsd.getSrcFieldName())) %7B%0A
 %09%09%09%09List%3CStr
@@ -1873,16 +1873,21 @@
 ), ll);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%7D

Fix case where annotation specified in type transform exists but is not a string