All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.mobile.taxonomy.TaxonHistory Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * blueBill Mobile - Android - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it/mobile
 * SCM: https://java.net/hg/bluebill-mobile~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.taxonomy;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.Id;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.mobile.io.FileSystem;
import it.tidalwave.mobile.io.MasterFileSystem;
import it.tidalwave.bluebill.taxonomy.mobile.Taxonomy;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.bluebill.mobile.preferences.TaxonomyPreferences;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class TaxonHistory
  {
    private static final String CLASS = TaxonHistory.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    public static final String PERSISTENCE_FILE_NAME = "TaxonPickHistory.ser";

    private static final int MAX_HISTORY_SIZE = 20;

    private FileSystem fileSystem;

    @Nonnegative
    /* package */ int maxHistorySize = MAX_HISTORY_SIZE;

    /* package */ List taxonIdHistory = new ArrayList();

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public TaxonHistory()
      {
        try
          {
            fileSystem = Locator.find(MasterFileSystem.class).getInternalFileSystem();
          }
        catch (Locator.NotFoundException e)
          {
            // ok
          }
        
        load();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void addToTaxonHistory (final @Nonnull Taxon taxon)
      {
        final Id id = taxon.getId();
        taxonIdHistory.remove(id);
        taxonIdHistory.add(0, id);

        while (taxonIdHistory.size() > maxHistorySize)
          {
            taxonIdHistory.remove(maxHistorySize);
          }

        save();
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public List getRecentTaxa()
      {
        logger.info("getRecentTaxa()");
        final List taxa = new ArrayList();
        final Taxonomy taxonomy = Locator.find(TaxonomyPreferences.class).getTaxonomy();

        for (final Id taxonId : taxonIdHistory)
          {
            try
              {
                taxa.add(taxonomy.findTaxonById(taxonId));
              }
            catch (NotFoundException e)
              {
                logger.warning("Can't retrieve taxon for id: %s", taxonId);
              }
          }

        return taxa;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void clearTaxonHistory()
      {
        logger.info("clearTaxonHistory()");
        taxonIdHistory.clear();
        save();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void load()
      {
        try
          {
            logger.info("Loading historic taxa from %s...", PERSISTENCE_FILE_NAME);
            final ObjectInputStream ois = new ObjectInputStream(fileSystem.openFileInput(PERSISTENCE_FILE_NAME));
            taxonIdHistory = (List)ois.readObject();
            ois.close();
          }
        catch (Exception e)
          {
            logger.throwing("load()", CLASS, e);
            logger.warning("File %s not found (%s), using a default taxa list", PERSISTENCE_FILE_NAME, e);
            taxonIdHistory = new ArrayList();

            taxonIdHistory.add(new Id("urn:lsid:catalogueoflife.org:taxon:eff424c2-29c1-102b-9a4a-00304854f820:ac2010")); // grey heron
            taxonIdHistory.add(new Id("urn:lsid:catalogueoflife.org:taxon:eff8967e-29c1-102b-9a4a-00304854f820:ac2010")); // black-headed gull
            taxonIdHistory.add(new Id("urn:bluebill.tidalwave.it:taxon:5c6c5ef0-95cb-11df-a4c2-002332c672e6"));           // flamingo
            taxonIdHistory.add(new Id("urn:lsid:catalogueoflife.org:taxon:415ad45c-52c2-102c-b3cd-957176fb88b9:ac2010")); // mallard
            taxonIdHistory.add(new Id("urn:lsid:catalogueoflife.org:taxon:eff83a9e-29c1-102b-9a4a-00304854f820:ac2010")); // sanderling
            taxonIdHistory.add(new Id("urn:lsid:catalogueoflife.org:taxon:eff82234-29c1-102b-9a4a-00304854f820:ac2010")); // dunlin
            taxonIdHistory.add(new Id("urn:lsid:catalogueoflife.org:taxon:245bd782-60a7-102d-be47-00304854f810:ac2010")); // little egret
            taxonIdHistory.add(new Id("urn:lsid:catalogueoflife.org:taxon:245bbb8a-60a7-102d-be47-00304854f810:ac2010")); // cormorant
          }

        logger.info(">>>> %d historic taxa loaded", taxonIdHistory.size());
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void save()
      {
        try
          {
            logger.info("Saving historic taxa to %s...", PERSISTENCE_FILE_NAME);
            final ObjectOutputStream oos = new ObjectOutputStream(fileSystem.openFileOutput(PERSISTENCE_FILE_NAME));
            oos.writeObject(taxonIdHistory);
            oos.close();
          }
        catch (IOException e)
          {
            logger.throwing("save()", CLASS, e);
            logger.severe("Can't save history: "+ e);
          }
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy