All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.helger.phoss.smp.backend.sql.mgr.SMPSettingsManagerJDBC Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2019-2024 Philip Helger and contributors
 * philip[at]helger[dot]com
 *
 * 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.
 */
package com.helger.phoss.smp.backend.sql.mgr;

import java.util.Map;
import java.util.function.Supplier;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.annotation.ReturnsMutableObject;
import com.helger.commons.annotation.UsedViaReflection;
import com.helger.commons.callback.CallbackList;
import com.helger.commons.collection.attr.IStringMap;
import com.helger.commons.collection.attr.StringMap;
import com.helger.commons.collection.impl.CommonsHashMap;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.collection.impl.ICommonsMap;
import com.helger.commons.state.EChange;
import com.helger.commons.state.ESuccess;
import com.helger.commons.string.StringHelper;
import com.helger.commons.string.StringParser;
import com.helger.commons.wrapper.Wrapper;
import com.helger.db.api.helper.DBValueHelper;
import com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider;
import com.helger.db.jdbc.executor.DBExecutor;
import com.helger.db.jdbc.executor.DBResultRow;
import com.helger.db.jdbc.mgr.AbstractJDBCEnabledManager;
import com.helger.phoss.smp.settings.ISMPSettings;
import com.helger.phoss.smp.settings.ISMPSettingsCallback;
import com.helger.phoss.smp.settings.ISMPSettingsManager;
import com.helger.phoss.smp.settings.SMPSettings;
import com.helger.web.scope.singleton.AbstractRequestWebSingleton;

public class SMPSettingsManagerJDBC extends AbstractJDBCEnabledManager implements ISMPSettingsManager
{
  private static final String SMP_REST_WRITABLE_API_DISABLED = "smp-rest-writable-api-disabled";
  private static final String DIRECTORY_INTEGRATION_REQUIRED = "directory-required";
  private static final String DIRECTORY_INTEGRATION_ENABLED = "directory-enabled";
  private static final String DIRECTORY_INTEGRATION_AUTO_UPDATE = "directory-auto-update";
  private static final String DIRECTORY_HOSTNAME = "directory-hostname";
  private static final String SML_REQUIRED = "sml-required";
  private static final String SML_ENABLED = "sml-enabled";
  private static final String SML_INFO_ID = "smlinfo-id";

  private final CallbackList  m_aCallbacks = new CallbackList <> ();

  /**
   * Constructor
   *
   * @param aDBExecSupplier
   *        The supplier for {@link DBExecutor} objects. May not be
   *        null.
   */
  public SMPSettingsManagerJDBC (@Nonnull final Supplier  aDBExecSupplier)
  {
    super (aDBExecSupplier);
  }

  @Nonnull
  @ReturnsMutableObject
  public final CallbackList  callbacks ()
  {
    return m_aCallbacks;
  }

  static void setSettingsValueInDB (@Nonnull final DBExecutor aExecutor,
                                    @Nonnull @Nonempty final String sKey,
                                    @Nullable final String sValue)
  {
    ValueEnforcer.notNull (aExecutor, "Executor");
    ValueEnforcer.notEmpty (sKey, "Key");

    // update
    final long nUpdated = aExecutor.insertOrUpdateOrDelete ("UPDATE smp_settings SET value=? WHERE id=?",
                                                            new ConstantPreparedStatementDataProvider (DBValueHelper.getTrimmedToLength (sValue,
                                                                                                                                         500),
                                                                                                       DBValueHelper.getTrimmedToLength (sKey,
                                                                                                                                         45)));
    if (nUpdated == 0)
    {
      // Create
      final long nCreated = aExecutor.insertOrUpdateOrDelete ("INSERT INTO smp_settings (id, value) VALUES (?, ?)",
                                                              new ConstantPreparedStatementDataProvider (DBValueHelper.getTrimmedToLength (sKey,
                                                                                                                                           45),
                                                                                                         DBValueHelper.getTrimmedToLength (sValue,
                                                                                                                                           500)));
      if (nCreated != 1)
        throw new IllegalStateException ("Failed to create new DB entry (" + nCreated + ")");
    }
  }

  @Nonnull
  public ESuccess setSettingsValuesInDB (@Nonnull @Nonempty final Map  aEntries)
  {
    ValueEnforcer.notEmpty (aEntries, "Entries");

    final DBExecutor aExecutor = newExecutor ();
    return aExecutor.performInTransaction ( () -> {
      for (final Map.Entry  aEntry : aEntries.entrySet ())
      {
        final String sKey = aEntry.getKey ();
        final String sValue = aEntry.getValue ();

        setSettingsValueInDB (aExecutor, sKey, sValue);
      }
    });
  }

  @Nonnull
  @ReturnsMutableCopy
  public ICommonsMap  getAllSettingsValuesFromDB ()
  {
    final ICommonsMap  ret = new CommonsHashMap <> ();
    final ICommonsList  aDBResult = newExecutor ().queryAll ("SELECT id, value FROM smp_settings");
    if (aDBResult != null)
      for (final DBResultRow aRow : aDBResult)
        ret.put (aRow.getAsString (0), aRow.getAsString (1));
    return ret;
  }

  @Nullable
  public static String getSettingsValueFromDB (@Nonnull final DBExecutor aExecutor, @Nullable final String sKey)
  {
    if (StringHelper.hasNoText (sKey))
      return null;

    final Wrapper  aDBResult = new Wrapper <> ();
    aExecutor.querySingle ("SELECT value FROM smp_settings WHERE id=?",
                           new ConstantPreparedStatementDataProvider (sKey),
                           aDBResult::set);
    if (aDBResult.isNotSet ())
      return null;

    return aDBResult.get ().getAsString (0);
  }

  @Nullable
  public String getSettingsValue (@Nullable final String sKey)
  {
    if (StringHelper.hasNoText (sKey))
      return null;

    return getSettingsValueFromDB (newExecutor (), sKey);
  }

  public static class SettingsSingleton extends AbstractRequestWebSingleton
  {
    private static final Logger LOGGER = LoggerFactory.getLogger (SMPSettingsManagerJDBC.SettingsSingleton.class);
    private ISMPSettings m_aSMPSettings;

    @Deprecated
    @UsedViaReflection
    public SettingsSingleton ()
    {}

    @Nonnull
    public static SettingsSingleton getInstance ()
    {
      return getRequestSingleton (SettingsSingleton.class);
    }

    @Nonnull
    private static ISMPSettings _createSettingsFromDB (@Nonnull final SMPSettingsManagerJDBC aMgr)
    {
      // Queries DB
      final ICommonsMap  aValues = aMgr.getAllSettingsValuesFromDB ();

      final SMPSettings ret = SMPSettings.createInitializedFromConfiguration ();
      ret.setRESTWritableAPIDisabled (StringParser.parseBool (aValues.get (SMP_REST_WRITABLE_API_DISABLED),
                                                              ret.isRESTWritableAPIDisabled ()));
      ret.setDirectoryIntegrationEnabled (StringParser.parseBool (aValues.get (DIRECTORY_INTEGRATION_ENABLED),
                                                                  ret.isDirectoryIntegrationEnabled ()));
      ret.setDirectoryIntegrationRequired (StringParser.parseBool (aValues.get (DIRECTORY_INTEGRATION_REQUIRED),
                                                                   ret.isDirectoryIntegrationRequired ()));
      ret.setDirectoryIntegrationAutoUpdate (StringParser.parseBool (aValues.get (DIRECTORY_INTEGRATION_AUTO_UPDATE),
                                                                     ret.isDirectoryIntegrationAutoUpdate ()));
      String sDirectoryHostName = aValues.get (DIRECTORY_HOSTNAME);
      if (StringHelper.hasNoText (sDirectoryHostName))
        sDirectoryHostName = ret.getDirectoryHostName ();
      ret.setDirectoryHostName (sDirectoryHostName);
      ret.setSMLEnabled (StringParser.parseBool (aValues.get (SML_ENABLED), ret.isSMLEnabled ()));
      ret.setSMLRequired (StringParser.parseBool (aValues.get (SML_REQUIRED), ret.isSMLRequired ()));
      ret.setSMLInfoID (aValues.get (SML_INFO_ID));
      return ret;
    }

    @Nonnull
    public ISMPSettings getSettings (@Nonnull final SMPSettingsManagerJDBC aMgr)
    {
      ISMPSettings ret = m_aSMPSettings;
      if (ret == null)
      {
        if (LOGGER.isDebugEnabled ())
          LOGGER.debug ("Loading SMP settings from DB");
        ret = m_aSMPSettings = _createSettingsFromDB (aMgr);
      }
      else
      {
        if (LOGGER.isDebugEnabled ())
          LOGGER.debug ("Reusing SMP settings of request");
      }
      return ret;
    }
  }

  @Nonnull
  public ISMPSettings getSettings ()
  {
    // Construct to read the settings only once per request
    return SettingsSingleton.getInstance ().getSettings (this);
  }

  @Nonnull
  public EChange updateSettings (final boolean bRESTWritableAPIDisabled,
                                 final boolean bDirectoryIntegrationEnabled,
                                 final boolean bDirectoryIntegrationRequired,
                                 final boolean bDirectoryIntegrationAutoUpdate,
                                 @Nullable final String sDirectoryHostName,
                                 final boolean bSMLEnabled,
                                 final boolean bSMLRequired,
                                 @Nullable final String sSMLInfoID)
  {
    final IStringMap aMap = new StringMap ();
    aMap.putIn (SMP_REST_WRITABLE_API_DISABLED, bRESTWritableAPIDisabled);
    aMap.putIn (DIRECTORY_INTEGRATION_ENABLED, bDirectoryIntegrationEnabled);
    aMap.putIn (DIRECTORY_INTEGRATION_REQUIRED, bDirectoryIntegrationRequired);
    aMap.putIn (DIRECTORY_INTEGRATION_AUTO_UPDATE, bDirectoryIntegrationAutoUpdate);
    aMap.putIn (DIRECTORY_HOSTNAME, sDirectoryHostName);
    aMap.putIn (SML_ENABLED, bSMLEnabled);
    aMap.putIn (SML_REQUIRED, bSMLRequired);
    aMap.putIn (SML_INFO_ID, sSMLInfoID);

    // Save
    if (setSettingsValuesInDB (aMap).isFailure ())
      return EChange.UNCHANGED;
    return EChange.CHANGED;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy