ResourcePropertiesSaveable.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.model.impl.admin.role;

  28. import javax.annotation.Nonnull;
  29. import java.time.ZoneId;
  30. import java.util.Arrays;
  31. import java.util.List;
  32. import java.io.IOException;
  33. import it.tidalwave.util.InstantProvider;
  34. import it.tidalwave.util.Key;
  35. import it.tidalwave.dci.annotation.DciRole;
  36. import it.tidalwave.northernwind.core.model.ResourceFile;
  37. import it.tidalwave.northernwind.core.model.ResourceProperties;
  38. import it.tidalwave.northernwind.model.admin.role.WritableFolder;
  39. import it.tidalwave.northernwind.model.admin.role.Saveable;
  40. import lombok.RequiredArgsConstructor;
  41. import lombok.extern.slf4j.Slf4j;
  42. import static it.tidalwave.role.io.Marshallable.*;
  43. import static it.tidalwave.northernwind.model.admin.Properties.*;
  44. import static it.tidalwave.northernwind.model.admin.role.WritableFolder.*;

  45. /***********************************************************************************************************************
  46.  *
  47.  * @author  Fabrizio Giudici
  48.  *
  49.  **********************************************************************************************************************/
  50. @DciRole(datumType = ResourceProperties.class)
  51. @RequiredArgsConstructor @Slf4j
  52. public class ResourcePropertiesSaveable implements Saveable
  53.   {
  54.     /** The properties that must be stored in a separate file. */
  55.     private static final List<Key<?>> EXTERNAL_PROPERTIES = Arrays.<Key<?>>asList(PROPERTY_FULL_TEXT);

  56.     @Nonnull
  57.     private final InstantProvider instantProvider;

  58.     @Nonnull
  59.     private final ResourceProperties properties;

  60.     @Override
  61.     public void saveIn (final @Nonnull ResourceFile folder)
  62.       {
  63.         try
  64.           {
  65.             log.debug("saveIn({}, {})", folder, properties);
  66.             final WritableFolder writableFolder = folder.as(WritableFolder);
  67.             final ResourceProperties p1 = properties.withProperty(PROPERTY_LATEST_MODIFICATION_DATE,
  68.                                                                   instantProvider.getInstant().atZone(ZoneId.systemDefault()));
  69.             final ResourceProperties p2 = saveExternalProperties(p1, writableFolder);
  70.             // FIXME: guess the localization (some properties go to Properties, some other to Properties_en.xml etc...
  71.             writableFolder.write("Properties.xml", p2.as(Marshallable));
  72.           }
  73.         catch (IOException e)
  74.           {
  75.             log.error("property class: " + properties.getClass(), e);
  76.           }
  77.       }

  78.     @Nonnull
  79.     private ResourceProperties saveExternalProperties (@Nonnull ResourceProperties properties,
  80.                                                        final @Nonnull WritableFolder writableFolder)
  81.       throws IOException
  82.       {
  83.         for (final Key<?> property : EXTERNAL_PROPERTIES)
  84.           {
  85.             // FIXME: localization
  86.             // FIXME: conversion to string when different types are used
  87.             writableFolder.write(property.stringValue() + "_en.xhtml", properties.getProperty(property).get().toString());
  88.             properties = properties.withoutProperty(property);
  89.           }

  90.         return properties;
  91.       }
  92.   }