How to I add search based capability to my simple plugin

CONRAD BRAGANZA August 25, 2011

Hi I need to add search based capability to my simple plugin below from the issue navigator search screen. can anyone please help? I just need either a text box or either the same select list to be displayed. If the user selects an option and hits search (or enters a value) all issues having that custom field value will be displayed.

package com.xyz.jira.plugin;

import com.atlassian.jira.issue.customfields.impl.SelectCFType;
import com.atlassian.jira.issue.customfields.persistence.CustomFieldValuePersister;
import com.atlassian.jira.issue.customfields.converters.StringConverter;
import com.atlassian.jira.issue.customfields.converters.SelectConverter;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.customfields.manager.GenericConfigManager;
import com.atlassian.jira.issue.customfields.view.CustomFieldParams;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.search.SearchContextImpl;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.jira.util.ErrorCollection;

import java.util.Map;
import java.util.HashMap;

/**
*/
public class CustomDBPlugin extends SelectCFType {
/** The options manager. */
private final OptionsManager optionsManager;
//private DatabaseCFOption conf;

public CustomDBPlugin(CustomFieldValuePersister customFieldValuePersister, StringConverter stringConverter, SelectConverter selectConverter, OptionsManager optionsManager, GenericConfigManager genericConfigManager) {
super(customFieldValuePersister, stringConverter, selectConverter, optionsManager, genericConfigManager);
this.optionsManager = optionsManager;
}

/**
*/
@Override
public void validateFromParams(CustomFieldParams relevantParams,
ErrorCollection errorCollectionToAddTo, FieldConfig config) {
// do nothing, no validation required yet.
}

/**
*/
@Override
@SuppressWarnings("unchecked")
public Map getVelocityParameters(Issue issue, CustomField field,
FieldLayoutItem fieldLayoutItem) {
Map parameters = super.getVelocityParameters(issue, field, fieldLayoutItem);
FieldConfig fieldConfig = null;
if(issue == null)
{
fieldConfig = field.getReleventConfig(new SearchContextImpl());
} else
{
fieldConfig = field.getRelevantConfig(issue);
}
Options options = this.optionsManager.getOptions(fieldConfig);
if (options.isEmpty()) {
this.optionsManager.createOption(fieldConfig, null, new Long(1), "A");
this.optionsManager.createOption(fieldConfig, null, new Long(2), "B");
}
options = this.optionsManager.getOptions(fieldConfig);
Map<Long, String> results = new HashMap<Long, String>();
Long selectedId= (long) -1;
boolean selected = false;
Object value = field.getValue(issue);
if (value!=null) {
selected=true;
}
for (Option option : (Iterable<Option>) options) {
results.put(option.getOptionId(), option.getValue());
if (selected && value.toString().equals(option.getValue())) {
selectedId = option.getOptionId();
}
}
parameters.put("results", results);
parameters.put("selectedId", selectedId);
return parameters;
}


}

Here is the velocity template custom-db-field-edit.vm
#* @vtlvariable name="results" type="java.util.Map" *#
#* @vtlvariable name="selectedId" type="java.lang.String" *#
#controlHeader ($action $customField.id $customField.name $fieldLayoutItem.required $displayParameters.noHeader)


<select name="$customField.id" id="$customField.id" >
<option value="">Not selected</option>
#foreach ($mapEntry in $results.entrySet())
#if ( $selectedId == $mapEntry.key )
<option selected="selected" value="$mapEntry.key">$mapEntry.value</option>
#else
<option value="$mapEntry.key">$mapEntry.value</option>
#end
#end
</select>

#controlFooter ($action $fieldLayoutItem.fieldDescription $displayParameters.noHeader)

Here is the plugin desctiptor.

<atlassian-plugin key="com.xyz.jira.plugin.customdbplugin" name="CustomDB" plugins-version="2">
<plugin-info>
<description>XYZ</description>
<version>1.0-SNAPSHOT</version>
<vendor name="XYZ" url="http://www.xyz.com/" />
</plugin-info>

<customfield-type key="customdb-field" name="CustomDB Field" class="com.xyz.jira.plugin.CustomDBPlugin">
<description>Custom DB Plugin</description>
<label>Custom DB Field</label>
<resource type="velocity" name="view" location="templates/plugins/custom-db/custom-db-field-edit.vm"/>
<resource type="velocity" name="edit" location="templates/plugins/custom-db/custom-db-field-edit.vm"/>
<resource type="velocity" name="xml" location="templates/plugins/fields/xml/xml-basictext.vm"/>
</customfield-type>

</atlassian-plugin>

6 answers

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 25, 2011

The xml looks right to me, I think you need to expand on the error message - "invalid field type" doesn't tell us much. What does the full error say?

0 votes
CONRAD BRAGANZA August 25, 2011

test leaving comment

0 votes
CONRAD BRAGANZA August 25, 2011

Option 2

<customfield-searcher key="selectsearcher" name="select searcher"
class="com.atlassian.jira.issue.customfields.searchers.SelectSearcher" i18n-name-key="admin.customfield.searcher.selectsearcher.name">
<description key="admin.customfield.searcher.selectsearcher.desc">Search CustomDB</description>
<resource type="velocity" name="search" location="templates/plugins/fields/edit-searcher/search-select.vm"/>
<resource type="velocity" name="view" location="templates/plugins/fields/view-searcher/view-searcher-option.vm"/>
<resource type="velocity" name="label" location="templates/plugins/fields/view-searcher/label-searcher-basictext.vm"/>
<valid-customfield-type package="com.xyz.jira.plugin.customdbplugin" key="customdb-field"/>
</customfield-searcher>
I have tried both options. I just want to be able to have a select or text box that allows me to enter or select a value and all issues that have the specified value assigned to that custom field appears. So even if it means assigning one of the preselected search templates that would do for me. Not sure whether I have to icorporate any search logic?

Sorry had to split my comments as it was not allowing me to add it as one big one.

0 votes
CONRAD BRAGANZA August 25, 2011

I have tried adding the following, the plugin installs then when i try to create a custom field xyz it gives me
Errors

Invalid field type specified. Then the plugin gets disapled.
Option 1

<customfield-searcher key="selectsearcher" name="select searcher"
class="com.atlassian.jira.issue.customfields.searchers.SelectSearcher" >
<descriptionSearch CustomDB</description>
<resource type="velocity" name="search" location="templates/plugins/fields/edit-searcher/search-select.vm"/>
<resource type="velocity" name="view" location="templates/plugins/fields/view-searcher/view-searcher-option.vm"/>
<resource type="velocity" name="label" location="templates/plugins/fields/view-searcher/label-searcher-basictext.vm"/>
<valid-customfield-type package="com.xyz.jira.plugin.customdbplugin" key="customdb-field"/>
</customfield-searcher>

0 votes
CONRAD BRAGANZA August 25, 2011

I have tried adding the following, the plugin installs then when i try to create a custom field xyz it gives me

Errors

  • Invalid field type specified. Then the plugin gets disapled.

Option 1

<customfield-searcher key="selectsearcher" name="select searcher"
class="com.atlassian.jira.issue.customfields.searchers.SelectSearcher" >
<descriptionSearch CustomDB</description>
<resource type="velocity" name="search" location="templates/plugins/fields/edit-searcher/search-select.vm"/>
<resource type="velocity" name="view" location="templates/plugins/fields/view-searcher/view-searcher-option.vm"/>
<resource type="velocity" name="label" location="templates/plugins/fields/view-searcher/label-searcher-basictext.vm"/>
<valid-customfield-type package="com.xyz.jira.plugin.customdbplugin" key="customdb-field"/>
</customfield-searcher>

Option 2

<customfield-searcher key="selectsearcher" name="select searcher"
class="com.atlassian.jira.issue.customfields.searchers.SelectSearcher" i18n-name-key="admin.customfield.searcher.selectsearcher.name">
<description key="admin.customfield.searcher.selectsearcher.desc">Search CustomDB</description>
<resource type="velocity" name="search" location="templates/plugins/fields/edit-searcher/search-select.vm"/>
<resource type="velocity" name="view" location="templates/plugins/fields/view-searcher/view-searcher-option.vm"/>
<resource type="velocity" name="label" location="templates/plugins/fields/view-searcher/label-searcher-basictext.vm"/>
<valid-customfield-type package="com.xyz.jira.plugin.customdbplugin" key="customdb-field"/>
</customfield-searcher>

I have tried both options. I just want to be able to have a select or text box that allows me to enter or select a value and all issues that have the specified value assigned to that custom field appears. So even if it means assigning one of the preselected search templates that would do for me. Not sure whether I have to icorporate any search logic?

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 25, 2011

Start with http://confluence.atlassian.com/pages/viewpage.action?pageId=236588798

Note - there is a page for 4.4, but the "include" it uses has been broken, so it's not working. It's almost identical though - the basic idea is to add a searcher to your atlassian-plugin.xml that says "use this searcher". If you want to use an existing one (text, select, date, number etc), it's a doddle

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events