MediaMetadataXsltAdapter.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * NorthernWind - lightweight CMS
  6.  * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
  7.  * %%
  8.  * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  * *********************************************************************************************************************
  11.  *
  12.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  13.  * the License. You may obtain a copy of the License at
  14.  *
  15.  *     http://www.apache.org/licenses/LICENSE-2.0
  16.  *
  17.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  18.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations under the License.
  20.  *
  21.  * *********************************************************************************************************************
  22.  *
  23.  *
  24.  * *********************************************************************************************************************
  25.  * #L%
  26.  */
  27. package it.tidalwave.northernwind.frontend.ui.component.gallery.spi;

  28. import javax.annotation.Nonnull;
  29. import javax.inject.Inject;
  30. import javax.inject.Provider;
  31. import it.tidalwave.util.Id;
  32. import it.tidalwave.util.NotFoundException;
  33. import it.tidalwave.northernwind.core.model.ResourceProperties;
  34. import it.tidalwave.northernwind.core.model.SiteNode;
  35. import it.tidalwave.northernwind.core.model.SiteProvider;
  36. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  37. import org.springframework.beans.factory.annotation.Configurable;
  38. import org.springframework.context.ApplicationContext;
  39. import lombok.extern.slf4j.Slf4j;

  40. /***********************************************************************************************************************
  41.  *
  42.  * @author  Fabrizio Giudici
  43.  *
  44.  **********************************************************************************************************************/
  45. @Configurable(preConstruction = true) @Slf4j
  46. public final class MediaMetadataXsltAdapter
  47.   {
  48.     private static MediaMetadataXsltAdapter instance;

  49.     @Inject
  50.     private ApplicationContext context;

  51.     @Inject
  52.     private Provider<SiteProvider> siteProvider;

  53.     @Nonnull
  54.     private final MediaMetadataProvider mediaMetadataProvider;

  55.     /*******************************************************************************************************************
  56.      *
  57.      *
  58.      *
  59.      ******************************************************************************************************************/
  60.     @Nonnull
  61.     public static String getMetadataString (@Nonnull final String id, @Nonnull final String format)
  62.       {
  63.         try
  64.           {
  65.             return getInstance().getMetadataString(new Id(id), format);
  66.           }
  67.         catch (Exception e)
  68.           {
  69.             log.error("During XSLT invocation", e);
  70.             return "ERROR";
  71.           }
  72.       }

  73.     /*******************************************************************************************************************
  74.      *
  75.      *
  76.      *
  77.      ******************************************************************************************************************/
  78.     @Nonnull
  79.     public static synchronized MediaMetadataXsltAdapter getInstance()
  80.       {
  81.         if (instance == null)
  82.           {
  83.             instance = new MediaMetadataXsltAdapter();
  84.           }

  85.         return instance;
  86.       }

  87.     /*******************************************************************************************************************
  88.      *
  89.      *
  90.      *
  91.      ******************************************************************************************************************/
  92.     private MediaMetadataXsltAdapter()
  93.       {
  94.         mediaMetadataProvider = findMediaMetadataProvider();
  95.       }

  96.     /*******************************************************************************************************************
  97.      *
  98.      *
  99.      *
  100.      ******************************************************************************************************************/
  101.     @Nonnull
  102.     private String getMetadataString (@Nonnull final Id id, @Nonnull final String format)
  103.       throws NotFoundException
  104.       {
  105.         // FIXME: should use current SiteNode properties
  106.         final ResourceProperties properties = siteProvider.get().getSite().find(SiteNode.class)
  107.                                                                           .withRelativePath("/")
  108.                                                                           .result()
  109.                                                                           .getProperties();
  110.         return mediaMetadataProvider.getMetadataString(id, format, properties);
  111.       }

  112.     /*******************************************************************************************************************
  113.      *
  114.      * Finds the {@link MediaMetadataProvider}.
  115.      *
  116.      ******************************************************************************************************************/
  117.     @Nonnull
  118.     private MediaMetadataProvider findMediaMetadataProvider()
  119.       {
  120.         // FIXME: should be read by Node properties
  121.         final String metadataProviderName = "EmbeddedMediaMetadataProvider";

  122.         try
  123.           {
  124.             return context.getBean(metadataProviderName, MediaMetadataProvider.class);
  125.           }
  126.         catch (NoSuchBeanDefinitionException e)
  127.           {
  128.             log.warn("Cannot find bean: {}", metadataProviderName);
  129.             return MediaMetadataProvider.VOID;
  130.           }
  131.       }
  132.   }