it.tidalwave.bluebill.mobile.taxonomy.factsheet.spi.TaxonFactSheetViewControllerSupport Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* 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.factsheet.spi;
import java.util.concurrent.ExecutorService;
import it.tidalwave.semantic.Type;
import it.tidalwave.netbeans.capabilitiesprovider.ThreadLookupBinder;
import it.tidalwave.util.Task;
import javax.inject.Provider;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import org.openrdf.model.Graph;
import it.tidalwave.util.AsException;
import it.tidalwave.util.thread.annotation.ThreadConfined;
import it.tidalwave.role.ui.PresentationModel;
import it.tidalwave.role.ui.spi.DefaultPresentationModel;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.observation.ObservationSet;
import it.tidalwave.mobile.asset.SmartAssetManager;
import it.tidalwave.semantic.io.GraphDeserializer;
import it.tidalwave.semantic.io.GraphUnmarshaller;
import it.tidalwave.semantic.io.MimeTypes;
import it.tidalwave.semantic.io.impl.DefaultGraphDeserializer;
import it.tidalwave.semantic.io.impl.GraphUnmarshallerImpl;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.bluebill.mobile.role.util.RoleRegister;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonHistory;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.ui.TaxonFactSheetView;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.ui.TaxonFactSheetViewController;
import it.tidalwave.observation.simple.SimpleObservationSet;
import it.tidalwave.util.NotFoundException;
import lombok.Cleanup;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.util.thread.ThreadType.*;
import static it.tidalwave.util.ui.UserNotification.notification;
import static it.tidalwave.role.Composite.Composite;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
@Slf4j @RequiredArgsConstructor
public abstract class TaxonFactSheetViewControllerSupport
implements TaxonFactSheetViewController
{
private static final Class _ = TaxonFactSheetViewControllerSupport.class;
private static final Type TYPE_OBSERVATION_SET = new Type("http://www.tidalwave.it/rdf/observation/2010/07/01#ObservationSet"); // FIXME: put in vocabulary
@Nonnull
protected final View view;
/* package */ final DefaultPresentationModel presentationModel = new DefaultPresentationModel(); // FIXME: PresentationModel
protected final RoleRegister roleRegister = new RoleRegister();
private final Provider taxonHistory = Locator.createProviderFor(TaxonHistory.class);
@Nonnull
private final Provider smartAssetManager = Locator.createProviderFor(SmartAssetManager.class);
@Nonnull
private final ExecutorService executorService = Locator.find(ExecutorService.class);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public PresentationModel getPresentationModel (final @Nonnegative int index)
{
return presentationModel.getChild(index);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@ThreadConfined(type=ANY)
public final void initialize (final @Nonnull Taxon taxon)
{
log.info("initialize({})", taxon);
view.lock(notification().withText(_, "preparingData"));
view.renderTaxon(taxon);
executorService.submit(new Runnable()
{
@Override
public void run()
{
try
{
taxonHistory.get().addToTaxonHistory(taxon);
clearPresentationModels();
createPresentationModelsWithRoleRegister(taxon);
}
catch (Throwable e)
{
log.error("While creating PresentationModels: {}", e.toString());
log.error("", e);
// FIXME: notify error on View, stop progress dialog
}
populateView();
}
});
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@ThreadConfined(type=NOT_UI)
protected void clearPresentationModels()
{
presentationModel.clear();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@ThreadConfined(type=NOT_UI)
protected void addPresentationModel (final @Nonnull PresentationModel presentationModel)
{
this.presentationModel.addChild(presentationModel);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@ThreadConfined(type=NOT_UI)
protected void createPresentationModelsWithRoleRegister (final @Nonnull Taxon taxon)
throws Exception
{
ThreadLookupBinder.with(roleRegister).run(new Task()
{
public Void run()
throws Exception
{
createPresentationModels(taxon);
return null;
}
});
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@ThreadConfined(type=NOT_UI)
protected abstract void createPresentationModels (final @Nonnull Taxon taxon)
throws Exception;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void populateView()
{
log.info("populate() - presentation model: {}", presentationModel);
try
{
if (presentationModel.as(Composite).findChildren().count() > 0)
{
view.populate(presentationModel);
}
else
{
view.notifyNoData(notification().withText(_, "noData"));
}
}
catch (AsException e)
{
view.notifyNoData(notification().withText(_, "noData"));
}
view.unlock();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public ObservationSet loadObservationSet (final @Nonnull Taxon taxon) // FIXME: should be protected,
throws IOException
{
// FIXME: now it loads xeno-canto, but should be a generic observation set with any kind of media
log.info("loadObservationSet({})", taxon);
final String resourceName = taxon.getId().stringValue().replaceAll(":", "_") + ".n3";
log.info(">>>> resourceName: {}", resourceName);
final File file = smartAssetManager.get().findAsset("factsheets.zip");
log.info(">>>> zip file: {}", file);
final @Cleanup ZipFile zipFile = new ZipFile(file);
final ZipEntry entry = zipFile.getEntry(resourceName);
if (entry == null)
{
log.warn("No entry for {}", resourceName);
return new SimpleObservationSet();
}
final @Cleanup InputStream is = zipFile.getInputStream(entry);
return loadObservationSet(is, MimeTypes.MIME_N3);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
protected static ObservationSet loadObservationSet (final @Nonnull InputStream is, final @Nonnull String mimeType)
throws IOException
{
try
{
final GraphDeserializer deserializer = new DefaultGraphDeserializer();
final Graph graph = deserializer.read(is, mimeType);
final GraphUnmarshaller unmarshaller = new GraphUnmarshallerImpl();
return unmarshaller.unmarshal(graph, TYPE_OBSERVATION_SET);
}
catch (NotFoundException e)
{
return new SimpleObservationSet();
}
catch (Exception e)
{
throw new RuntimeException(e);
// throw new IOException(e.toString()); // Java 5 / Android compatibility
}
}
}