Package: ContentPresentationModelFactory
ContentPresentationModelFactory
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ContentPresentationModelFactory() |
|
|
|
|
|
||||||||||||||||||||
afterPropertiesSet() |
|
|
|
|
|
||||||||||||||||||||
createPresentationModel(Object, Collection) |
|
|
|
|
|
||||||||||||||||||||
destroy() |
|
|
|
|
|
||||||||||||||||||||
lambda$createPresentationModel$0(Object) |
|
|
|
|
|
||||||||||||||||||||
onContentCreated(ContentCreatedEvent) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone git@bitbucket.org:tidalwave/northernwind-rca-src.git
7: * %%
8: * Copyright (C) 2013 - 2021 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.model.impl.admin.role;
28:
29: import javax.annotation.Nonnull;
30: import java.beans.PropertyChangeSupport;
31: import java.util.Collection;
32: import java.util.WeakHashMap;
33: import it.tidalwave.util.Callback;
34: import it.tidalwave.util.NamedCallback;
35: import it.tidalwave.util.annotation.VisibleForTesting;
36: import it.tidalwave.role.ui.PresentationModel;
37: import it.tidalwave.role.ui.PresentationModelFactory;
38: import it.tidalwave.messagebus.annotation.ListensTo;
39: import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
40: import it.tidalwave.northernwind.core.model.Content;
41: import it.tidalwave.northernwind.rca.ui.event.ContentCreatedEvent;
42: import lombok.extern.slf4j.Slf4j;
43: import static it.tidalwave.role.ui.PresentationModel.PROPERTY_CHILDREN;
44: import static it.tidalwave.util.Parameters.r;
45:
46: /***********************************************************************************************************************
47: *
48: * An implementation of {@link PresentationModelFactory} for {@link Content} that keeps track of
49: * created instances and dispatches {@link ContentCreatedEvent}s to them. This to avoid that every single
50: * {@code ContentPresentationModel} subscribes to the message bus and have all of them but one to discard the received
51: * event if it's not related to their datum.
52: *
53: * @author Fabrizio Giudici
54: *
55: **********************************************************************************************************************/
56: @SimpleMessageSubscriber @Slf4j
57: public class ContentPresentationModelFactory implements PresentationModelFactory
58: {
59: @VisibleForTesting final WeakHashMap<Content, PresentationModel> map = new WeakHashMap<>();
60:
61: @Override @Nonnull
62: public PresentationModel createPresentationModel (@Nonnull final Object datum,
63: @Nonnull final Collection<Object> roles)
64: {
65: final Callback cb = NamedCallback.of(PresentationModel.CALLBACK_DISPOSE, () ->
66: {
67: map.remove(datum);
68: log.debug(">>>> unregistered: {}", this);
69: });
70:
71: final PresentationModel contentPM = PresentationModel.of(datum, r(cb, roles));
72: map.put((Content)datum, contentPM);
73: log.debug(">>>> created and registered: {}", contentPM);
74: return contentPM;
75: }
76:
77: @VisibleForTesting void onContentCreated (@ListensTo @Nonnull final ContentCreatedEvent event)
78: {
79: log.debug("onContentCreated({})", event);
80: // FIXME: map.getOptional(event.getParentContent()).ifPresent(cpm -> ...);
81: final PresentationModel contentPM = map.get(event.getParentContent());
82:
83:• if (contentPM != null)
84: {
85: log.debug(">>>> dispatching {} to {}", event, contentPM);
86: // FIXME: this is an undocumented feature
87: contentPM.as(PropertyChangeSupport.class).firePropertyChange(PROPERTY_CHILDREN, null, null);
88: }
89: }
90: }