搜索请求View Plugin实现的类

时间:2015-11-10 11:07:52

标签: java eclipse jira jira-plugin

我已将我的请求更新为当前状态 我只留下了一个错误,我不知道如何解决它 首先这是我的代码:

package com.example.plugins.tutorial;

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueFactory;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.issue.search.SearchProviderFactory;
import com.atlassian.jira.issue.search.SearchRequest;
import com.atlassian.jira.issue.views.util.IssueWriterHitCollector;
import com.atlassian.jira.plugin.searchrequestview.AbstractSearchRequestView;
import com.atlassian.jira.plugin.searchrequestview.SearchRequestParams;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.util.JiraVelocityUtils;
import com.atlassian.sal.api.search.SearchProvider;
import com.atlassian.jira.issue.search.providers.LuceneSearchProvider;

import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Collector;

import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


public class TestSearch extends AbstractSearchRequestView
{
    private final JiraAuthenticationContext authenticationContext;
    private final SearchProviderFactory searchProviderFactory;
    private final IssueFactory issueFactory;
    private final SearchProvider searchProvider;

    public TestSearch(JiraAuthenticationContext authenticationContext, SearchProviderFactory searchProviderFactory,
        IssueFactory issueFactory, SearchProvider searchProvider)
{
    this.authenticationContext = authenticationContext;
    this.searchProviderFactory = searchProviderFactory;
    this.issueFactory = issueFactory;
    this.searchProvider = searchProvider;
}

public void writeSearchResults(final SearchRequest searchRequest, final SearchRequestParams searchRequestParams, final Writer writer)
{
    final Map defaultParams = JiraVelocityUtils.getDefaultVelocityParams(authenticationContext);

    //Need to put the filtername into the velocity context.  This may be null if this is an anoymous filter.
    final Map headerParams = new HashMap(defaultParams);
    headerParams.put("filtername", searchRequest.getName());
    try
    {
        //First we need to write the header
        writer.write(descriptor.getHtml("header", headerParams));

        //now lets write the search results.  This basically iterates over each issue in the search results and writes
        //it to the writer using the format defined by this plugin.  To ensure that this doesn't result in huge
        //memory consumption only one issue should be loaded into memory at a time.  This can be guaranteed by using a
        //Hitcollector.
        final IndexSearcher searcher = searchProviderFactory.getSearcher(SearchProviderFactory.ISSUE_INDEX);
        final Map issueParams = new HashMap(defaultParams);
        //This hit collector is responsible for writing out each issue as it is encountered in the search results.
        //It will be called for each search result by the underlying Lucene search code.
        final Collector hitCollector = new IssueWriterHitCollector(searcher, writer, issueFactory)
        {
            protected void writeIssue(Issue issue, Writer writer) throws IOException
            {
                //put the current issue into the velocity context and render the single issue view
                issueParams.put("issue", issue);
                writer.write(descriptor.getHtml("singleissue", issueParams));
            }
        };
        //now run the search that's defined in the issue navigator and pass in the hitcollector from above which will
        //write out each issue in the format specified in this plugin.
        searchProvider.searchAndSort(searchRequest.getQuery(), authenticationContext.getLoggedInUser(), hitCollector, searchRequestParams.getPagerFilter());

        //finally lets write the footer.
        writer.write(descriptor.getHtml("footer", Collections.emptyMap()));
    }
    catch (IOException e)
    {
        throw new RuntimeException(e);
    }
    catch (SearchException e)
    {
        throw new RuntimeException(e);
    }
}
}

我的问题是searchAndSort不起作用

searchProvider.searchAndSort(searchRequest.getQuery(), authenticationContext.getLoggedInUser(), hitCollector, searchRequestParams.getPagerFilter());

Eclipse向我显示错误“类型SearchProvider未定义方法searchAndSort”。

相同问题和具有相同错误的第二个示例:

@Override
public void writeSearchResults(final SearchRequest searchRequest,final SearchRequestParams searchRequestParams,final Writer writer) throws SearchException 
{
    final Map defaultParams = JiraVelocityUtils.getDefaultVelocityParams(authenticationContext);
    final Map headerParams = new HashMap(defaultParams);
    headerParams.put("filtername", searchRequest.getName());
    headerParams.put("user", authenticationContext.getLoggedInUser());
    try 
    {
        //Header
        writer.write(descriptor.getHtml("header", headerParams));
        //Body
        final IndexSearcher searcher = searchProviderFactory.getSearcher(SearchProviderFactory.ISSUE_INDEX);        
        final Map issueParams = new HashMap(defaultParams);
        final Collector hitCollector = new IssueWriterHitCollector(searcher, writer, issueFactory) 
        {
            protected void writeIssue(Issue issue, Writer writer) throws IOException 
            {
                //put the current issue into the velocity context and render the single issue view
                issueParams.put("issue", issue);
                writer.write(descriptor.getHtml("body", issueParams));
            }
        };
        searchProvider.searchAndSort(searchRequest.getQuery(), authenticationContext.getLoggedInUser(), hitCollector, searchRequestParams.getPagerFilter());
        //Footer
        writer.write(descriptor.getHtml("footer", MapBuilder.build("user", authenticationContext.getLoggedInUser())));
    }
    catch (IOException e) 
    {
        throw new RuntimeException(e);
    }
    catch (SearchException e) 
    {
        throw new RuntimeException(e);
    }
}

与searchAndSort相同的问题...

如果有人可以提供帮助,那将是非常好的。

1 个答案:

答案 0 :(得分:2)

I have solved it, I imported the false SearchProvider and now its works. for others who have the same problem and want to make a search request view plugin witch gives back the issues in html, you can have my code:

package com.example.plugins.tutorial;

import com.atlassian.jira.util.collect.MapBuilder;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueFactory;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.issue.search.SearchProviderFactory;
import com.atlassian.jira.issue.search.SearchRequest;
import com.atlassian.jira.issue.views.util.IssueWriterHitCollector;
import com.atlassian.jira.plugin.searchrequestview.AbstractSearchRequestView;
import com.atlassian.jira.plugin.searchrequestview.SearchRequestParams;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.util.JiraVelocityUtils;
import com.atlassian.jira.issue.search.SearchProvider;

import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Collector;

import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class NextTestSearch extends AbstractSearchRequestView {

private final JiraAuthenticationContext authenticationContext;
private final SearchProviderFactory searchProviderFactory;
private final IssueFactory issueFactory;
private final SearchProvider searchProvider;

public NextTestSearch(JiraAuthenticationContext authenticationContext, SearchProviderFactory searchProviderFactory,
        IssueFactory issueFactory, SearchProvider searchProvider)
{
    this.authenticationContext = authenticationContext;
    this.searchProviderFactory = searchProviderFactory;
    this.issueFactory = issueFactory;
    this.searchProvider = searchProvider;
}

@Override
public void writeSearchResults(final SearchRequest searchRequest,final SearchRequestParams searchRequestParams,final Writer writer) throws SearchException 
{
    final Map defaultParams = JiraVelocityUtils.getDefaultVelocityParams(authenticationContext);
    final Map headerParams = new HashMap(defaultParams);
    headerParams.put("filtername", searchRequest.getName());
    headerParams.put("user", authenticationContext.getLoggedInUser());
    try 
    {
        //Header
        writer.write(descriptor.getHtml("header", headerParams));
        //Body
        final IndexSearcher searcher = searchProviderFactory.getSearcher(SearchProviderFactory.ISSUE_INDEX);        
        final Map issueParams = new HashMap(defaultParams);
        final Collector hitCollector = new IssueWriterHitCollector(searcher, writer, issueFactory) 
        {
            protected void writeIssue(Issue issue, Writer writer) throws IOException 
            {
                //put the current issue into the velocity context and render the single issue view
                issueParams.put("issue", issue);
                writer.write(descriptor.getHtml("body", issueParams));
            }
        };
        searchProvider.searchAndSort(searchRequest.getQuery(), authenticationContext.getLoggedInUser(), hitCollector, searchRequestParams.getPagerFilter());
        //Footer
        writer.write(descriptor.getHtml("footer", MapBuilder.build("user", authenticationContext.getLoggedInUser())));
    }
    catch (IOException e) 
    {
        throw new RuntimeException(e);
    }
    catch (SearchException e) 
    {
        throw new RuntimeException(e);
    }
}
}

the rest you need for the plugin should work of the officele tutorial, This is the Link there is the tutorial but the java source code is deprecated.