Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 JBoss, by Red Hat, Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.kie.workbench.common.services.refactoring.backend.server.query;
 
 import java.util.List;
 import java.util.Set;
 
 import  org.drools.workbench.models.datamodel.util.PortablePreconditions;
 import  org.kie.workbench.common.services.refactoring.model.index.terms.IndexTerm;
 import  org.kie.workbench.common.services.refactoring.model.index.terms.valueterms.ValueIndexTerm;
 import  org.kie.workbench.common.services.refactoring.model.query.RefactoringPageRequest;
 import  org.kie.workbench.common.services.refactoring.model.query.RefactoringPageRow;
 import  org.kie.workbench.common.services.refactoring.service.RefactoringQueryService;
 import  org.uberfire.ext.metadata.backend.lucene.LuceneConfig;
 import  org.uberfire.ext.metadata.backend.lucene.index.LuceneIndexManager;
 import  org.uberfire.ext.metadata.model.KObject;
 import  org.uberfire.ext.metadata.search.ClusterSegment;
 import  org.uberfire.paging.PageResponse;
 
 import static org.uberfire.ext.metadata.backend.lucene.util.KObjectUtil.*;
 
 public class RefactoringQueryServiceImpl implements RefactoringQueryService {
 
     private LuceneConfig config;
     private Set<NamedQuerynamedQueries = new HashSet<NamedQuery>();
     private PageResponse<RefactoringPageRow> emptyResponse;
 
     public RefactoringQueryServiceImpl() {
         //Make proxyable
     }
 
     @Inject
     public RefactoringQueryServiceImpl( @Named("luceneConfig"final LuceneConfig config,
                                         @Any final Instance<NamedQuerynamedQueries ) {
         this. = PortablePreconditions.checkNotNull( "config",
                                                           config );
         PortablePreconditions.checkNotNull( "namedQueries",
                                             namedQueries );
         for ( NamedQuery namedQuery : namedQueries ) {
             this..addnamedQuery );
         }
     }
 
     @PostConstruct
     public void init() {
          = new PageResponse<RefactoringPageRow>();
         .setPageRowList( Collections.<RefactoringPageRow>emptyList() );
         .setStartRowIndex( 0 );
         .setTotalRowSize( 0 );
         .setLastPage( true );
         .setTotalRowSizeExact( true );
     }
 
     @Override
     public Set<StringgetQueries() {
         final Set<StringqueryNames = new HashSet<String>();
         for ( NamedQuery namedQuery :  ) {
             queryNames.addnamedQuery.getName() );
         }
         return queryNames;
     }
 
     @Override
     public Set<IndexTerm> getTermsfinal String queryName ) {
         for ( NamedQuery namedQuery :  ) {
             if ( namedQuery.getName().equalsqueryName ) ) {
                 return namedQuery.getTerms();
             }
        }
        throw new IllegalArgumentException"Named Query '" + queryName + "' does not exist." );
    }
    @Override
    public PageResponse<RefactoringPageRow> queryfinal RefactoringPageRequest request ) {
        PortablePreconditions.checkNotNull( "request",
                                            request );
        final String queryName = PortablePreconditions.checkNotNull( "queryName",
                                                                     request.getQueryName() );
        NamedQuery namedQuery = null;
        for ( NamedQuery nq :  ) {
            if ( nq.getName().equalsqueryName ) ) {
                namedQuery = nq;
                break;
            }
        }
        if ( namedQuery == null ) {
            throw new IllegalArgumentException"Named Query '" + queryName + "' does not exist." );
        }
        //Validate provided terms against those required for the named query
        final Set<IndexTerm> namedQueryTerms = namedQuery.getTerms();
        final Set<ValueIndexTerm> queryTerms = request.getQueryTerms();
        for ( IndexTerm term : namedQueryTerms ) {
            if ( !valueTermsContainsRequiredTermqueryTerms,
                                                  term ) ) {
                throw new IllegalArgumentException"Expected IndexTerm '" + term.getTerm() + "' was not found." );
            }
        }
        //Validate provided terms against those required for the named query
        for ( ValueIndexTerm term : queryTerms ) {
            if ( !requiredTermsContainsValueTermnamedQueryTerms,
                                                  term ) ) {
                //log.warning - term will not be used
            }
        }
        final Query query = namedQuery.toQueryqueryTerms,
                                                request.useWildcards() );
        final int hits = searchHitsquery );
        if ( hits > 0 ) {
            final int pageSize = request.getPageSize();
            final int startIndex = request.getStartRowIndex();
            final List<KObject> kObjects = searchquery,
                                                   pageSize,
                                                   startIndex );
            final ResponseBuilder responseBuilder = namedQuery.getResponseBuilder();
            return responseBuilder.buildResponsepageSize,
                                                  startIndex,
                                                  kObjects );
        }
        return ;
    }
    @Override
    public List<RefactoringPageRow> queryfinal String queryName,
                                           final Set<ValueIndexTerm> queryTerms,
                                           final boolean useWildcards ) {
        PortablePreconditions.checkNotNull( "queryName",
                                            queryName );
        PortablePreconditions.checkNotNull( "queryTerms",
                                            queryTerms );
        NamedQuery namedQuery = null;
        for ( NamedQuery nq :  ) {
            if ( nq.getName().equalsqueryName ) ) {
                namedQuery = nq;
                break;
            }
        }
        if ( namedQuery == null ) {
            throw new IllegalArgumentException"Named Query '" + queryName + "' does not exist." );
        }
        //Validate provided terms against those required for the named query
        final Set<IndexTerm> namedQueryTerms = namedQuery.getTerms();
        for ( IndexTerm term : namedQueryTerms ) {
            if ( !valueTermsContainsRequiredTermqueryTerms,
                                                  term ) ) {
                throw new IllegalArgumentException"Expected IndexTerm '" + term.getTerm() + "' was not found." );
            }
        }
        //Validate provided terms against those required for the named query
        for ( ValueIndexTerm term : queryTerms ) {
            if ( !requiredTermsContainsValueTermnamedQueryTerms,
                                                  term ) ) {
                //log.warning - term will not be used
            }
        }
        final Query query = namedQuery.toQueryqueryTerms,
                                                useWildcards );
        final int hits = searchHitsquery );
        if ( hits > 0 ) {
            final List<KObject> kObjects = searchquery,
                                                   hits );
            final ResponseBuilder responseBuilder = namedQuery.getResponseBuilder();
            return responseBuilder.buildResponsekObjects );
        }
        return Collections.emptyList();
    }
    private boolean valueTermsContainsRequiredTermfinal Set<ValueIndexTerm> providedTerms,
                                                    final IndexTerm requiredTerm ) {
        for ( ValueIndexTerm valueTerm : providedTerms ) {
            if ( valueTerm.getTerm().equals( requiredTerm.getTerm() ) ) {
                return true;
            }
        }
        return false;
    }
    private boolean requiredTermsContainsValueTermfinal Set<IndexTerm> requiredTerms,
                                                    final ValueIndexTerm providedTerm ) {
        for ( IndexTerm valueTerm : requiredTerms ) {
            if ( valueTerm.getTerm().equals( providedTerm.getTerm() ) ) {
                return true;
            }
        }
        return false;
    }
    private int searchHitsfinal Query query,
                            final ClusterSegment... clusterSegments ) {
        final LuceneIndexManager indexManager = ( (LuceneIndexManager) .getIndexManager() );
        final IndexSearcher index = indexManager.getIndexSearcher( clusterSegments );
        try {
            final TotalHitCountCollector collector = new TotalHitCountCollector();
            index.searchquery,
                          collector );
            return collector.getTotalHits();
        } catch ( final Exception ex ) {
            throw new RuntimeException"Error during Query!"ex );
        } finally {
            indexManager.release( index );
        }
    }
    private List<KObject> searchfinal Query query,
                                  final int pageSize,
                                  final int startIndex,
                                  final ClusterSegment... clusterSegments ) {
        final LuceneIndexManager indexManager = ( (LuceneIndexManager) .getIndexManager() );
        final TopScoreDocCollector collector = TopScoreDocCollector.create( ( startIndex + 1 ) * pageSize,
                                                                            true );
        final IndexSearcher index = indexManager.getIndexSearcher( clusterSegments );
        final List<KObject> result = new ArrayList<KObject>( pageSize );
        try {
            index.searchquery,
                          collector );
            final ScoreDoc[] hits = collector.topDocsstartIndex ).;
            int iterations = hits.length > pageSize ? pageSize : hits.length;
            for ( int i = 0; i < iterationsi++ ) {
                result.add( toKObjectindex.dochitsi ]. ) ) );
            }
        } catch ( final Exception ex ) {
            throw new RuntimeException"Error during Query!",
                                        ex );
        } finally {
            indexManager.release( index );
        }
        return result;
    }
    private List<KObject> searchfinal Query query,
                                  final int totalHits,
                                  final ClusterSegment... clusterSegments ) {
        final LuceneIndexManager indexManager = ( (LuceneIndexManager) .getIndexManager() );
        final TopScoreDocCollector collector = TopScoreDocCollector.createtotalHits,
                                                                            true );
        final IndexSearcher index = indexManager.getIndexSearcher( clusterSegments );
        final List<KObject> result = new ArrayList<KObject>();
        try {
            index.searchquery,
                          collector );
            final ScoreDoc[] hits = collector.topDocs().;
            for ( int i = 0; i < hits.lengthi++ ) {
                result.add( toKObjectindex.dochitsi ]. ) ) );
            }
        } catch ( final Exception ex ) {
            throw new RuntimeException"Error during Query!",
                                        ex );
        } finally {
            indexManager.release( index );
        }
        return result;
    }
New to GrepCode? Check out our FAQ X