<commit_before>package ch.unibas.dmi.dbis.reqman.session;

/**
 * The session manager stores and reads the session file
 *
 * @author loris.sauter
 */
public class SessionManager {
  
  public static final String REQMAN_DIRECTORY = ".reqman";
  
  public static final String SESSION_FILE_NAME = "session.reqman";
}<commit_msg>Implementing session store / load logic<commit_after>package ch.unibas.dmi.dbis.reqman.session;

import ch.unibas.dmi.dbis.reqman.common.JSONUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * The session manager stores and reads the session file
 *
 * @author loris.sauter
 */
public class SessionManager {
  
  private static final Logger LOGGER = LogManager.getLogger();
  
  public static final String REQMAN_DIRECTORY = ".reqman";
  
  public static final String SESSION_FILE_NAME = "session.reqman";
  
  private SessionStorage sessionStorage = null;
  /**
   * Loads the session storage.
   * Loading order is as follows:
   * <ol>
   *   <li>A file named like {@link #SESSION_FILE_NAME} in the current working dir</li>
   *   <li>A folder named like {@link #REQMAN_DIRECTORY} in the current working dir contains a  {@link #SESSION_FILE_NAME} named file</li>
   *   <li>A file named like {@link #SESSION_FILE_NAME} in the user's home dir</li>
   *   <li>A folder named like {@link #REQMAN_DIRECTORY} in the user's home dir contains a {@link #SESSION_FILE_NAME} named file</li>
   * </ol>
   * @return The session file or null if there was none available
   */
  private SessionStorage loadSessionStorage(){
    String home = System.getProperty("user.home");
    String cwd = System.getProperty("user.dir");
  
    SessionStorage session = null;
    
    Path[] paths = new Path[4];
    paths[0] = Paths.get(cwd, SESSION_FILE_NAME);
    paths[1] = Paths.get(cwd, REQMAN_DIRECTORY, SESSION_FILE_NAME);
    paths[2] = Paths.get(home, SESSION_FILE_NAME);
    paths[3] = Paths.get(home, REQMAN_DIRECTORY, SESSION_FILE_NAME);
    for (Path path : paths) {
      session = loadSessionStorage(path.toFile());
      if (session != null) {
        break;
      }
    }
    return session;
  }
  
  private SessionStorage loadSessionStorage(File file){
    LOGGER.debug("Loading session from {}", file);
    try {
      SessionStorage session = JSONUtils.readFromJSONFile(file, SessionStorage.class);
      LOGGER.debug("Successfully loaded session file {}", file);
      return session;
    } catch (IOException e) {
      LOGGER.debug("Couldn't read session-file {} for reason {}. Returning null.",file,e);
      return null;
    }
  }
  
  /**
   * Loads the session storage.
   * Loading order is as follows:
   * <ol>
   *   <li>A file named like {@link #SESSION_FILE_NAME} in the current working dir</li>
   *   <li>A folder named like {@link #REQMAN_DIRECTORY} in the current working dir contains a  {@link #SESSION_FILE_NAME} named file</li>
   *   <li>A file named like {@link #SESSION_FILE_NAME} in the user's home dir</li>
   *   <li>A folder named like {@link #REQMAN_DIRECTORY} in the user's home dir contains a {@link #SESSION_FILE_NAME} named file</li>
   * </ol>
   */
  public void loadSession(){
    sessionStorage = loadSessionStorage();
  }
  
  public SessionStorage getSessionStorage() {
    return sessionStorage;
  }
  
  /**
   * Stores the given session file.
   * The location is a file named {@link #SESSION_FILE_NAME} in a folder named {@link #REQMAN_DIRECTORY} in the user's home directory.
   * @param session
   */
  public boolean storeSession(SessionStorage session){
    Path location = Paths.get(System.getProperty("user.home"), REQMAN_DIRECTORY, SESSION_FILE_NAME);
    try {
      JSONUtils.writeToJSONFile(session, location.toFile());
      return true;
    } catch (IOException e) {
      LOGGER.warn("Couldn't write session to {} for reason {}", location, e);
      return false;
    }
  }
  
  
  
  /**
   * Returns whether there is a session file.
   * @return
   */
  public boolean hasSession(){
    SessionStorage session = loadSessionStorage();
    return session != null;
  }
  
  
}
{'fraction_non_alphanumeric': 0.11150737042320495, 'fraction_numerical': 0.0016642891107941037, 'mean_word_length': 3.461293743372216, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5527886', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1312, 'n_words': 383}