Skip to content

Package: GalleryLoaderSupport

GalleryLoaderSupport

nameinstructionbranchcomplexitylinemethod
GalleryLoaderSupport(BeanFactory, ResourceProperties)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
createItem(Id)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
findMediaMetadataProvider()
M: 22 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

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.loader;
28:
29: import javax.annotation.Nonnull;
30: import org.springframework.beans.factory.BeanFactory;
31: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
32: import it.tidalwave.util.Id;
33: import it.tidalwave.util.Key;
34: import it.tidalwave.northernwind.core.model.ResourceProperties;
35: import it.tidalwave.northernwind.frontend.ui.component.gallery.GalleryViewController.GalleryItem;
36: import it.tidalwave.northernwind.frontend.ui.component.gallery.spi.GalleryLoader;
37: import it.tidalwave.northernwind.frontend.ui.component.gallery.spi.MediaMetadataProvider;
38: import lombok.extern.slf4j.Slf4j;
39:
40: /***********************************************************************************************************************
41: *
42: * A support implementation for {@link GalleryLoader}, provides a default factory method for creating gallery
43: * {@link GalleryItem}s which delegates to a {@link MediaMetadataProvider}.
44: *
45: * @author Fabrizio Giudici
46: *
47: **********************************************************************************************************************/
48: @Slf4j
49: public abstract class GalleryLoaderSupport implements GalleryLoader
50: {
51: private static final Key<String> P_MEDIA_METADATA_PROVIDER = Key.of("mediaMetadataProvider", String.class);
52:
53: @Nonnull
54: private final BeanFactory beanFactory;
55:
56: @Nonnull
57: private final MediaMetadataProvider mediaMetadataProvider;
58:
59: @Nonnull
60: private final ResourceProperties properties;
61:
62: /*******************************************************************************************************************
63: *
64: *
65: ******************************************************************************************************************/
66: protected GalleryLoaderSupport (@Nonnull final BeanFactory beanFactory,
67: @Nonnull final ResourceProperties properties)
68: {
69: this.beanFactory = beanFactory;
70: this.properties = properties;
71: mediaMetadataProvider = findMediaMetadataProvider();
72: }
73:
74: /*******************************************************************************************************************
75: *
76: * Creates a gallery {@link GalleryItem} for the given media id.
77: *
78: * @param mediaId the media id
79: * @return the gallery {@code Item}
80: *
81: *
82: ******************************************************************************************************************/
83: @Nonnull
84: public GalleryItem createItem (@Nonnull final Id mediaId)
85: {
86: return new GalleryItem(mediaId, mediaMetadataProvider.getMetadataString(mediaId, "$IPTC.IIM.title$", properties));
87: }
88:
89: /*******************************************************************************************************************
90: *
91: * Finds the {@link MediaMetadataProvider}.
92: *
93: ******************************************************************************************************************/
94: @Nonnull
95: private MediaMetadataProvider findMediaMetadataProvider()
96: {
97: final var metadataProviderName = properties.getProperty(P_MEDIA_METADATA_PROVIDER).orElse("");
98:
99: try
100: {
101: return beanFactory.getBean(metadataProviderName, MediaMetadataProvider.class);
102: }
103: catch (NoSuchBeanDefinitionException e)
104: {
105: log.warn("Cannot find bean: {}", metadataProviderName);
106: return MediaMetadataProvider.VOID;
107: }
108: }
109: }