Package models

Source Code of models.Stars

package models;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map.Entry;

import models.files.DatDatabaseReader;
import models.files.EdbDatabaseReader;
import models.files.IDatabaseReader;
import models.files.UnknownFormatException;
import views.Star;
import controllers.Envs;
import controllers.Messages;

/**
* Class making the link between Database objects and Storage of the given stars
*
* @see StarStorage
* @author francois
*
*/
public class Stars {

  private static HashMap<StarDatabase, StarStorage> starDatabases = new HashMap<>();

  public static Charset charset = Charset.forName("UTF-8");
  private static final String cacheFileName = "db.dat";
 
  /**
   * Importation du fichier dans la mémoire
   * @param filename
   */
  private static void loadAndRegisterDatabaseFromFileWithReader(StarDatabase database, IDatabaseReader reader) {
    Path filename = database.dataPath;
    final Path cacheFilename = Envs.join(database.cachePath, Paths.get(cacheFileName));
    boolean createCache = false;
    if (Files.isRegularFile(cacheFilename)) {
      filename = cacheFilename;
      reader = new DatDatabaseReader();
    } else {
      createCache = true;;
    }
   
    // try with limit, assure la fermeture de l'input stream à la fin du try
    try (BufferedReader br = Files.newBufferedReader(filename, charset)) {
      final StarStorage stars = new StarStorage();
      reader.read(br, stars);
      starDatabases.put(database, stars);
      if (createCache)
        try (BufferedWriter br2 = Files.newBufferedWriter(cacheFilename, charset)) {
          for (Star s : stars) {
            br2.append(s.toDatFormat());
          }
          br2.flush();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
    } catch (Exception e) {
      Messages.addError("Error while reading database: " + e.getMessage());
      e.printStackTrace();
    }
  }
 
  public static void loadAndRegisterDatabaseFromFile(StarDatabase database) {
    try {
      loadAndRegisterDatabaseFromFileWithReader(database, createReaderFromFormat(database.getFormat()));
    } catch (UnknownFormatException e) {
      Messages.addError("Error: " + e.getMessage());
      e.printStackTrace();
    }
  }
 
  public static IDatabaseReader createReaderFromFormat(String format) throws UnknownFormatException {
    if (format.equalsIgnoreCase("dat")) {
      return new DatDatabaseReader();
    } else if (format.equalsIgnoreCase("edb")) {
      return new EdbDatabaseReader();
    }
    throw new UnknownFormatException(format);
  }
 
  public static void unloadStarsFromDatabase(StarDatabase db) {
    starDatabases.get(db).clear();
  }

  /**
   *
   * @return the currently loaded set of Stars
   */
  public static StarStorage getStarsFromDatabase(StarDatabase db) {
    return starDatabases.get(db);
  }
 
  /**
   * TODO: refactor to return global result
   * @return
   */
  public static StarStorage getStars() {
    StarStorage n = new StarStorage();
    for ( Entry<StarDatabase, StarStorage> s : starDatabases.entrySet()) {
      if (s.getKey().isLoaded())
        n.chain(s.getValue());
    }
    return n;
  }
 
  public static StarStorage getSelectedStars(StarSelector selector) {
    StarStorage n = getStars();
    n.setSelector(selector);
    return n;
  }
}
TOP

Related Classes of models.Stars

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.