gt:                 String sTo = DAY_FORMAT.format(to);
context: /**
   * OpenKM, Open Document Management System (http://www.openkm.com)
   * Copyright (c) 2006-2015 Paco Avila & Josep Llort
   * 
   * No bytes were intentionally harmed during the development of this application.
   * 
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   * 
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU General Public License for more details.
   * 
   * You should have received a copy of the GNU General Public License along
   * with this program; if not, write to the Free Software Foundation, Inc.,
   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   */
  
  package com.openkm.module.db;
  
  import com.openkm.bean.*;
  import com.openkm.bean.form.FormElement;
  import com.openkm.bean.form.Input;
  import com.openkm.bean.form.Select;
  import com.openkm.bean.form.TextArea;
  import com.openkm.bean.nr.NodeQueryResult;
  import com.openkm.bean.nr.NodeResultSet;
  import com.openkm.core.*;
  import com.openkm.dao.*;
  import com.openkm.dao.bean.NodeDocument;
  import com.openkm.dao.bean.NodeFolder;
  import com.openkm.dao.bean.NodeMail;
  import com.openkm.dao.bean.QueryParams;
  import com.openkm.module.SearchModule;
  import com.openkm.module.db.base.BaseDocumentModule;
  import com.openkm.module.db.base.BaseFolderModule;
  import com.openkm.module.db.base.BaseMailModule;
  import com.openkm.spring.PrincipalUtils;
  import com.openkm.util.*;
  import org.apache.lucene.index.Term;
  import org.apache.lucene.search.*;
  import org.hibernate.HibernateException;
  import org.hibernate.Transaction;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.security.core.Authentication;
  
  import java.io.IOException;
  import java.text.SimpleDateFormat;
  import java.util.*;
  import java.util.Map.Entry;
  
  public class DbSearchModule implements SearchModule {
      private static Logger log = LoggerFactory.getLogger(DbSearchModule.class);
      private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("yyyyMMdd");
  
      @Override
      public List<QueryResult> findByContent(String token, String expression) throws IOException, ParseException, AccessDeniedException,
              RepositoryException, DatabaseException {
          log.debug("findByContent({}, {})", token, expression);
          QueryParams params = new QueryParams();
          params.setContent(expression);
          List<QueryResult> ret = find(token, params);
          log.debug("findByContent: {}", ret);
          return ret;
      }
  
      @Override
      public List<QueryResult> findByName(String token, String expression) throws IOException, ParseException, AccessDeniedException,
              RepositoryException, DatabaseException {
          log.debug("findByName({}, {})", token, expression);
          QueryParams params = new QueryParams();
          params.setName(expression);
          List<QueryResult> ret = find(token, params);
          log.debug("findByName: {}", ret);
          return ret;
      }
  
      @Override
      public List<QueryResult> findByKeywords(String token, Set<String> expression) throws IOException, ParseException,
              AccessDeniedException, RepositoryException, DatabaseException {
          log.debug("findByKeywords({}, {})", token, expression);
          QueryParams params = new QueryParams();
          params.setKeywords(expression);
          List<QueryResult> ret = find(token, params);
          log.debug("findByKeywords: {}", ret);
          return ret;
      }
  
      @Override
      public List<QueryResult> find(String token, QueryParams params) throws IOException, ParseException, AccessDeniedException,
              RepositoryException, DatabaseException {
          log.debug("find({}, {})", token, params);
          List<QueryResult> ret = findPaginated(token, params, 0, Config.MAX_SEARCH_RESULTS).getResults();
          log.debug("find: {}", ret);
          return ret;
      }
  
      @Override
      public ResultSet findPaginated(String token, QueryParams params, int offset, int limit) throws IOException, ParseException,
              AccessDeniedException, RepositoryException, DatabaseException {
          log.debug("findPaginated({}, {}, {}, {})", new Object[] { token, params, offset, limit });
          Authentication auth = null, oldAuth = null;
          Query query = null;
  
          try {
              if (token == null) {
                  auth = PrincipalUtils.getAuthentication();
              } else {
                  oldAuth = PrincipalUtils.getAuthentication();
                  auth = PrincipalUtils.getAuthenticationByToken(token);
              }
  
              if (params.getStatementQuery() != null && !params.getStatementQuery().equals("")) {
                  // query = params.getStatementQuery();
              } else {
                  query = prepareStatement(params);
              }
  
              ResultSet rs = findByStatementPaginated(auth, query, offset, limit);
              log.debug("findPaginated: {}", rs);
              return rs;
          } finally {
              if (token != null) {
                  PrincipalUtils.setAuthentication(oldAuth);
              }
          }
      }
  
      /**
       * Prepare statement
       */
      public Query prepareStatement(QueryParams params) throws IOException, ParseException, RepositoryException, DatabaseException {
          log.debug("prepareStatement({})", params);
          BooleanQuery query = new BooleanQuery();
  
          // Clean params
          params.setName(params.getName() != null ? params.getName().trim() : "");
          params.setContent(params.getContent() != null ? params.getContent().trim() : "");
          params.setKeywords(params.getKeywords() != null ? params.getKeywords() : new HashSet<String>());
          params.setCategories(params.getCategories() != null ? params.getCategories() : new HashSet<String>());
          params.setMimeType(params.getMimeType() != null ? params.getMimeType().trim() : "");
          params.setAuthor(params.getAuthor() != null ? params.getAuthor().trim() : "");
          params.setPath(params.getPath() != null ? params.getPath().trim() : "");
          params.setMailSubject(params.getMailSubject() != null ? params.getMailSubject().trim() : "");
          params.setMailFrom(params.getMailFrom() != null ? params.getMailFrom().trim() : "");
          params.setMailTo(params.getMailTo() != null ? params.getMailTo().trim() : "");
          params.setProperties(params.getProperties() != null ? params.getProperties() : new HashMap<String, String>());
  
          // Domains
          boolean document = (params.getDomain() & QueryParams.DOCUMENT) != 0;
          boolean folder = (params.getDomain() & QueryParams.FOLDER) != 0;
          boolean mail = (params.getDomain() & QueryParams.MAIL) != 0;
          log.debug("doc={}, fld={}, mail={}", new Object[] { document, folder, mail });
  
          /**
           * DOCUMENT
           */
          if (document) {
              BooleanQuery queryDocument = new BooleanQuery();
              Term tEntity = new Term("_hibernate_class", NodeDocument.class.getCanonicalName());
              queryDocument.add(new TermQuery(tEntity), BooleanClause.Occur.MUST);
  
              if (!params.getContent().isEmpty()) {
                  Query parsedQuery = SearchDAO.getInstance().parseQuery(params.getContent(), "text");
                  queryDocument.add(parsedQuery, BooleanClause.Occur.MUST);
              }
  
              if (!params.getName().isEmpty()) {
                  if (!params.getName().contains("*") && !params.getName().contains("?")) {
                      params.setName("*" + params.getName() + "*");
                  }
  
                  Term t = new Term("name", PathUtils.encodeEntities(params.getName().toLowerCase()));
                  queryDocument.add(new WildcardQuery(t), BooleanClause.Occur.MUST);
              }
  
              if (!params.getMimeType().isEmpty()) {
                  Term t = new Term("mimeType", params.getMimeType());
                  queryDocument.add(new TermQuery(t), BooleanClause.Occur.MUST);
              }
  
              if (!params.getAuthor().isEmpty()) {
                  Term t = new Term("author", params.getAuthor());
                  queryDocument.add(new TermQuery(t), BooleanClause.Occur.MUST);
              }
  
              if (params.getLastModifiedFrom() != null && params.getLastModifiedTo() != null) {
                  Date from = params.getLastModifiedFrom().getTime();
                  String sFrom = DAY_FORMAT.format(from);
                  Date to = params.getLastModifiedTo().getTime();
                  String sTo = DAY_FORMAT.format(to);
                  queryDocument.add(new TermRangeQuery("lastModified", sFrom, sTo, true, true), BooleanClause.Occur.MUST);
              }
  
              appendCommon(params, queryDocument);
              query.add(queryDocument, BooleanClause.Occur.SHOULD);
          }
  
          /**
           * FOLDER
           */
          if (folder) {
              BooleanQuery queryFolder = new BooleanQuery();
              Term tEntity = new Term("_hibernate_class", NodeFolder.class.getCanonicalName());
              queryFolder.add(new TermQuery(tEntity), BooleanClause.Occur.MUST);
  
              if (!params.getName().isEmpty()) {
                  Term t = new Term("name", PathUtils.encodeEntities(params.getName().toLowerCase()));
                  queryFolder.add(new WildcardQuery(t), BooleanClause.Occur.MUST);
              }
  
              appendCommon(params, queryFolder);
              query.add(queryFolder, BooleanClause.Occur.SHOULD);
          }
  
          /**
           * MAIL
           */
          if (mail) {
              BooleanQuery queryMail = new BooleanQuery();
              Term tEntity = new Term("_hibernate_class", NodeMail.class.getCanonicalName());
              queryMail.add(new TermQuery(tEntity), BooleanClause.Occur.MUST);
  
              if (!params.getContent().isEmpty()) {
                  Query parsedQuery = SearchDAO.getInstance().parseQuery(params.getContent(), "content");
                  queryMail.add(parsedQuery, BooleanClause.Occur.MUST);
              }
  
              if (!params.getMailSubject().isEmpty()) {
                  Term t = new Term("subject", params.getMailSubject().toLowerCase());
                  queryMail.add(new WildcardQuery(t), BooleanClause.Occur.MUST);
              }
  
              if (!params.getMailFrom().isEmpty()) {
                  Term t = new Term("from", params.getMailFrom().toLowerCase());
                  queryMail.add(new WildcardQuery(t), BooleanClause.Occur.MUST);
              }
  
              if (!params.getMailTo().isEmpty()) {
                  Term t = new Term("to", params.getMailTo().toLowerCase());
                  queryMail.add(new WildcardQuery(t), BooleanClause.Occur.MUST);
              }
  
              if (!params.getMimeType().isEmpty()) {
                  Term t = new Term("mimeType", params.getMimeType());
                  queryMail.add(new TermQuery(t), BooleanClause.Occur.MUST);
              }
  
              // We also use the getLastModifiedFrom and getLastModifiedTo also to filter mails by sentDate
              if (params.getLastModifiedFrom() != null && params.getLastModifiedTo() != null) {
                  Date from = params.getLastModifiedFrom().getTime();
                  String sFrom = DAY_FORMAT.format(from);
                  Date to = params.getLastModifiedTo().getTime();
  