package won.protocol.util.linkeddata;

import com.hp.hpl.jena.query.Dataset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: ypanchenko
 * Date: 06.11.2015
 */
public class CachingAllButListsLinkedDataSource extends CachingLinkedDataSource
{
  //TODO instead of predefined resources should look into RDF and decide whether it is a list or not

  //TODO actually the connection itself should also not be cached - i.e. status can be updated

  private Pattern pattern_connections_list_uri = Pattern.compile("(.+)/resource/connection(/)+");

  private final Logger logger = LoggerFactory.getLogger(getClass());

  @Override
  public Dataset getDataForResource(URI resource) {
    if (isCachingAllowed(resource)) {
      return super.getDataForResource(resource);
    } else {
      Dataset dataset = linkedDataRestClient.readResourceData(resource);
      //TODO debug log level
      logger.info("connections list uri request performed");
      return dataset;
    }
  }

  @Override
  public Dataset getDataForResource(URI resource, URI requesterWebID) {
    if (isCachingAllowed(resource)) {
      return super.getDataForResource(resource, requesterWebID);
    } else {
      Dataset dataset = linkedDataRestClient.readResourceData(resource, requesterWebID);
      logger.info("connections list uri request performed");
      return dataset;
    }
  }

  private boolean isCachingAllowed(final URI resource) {
    Matcher matcher = pattern_connections_list_uri.matcher(resource.toString());
    if (matcher.matches()) {
      return false;
    }
    return true;
  }

}

@@ -617,17 +617,8 @@
 .+)/
-resource/
 conn
@@ -627,12 +627,13 @@
 tion
+s
 (/)
-+
+?
 %22);%0A
@@ -1487,16 +1487,46 @@
 d(final 
+Pattern noCachePattern, final 
 URI reso
@@ -1559,36 +1559,22 @@
 r = 
-pattern_connections_list_uri
+noCachePattern
 .mat
@@ -1677,10 +1677,141 @@
 e;%0A  %7D%0A%0A
+  private boolean isCachingAllowed(final URI resource) %7B%0A    return isCachingAllowed(pattern_connections_list_uri, resource);%0A  %7D%0A%0A
 %7D%0A

fix uri for connections pattern