Content of file DefaultContentEditorPresentationControl.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>DefaultContentEditorPresentationControl.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 Editor</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.rca.ui.contenteditor.spi</a> &gt; <span class="el_source">DefaultContentEditorPresentationControl.java</span></div><h1>DefaultContentEditorPresentationControl.java</h1><pre class="source lang-java linenums"><span class="fc" 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.contenteditor.spi;

import javax.annotation.Nonnull;
import java.util.Optional;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import it.tidalwave.role.ui.BoundProperty;
import it.tidalwave.role.ui.UserAction;
import it.tidalwave.messagebus.annotation.ListensTo;
import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
import it.tidalwave.northernwind.core.model.Content;
import it.tidalwave.northernwind.core.model.ResourceProperties;
import it.tidalwave.northernwind.rca.embeddedserver.EmbeddedServer;
import it.tidalwave.northernwind.rca.embeddedserver.EmbeddedServer.Document;
import it.tidalwave.northernwind.rca.ui.event.ContentSelectedEvent;
import it.tidalwave.northernwind.rca.ui.contenteditor.ContentEditorPresentation;
import it.tidalwave.northernwind.rca.ui.contenteditor.ContentEditorPresentation.Bindings;
import it.tidalwave.northernwind.rca.ui.contenteditor.ContentEditorPresentationControl;
import it.tidalwave.northernwind.rca.ui.contenteditor.impl.JSoupXhtmlNormalizer;
import it.tidalwave.northernwind.rca.ui.contenteditor.impl.ProcessExecutor;
import it.tidalwave.util.annotation.VisibleForTesting;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.northernwind.model.admin.Properties.*;
import static it.tidalwave.northernwind.model.admin.role.Saveable._Saveable_;
import static it.tidalwave.northernwind.rca.ui.contenteditor.spi.PropertyBinder.*;
import static it.tidalwave.role.ui.Presentable._Presentable_;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.joining;

/***********************************************************************************************************************
 *
 * A default implementation of the {@link ContentEditorPresentationControl}.
 *
 * @stereotype Control
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="pc bpc" id="L69" title="3 of 6 branches missed.">@RequiredArgsConstructor @SimpleMessageSubscriber @Slf4j</span>
public class DefaultContentEditorPresentationControl implements ContentEditorPresentationControl
  {
    @Nonnull
    private final EmbeddedServer documentServer;

    @Nonnull
    private final ContentEditorPresentation presentation;

<span class="fc" id="L78">    @Nonnull</span>
<span class="fc" id="L79">    private Optional&lt;Content&gt; content = Optional.empty();</span>

<span class="fc" id="L81">    @Nonnull</span>
<span class="fc" id="L82">    private Optional&lt;ResourceProperties&gt; properties = Optional.empty();</span>

<span class="fc" id="L84">    @VisibleForTesting final Bindings bindings = Bindings.builder()</span>
<span class="fc" id="L85">             .openExternalEditorAction(UserAction.of(this::openExternalEditor))</span>
<span class="fc" id="L86">             .openExternalEditorBrowserAction(UserAction.of(this::openExternalEditorBrowser))</span>
<span class="fc" id="L87">             .title(new BoundProperty&lt;&gt;(&quot;&quot;))</span>
<span class="fc" id="L88">             .build();</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
<span class="fc" id="L95">    @VisibleForTesting final UpdateCallback propertyUpdateCallback = (updatedProperties) -&gt;</span>
      {
<span class="nc" id="L97">        updatedProperties.as(_Saveable_).saveIn(content.get().getFile());</span>
<span class="nc" id="L98">        unbindProperties();</span>
<span class="nc" id="L99">        properties = content.map(Content::getProperties); // reload them</span>
        // FIXME: properties have to be re-bound, since they have been reloaded - but this makes the HTML editor
properties have to be re-bound, since they have been reloaded - but this makes the HTML editor
// to flicker and the caret in text editor to reset at position 0 - and to loop forever <span class="nc" id="L102"> properties.ifPresent(this::bindProperties);</span> <span class="nc" id="L103"> };</span> /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override public void initialize() { <span class="fc" id="L113"> presentation.bind(bindings);</span> <span class="fc" id="L114"> }</span> // FIXME: unbind at termination /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @VisibleForTesting void onContentSelected (@ListensTo @Nonnull final ContentSelectedEvent selectionEvent) { <span class="fc" id="L125"> log.debug(&quot;onContentSelected({})&quot;, selectionEvent);</span> <span class="fc" id="L127"> unbindProperties();</span> <span class="fc" id="L128"> content = selectionEvent.getContent();</span> <span class="fc" id="L129"> properties = content.map(Content::getProperties); // reload them</span> <span class="fc bfc" id="L131" title="All 2 branches covered."> if (content.isEmpty())</span> { <span class="fc" id="L133"> presentation.clear();</span> } else { <span class="fc" id="L137"> presentation.showUp();</span> } <span class="fc" id="L140"> properties.ifPresent(this::bindPropertiesAndLoadDocument);</span> <span class="fc" id="L141"> }</span> /******************************************************************************************************************* * * * ******************************************************************************************************************/ private void bindPropertiesAndLoadDocument (@Nonnull final ResourceProperties properties) { <span class="fc" id="L150"> bindProperties(properties);</span> <span class="fc" id="L151"> final PropertyBinder propertyBinder = properties.as(_PropertyBinder_);</span> <span class="fc" id="L152"> final Document document = propertyBinder.createBoundDocument(PROPERTY_FULL_TEXT, propertyUpdateCallback);</span> <span class="fc" id="L153"> presentation.populateDocument(documentServer.putDocument(&quot;/&quot;, document));</span> <span class="fc" id="L154"> }</span> /******************************************************************************************************************* * * * ******************************************************************************************************************/ private void bindProperties (@Nonnull final ResourceProperties properties) { <span class="pc bpc" id="L163" title="2 of 4 branches missed."> assert properties != null;</span> <span class="fc" id="L164"> presentation.bind(bindings); // FIXME: needed because of unbindAll()</span> <span class="fc" id="L165"> final PropertyBinder propertyBinder = properties.as(_PropertyBinder_);</span> <span class="fc" id="L166"> propertyBinder.bind(PROPERTY_TITLE, bindings.title, propertyUpdateCallback);</span> <span class="fc" id="L167"> presentation.populateProperties(properties.as(_Presentable_).createPresentationModel());</span> // presentation.populateProperties(PresentationModel.ofMaybePresentable(properties)); <span class="fc" id="L169"> }</span> /******************************************************************************************************************* * * * ******************************************************************************************************************/ private void unbindProperties() { <span class="fc" id="L178"> bindings.title.unbindAll();</span> <span class="fc" id="L179"> }</span> /******************************************************************************************************************* * * * ******************************************************************************************************************/ private void openExternalEditorBrowser() { <span class="nc" id="L188"> log.info(&quot;openExternalEditorBrowser&quot;);</span> // FIXME <span class="nc" id="L190"> final String url = &quot;http://localhost:12345/&quot;;</span> // Safari breaks Aloha editor <span class="nc" id="L193"> ProcessExecutor.forExecutable(&quot;Google Chrome.app&quot;)</span> <span class="nc" id="L194"> .withArguments2(url)</span> <span class="nc" id="L195"> .withPostMortemTask(() -&gt; properties.ifPresent(this::bindPropertiesAndLoadDocument))</span> <span class="nc" id="L196"> .execute();</span> <span class="nc" id="L197"> }</span> /******************************************************************************************************************* * * * ******************************************************************************************************************/ private void openExternalEditor() { <span class="nc" id="L206"> log.info(&quot;openExternalEditor&quot;);</span> <span class="nc" id="L207"> final String filePath = content.get().getFile().toFile().getAbsolutePath() + &quot;/fullText_en.xhtml&quot;;</span> <span class="nc" id="L209"> ProcessExecutor.forExecutable(&quot;BlueGriffon.app&quot;)</span> // FIXME // .withArguments2(content.get().getFile().getPath().asString()) <span class="nc" id="L212"> .withArguments2(filePath)</span> <span class="nc" id="L213"> .withPostMortemTask(() -&gt; properties.ifPresent(p -&gt; fix(p, filePath)))</span> <span class="nc" id="L214"> .execute();</span> <span class="nc" id="L215"> }</span> /******************************************************************************************************************* * * * ******************************************************************************************************************/ private void fix (@Nonnull final ResourceProperties properties, @Nonnull final String filePath) { try { <span class="nc" id="L226"> final Path path = Paths.get(filePath);</span> <span class="nc" id="L227"> final String s = Files.lines(path, UTF_8).collect(joining(&quot;\n&quot;))</span> <span class="nc" id="L228"> .replace(&quot;%27&quot;, &quot;'&quot;)</span> <span class="nc" id="L229"> .replace(&quot;%28&quot;, &quot;(&quot;)</span> <span class="nc" id="L230"> .replace(&quot;%29&quot;, &quot;)&quot;)</span> <span class="nc" id="L231"> .replace(&quot;%7B&quot;, &quot;{&quot;)</span> <span class="nc" id="L232"> .replace(&quot;%7D&quot;, &quot;}&quot;)</span> <span class="nc" id="L233"> .replace(&quot;%20&quot;, &quot; &quot;);</span> <span class="nc" id="L234"> final JSoupXhtmlNormalizer normalizer = new JSoupXhtmlNormalizer();</span> <span class="nc" id="L235"> Files.write(path, asList(normalizer.asNormalizedString(s)), UTF_8);</span> <span class="nc" id="L236"> bindPropertiesAndLoadDocument(properties);</span> } <span class="nc" id="L238"> catch (IOException e)</span> { <span class="nc" id="L240"> log.error(&quot;&quot;, e);</span> <span class="nc" id="L241"> }</span> <span class="nc" id="L242"> }</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>