it.tidalwave.bluebill.mobile.observation.BlueBillObservationManager Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* 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 3e7ea5dbefcb 2010/05/17 15:10:54 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;
import org.openide.util.lookup.ServiceProvider;
import it.tidalwave.util.Id;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.mobile.FileSystem;
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.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 final Lookup lookup = Lookup.getDefault();
private final 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()
{
fileSystem = lookup.lookup(FileSystem.class);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@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()
{
logger.warning(">>>> 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