package edu.isi.karma.controller.command;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.isi.karma.controller.command.selection.SuperSelection;
import edu.isi.karma.controller.history.HistoryJsonUtil.ParameterType;
import edu.isi.karma.rep.Worksheet;
import edu.isi.karma.rep.Workspace;
import edu.isi.karma.util.CommandInputJSONUtil;
import edu.isi.karma.webserver.KarmaException;

public abstract class JSONInputCommandFactory extends CommandFactory {
	private enum Arguments {
		selectionName		
	}
	@Override
	public abstract Command createCommand(JSONArray inputJson, Workspace workspace) throws JSONException, KarmaException;
	protected void normalizeSelectionId(String worksheetId, JSONArray inputJson, Workspace workspace) {
		Worksheet worksheet = workspace.getWorksheet(worksheetId);
		String selectionName = CommandInputJSONUtil.getStringValue(Arguments.selectionName.name(), inputJson);
		SuperSelection sel = worksheet.getSuperSelectionManager().getSuperSelection(selectionName);
		JSONObject obj = CommandInputJSONUtil.getJSONObjectWithName(selectionName, inputJson);
		if (obj != null)
			obj.put("value", sel.getName());
		else
			CommandInputJSONUtil.createJsonObject(Arguments.selectionName.name(), sel.getName(), ParameterType.other);
	}
}

@@ -943,16 +943,52 @@
 tJson);%0A
+%09%09if (worksheet == null)%0A%09%09%09return;%0A
 %09%09SuperS

check null pointer