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

it.tidalwave.bluebill.factsheet.impl.DefaultDocumentable 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.factsheet.impl;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.lang.ref.WeakReference;
import org.openide.util.Lookup;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.bluebill.factsheet.Documentable;
import it.tidalwave.bluebill.factsheet.FactSheet;
import it.tidalwave.bluebill.factsheet.spi.FactSheetProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * @stereotype Role
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@RequiredArgsConstructor @Slf4j
public class DefaultDocumentable implements Documentable
  {
    @Nonnull
    private final Taxon taxon;

    @CheckForNull
    private WeakReference factSheetRef;
    
    @Nonnull
    public synchronized FactSheet getFactSheet()
      throws NotFoundException 
      {
        log.info("getFactSheet() - {}", taxon);
        log.debug(">>>> weak reference: {}", factSheetRef);
        FactSheet factSheet = null;

        if (factSheetRef != null)
          {
            factSheet = factSheetRef.get();
            log.debug(">>>> factSheet from weak reference: {}", factSheet);
          }

        if (factSheet == null)
          {
            log.info(">>>> loading fact sheet for {}...", taxon);
            final long time = System.currentTimeMillis();
            factSheet = new FactSheet(taxon.getId());

            // FIXME: use Locator.findAll() or Locator.createAllProvidersFor()
            for (final FactSheetProvider provider : Lookup.getDefault().lookupAll(FactSheetProvider.class))
              {
                log.debug(">>>> calling {} ...", provider);
                factSheet = factSheet.mergedWith(provider.createFactSheetFor(taxon));  
              }

            log.debug(">>>> fact sheet loaded in {} msec...", System.currentTimeMillis() - time);
            factSheetRef = new WeakReference(factSheet);
          }

        return factSheet;
      }
  }

//    @Nonnull
//    public FactSheet createFactSheet (final @Nonnull As object)
//      {
//        log.info("createFactSheet({})", object);
//        
//        FactSheet factSheet = new FactSheet(object.as(Identifiable).getId());
//
//        for (final FactSheetProvider factSheetProvider : Lookup.getDefault().lookupAll(FactSheetProvider.class))
//          {
//            try
//              {
//                log.info(">>>> calling {}...", factSheetProvider);
//                factSheet = factSheetProvider.fillFactSheet(factSheet);
//              }
//            catch (IOException e)
//              {
//                log.error("Failed calling {}", factSheetProvider);
//                log.error("createFactSheed()", e);
//              }
//          }
//
//        return factSheet;
//      }    




© 2015 - 2025 Weber Informatics LLC | Privacy Policy