package org.sagebionetworks.web.client.widget.table;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.view.client.ProvidesKey;

public class TableModel implements Comparable<TableModel> {
	
	private static final String TEMP_ID_PREFIX = "__tm";
	
	// The key provider that provides the unique ID of a contact.
	public static final ProvidesKey<TableModel> KEY_PROVIDER = new ProvidesKey<TableModel>() {
		@Override
		public Object getKey(TableModel item) {
			if(item == null || item.id.startsWith(TEMP_ID_PREFIX)) {
				return null;
			} else {
				return item.id;
			}
		}
	};

	private static int sequence = 0;
	private String id;
	private Map<String, String> map;

	/**
	 * Construct TableModel with default (temp) id
	 */
	public TableModel() {
		this(TEMP_ID_PREFIX + String.valueOf(++sequence));
	}

	/**
	 * Construct TableModel with provided id
	 * @param id - identified for this model. MUST be unique amongst other table models! If not, use the default constructor.
	 */
	public TableModel(String id) {
		this.id = id;
		map = new HashMap<String, String>();
	}

	/**
	 * This should only be used to set the id for a new row that has finally acquired a rowId from the system
	 * @param id
	 */
	public void setId(String id) {
		this.id = id;
	}
	
	public String getId() {
		if(id.startsWith(TEMP_ID_PREFIX)) return null;
		return id;
	}
	
	@Override
	public int compareTo(TableModel o) {
		return (o == null || o.getId() == null) ? -1 : -o.getId().compareTo(getId());
	}

	/**
	 * For use by the GWT CellTable which doesn't handle null values well
	 * @param key
	 * @return
	 */
	public String getNeverNull(String key) {
		String val = map.get(key) == null ? "" : map.get(key);
		return val;
	}
	
	public String get(String key) {
		return map.get(key);
	}

	public void put(String key, String value) {
		map.put(key, value);
	}
	
	public boolean containsKey(String key) {
		return map.containsKey(key);
	}
	
}

@@ -474,19 +474,23 @@
 m) %7B%0A%09%09%09
-if(
+return 
 item == 
@@ -498,88 +498,16 @@
 ull 
-%7C%7C item.id.startsWith(TEMP_ID_PREFIX)) %7B%0A%09%09%09%09return null;%0A%09%09%09%7D else %7B%0A%09%09%09%09return
+? null :
  ite
@@ -516,13 +516,8 @@
 id;%0A
-%09%09%09%7D%0A
 %09%09%7D%0A

fix id of table model