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

it.tidalwave.bluebill.mobile.observation.BlueBillObservationManager Maven / Gradle / Ivy

There is a newer version: 1.0.15
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: BlueBillObservationManager.java,v ee8fe6a3fbd7 2010/07/01 00:13:27 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.observation;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import org.openide.util.lookup.ServiceProvider;
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.observation.ObservationManager;
import it.tidalwave.bluebill.observation.ObservationSet;
import it.tidalwave.bluebill.observation.ObservationSet.Event;
import it.tidalwave.bluebill.observation.ObservationSet.Listener;
import it.tidalwave.bluebill.observation.spi.DefaultObservationSetVisitor;
import it.tidalwave.bluebill.observation.simple.SimpleObservationSet;
import static it.tidalwave.bluebill.observation.Observation.Observation;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
@ServiceProvider(service=ObservationManager.class)
public class BlueBillObservationManager implements ObservationManager
  {
    private static final String CLASS = BlueBillObservationManager.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    public final static String NAME = "ObservationSet.ser";

    @CheckForNull
    private SimpleObservationSet observationSet;

    private FileSystem fileSystem;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private final Listener listener = new Listener()
      {
        public void notifyObservationAdded (final @Nonnull Event event)
          {
            save();
          }

        public void notifyObservationRemoved (final @Nonnull Event event)
          {
            save();
          }

        public void notifyObservationChanged (final @Nonnull Event event)
          {
            save();
          }

        public void notifyCleared (final @Nonnull Event event)
          {
            save();
          }
      };

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

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public ObservationSet createObservationSet()
      {
        throw new UnsupportedOperationException("Not supported yet.");
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public ObservationSet createObservationSet (final @Nonnull Object repository)
      {
        throw new UnsupportedOperationException("Not supported yet.");
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized ObservationSet findOrCreateObservationSetById (final @Nonnull Id id)
      {
        if (observationSet == null)
          {
            ObjectInputStream ois = null;

            try
              {
                logger.info("Loading observations from %s...", NAME);
                final long time = System.currentTimeMillis();
                ois = new ObjectInputStream(fileSystem.openFileInput(NAME));
                observationSet = (SimpleObservationSet)ois.readObject();
                logger.info(">>>> loaded %d items in %d msec", observationSet.find(Observation).count(),
                                                               System.currentTimeMillis() - time);
              }
            catch (Throwable t)
              {
                logger.throwing("findOrCreateObservationSetById()", CLASS, t);
                logger.warning("Cannot load from %s: %s", NAME, t);
                observationSet = new SimpleObservationSet();
              }
            finally
              {
                try
                  {
                    if (ois != null)
                      {
                        ois.close();
                      }
                  }
                catch (IOException e)
                  {
                  }
              }

            observationSet.addListener(listener);
            observationSet.addCapabilities(new DefaultObservationSetVisitor(observationSet));
          }

        return observationSet;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void save()
      {
        if (fileSystem == null)
          {
            logger.warning("Not saving observations, no FileSystem availble");
          }
        else
          {
            logger.info(">>>> saving %s...", observationSet);

            ObjectOutputStream oos = null;

            try
              {
                logger.info("Saving observations to %s...", NAME);
                final long time = System.currentTimeMillis();
                oos = new ObjectOutputStream(fileSystem.openFileOutput(BlueBillObservationManager.NAME));
                oos.writeObject(observationSet);
                logger.info(">>>> saved %d items in %d msec", observationSet.find(Observation).count(),
                                                              System.currentTimeMillis() - time);
              }
            catch (Throwable t)
              {
                logger.throwing("save()", CLASS, t);
                logger.severe("Can't save observations: "+ t);
              }
            finally
              {
                try
                  {
                    if (oos != null)
                      {
                        oos.close();
                      }
                  }
                catch (IOException e)
                  {
                  }
              }
          }
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy