Skip to content

Package: DefaultAddContentPresentationControl

DefaultAddContentPresentationControl

nameinstructionbranchcomplexitylinemethod
DefaultAddContentPresentationControl(AddContentPresentation, IdFactory, TimeProvider)
M: 15 C: 29
66%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 0 C: 3
100%
M: 0 C: 1
100%
afterPropertiesSet()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createContent(Content)
M: 0 C: 92
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
destroy()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
initialize()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$onCreateContentRequest$0(CreateContentRequest)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$onCreateContentRequest$1()
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
loadResource(String)
M: 0 C: 34
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
onCreateContentRequest(CreateContentRequest)
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
putIfNonEmpty(TypeSafeMap, Key, Object)
M: 0 C: 13
100%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 3
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
urlEncoded(String)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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.rca.ui.contentmanager.impl;
28:
29: import javax.annotation.CheckForNull;
30: import javax.annotation.Nonnull;
31: import javax.annotation.PostConstruct;
32: import java.io.BufferedReader;
33: import java.util.Arrays;
34: import java.util.List;
35: import java.io.IOException;
36: import java.io.InputStreamReader;
37: import java.net.URLEncoder;
38: import java.util.stream.Collectors;
39: import it.tidalwave.util.TypeSafeMap;
40: import it.tidalwave.util.annotation.VisibleForTesting;
41: import lombok.RequiredArgsConstructor;
42: import org.springframework.core.io.ClassPathResource;
43: import it.tidalwave.util.Key;
44: import it.tidalwave.util.TimeProvider;
45: import it.tidalwave.role.IdFactory;
46: import it.tidalwave.messagebus.annotation.ListensTo;
47: import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
48: import it.tidalwave.northernwind.core.model.Content;
49: import it.tidalwave.northernwind.rca.ui.event.CreateContentRequest;
50: import it.tidalwave.northernwind.rca.ui.contentmanager.AddContentPresentation;
51: import it.tidalwave.northernwind.rca.ui.contentmanager.AddContentPresentation.Bindings;
52: import it.tidalwave.northernwind.rca.ui.contentmanager.AddContentPresentationControl;
53: import lombok.extern.slf4j.Slf4j;
54: import static it.tidalwave.util.ui.UserNotificationWithFeedback.*;
55: import static it.tidalwave.northernwind.model.admin.Properties.*;
56: import static it.tidalwave.northernwind.rca.ui.contentmanager.impl.ContentChildCreator._ContentChildCreator_;
57: import static java.nio.charset.StandardCharsets.UTF_8;
58: import static java.util.function.Predicate.not;
59: import static java.util.stream.Collectors.toList;
60:
61: /***********************************************************************************************************************
62: *
63: * The default implementation of {@link AddContentPresentationControl}.
64: *
65: * @stereotype control
66: *
67: * @author Fabrizio Giudici
68: *
69: **********************************************************************************************************************/
70:•@SimpleMessageSubscriber @RequiredArgsConstructor @Slf4j
71: public class DefaultAddContentPresentationControl implements AddContentPresentationControl
72: {
73: @Nonnull
74: private final AddContentPresentation presentation;
75:
76: @Nonnull
77: private final IdFactory idFactory;
78:
79: @Nonnull
80: private final TimeProvider timeProvider;
81:
82: private final Bindings bindings = new ValidatingBindings();
83:
84: @VisibleForTesting String xhtmlSkeleton = "tbd";
85:
86: /*******************************************************************************************************************
87: *
88: ******************************************************************************************************************/
89: @PostConstruct
90: @VisibleForTesting void initialize()
91: throws IOException
92: {
93: xhtmlSkeleton = loadResource("Skeleton.xhtml");
94: }
95:
96: /*******************************************************************************************************************
97: *
98: ******************************************************************************************************************/
99: @VisibleForTesting void onCreateContentRequest (@ListensTo @Nonnull final CreateContentRequest event)
100: {
101: log.info("onCreateContentRequest({})", event);
102: bindings.publishingDateTime.set(timeProvider.currentZonedDateTime());
103: presentation.bind(bindings);
104: presentation.showUp(notificationWithFeedback()
105: .withCaption("Create new content")
106: .withFeedback(feedback().withOnConfirm(() -> createContent(event.getParentContent()))
107: .withOnCancel(() -> {}))); // FIXME: withOnCancel(DO_NOTHING)
108: }
109:
110: /*******************************************************************************************************************
111: *
112: ******************************************************************************************************************/
113: private void createContent (@Nonnull final Content parentContent)
114: throws IOException
115: {
116: final String folderName = urlEncoded(bindings.folder.get());
117: // TODO: use TypeSafeMap, with a safe put() method
118: TypeSafeMap map = TypeSafeMap.newInstance();
119: map = putIfNonEmpty(map, PROPERTY_TITLE, bindings.title.get());
120: map = putIfNonEmpty(map, PROPERTY_EXPOSED_URI, bindings.exposedUri.get());
121: map = putIfNonEmpty(map, PROPERTY_CREATION_TIME, timeProvider.currentZonedDateTime());
122: map = putIfNonEmpty(map, PROPERTY_PUBLISHING_TIME, bindings.publishingDateTime.get());
123: map = putIfNonEmpty(map, PROPERTY_FULL_TEXT, xhtmlSkeleton);
124: map = putIfNonEmpty(map, PROPERTY_ID, idFactory.createId());
125:
126: final List<String> tags = Arrays.stream(bindings.tags.get().split(","))
127: .map(String::trim)
128: .filter(not(String::isEmpty))
129: .collect(toList());
130:
131:• if (!tags.isEmpty())
132: {
133: // See NWRCA-69
134: map = map.with(PROPERTY_TAGS, String.join(",", tags));
135: // map.put(PROPERTY_TAGS, tags);
136: }
137:
138: parentContent.as(_ContentChildCreator_).createContent(folderName, map);
139: }
140:
141: /*******************************************************************************************************************
142: *
143: ******************************************************************************************************************/
144: private static <T> TypeSafeMap putIfNonEmpty (@Nonnull TypeSafeMap values,
145: @Nonnull final Key<T> key,
146: @CheckForNull final T value)
147: {
148:• if ((value != null) && !"".equals(value))
149: {
150: values = values.with(key, value);
151: }
152:
153: return values;
154: }
155:
156: /*******************************************************************************************************************
157: *
158: ******************************************************************************************************************/
159: @Nonnull
160: private static String urlEncoded (@Nonnull final String string)
161: {
162: return URLEncoder.encode(string, UTF_8);
163: }
164:
165: /*******************************************************************************************************************
166: *
167: ******************************************************************************************************************/
168: @Nonnull
169: @VisibleForTesting String loadResource (@Nonnull final String path)
170: throws IOException
171: {
172: final String prefix = getClass().getPackage().getName().replace(".", "/");
173: final ClassPathResource resource = new ClassPathResource(prefix + "/" + path);
174:
175: try (final BufferedReader r = new BufferedReader(new InputStreamReader(resource.getInputStream(), UTF_8)))
176: {
177: return r.lines().collect(Collectors.joining("\n"));
178: }
179: }
180: }