Skip to content

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 (JavaFX)</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-rca-ui-content-manager</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.rca.ui.contentmanager.impl</a> &gt; <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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.time.Instant;
import java.time.ZoneId;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URLEncoder;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.io.CharStreams;
import org.springframework.core.io.ClassPathResource;
import it.tidalwave.util.Key;
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.code.model.TimeProvider;
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.Cleanup;
import lombok.extern.slf4j.Slf4j;
import static java.util.stream.Collectors.joining;
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;

/***********************************************************************************************************************
 *
 * The default implementation of {@link AddContentPresentationControl}.
 *
 * @stereotype control
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L73">@SimpleMessageSubscriber @Slf4j</span>
<span class="fc" id="L74">public class DefaultAddContentPresentationControl implements AddContentPresentationControl</span>
  {
    @Inject
    private AddContentPresentation presentation;

    @Inject
    private IdFactory idFactory;

    @Inject
    private TimeProvider timeProvider;

<span class="fc" id="L85">    private final Bindings bindings = new ValidatingBindings();</span>

<span class="fc" id="L87">    /* visible for testing */ String xhtmlSkeleton = &quot;tbd&quot;;</span>

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @PostConstruct
    /* visible for testing */ void initialize()
      throws IOException
      {
<span class="fc" id="L96">        xhtmlSkeleton = loadResource(&quot;Skeleton.xhtml&quot;);</span>
<span class="fc" id="L97">      }</span>

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    /* visible for testing */ void onCreateContentRequest (final @ListensTo @Nonnull CreateContentRequest event)
      {
<span class="fc" id="L104">        log.info(&quot;onCreateContentRequest({})&quot;, event);</span>
<span class="fc" id="L105">        bindings.publishingDateTime.set(timeProvider.getNow());</span>
<span class="fc" id="L106">        presentation.bind(bindings);</span>
<span class="fc" id="L107">        presentation.showUp(notificationWithFeedback()</span>
<span class="fc" id="L108">                .withCaption(&quot;Create new content&quot;)</span>
<span class="fc" id="L109">                .withFeedback(feedback().withOnConfirm(() -&gt; createContent(event.getParentContent()))</span>
<span class="fc" id="L110">                                        .withOnCancel(() -&gt; {}))); // FIXME: withOnCancel(DO_NOTHING)</span>
<span class="fc" id="L111">      }</span>

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    private void createContent (final @Nonnull Content parentContent)
      throws IOException
      {
<span class="fc" id="L119">        final String folderName = urlEncoded(bindings.folder.get());</span>
        // TODO: use TypeSafeMap, with a safe put() method
use TypeSafeMap, with a safe put() method
<span class="fc" id="L121"> final Map&lt;Key&lt;?&gt;, Object&gt; propertyValues = new HashMap&lt;&gt;();</span> <span class="fc" id="L122"> putIfNonEmpty(propertyValues, PROPERTY_TITLE, bindings.title.get());</span> <span class="fc" id="L123"> putIfNonEmpty(propertyValues, PROPERTY_EXPOSED_URI, bindings.exposedUri.get());</span> <span class="fc" id="L124"> putIfNonEmpty(propertyValues, PROPERTY_CREATION_TIME, timeProvider.getNow());</span> <span class="fc" id="L125"> putIfNonEmpty(propertyValues, PROPERTY_PUBLISHING_TIME, bindings.publishingDateTime.get());</span> <span class="fc" id="L126"> putIfNonEmpty(propertyValues, PROPERTY_FULL_TEXT, xhtmlSkeleton);</span> <span class="fc" id="L127"> putIfNonEmpty(propertyValues, PROPERTY_ID, idFactory.createId());</span> <span class="fc" id="L129"> final Splitter splitter = Splitter.on(',').trimResults().omitEmptyStrings();</span> <span class="fc" id="L130"> final List&lt;String&gt; tags = Lists.newArrayList(splitter.split(bindings.tags.get()));</span> <span class="fc bfc" id="L132" title="All 2 branches covered."> if (!tags.isEmpty())</span> { // See NWRCA-69 <span class="fc" id="L135"> propertyValues.put(PROPERTY_TAGS, tags.stream().collect(joining(&quot;,&quot;)));</span> // propertyValues.put(PROPERTY_TAGS, tags); } <span class="fc" id="L139"> parentContent.as(ContentChildCreator).createContent(folderName, propertyValues);</span> <span class="fc" id="L140"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ private static &lt;T&gt; void putIfNonEmpty (final @Nonnull Map&lt;Key&lt;?&gt;, Object&gt; values, final @Nonnull Key&lt;T&gt; key, final @CheckForNull T value) { <span class="pc bpc" id="L149" title="2 of 4 branches missed."> if ((value != null) &amp;&amp; !&quot;&quot;.equals(value))</span> { <span class="fc" id="L151"> values.put(key, value);</span> } <span class="fc" id="L153"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private static String urlEncoded (final @Nonnull String string) { try { <span class="fc" id="L163"> return URLEncoder.encode(string, &quot;UTF-8&quot;);</span> } <span class="nc" id="L165"> catch (UnsupportedEncodingException e)</span> { <span class="nc" id="L167"> throw new RuntimeException(e); // never happens</span> } } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull /* visible for testing */ String loadResource (final @Nonnull String path) throws IOException { <span class="fc" id="L178"> final String prefix = getClass().getPackage().getName().replace(&quot;.&quot;, &quot;/&quot;);</span> <span class="fc" id="L179"> final ClassPathResource resource = new ClassPathResource(prefix + &quot;/&quot; + path);</span> <span class="pc bpc" id="L180" title="1 of 2 branches missed."> final @Cleanup Reader r = new InputStreamReader(resource.getInputStream(), &quot;UTF-8&quot;);</span> <span class="fc" id="L181"> return CharStreams.toString(r);</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.6.202009150832</span></div></body></html>