public PagingLoadResult<Example> getQueryResult(String query, PagingLoadConfig config)
      throws AutoSPARQLException {
    try {
      int limit = config.getLimit();
      int offset = config.getOffset();

      List<Example> searchResult = search.getExamples(query, offset);
      for (Example example : searchResult) {
        examplesCache.put(example.getURI(), example);
      }
      if (offset == 0) {
        topKResources = new ArrayList<String>();
        for (Example ex : searchResult) {
          topKResources.add(ex.getURI());
        }
      }
      int totalLength = search.getTotalHits(query);

      PagingLoadResult<Example> result = new BasePagingLoadResult<Example>(searchResult);
      result.setOffset(offset);
      result.setTotalLength(totalLength);

      return result;
    } catch (Exception e) {
      logger.error("Error while searching for \"" + query + "\".", e);
      throw new AutoSPARQLException(e);
    }
  }
  public PagingLoadResult<Example> getCurrentQueryResult(PagingLoadConfig config)
      throws SPARQLQueryException {
    logger.debug("Retrieving results for current query.");
    List<Example> queryResult = new ArrayList<Example>();

    String currentQuery = exampleFinder.getCurrentQuery();
    logger.debug("Current query:\n");
    logger.debug(currentQuery);
    int limit = config.getLimit();
    int offset = config.getOffset();
    int totalLength = 10;

    try {
      ResultSetRewindable rs =
          SparqlQuery.convertJSONtoResultSet(
              selectCache.executeSelectQuery(endpoint, getCountQuery(currentQuery)));
      totalLength = rs.next().getLiteral(rs.getResultVars().get(0)).getInt();
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      ResultSetRewindable rs =
          SparqlQuery.convertJSONtoResultSet(
              selectCache.executeSelectQuery(
                  endpoint, modifyQuery(currentQuery + " LIMIT " + limit + " OFFSET " + offset)));

      String uri;
      String label = "";
      String imageURL = "";
      String comment = "";
      QuerySolution qs;
      while (rs.hasNext()) {
        qs = rs.next();
        uri = qs.getResource("x0").getURI();
        label = qs.getLiteral("label").getLexicalForm();
        queryResult.add(new Example(uri, label, imageURL, comment));
      }
    } catch (Exception e) {
      logger.error("Error while getting result for query \n" + currentQuery, e);
    }

    PagingLoadResult<Example> result = new BasePagingLoadResult<Example>(queryResult);
    result.setOffset(offset);
    result.setTotalLength(totalLength);

    return result;
  }
  @Override
  public BasePagingLoadResult<GPClientProject> searchProjects(
      PagingLoadConfig config,
      String searchText,
      String imageURL,
      HttpServletRequest httpServletRequest)
      throws GeoPlatformException {
    GPAccount account = null;
    try {
      account = this.sessionUtility.getLoggedAccount(httpServletRequest);
    } catch (GPSessionTimeout timeout) {
      throw new GeoPlatformException(timeout);
    }

    int start = config.getOffset();
    SearchRequest srq = new SearchRequest(searchText);
    try {
      Long projectsCount =
          this.geoPlatformServiceClient.getAccountProjectsCount(account.getId(), srq);

      int page = start == 0 ? start : start / config.getLimit();

      PaginatedSearchRequest psr = new PaginatedSearchRequest(searchText, config.getLimit(), page);

      List<ProjectDTO> projectsDTO =
          this.geoPlatformServiceClient.searchAccountProjects(account.getId(), psr);

      if (projectsDTO == null) {
        throw new GeoPlatformException("There are no results");
      }

      ArrayList<GPClientProject> clientProjects = new ArrayList<GPClientProject>();

      for (ProjectDTO projectDTO : projectsDTO) {
        GPClientProject clientProject =
            this.dtoLayerConverter.convertToGPCLientProject(projectDTO, imageURL);
        clientProjects.add(clientProject);
      }

      return new BasePagingLoadResult<GPClientProject>(
          clientProjects, config.getOffset(), projectsCount.intValue());

    } catch (ResourceNotFoundFault ex) {
      logger.error("An Error Occured : " + ex.getMessage());
      throw new GeoPlatformException(ex.getMessage());
    }
  }
  @Override
  public PagingLoadResult<FilterWordInstance> getHelpTextWords(PagingLoadConfig loadConfig)
      throws IllegalArgumentException, ServiceNotReadyException {

    authenticate("get help text words"); // 	SecurityManager.ROLE_CONFIG);

    BasePagingLoadResult<FilterWordInstance> result = null;
    try {

      if (HelpTextCache.getSingleton() == null || !HelpTextCache.getSingleton().isMapsReady())
        throw new ServiceNotReadyException("help text word filter function");

      List<FilterWordInstance> list = new ArrayList<FilterWordInstance>();

      //	Filtering... only get the words for the first term listed, using the cache
      String query = loadConfig.get("query").toString();
      String filters[] = HelpTextCache.getSingleton().parseFilter(query);
      int wordCount = 0;
      if (filters.length > 0 && query.endsWith(filters[filters.length - 1])) {
        String search = filters[filters.length - 1];
        String prefix = query.substring(0, query.length() - search.length());
        String[] words = HelpTextCache.getSingleton().getWords(search);
        wordCount = words.length;
        for (int i = loadConfig.getOffset(); i < words.length; i++) {
          FilterWordInstance word = new FilterWordInstance();
          word.setWord(prefix + words[i]);
          list.add(word);
          if (list.size() >= loadConfig.getLimit()) break;
        }
      }

      result =
          new BasePagingLoadResult<FilterWordInstance>(list, loadConfig.getOffset(), wordCount);

    } catch (HelpTextCache.HelpTextCacheNotReady exc) {
      throw new ServiceNotReadyException("help text word filter function");
    } catch (ServiceNotReadyException exc) {
      throw exc;
    } catch (Exception exc) {
      exc.printStackTrace();
    }

    return result;
  }
  public PagingLoadResult<Example> getSearchResult(String searchTerm, PagingLoadConfig config)
      throws AutoSPARQLException {
    try {
      int limit = config.getLimit();
      int offset = config.getOffset();

      List<Example> searchResult =
          search.getExamples("label:" + getQuotedString(searchTerm), offset);
      for (Example example : searchResult) {
        examplesCache.put(example.getURI(), example);
      }
      int totalLength = search.getTotalHits(searchTerm);

      PagingLoadResult<Example> result = new BasePagingLoadResult<Example>(searchResult);
      result.setOffset(offset);
      result.setTotalLength(totalLength);

      return result;
    } catch (Exception e) {
      logger.error("Error while searching for term \"" + searchTerm + "\".", e);
      throw new AutoSPARQLException(e);
    }
  }
 @Override
 public PagingLoadResult<GWTExecuteSetExecutePlan> GetExecuteSetExecutePlanList(
     String execPlanID, String systemID, String searchKey, PagingLoadConfig config) {
   // TODO Auto-generated method stub
   try {
     int count;
     List<ExecuteSetExecutePlan> lst;
     List<Op> ops = new ArrayList<Op>();
     ops.add(Op.EQ(GWTExecuteSetExecutePlan.N_SystemID, systemID));
     if (!execPlanID.isEmpty()) {
       ops.add(Op.EQ(GWTExecuteSetExecutePlan.N_ExecutePlanID, Integer.parseInt(execPlanID)));
     }
     Op[] condition = new Op[ops.size()];
     for (int i = 0; i < ops.size(); i++) { // 把集合类转成数组
       condition[i] = ops.get(i);
     }
     if (searchKey.isEmpty()) {
       count = esExecutePlanDAL.Count(condition);
       PageStartEnd pse = new PageStartEnd(config, count);
       lst =
           esExecutePlanDAL.List(
               GWTExecuteSetExecutePlan.N_ID, true, pse.getStart(), pse.getEnd(), condition);
     } else {
       String[] searchField = new String[] {};
       count = esExecutePlanDAL.MatchCount(searchKey, searchField, condition);
       PageStartEnd pse = new PageStartEnd(config, count);
       lst =
           esExecutePlanDAL.Match(searchKey, searchField, pse.getStart(), pse.getEnd(), condition);
     }
     List<GWTExecuteSetExecutePlan> result = new ArrayList<GWTExecuteSetExecutePlan>();
     for (ExecuteSetExecutePlan data : lst) {
       result.add(BeanToModel(data));
     }
     return new BasePagingLoadResult<GWTExecuteSetExecutePlan>(result, config.getOffset(), count);
   } catch (Exception e) {
     log.error(e, e);
     throw new RuntimeException(e);
   }
 }
  public PagingLoadResult<Example> getSPARQLQueryResultWithProperties(
      String query, List<String> properties, PagingLoadConfig config) throws AutoSPARQLException {
    List<Example> queryResult = new ArrayList<Example>();
    //		properties.remove("label");
    int limit = config.getLimit();
    int offset = config.getOffset();
    int totalLength = 10;

    if (currentQueryResultSize == -1) {
      try {
        ResultSetRewindable rs =
            SparqlQuery.convertJSONtoResultSet(
                selectCache.executeSelectQuery(endpoint, getCountQuery(query)));
        currentQueryResultSize = rs.next().getLiteral(rs.getResultVars().get(0)).getInt();
      } catch (Exception e) {
        e.printStackTrace();
        currentQueryResultSize = 10;
      }
    }
    totalLength = currentQueryResultSize;

    List<String> propertiesToDo = new ArrayList<String>(properties);

    for (Map<String, Object> prop2Value : propertiesCache.values()) {
      propertiesToDo.removeAll(prop2Value.keySet());
    }

    if (propertiesToDo.size() > 0) {
      String queryTriples = query.substring(18, query.length() - 1);
      StringBuilder newQuery = new StringBuilder();
      Map<String, String> var2URIMap = new HashMap<String, String>(propertiesToDo.size());

      if (propertiesToDo.size() == 1 && propertiesToDo.get(0).equals(RDFS.label.getURI())) {
        newQuery.append("SELECT DISTINCT ?x0 ?label ?imageURL{");
        newQuery.append(queryTriples);
        newQuery.append("?x0 <").append(RDFS.label).append("> ?label.\n");
        newQuery
            .append("OPTIONAL{?x0 <")
            .append("http://dbpedia.org/ontology/thumbnail")
            .append("> ?imageURL.}\n");
        newQuery.append("FILTER(LANGMATCHES(LANG(?label),'en'))");
        newQuery.append("}");
      } else {
        for (String property : propertiesToDo) {
          var2URIMap.put(
              property2LabelMap.get(property).replace(" ", "_").replace("(", "").replace(")", ""),
              property);
        }

        newQuery.append("SELECT DISTINCT ?x0 ?label ?imageURL ");
        for (String var : var2URIMap.keySet()) {
          newQuery.append("?").append(var).append(" ");
          newQuery.append("?").append(var).append("_label ");
        }
        newQuery.append("{");
        newQuery.append(queryTriples);
        newQuery.append("?x0 <").append(RDFS.label).append("> ?label.\n");
        newQuery
            .append("OPTIONAL{?x0 <")
            .append("http://dbpedia.org/ontology/thumbnail")
            .append("> ?imageURL.}\n");
        for (Entry<String, String> entry : var2URIMap.entrySet()) {
          newQuery
              .append("OPTIONAL{?x0 <")
              .append(entry.getValue())
              .append("> ?")
              .append(entry.getKey())
              .append(".}\n");
        }
        for (Entry<String, String> entry : var2URIMap.entrySet()) {
          newQuery
              .append("OPTIONAL{?")
              .append(entry.getKey())
              .append(" <")
              .append(RDFS.label)
              .append("> ?")
              .append(entry.getKey())
              .append("_label.\n");
          newQuery.append("FILTER(LANGMATCHES(LANG(?" + entry.getKey() + "_label),'en'))}\n");
        }
        newQuery.append("FILTER(LANGMATCHES(LANG(?label),'en'))");
        newQuery.append("}");
      }

      logger.debug("Query with properties:\n" + newQuery.toString());
      try {
        ResultSetRewindable rs =
            SparqlQuery.convertJSONtoResultSet(
                selectCache.executeSelectQuery(endpoint, modifyQuery(newQuery + " LIMIT 1000")));

        String uri;
        String label = "";
        String imageURL = "";
        QuerySolution qs;
        RDFNode object;
        while (rs.hasNext()) {
          qs = rs.next();
          uri = qs.getResource("x0").getURI();
          label = qs.getLiteral("label").getLexicalForm();
          imageURL = qs.getResource("imageURL") != null ? qs.getResource("imageURL").getURI() : "";
          Map<String, Object> properties2Value = propertiesCache.get(uri);
          if (properties2Value == null) {
            properties2Value = new HashMap<String, Object>();
            properties2Value.put(RDFS.label.getURI(), label);
            properties2Value.put("http://dbpedia.org/ontology/thumbnail", imageURL);
            propertiesCache.put(uri, properties2Value);
          }

          Object value;
          String property;
          for (Entry<String, String> entry : var2URIMap.entrySet()) {
            value = "";
            property = entry.getValue();

            object = qs.get(entry.getKey() + "_label");
            if (object == null) {
              object = qs.get(entry.getKey());
            }
            if (object != null) {
              if (object.isURIResource()) {
                value = object.asResource().getURI();
              } else if (object.isLiteral()) {
                Literal lit = object.asLiteral();
                //								if(lit.getDatatypeURI().equals(XSD.BOOLEAN)){
                //									property2DatatypeMap.put(property, Boolean.class);
                //									value = lit.getBoolean();
                //								} else if(lit.getDatatypeURI().equals(XSD.INT)){
                //									property2DatatypeMap.put(property, Integer.class);
                //									value = lit.getInt();
                //								} else if(lit.getDatatypeURI().equals(XSD.DOUBLE)){
                //									property2DatatypeMap.put(property, Double.class);
                //									value = lit.getDouble();
                //								} else if(lit.getDatatypeURI().equals(XSD.FLOAT)){
                //									property2DatatypeMap.put(property, Float.class);
                //									value = lit.getFloat();
                //								} else {
                //									property2DatatypeMap.put(property, String.class);
                //									value = object.asLiteral().getLexicalForm();
                //								}
                value = object.asLiteral().getLexicalForm();
              }
            }
            Object oldValue = properties2Value.get(property);
            if (oldValue != null && value != null) {
              value = oldValue + ", " + value;
            }
            properties2Value.put(property, value);
          }
        }
      } catch (Exception e) {
        logger.error("Error while getting result for query \n" + newQuery, e);
      }
    }

    Example example;
    int cnt = 0;
    for (Entry<String, Map<String, Object>> uri2PropertyValues : propertiesCache.entrySet()) {
      //			if(cnt++ == limit+offset){
      //				break;
      //			}
      //			if(cnt > offset){
      example = new Example();
      example.setAllowNestedValues(false);
      example.set("uri", uri2PropertyValues.getKey());
      Object value;
      String property;
      for (Entry<String, Object> property2Value : uri2PropertyValues.getValue().entrySet()) {
        property = property2Value.getKey();
        value = property2Value.getValue();
        //					if(value == null){
        //						Class cls = property2DatatypeMap.get(property);
        //						if(cls == String.class){
        //							value = "";
        //						} else if(cls == Integer.class){
        //							value = Integer.valueOf(-1);
        //						} else if(cls == Double.class){
        //							value = Double.valueOf(-1);
        //						} else if(cls == Float.class){
        //							value = Float.valueOf(-1);
        //						} else if(cls == Boolean.class){
        //							value = Boolean.FALSE;
        //						}
        //					}
        example.set(property, value);
      }
      queryResult.add(example);
      //			}

    }
    if (config.getSortInfo().getSortField() != null) {
      final String sortField = config.getSortInfo().getSortField();
      Collections.sort(
          queryResult,
          config
              .getSortInfo()
              .getSortDir()
              .comparator(
                  new Comparator<Example>() {

                    @Override
                    public int compare(Example o1, Example o2) {
                      return ((String) o1.get(sortField)).compareTo((String) o2.get(sortField));
                    }
                  }));
    }
    int start = config.getOffset();
    int end = queryResult.size();
    if (limit > 0) {
      end = Math.min(start + limit, end);
    }
    //		queryResult = queryResult.subList(start, end);
    ArrayList<Example> tmp = new ArrayList<Example>();
    for (int i = start; i < end; i++) {
      tmp.add(queryResult.get(i));
    }

    PagingLoadResult<Example> result = new BasePagingLoadResult<Example>(tmp);
    result.setOffset(offset);
    result.setTotalLength(totalLength);

    return result;
  }
  @Override
  public PagingLoadResult<GWTRecordedCase> GetGWTRecordedCasePageList(
      String sysId, String searchKey, PagingLoadConfig config) {

    try {
      List<GWTRecordedCase> returnList = new ArrayList<GWTRecordedCase>();
      Op[] conditions;
      int count;
      List<RecordedCase> lst;

      List<Op> conList = new ArrayList<Op>();
      conList.add(Op.EQ("systemId", Integer.parseInt(sysId)));
      if (config.get("date") != null) {
        String date = config.get("date");
        conList.add(Op.LIKE("createTime", date));
      }
      if (config.get("reponseFlag") != null) {
        if (!config.get("reponseFlag").equals("-1")) {
          conList.add(
              Op.EQ(
                  GWTRecordedCase.N_ResponseFlag,
                  Integer.parseInt(config.get("reponseFlag").toString())));
        }
      }
      if (config.get("isCased") != null) {
        if (!config.get("isCased").equals("-1")) {
          conList.add(
              Op.EQ(GWTRecordedCase.N_IsCased, Integer.parseInt(config.get("isCased").toString())));
        }
      }
      if (config.get("user") != null) {
        if (!config.get("user").equals("-1")) {
          conList.add(Op.EQ("recordUserId", Integer.parseInt(config.get("user").toString())));
        }
      }

      conditions = new Op[conList.size()];
      for (int i = 0; i < conList.size(); i++) {
        conditions[i] = conList.get(i);
      }

      if (searchKey.isEmpty()) {
        count = dataDao.Count(conditions);
        PageStartEnd pse = new PageStartEnd(config, count);
        lst = dataDao.List(pse.getStart(), pse.getEnd(), conditions);
      } else {
        String[] properties = {"requestMsg", "responseMsg", GWTRecordedCase.N_Memo};
        count = dataDao.MatchCount(searchKey, properties, conditions);
        PageStartEnd pse = new PageStartEnd(config, count);
        lst = dataDao.Match(searchKey, properties, pse.getStart(), pse.getEnd(), conditions);
      }

      for (RecordedCase recrdCase : lst) {
        if (recrdCase != null) returnList.add(BeanToModel(recrdCase));
      }

      return new BasePagingLoadResult<GWTRecordedCase>(returnList, config.getOffset(), count);
    } catch (Exception ex) {
      log.error(ex, ex);
      throw new RuntimeException(ex);
    }
  }
  public PagingLoadResult<BeanObject> getPagingList(
      String modelName, Criteria criteria, List<String> wantedFields, PagingLoadConfig pgc) {
    List<BeanObject> list = new ArrayList<BeanObject>();

    Manager manager = getManager(modelName);
    String methodName = config.getListMethod(modelName);
    try {
      Method method =
          manager
              .getClass()
              .getMethod(methodName, new Class[] {int.class, int.class, Criteria.class});
      if (method == null) {
        throw new RuntimeException("Method with paging not found: " + methodName);
      }

      List ret = (List) method.invoke(manager, pgc.getOffset(), pgc.getLimit(), criteria);
      for (Iterator it = ret.iterator(); it.hasNext(); ) {
        ModelObject model = (ModelObject) it.next();
        try {
          Map<String, Object> props = getProperties(model, wantedFields);
          list.add(new BeanObject(model.getClass().getSimpleName(), props));
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (InvocationTargetException e) {
          e.printStackTrace();
        } catch (NoSuchMethodException e) {
          e.printStackTrace();
        }
      }
    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }

    if (pgc.getSortInfo().getSortField() != null) {
      final String sortField = pgc.getSortInfo().getSortField();
      if (sortField != null) {
        Collections.sort(
            list,
            pgc.getSortInfo()
                .getSortDir()
                .comparator(
                    new Comparator() {
                      public int compare(Object o1, Object o2) {
                        Object s1 = ((BeanObject) o1).get(sortField);
                        Object s2 = ((BeanObject) o2).get(sortField);
                        if (s1 instanceof Comparable && s2 instanceof Comparable) {
                          return ((Comparable) s1).compareTo((Comparable) s2);
                        }
                        if (s1 != null && s2 != null) {
                          return s1.toString().compareTo(s2.toString());
                        } else if (s2 != null) {
                          return -1;
                        }
                        return 0;
                      }
                    }));
      }
    }

    return new BasePagingLoadResult(list, pgc.getOffset(), getCount(modelName, criteria));
  }