Content of file DefaultAddContentPresentationControl.java.html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>DefaultAddContentPresentationControl.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Zephyr - UI Content Manager</a> > <a href="index.source.html" class="el_package">it.tidalwave.northernwind.rca.ui.contentmanager.impl</a> > <span class="el_source">DefaultAddContentPresentationControl.java</span></div><h1>DefaultAddContentPresentationControl.java</h1><pre class="source lang-java linenums"><span class="pc" id="L1">/*</span>
* #%L
* *********************************************************************************************************************
*
* NorthernWind - lightweight CMS
* http://northernwind.tidalwave.it - git clone git@bitbucket.org:tidalwave/northernwind-rca-src.git
* %%
* Copyright (C) 2013 - 2021 Tidalwave s.a.s. (http://tidalwave.it)
* %%
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
*
* *********************************************************************************************************************
* #L%
*/
package it.tidalwave.northernwind.rca.ui.contentmanager.impl;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.stream.Collectors;
import it.tidalwave.util.TypeSafeMap;
import it.tidalwave.util.annotation.VisibleForTesting;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import it.tidalwave.util.Key;
import it.tidalwave.util.TimeProvider;
import it.tidalwave.role.IdFactory;
import it.tidalwave.messagebus.annotation.ListensTo;
import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
import it.tidalwave.northernwind.core.model.Content;
import it.tidalwave.northernwind.rca.ui.event.CreateContentRequest;
import it.tidalwave.northernwind.rca.ui.contentmanager.AddContentPresentation;
import it.tidalwave.northernwind.rca.ui.contentmanager.AddContentPresentation.Bindings;
import it.tidalwave.northernwind.rca.ui.contentmanager.AddContentPresentationControl;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.util.ui.UserNotificationWithFeedback.*;
import static it.tidalwave.northernwind.model.admin.Properties.*;
import static it.tidalwave.northernwind.rca.ui.contentmanager.impl.ContentChildCreator._ContentChildCreator_;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.toList;
/***********************************************************************************************************************
*
* The default implementation of {@link AddContentPresentationControl}.
*
* @stereotype control
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="pc bpc" id="L70" title="3 of 6 branches missed.">@SimpleMessageSubscriber @RequiredArgsConstructor @Slf4j</span>
public class DefaultAddContentPresentationControl implements AddContentPresentationControl
{
@Nonnull
private final AddContentPresentation presentation;
@Nonnull
private final IdFactory idFactory;
@Nonnull
private final TimeProvider timeProvider;
<span class="fc" id="L82"> private final Bindings bindings = new ValidatingBindings();</span>
<span class="fc" id="L84"> @VisibleForTesting String xhtmlSkeleton = "tbd";</span>
/*******************************************************************************************************************
*
******************************************************************************************************************/
@PostConstruct
@VisibleForTesting void initialize()
throws IOException
{
<span class="fc" id="L93"> xhtmlSkeleton = loadResource("Skeleton.xhtml");</span>
<span class="fc" id="L94"> }</span>
/*******************************************************************************************************************
*
******************************************************************************************************************/
@VisibleForTesting void onCreateContentRequest (@ListensTo @Nonnull final CreateContentRequest event)
{
<span class="fc" id="L101"> log.info("onCreateContentRequest({})", event);</span>
<span class="fc" id="L102"> bindings.publishingDateTime.set(timeProvider.currentZonedDateTime());</span>
<span class="fc" id="L103"> presentation.bind(bindings);</span>
<span class="fc" id="L104"> presentation.showUp(notificationWithFeedback()</span>
<span class="fc" id="L105"> .withCaption("Create new content")</span>
<span class="fc" id="L106"> .withFeedback(feedback().withOnConfirm(() -> createContent(event.getParentContent()))</span>
<span class="fc" id="L107"> .withOnCancel(() -> {}))); // FIXME: withOnCancel(DO_NOTHING)</span>
<span class="fc" id="L108"> }</span>
/*******************************************************************************************************************
*
******************************************************************************************************************/
private void createContent (@Nonnull final Content parentContent)
throws IOException
{
<span class="fc" id="L116"> final String folderName = urlEncoded(bindings.folder.get());</span>
// TODO: use TypeSafeMap, with a safe put() method
<span class="fc" id="L118"> TypeSafeMap map = TypeSafeMap.newInstance();</span>
<span class="fc" id="L119"> map = putIfNonEmpty(map, PROPERTY_TITLE, bindings.title.get());</span>
<span class="fc" id="L120"> map = putIfNonEmpty(map, PROPERTY_EXPOSED_URI, bindings.exposedUri.get());</span>
<span class="fc" id="L121"> map = putIfNonEmpty(map, PROPERTY_CREATION_TIME, timeProvider.currentZonedDateTime());</span>
<span class="fc" id="L122"> map = putIfNonEmpty(map, PROPERTY_PUBLISHING_TIME, bindings.publishingDateTime.get());</span>
<span class="fc" id="L123"> map = putIfNonEmpty(map, PROPERTY_FULL_TEXT, xhtmlSkeleton);</span>
<span class="fc" id="L124"> map = putIfNonEmpty(map, PROPERTY_ID, idFactory.createId());</span>
<span class="fc" id="L126"> final List<String> tags = Arrays.stream(bindings.tags.get().split(","))</span>
<span class="fc" id="L127"> .map(String::trim)</span>
<span class="fc" id="L128"> .filter(not(String::isEmpty))</span>
<span class="fc" id="L129"> .collect(toList());</span>
<span class="fc bfc" id="L131" title="All 2 branches covered."> if (!tags.isEmpty())</span>
{
// See NWRCA-69
<span class="fc" id="L134"> map = map.with(PROPERTY_TAGS, String.join(",", tags));</span>
// map.put(PROPERTY_TAGS, tags);
}
<span class="fc" id="L138"> parentContent.as(_ContentChildCreator_).createContent(folderName, map);</span>
<span class="fc" id="L139"> }</span>
/*******************************************************************************************************************
*
******************************************************************************************************************/
private static <T> TypeSafeMap putIfNonEmpty (@Nonnull TypeSafeMap values,
@Nonnull final Key<T> key,
@CheckForNull final T value)
{
<span class="pc bpc" id="L148" title="2 of 4 branches missed."> if ((value != null) && !"".equals(value))</span>
{
<span class="fc" id="L150"> values = values.with(key, value);</span>
}
<span class="fc" id="L153"> return values;</span>
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
@Nonnull
private static String urlEncoded (@Nonnull final String string)
{
<span class="fc" id="L162"> return URLEncoder.encode(string, UTF_8);</span>
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
@Nonnull
@VisibleForTesting String loadResource (@Nonnull final String path)
throws IOException
{
<span class="fc" id="L172"> final String prefix = getClass().getPackage().getName().replace(".", "/");</span>
<span class="fc" id="L173"> final ClassPathResource resource = new ClassPathResource(prefix + "/" + path);</span>
<span class="fc" id="L175"> try (final BufferedReader r = new BufferedReader(new InputStreamReader(resource.getInputStream(), UTF_8)))</span>
{
<span class="fc" id="L177"> return r.lines().collect(Collectors.joining("\n"));</span>
}
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>