<commit_before>package org.apache.olingo.odata2.client.fxml;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.Proxy;
import java.net.URL;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListView;
import javafx.scene.control.PasswordField;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.web.WebView;
import javafx.util.Callback;
import org.apache.olingo.odata2.api.edm.Edm;
import org.apache.olingo.odata2.api.edm.EdmEntitySetInfo;
import org.apache.olingo.odata2.api.edm.EdmEntityType;
import org.apache.olingo.odata2.api.edm.EdmException;
import org.apache.olingo.odata2.api.ep.entry.ODataEntry;
import org.apache.olingo.odata2.api.ep.feed.ODataFeed;
import org.apache.olingo.odata2.api.exception.ODataException;
import org.apache.olingo.odata2.client.HttpException;
import org.apache.olingo.odata2.client.ODataClient;
import org.apache.olingo.odata2.client.StringHelper;

/**
 *
 * @author mibo
 */
public class BasicController implements Initializable {

  @FXML
  TextField inputArea;
  @FXML
  TextArea rawView;
  @FXML
  WebView webView;
  @FXML
  SplitPane edmPane;
  @FXML
  TextArea logArea;
  @FXML
  ListView entityListView;
  @FXML
  ProgressIndicator progress;
  @FXML
  CheckBox proxyCheckbox;
  @FXML
  TextField proxyHost;
  @FXML
  TextField proxyPort;
  @FXML
  CheckBox loginCheckbox;
  @FXML
  TextField loginUser;
  @FXML
  PasswordField loginPassword;
  //
  private TableView tableView;

  @Override
  public void initialize(URL url, ResourceBundle rb) {
//    inputArea.setText("http://www.olingo.org");
//    inputArea.setText("http://localhost:8080/com.sap.core.odata.performance-web/ReferenceScenario.svc/");
    inputArea.setText("http://services.odata.org/Northwind/Northwind.svc/");

  }

  @FXML
  public void changeProxy(ActionEvent event) {
    if (proxyCheckbox.isSelected()) {
      proxyHost.setDisable(false);
      proxyPort.setDisable(false);
    } else {
      proxyHost.setDisable(true);
      proxyPort.setDisable(true);
    }
  }

  @FXML
  public void changeLogin(ActionEvent event) {
    if (loginCheckbox.isSelected()) {
      loginUser.setDisable(false);
      loginPassword.setDisable(false);
    } else {
      loginUser.setDisable(true);
      loginPassword.setDisable(true);
    }
  }

  @FXML
  public void sendSingleRequest(ActionEvent event) {

    Task<Void> singleRequest = new Task<Void>() {
      @Override
      protected Void call() throws Exception {
        try {
          String serviceUrl = getValidUrl();
          InputStream is = ODataClient.getRawHttpEntity(serviceUrl, ODataClient.APPLICATION_JSON);
          final String content = StringHelper.inputStreamToString(is);

          Platform.runLater(new Runnable() {
            @Override
            public void run() {
              rawView.setText(content);
              webView.getEngine().loadContent(content);
              writeToLogArea("All requests successfull processed");
              progress.setProgress(1);
            }
          });

        } catch (IllegalArgumentException | IOException | HttpException ex) {
          Platform.runLater(new Runnable() {
            @Override
            public void run() {
              progress.setProgress(0);
              writeToLogArea(ex);
            }
          });
        }
        return null;
      }
    };

    clearViews();
    new Thread(singleRequest).start();
  }

  @FXML
  public void exploreService(ActionEvent event) {
    Task<Void> singleRequest = new Task<Void>() {
      @Override
      protected Void call() throws Exception {
        try {
          final String serviceUrl = getValidUrl();
          final ODataClient client = getODataClient(serviceUrl);
          createEdmView(client);

          Platform.runLater(new Runnable() {
            @Override
            public void run() {
              String rawContent = client.getRawContentOfLastRequest();
              webView.getEngine().loadContent(rawContent);
              rawView.setText(rawContent);
              writeToLogArea("All requests successfull processed");
              progress.setProgress(1);
            }
          });

        } catch (IllegalArgumentException | IOException | HttpException ex) {
          Platform.runLater(new Runnable() {
            @Override
            public void run() {
              progress.setProgress(0);
              writeToLogArea(ex);
            }
          });
        }
        return null;
      }
    };

    clearViews();
    new Thread(singleRequest).start();
  }

  private ODataClient getODataClient(final String serviceUrl) throws ODataException, IOException, HttpException {
    if(proxyCheckbox.isSelected() && loginCheckbox.isSelected()) {
      return new ODataClient(serviceUrl, Proxy.Type.HTTP, proxyHost.getText(), Integer.valueOf(proxyPort.getText()),
              loginUser.getText(), loginPassword.getText());
    } else if(proxyCheckbox.isSelected()) {
      return new ODataClient(serviceUrl, Proxy.Type.HTTP, proxyHost.getText(), Integer.valueOf(proxyPort.getText()));
    } else if(loginCheckbox.isSelected()) {
      return new ODataClient(serviceUrl, loginUser.getText(), loginPassword.getText());
    }
    return new ODataClient(serviceUrl);
  }

  private void clearViews() {
    logArea.setText("");
    webView.getEngine().loadContent("");
    rawView.setText("");
    progress.setProgress(-1);
  }

  private void writeToLogArea(Exception ex) {
    StringBuilder b = new StringBuilder();
    Writer out = new StringWriter();
    PrintWriter pw = new PrintWriter(out, true);
    ex.printStackTrace(pw);
    b.append("Exception occured: ").append(ex.getMessage()).append("\n").append(out.toString());
    writeToLogArea(b.toString());
  }

  private void writeToLogArea(String content) {
    StringBuilder b = new StringBuilder();
    b.append("--- ").append(DateFormat.getDateTimeInstance().format(new Date())).append("--------\n");
    b.append(content);
    b.append("\n--------------------------");
    logArea.setText(b.toString());
  }

  private void createEdmView(ODataClient client) throws ODataException, EdmException, IOException, HttpException {
    entityListView.getItems().clear();
    List<EdmEntitySetInfo> entitySets = client.getEntitySets();

    for (EdmEntitySetInfo edmEntitySetInfo : entitySets) {
      String containerName = edmEntitySetInfo.getEntityContainerName();
      String setName = edmEntitySetInfo.getEntitySetName();

      ODataFeed feed = client.readFeed(containerName, setName, ODataClient.APPLICATION_ATOM_XML);
      Edm edm = client.getEdm();
      EdmEntityType entityType = edm.getEntityContainer(containerName).getEntitySet(setName).getEntityType();

      final ODataFeedItemHolder holder = new ODataFeedItemHolder(feed, entityType, setName);

      Platform.runLater(new Runnable() {
        @Override
        public void run() {
          entityListView.getItems().add(holder);
        }
      });
    }
  }

  private String getValidUrl() throws IllegalArgumentException {
    String serviceUrl = inputArea.getText();
    if (isEmpty(serviceUrl)) {
      throw new IllegalArgumentException("No url given.");
    }
    return serviceUrl;
  }

  private class ODataFeedItemHolder {

    private final ODataFeed feed;
    private final EdmEntityType type;
    private final String name;
<commit_msg>Added real progress for explore requests<commit_after>package org.apache.olingo.odata2.client.fxml;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.Proxy;
import java.net.URL;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListView;
import javafx.scene.control.PasswordField;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.web.WebView;
import javafx.util.Callback;
import org.apache.olingo.odata2.api.edm.Edm;
import org.apache.olingo.odata2.api.edm.EdmEntitySetInfo;
import org.apache.olingo.odata2.api.edm.EdmEntityType;
import org.apache.olingo.odata2.api.edm.EdmException;
import org.apache.olingo.odata2.api.ep.entry.ODataEntry;
import org.apache.olingo.odata2.api.ep.feed.ODataFeed;
import org.apache.olingo.odata2.api.exception.ODataException;
import org.apache.olingo.odata2.client.HttpException;
import org.apache.olingo.odata2.client.ODataClient;
import org.apache.olingo.odata2.client.StringHelper;

/**
 *
 * @author mibo
 */
public class BasicController implements Initializable {

  @FXML
  TextField inputArea;
  @FXML
  TextArea rawView;
  @FXML
  WebView webView;
  @FXML
  SplitPane edmPane;
  @FXML
  TextArea logArea;
  @FXML
  ListView entityListView;
  @FXML
  ProgressIndicator progress;
  @FXML
  CheckBox proxyCheckbox;
  @FXML
  TextField proxyHost;
  @FXML
  TextField proxyPort;
  @FXML
  CheckBox loginCheckbox;
  @FXML
  TextField loginUser;
  @FXML
  PasswordField loginPassword;
  //
  private TableView tableView;

  @Override
  public void initialize(URL url, ResourceBundle rb) {
//    inputArea.setText("http://www.olingo.org");
//    inputArea.setText("http://localhost:8080/com.sap.core.odata.performance-web/ReferenceScenario.svc/");
    inputArea.setText("http://services.odata.org/Northwind/Northwind.svc/");

  }

  @FXML
  public void changeProxy(ActionEvent event) {
    if (proxyCheckbox.isSelected()) {
      proxyHost.setDisable(false);
      proxyPort.setDisable(false);
    } else {
      proxyHost.setDisable(true);
      proxyPort.setDisable(true);
    }
  }

  @FXML
  public void changeLogin(ActionEvent event) {
    if (loginCheckbox.isSelected()) {
      loginUser.setDisable(false);
      loginPassword.setDisable(false);
    } else {
      loginUser.setDisable(true);
      loginPassword.setDisable(true);
    }
  }

  @FXML
  public void sendSingleRequest(ActionEvent event) {

    Task<Void> singleRequest = new Task<Void>() {
      @Override
      protected Void call() throws Exception {
        try {
          String serviceUrl = getValidUrl();
          InputStream is = ODataClient.getRawHttpEntity(serviceUrl, ODataClient.APPLICATION_JSON);
          final String content = StringHelper.inputStreamToString(is);

          Platform.runLater(new Runnable() {
            @Override
            public void run() {
              rawView.setText(content);
              webView.getEngine().loadContent(content);
              writeToLogArea("All requests successfull processed");
              progress.setProgress(1);
            }
          });

        } catch (IllegalArgumentException | IOException | HttpException ex) {
          Platform.runLater(new Runnable() {
            @Override
            public void run() {
              progress.setProgress(0);
              writeToLogArea(ex);
            }
          });
        }
        return null;
      }
    };

    clearViews();
    new Thread(singleRequest).start();
  }

  @FXML
  public void exploreService(ActionEvent event) {
    Task<Void> singleRequest = new Task<Void>() {
      @Override
      protected Void call() throws Exception {
        try {
          final String serviceUrl = getValidUrl();
          final ODataClient client = getODataClient(serviceUrl);
          createEdmView(client);

          Platform.runLater(new Runnable() {
            @Override
            public void run() {
              String rawContent = client.getRawContentOfLastRequest();
              webView.getEngine().loadContent(rawContent);
              rawView.setText(rawContent);
              writeToLogArea("All requests successfull processed");
              progress.setProgress(1);
            }
          });

        } catch (IllegalArgumentException | IOException | HttpException ex) {
          Platform.runLater(new Runnable() {
            @Override
            public void run() {
              progress.setProgress(0);
              writeToLogArea(ex);
            }
          });
        }
        return null;
      }
    };

    clearViews();
    new Thread(singleRequest).start();
  }

  private ODataClient getODataClient(final String serviceUrl) throws ODataException, IOException, HttpException {
    if(proxyCheckbox.isSelected() && loginCheckbox.isSelected()) {
      return new ODataClient(serviceUrl, Proxy.Type.HTTP, proxyHost.getText(), Integer.valueOf(proxyPort.getText()),
              loginUser.getText(), loginPassword.getText());
    } else if(proxyCheckbox.isSelected()) {
      return new ODataClient(serviceUrl, Proxy.Type.HTTP, proxyHost.getText(), Integer.valueOf(proxyPort.getText()));
    } else if(loginCheckbox.isSelected()) {
      return new ODataClient(serviceUrl, loginUser.getText(), loginPassword.getText());
    }
    return new ODataClient(serviceUrl);
  }

  private void clearViews() {
    logArea.setText("");
    webView.getEngine().loadContent("");
    rawView.setText("");
    progress.setProgress(-1);
  }

  private void writeToLogArea(Exception ex) {
    StringBuilder b = new StringBuilder();
    Writer out = new StringWriter();
    PrintWriter pw = new PrintWriter(out, true);
    ex.printStackTrace(pw);
    b.append("Exception occured: ").append(ex.getMessage()).append("\n").append(out.toString());
    writeToLogArea(b.toString());
  }

  private void writeToLogArea(String content) {
    writeToLogArea(content, true);
  }
  
  private void writeToLogArea(String content, boolean append) {
    final StringBuilder b;
    if(append) {
      b = new StringBuilder(logArea.getText());
    } else {
      b = new StringBuilder();
    }
    b.append("--- ").append(DateFormat.getDateTimeInstance().format(new Date())).append("--------\n");
    b.append(content);
    b.append("\n--------------------------");
    logArea.setText(b.toString());
  }

  private void createEdmView(ODataClient client) throws ODataException, EdmException, IOException, HttpException {
    entityListView.getItems().clear();
    List<EdmEntitySetInfo> entitySets = client.getEntitySets();
    final double countStep = 1d / entitySets.size();

    for (EdmEntitySetInfo edmEntitySetInfo : entitySets) {
      String containerName = edmEntitySetInfo.getEntityContainerName();
      String setName = edmEntitySetInfo.getEntitySetName();

      ODataFeed feed = client.readFeed(containerName, setName, ODataClient.APPLICATION_ATOM_XML);
      Edm edm = client.getEdm();
      EdmEntityType entityType = edm.getEntityContainer(containerName).getEntitySet(setName).getEntityType();

      final ODataFeedItemHolder holder = new ODataFeedItemHolder(feed, entityType, setName);

      Platform.runLater(new Runnable() {
        @Override
        public void run() {
          entityListView.getItems().add(holder);
          if(progress.getProgress() < 0) {
            progress.setProgress(0);
          }
          progress.setProgress(progress.getProgress() + countStep);
        }
      });
    }
  }

  private String getValidUrl() throws IllegalArgumentException {
    String serviceUrl = inputArea.getText();
    if (isEmpty(serviceUrl)) {
      throw new IllegalArgumentException("No url given.");
    }
    return serviceUrl;
  }

  private class ODataFeedItemHolder {

    private final ODataFeed feed;
    private final EdmEntityType type;
    private final String name;

{'fraction_non_alphanumeric': 0.11328147844903211, 'fraction_numerical': 0.0025147669454354056, 'mean_word_length': 3.2305789213260763, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5013711', 'n_tokens_mistral': 4994, 'n_tokens_neox': 4896, 'n_words': 950}