

package org.sakaiproject.nakamura.files.search;

import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.sakaiproject.nakamura.api.search.SearchConstants;
import org.sakaiproject.nakamura.api.search.solr.SolrSearchPropertyProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

@Component
@Service
@Properties({
    @Property(name = "service.vendor", value = "The Sakai Foundation"),
    @Property(name = SearchConstants.REG_PROVIDER_NAMES, value = "TagRefined")})
public class TagRefinedSearchPropertyProvider implements SolrSearchPropertyProvider {

  private static final Logger LOGGER = LoggerFactory
      .getLogger(TagRefinedSearchPropertyProvider.class);

  public void loadUserProperties(SlingHttpServletRequest request,
                                 Map<String, String> propertiesMap) {
    String tagClause = "";
    String tagParam = request.getParameter("tags");
    if (!StringUtils.isBlank(tagParam) && !"*".equals(tagParam)) {
      tagClause = "AND tag:(" + tagParam + ")";
    }

    propertiesMap.put("_tags", tagClause);
    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("_tags = " + tagClause);
    }
  }

}
