private String getTemplateTermSetPopulatedWithValues(
     Map<String, String> columnValues, TemplateTermSet termSet)
     throws ValueNotFoundKarmaException, NoValueFoundInNodeException {
   StringBuilder output = new StringBuilder();
   for (TemplateTerm term : termSet.getAllTerms()) {
     // String template term
     if (term instanceof StringTemplateTerm) {
       output.append(term.getTemplateTermValue());
     }
     // Column template term
     else if (term instanceof ColumnTemplateTerm) {
       String hNodeId = term.getTemplateTermValue();
       if (columnValues.containsKey(hNodeId)) {
         Node node = factory.getNode(columnValues.get(hNodeId));
         if (node != null) {
           if (node.getValue().asString() == null || node.getValue().asString().equals("")) {
             throw new NoValueFoundInNodeException();
           }
           output.append(node.getValue().asString());
         }
       } else {
         String columnName = this.factory.getHNode(hNodeId).getColumnName();
         throw new ValueNotFoundKarmaException(
             "Could not retrieve value from column: " + columnName + ".", hNodeId);
       }
     }
   }
   return output.toString();
 }
  @Override
  public UpdateContainer doIt(Workspace workspace) throws CommandException {
    Worksheet worksheet = workspace.getWorksheet(worksheetId);
    RepFactory factory = workspace.getFactory();
    SuperSelection superSel = getSuperSelection(worksheet);
    HTable hTable = factory.getHTable(factory.getHNode(hNodeId).getHTableId());
    Selection currentSel = superSel.getSelection(hTable.getId());
    if (currentSel != null) {
      currentSel.updateSelection();
    }
    CommandHistory history = workspace.getCommandHistory();
    List<Command> tmp =
        gatherAllOperateSelectionCommands(
            history.getCommandsFromWorksheetId(worksheetId), workspace);
    if (tmp.size() > 0) {
      JSONArray inputJSON = new JSONArray();
      inputJSON.put(
          CommandInputJSONUtil.createJsonObject(
              "worksheetId", worksheetId, ParameterType.worksheetId));
      inputJSON.put(
          CommandInputJSONUtil.createJsonObject("hNodeId", hNodeId, ParameterType.hNodeId));
      inputJSON.put(
          CommandInputJSONUtil.createJsonObject(
              "operation", Operation.Intersect.name(), ParameterType.other));
      inputJSON.put(
          CommandInputJSONUtil.createJsonObject(
              "pythonCode", SelectionManager.defaultCode, ParameterType.other));
      inputJSON.put(CommandInputJSONUtil.createJsonObject("onError", "false", ParameterType.other));
      inputJSON.put(
          CommandInputJSONUtil.createJsonObject(
              "selectionName", superSel.getName(), ParameterType.other));
      Command t = null;
      try {
        t = new OperateSelectionCommandFactory().createCommand(inputJSON, workspace);
      } catch (Exception e) {

      }
      if (t != null) history._getHistory().add(t);
      history._getHistory().addAll(tmp);
    }
    UpdateContainer uc =
        WorksheetUpdateFactory.createWorksheetHierarchicalAndCleaningResultsUpdates(
            worksheetId, superSel);
    uc.add(new HistoryUpdate(history));
    return uc;
  }
  private void generateRDF(
      String wkname,
      String query,
      List<KR2RMLRDFWriter> writers,
      R2RMLMappingIdentifier id,
      String baseURI)
      throws IOException, JSONException, KarmaException, SQLException, ClassNotFoundException {
    logger.debug("Generating RDF...");

    WorksheetR2RMLJenaModelParser parserTest = new WorksheetR2RMLJenaModelParser(id);
    KR2RMLMapping mapping = parserTest.parse();

    AbstractJDBCUtil dbUtil = JDBCUtilFactory.getInstance(dbType);
    Connection conn = dbUtil.getConnection(hostname, portnumber, username, password, dBorSIDName);
    conn.setAutoCommit(false);

    java.sql.Statement stmt =
        conn.createStatement(
            java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
    stmt.setFetchSize(DATABASE_TABLE_FETCH_SIZE);

    ResultSet r = stmt.executeQuery(query);
    ResultSetMetaData meta = r.getMetaData();
    ;

    // Get the column names
    List<String> columnNames = new ArrayList<>();
    for (int i = 1; i <= meta.getColumnCount(); i++) {
      columnNames.add(meta.getColumnName(i));
    }

    // Prepare required Karma objects
    Workspace workspace = initializeWorkspace();

    RepFactory factory = workspace.getFactory();
    Worksheet wk = factory.createWorksheet(wkname, workspace, encoding);
    List<String> headersList = addHeaders(wk, columnNames, factory);

    int counter = 0;

    ArrayList<String> rowValues = null;
    while ((rowValues = dbUtil.parseResultSetRow(r)) != null) {
      // Generate RDF and create a new worksheet for every DATABASE_TABLE_FETCH_SIZE rows
      if (counter % DATABASE_TABLE_FETCH_SIZE == 0 && counter != 0) {
        generateRDFFromWorksheet(wk, workspace, mapping, writers, baseURI);
        logger.debug("Done for " + counter + " rows ...");
        removeWorkspace(workspace);

        parserTest = new WorksheetR2RMLJenaModelParser(id);
        mapping = parserTest.parse();
        workspace = initializeWorkspace();
        factory = workspace.getFactory();
        wk = factory.createWorksheet(wkname, workspace, encoding);
        headersList = addHeaders(wk, columnNames, factory);
      }

      /** Add the data * */
      Table dataTable = wk.getDataTable();
      Row row = dataTable.addRow(factory);
      for (int i = 0; i < rowValues.size(); i++) {
        row.setValue(headersList.get(i), rowValues.get(i), factory);
      }

      counter++;
    }

    generateRDFFromWorksheet(wk, workspace, mapping, writers, baseURI);

    // Releasing all the resources
    r.close();
    conn.close();
    stmt.close();
    logger.debug("done");
  }
  @Override
  public UpdateContainer doIt(Workspace workspace) throws CommandException {
    ModelingConfiguration modelingConfiguration =
        ModelingConfigurationRegistry.getInstance()
            .getModelingConfiguration(
                WorkspaceKarmaHomeRegistry.getInstance().getKarmaHome(workspace.getId()));
    TripleStoreUtil utilObj = new TripleStoreUtil();
    boolean showModelsWithoutMatching = modelingConfiguration.isShowModelsWithoutMatching();
    try {
      HashMap<String, List<String>> metadata =
          utilObj.getMappingsWithMetadata(TripleStoreUrl, context);
      RepFactory factory = workspace.getFactory();
      List<String> model_Names = metadata.get("model_names");
      List<String> model_Urls = metadata.get("model_urls");
      List<String> model_Times = metadata.get("model_publishtimes");
      List<String> model_Contexts = metadata.get("model_contexts");
      List<String> model_inputColumns = metadata.get("model_inputcolumns");
      final List<JSONObject> list = new ArrayList<>();
      Set<String> worksheetcolumns = new HashSet<>();
      if (worksheetId != null && !worksheetId.trim().isEmpty()) {
        HTable htable = factory.getWorksheet(worksheetId).getHeaders();
        getHNodesForWorksheet(htable, worksheetcolumns, factory);
      }
      Iterator<String> nameitr = model_Names.iterator();
      Iterator<String> urlitr = model_Urls.iterator();
      Iterator<String> timeitr = model_Times.iterator();
      Iterator<String> contextitr = model_Contexts.iterator();
      Iterator<String> inputitr = model_inputColumns.iterator();
      while (nameitr.hasNext()
          && urlitr.hasNext()
          && timeitr.hasNext()
          && contextitr.hasNext()
          && inputitr.hasNext()) {
        JSONObject obj = new JSONObject();
        Set<String> inputs = new HashSet<>();
        obj.put("name", nameitr.next());
        obj.put("url", urlitr.next());
        obj.put("publishTime", timeitr.next());
        obj.put("context", contextitr.next());
        String columns = inputitr.next();
        if (columns != null && !columns.isEmpty()) {
          JSONArray array = new JSONArray(columns);
          for (int i = 0; i < array.length(); i++) inputs.add(array.get(i).toString());
        } else if (showModelsWithoutMatching) {
          list.add(obj);
        }
        if (worksheetId != null && !worksheetId.isEmpty()) {
          inputs.retainAll(worksheetcolumns);
          obj.put("inputColumns", inputs.size());
        } else obj.put("inputColumns", 0);
        if (!inputs.isEmpty() || (worksheetId == null || worksheetId.trim().isEmpty()))
          list.add(obj);
      }

      Collections.sort(
          list,
          new Comparator<JSONObject>() {

            @Override
            public int compare(JSONObject a, JSONObject b) {
              return b.getInt("inputColumns") - a.getInt("inputColumns");
            }
          });

      return new UpdateContainer(
          new AbstractUpdate() {
            @Override
            public void generateJson(String prefix, PrintWriter pw, VWorkspace vWorkspace) {
              try {
                JSONArray array = new JSONArray();
                for (JSONObject obj : list) {
                  array.put(obj);
                }
                pw.print(array.toString());
              } catch (Exception e) {
                logger.error("Error generating JSON!", e);
              }
            }
          });
    } catch (Exception e) {
      return new UpdateContainer(
          new ErrorUpdate("Unable to get mappings with metadata: " + e.getMessage()));
    }
  }