Skip to contentMethod: lambda$createMediaFolder$4(Identifiable)
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.mediaserver.impl;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.PostConstruct;
31: import java.util.Collection;
32: import java.util.Collections;
33: import java.util.List;
34: import java.util.Optional;
35: import java.nio.file.Path;
36: import java.nio.file.Paths;
37: import org.springframework.beans.factory.annotation.Autowired;
38: import it.tidalwave.role.ui.Displayable;
39: import it.tidalwave.bluemarine2.model.MediaFolder;
40: import it.tidalwave.bluemarine2.model.VirtualMediaFolder;
41: import it.tidalwave.bluemarine2.model.VirtualMediaFolder.EntityCollectionFactory;
42: import it.tidalwave.bluemarine2.model.impl.PathAwareMediaFolderDecorator;
43: import it.tidalwave.bluemarine2.model.role.EntityBrowser;
44: import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
45: import it.tidalwave.bluemarine2.mediaserver.ContentDirectory;
46: import it.tidalwave.bluemarine2.mediaserver.spi.MediaServerService;
47: import lombok.extern.slf4j.Slf4j;
48: import static java.util.Comparator.*;
49: import static java.util.stream.Collectors.*;
50: import static it.tidalwave.util.Parameters.r;
51: import static it.tidalwave.role.Identifiable._Identifiable_;
52:
53: /***********************************************************************************************************************
54: *
55: * @author Fabrizio Giudici
56: *
57: **********************************************************************************************************************/
58: @Slf4j
59: public class DefaultContentDirectory implements ContentDirectory
60: {
61: private static final Path PATH_ROOT = Paths.get("/");
62:
63: private static final Path PATH_VIDEOS = Paths.get("videos");
64:
65: private static final Path PATH_PHOTOS = Paths.get("photos");
66:
67: private static final Path PATH_MUSIC = Paths.get("music");
68:
69: private static final Path PATH_SERVICES = Paths.get("services");
70:
71: private static final EntityCollectionFactory EMPTY = x -> Collections.emptyList(); // FIXME: move to ECF
72:
73: // @Inject FIXME
74: @Autowired(required = false)
75: private final List<EntityBrowser> entityBrowsers = Collections.emptyList();
76:
77: // @Inject FIXME
78: @Autowired(required = false)
79: private final List<MediaServerService> services = Collections.emptyList();
80:
81: private MediaFolder root;
82:
83: @Override @Nonnull
84: public MediaFolder findRoot()
85: {
86: return root;
87: }
88:
89: @PostConstruct
90: private void initialize()
91: {
92: // FIXME: why is this called multiple times?
93: log.info(">>>> discovered entity browsers: {}", entityBrowsers);
94: log.info(">>>> discovered services: {}", services);
95: root = new VirtualMediaFolder(Optional.empty(), PATH_ROOT, "", this::childrenFactory);
96: }
97:
98: @Nonnull
99: private Collection<PathAwareEntity> childrenFactory (@Nonnull final MediaFolder parent)
100: {
101: return List.of(new VirtualMediaFolder(parent, PATH_MUSIC, "Music", this::musicFactory),
102: new VirtualMediaFolder(parent, PATH_PHOTOS, "Photos", EMPTY),
103: new VirtualMediaFolder(parent, PATH_VIDEOS, "Videos", EMPTY),
104: new VirtualMediaFolder(parent, PATH_SERVICES, "Services", this::servicesFactory));
105: }
106:
107: @Nonnull
108: private Collection<MediaFolder> musicFactory (@Nonnull final MediaFolder parent)
109: {
110: // TODO: filter by MIME type
111: return entityBrowsers.stream()
112: .sorted(comparing(browser -> browser.as(Displayable.class).getDisplayName()))
113: .map(browser -> createMediaFolder(parent, browser))
114: .collect(toList());
115: }
116:
117: @Nonnull
118: private Collection<MediaFolder> servicesFactory (@Nonnull final MediaFolder parent)
119: {
120: return services.stream().map(service -> service.createRootFolder(parent)).collect(toList());
121: }
122:
123: @Nonnull
124: private static MediaFolder createMediaFolder (@Nonnull final MediaFolder parent,
125: @Nonnull final EntityBrowser browser)
126: {
127: final String fallBack = browser.getClass().getSimpleName();
128: final String pathSegment = browser.maybeAs(_Identifiable_).map(i -> i.getId().stringValue()).orElse(fallBack);
129: final Displayable displayable = browser.maybeAs(Displayable.class).orElse(Displayable.of(fallBack));
130: log.trace("createMediaFolder({}, {}) - path: {} displayable: {}", parent, browser, pathSegment, displayable);
131: return new PathAwareMediaFolderDecorator(browser.getRoot(), parent, Paths.get(pathSegment), r(displayable));
132: }
133: }