ResourcePropertiesBinder.java

  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.contenteditor.impl;

  28. import javax.annotation.Nonnull;
  29. import java.io.BufferedReader;
  30. import java.io.IOException;
  31. import java.io.InputStreamReader;
  32. import org.springframework.core.io.ClassPathResource;
  33. import it.tidalwave.util.Key;
  34. import it.tidalwave.role.ui.BoundProperty;
  35. import it.tidalwave.dci.annotation.DciRole;
  36. import it.tidalwave.northernwind.core.model.ResourceProperties;
  37. import it.tidalwave.northernwind.rca.embeddedserver.EmbeddedServer;
  38. import it.tidalwave.northernwind.rca.ui.contenteditor.spi.PropertyBinder;
  39. import lombok.RequiredArgsConstructor;

  40. /***********************************************************************************************************************
  41.  *
  42.  * @author  Fabrizio Giudici
  43.  *
  44.  **********************************************************************************************************************/
  45. @DciRole(datumType = ResourceProperties.class) @RequiredArgsConstructor
  46. public class ResourcePropertiesBinder implements PropertyBinder
  47.   {
  48.     private final static String EDITOR_TEMPLATE =
  49.             "it/tidalwave/northernwind/rca/ui/contenteditor/spi/EditorTemplate.xhtml";

  50.     @Nonnull
  51.     private final ResourceProperties properties;

  52.     /* visible for testing */ final static String EDITOR_PROLOG;

  53.     /* visible for testing */ final static String EDITOR_EPILOG;

  54.     /*******************************************************************************************************************
  55.      *
  56.      *
  57.      *
  58.      ******************************************************************************************************************/
  59.     static
  60.       {
  61.         try (final BufferedReader r = new BufferedReader(new InputStreamReader(
  62.                     new ClassPathResource(EDITOR_TEMPLATE).getInputStream(), "UTF-8")))
  63.           {
  64.             final StringBuilder prologBuilder = new StringBuilder();
  65.             final StringBuilder epilogBuilder = new StringBuilder();
  66.             boolean prolog = true;

  67.             for (String line = r.readLine(); line != null; line = r.readLine())
  68.               {
  69.                 if (line.trim().equals("<!-- split here -->"))
  70.                   {
  71.                     prolog = false;
  72.                     continue;
  73.                   }

  74.                 (prolog ? prologBuilder : epilogBuilder).append(line).append("\n");
  75.               }

  76.             EDITOR_PROLOG = prologBuilder.toString();
  77.             EDITOR_EPILOG = epilogBuilder.toString();
  78.           }
  79.         catch (IOException e)
  80.           {
  81.             throw new ExceptionInInitializerError(e);
  82.           }
  83.       }

  84.     /*******************************************************************************************************************
  85.      *
  86.      * {@inheritDoc}
  87.      *
  88.      ******************************************************************************************************************/
  89.     @Override
  90.     public <T> void bind (final @Nonnull Key<T> propertyName,
  91.                           final @Nonnull BoundProperty<T> boundProperty,
  92.                           final @Nonnull UpdateCallback callback)
  93.       {
  94.         properties.getProperty(propertyName).ifPresent(boundProperty::set);
  95.         boundProperty.addPropertyChangeListener(
  96.                 event -> callback.notify(properties.withProperty(propertyName, boundProperty.get())));
  97.       }

  98.     /*******************************************************************************************************************
  99.      *
  100.      * {@inheritDoc}
  101.      *
  102.      ******************************************************************************************************************/
  103.     @Override @Nonnull
  104.     public EmbeddedServer.Document createBoundDocument (final @Nonnull Key<String> propertyName,
  105.                                                         final @Nonnull UpdateCallback callback)
  106.       {
  107.         final String text = properties.getProperty(propertyName).orElse("");
  108.         final HtmlDocument originalDocument = HtmlDocument.createFromText(text);
  109.         final HtmlDocument editableDocument = originalDocument.withProlog(EDITOR_PROLOG)
  110.                                                               .withEpilog(EDITOR_EPILOG);
  111.         // FIXME: mime type - XHTML?
  112.         return new EmbeddedServer.Document().withMimeType("text/html")
  113.                                             .withContent(editableDocument.asString())
  114.                                             .withUpdateListener(
  115.             text1 -> callback.notify(properties.withProperty(propertyName,
  116.                                                              originalDocument.withBody(text1).asString())));
  117.       }
  118.   }