/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2020 wcm.io
 * %%
 * 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.
 * #L%
 */
package io.wcm.qa.glnm.persistence;

import java.util.NoSuchElementException;

import org.apache.commons.configuration2.PropertiesConfiguration;

import io.wcm.qa.glnm.differences.base.Differences;
import io.wcm.qa.glnm.differences.generic.MutableDifferences;

abstract class SamplePersistenceBase<T> implements SamplePersistence<T> {

  private Class resourceClass;

  /** {@inheritDoc} */
  @Override
  public T loadFromBaseline(Differences key) {
    try {
      return fetchBaseline(keyWithContextDifferences(key));
    }
    catch (NoSuchElementException ex) {
      return handleNotFound();
    }
  }

  /** {@inheritDoc} */
  @Override
  public void storeToBaseline(Differences key, T sample) {
    samples().setProperty(keyWithContextDifferences(key), sample);
  }

  private Class initResourceClass() {
    Class contextResourceClass = BaselinePersistenceExtension.getContextResourceClass();
    if (contextResourceClass != null) {
      return contextResourceClass;
    }
    return getClass();
  }

  protected PropertiesConfiguration baseline() {
    return PersistingCacheUtil.cachedBaselineForProperties(getResourceClass());
  }

  protected Class getResourceClass() {
    if (resourceClass == null) {
      resourceClass = initResourceClass();
    }
    return resourceClass;
  }

  protected T handleNotFound() {
    return null;
  }

  protected String keyWithContextDifferences(Differences key) {
    MutableDifferences differences = new MutableDifferences();
    Differences contextDifferences = BaselinePersistenceExtension.getContextDifferences();
    if (contextDifferences != null) {
      differences.addAll(contextDifferences);
    }
    differences.addAll(key);
    return differences.getKey();
  }

  protected abstract T fetchBaseline(String keyWithContextDifferences);

  protected PropertiesConfiguration samples() {
    return PersistingCacheUtil.cachedSamplesForProperties(getResourceClass());
  }

  protected void setResourceClass(Class resourceClass) {
    this.resourceClass = resourceClass;
  }

}

@@ -761,24 +761,81 @@
 nfiguration;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport io.
@@ -1014,16 +1014,107 @@
 e%3CT%3E %7B%0A%0A
+  private static final Logger LOG = LoggerFactory.getLogger(SamplePersistenceBase.class);%0A%0A
   privat
@@ -1215,24 +1215,24 @@
 nces key) %7B%0A
-
     try %7B%0A  
@@ -1229,16 +1229,139 @@
   try %7B%0A
+      if (LOG.isTraceEnabled()) %7B%0A        LOG.trace(%22loading for %22 + resourceClass + %22 with key %22 + key.getKey());%0A      %7D%0A
       re

add logging to retrieval