private void handleJsonSearchNotes(RequestAndResponse requestAndResponse) throws IOException, ServletException {
  requestAndResponse.setResponseContentTypeJson();
  String query=requestAndResponse.getParameter("q");
  if (query == null) {
    query="";
  }
  query=query.trim();
  if (!isUserSignedIn(requestAndResponse)) {
    returnJson400(requestAndResponse,servletText.errorRequiresSignIn(allowSaveIfNotSignedIn));
  }
 else   if (isUsersAccountClosed(requestAndResponse)) {
    returnJson400(requestAndResponse,servletText.errorAccountIsClosed());
  }
 else {
    try {
      final StringBuilder result=new StringBuilder();
      final User user=dbLogic.getUserById(getEffectiveUserId(requestAndResponse));
      if (user != null) {
        final ResultsPaginator paginator=new ResultsPaginator(requestAndResponse,null,result,null);
        result.append("{ \"results\": [");
        if (user != null) {
          boolean first=true;
          final List<?> results=dbLogic.searchEntriesForUserByNote(user.getId(),query,paginator.getStartPosition(),paginator.getMaxResults());
          for (          final Object entryUncasted : results) {
            final Entry entry=(Entry)entryUncasted;
            final int resultNumber=paginator.next();
            if (resultNumber == -1) {
              continue;
            }
 else             if (resultNumber == 0) {
              break;
            }
            if (!first) {
              result.append(",");
              first=false;
            }
            result.append("\n");
            result.append("{ \"id\":\"");
            result.append(entry.getId());
            result.append("\", \"note\":\"");
            result.append(StringEscapeUtils.escapeJson(entry.getNoteOrTitle("")));
            result.append("\", \"quotation\":\"");
            result.append(StringEscapeUtils.escapeJson(entry.getQuotation("")));
            result.append("\"}");
          }
        }
        result.append("\n],\n\"more\": " + (paginator.hasMore() ? "true" : "false") + " }\n");
      }
      dbLogic.commit();
      requestAndResponse.print(result.toString());
    }
 catch (    EmptyQueryException e) {
      returnJson400(requestAndResponse,servletText.errorNeedLongerQuery());
    }
catch (    final PersistenceException e) {
      logger.log(Level.INFO,"Exception",e);
      returnJson500(requestAndResponse,servletText.errorInternalDatabase());
    }
  }
}

private void handleJsonSearchNotes ( RequestAndResponse requestAndResponse ) throws IOException , ServletException { requestAndResponse . setResponseContentTypeJson ( ) ; String query = requestAndResponse . getParameter ( "q" ) ; if ( query == null ) { query = "" ; } query = query . trim ( ) ; if ( ! isUserSignedIn ( requestAndResponse ) ) { returnJson400 ( requestAndResponse , servletText . errorRequiresSignIn ( allowSaveIfNotSignedIn ) ) ; } else if ( isUsersAccountClosed ( requestAndResponse ) ) { returnJson400 ( requestAndResponse , servletText . errorAccountIsClosed ( ) ) ; } else { try { final StringBuilder result = new StringBuilder ( ) ; final User user = dbLogic . getUserById ( getEffectiveUserId ( requestAndResponse ) ) ; if ( user != null ) { final ResultsPaginator paginator = new ResultsPaginator ( requestAndResponse , null , result , null ) ; result . append ( "{ \"results\": [" ) ; if ( user != null ) { boolean first = true ; final List < ? > results = dbLogic . searchEntriesForUserByNote ( user . getId ( ) , query , paginator . getStartPosition ( ) , paginator . getMaxResults ( ) ) ; for ( final Object entryUncasted : results ) { final Entry entry = ( Entry ) entryUncasted ; final int resultNumber = paginator . next ( ) ; if ( resultNumber == - 1 ) { continue ; } else if ( resultNumber == 0 ) { break ; } if ( ! first ) { result . append ( "," ) ; first = false ; } result . append ( "\n" ) ; result . append ( "{ \"id\":\"" ) ; result . append ( entry . getId ( ) ) ; result . append ( "\", \"note\":\"" ) ; result . append ( StringEscapeUtils . escapeJson ( entry . getNoteOrTitle ( "" ) ) ) ; result . append ( "\", \"quotation\":\"" ) ; result . append ( StringEscapeUtils . escapeJson ( entry . getQuotation ( "" ) ) ) ; result . append ( "\"}" ) ; } } result . append ( "\n],\n\"more\": " + ( paginator . hasMore ( ) ? "true" : "false" ) + " }\n" ) ; } dbLogic . commit ( ) ; requestAndResponse . print ( result . toString ( ) ) ; } catch ( EmptyQueryException e ) { returnJson400 ( requestAndResponse , servletText . errorNeedLongerQuery ( ) ) ; } catch ( final PersistenceException e ) { logger . log ( Level . INFO , "Exception" , e ) ; returnJson500 ( requestAndResponse , servletText . errorInternalDatabase ( ) ) ; } } }
part of the json api . returns the matches as json .
