View Javadoc

1   package com.atlassian.jira.web.dwr;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   
6   import org.apache.log4j.Logger;
7   import org.ofbiz.core.entity.GenericValue;
8   
9   import com.atlassian.jira.bc.JiraServiceContext;
10  import com.atlassian.jira.bc.JiraServiceContextImpl;
11  import com.atlassian.jira.bc.search.PickerSearchService;
12  import com.atlassian.jira.config.properties.APKeys;
13  import com.atlassian.jira.config.properties.ApplicationProperties;
14  import com.atlassian.jira.security.JiraAuthenticationContext;
15  import com.atlassian.jira.util.ErrorCollection;
16  import com.atlassian.jira.util.SimpleErrorCollection;
17  import com.opensymphony.user.User;
18  
19  public abstract class AbstractAjaxPicker {
20  
21  	private static final Logger log = Logger.getLogger(AbstractAjaxPicker.class);
22  
23  	private final JiraAuthenticationContext authContext;
24  
25  	private final PickerSearchService service;
26  
27  	private final ApplicationProperties applicationProperties;
28  
29  	/**
30  	 * The bean AjaxManageableEntityPicker is instantiated by the DWR pico create in response to a DWR request
31  	 * 
32  	 * @param authContext
33  	 *           the Jira authentication context
34  	 * @param service
35  	 *           the business component that does issue searching
36  	 * @param applicationProperties
37  	 *           JIRA app properties
38  	 */
39  	public AbstractAjaxPicker(JiraAuthenticationContext authContext, PickerSearchService service, ApplicationProperties applicationProperties) {
40  		this.authContext = authContext;
41  		this.service = service;
42  		this.applicationProperties = applicationProperties;
43  	}
44  
45  	/**
46  	 * This is the AJAX entry point to find searched Entities from given a query string.
47  	 * 
48  	 * @param fieldName
49  	 *           The field that we are giving results for
50  	 * @param query
51  	 *           String Query to search on
52  	 * @return an AutoCompleteResults as required by the YUI client side code
53  	 */
54  	public AutoCompleteResults getEntities(final String _fieldName, final String _entityName, final String _query) {
55  
56  		final JiraServiceContext jiraServiceCtx = getContext();
57  		final AutoCompleteResults acResults = new AutoCompleteResults();
58  
59  		if (service.canPerformAjaxSearch(jiraServiceCtx)) {
60  			int limit = getLimit();
61  			int count = 0;
62  			Collection results = service.getResults(jiraServiceCtx, _entityName, _query);
63  			for (Iterator iterator = results.iterator(); iterator.hasNext();) {
64  				if (count >= limit) {
65  					break;
66  				}
67  				GenericValue gv = (GenericValue) iterator.next();
68  				String html = formatEntity(_fieldName, gv, _query);
69  				acResults.addResult(gv.get("id").toString(), html);
70  				count++;
71  			}
72  			acResults.setFooterMessage(getText("jira.ajax.autocomplete.manageableAsProject.more.results", String.valueOf(count), String.valueOf(results.size())));
73  		}
74  		return acResults;
75  	}
76  
77  	protected String getElementId(String fieldName, String type, String field) {
78  		return " id=\"" + fieldName + "_" + type + "_" + field + "\" ";
79  	}
80  
81  	// get the number of items to display.
82  	private int getLimit() {
83  		// Default limit to 20
84  		int limit = 20;
85  		try {
86  			limit = Integer.valueOf(applicationProperties.getDefaultBackedString(APKeys.JIRA_AJAX_AUTOCOMPLETE_LIMIT)).intValue();
87  		} catch (Exception nfe) {
88  			log.error("jira.ajax.autocomplete.limit does not exist or is an invalid number in jira-application.properties.", nfe);
89  		}
90  		return limit;
91  	}
92  
93  	protected abstract String formatEntity(String fieldName, GenericValue _gv, String query);
94  	
95  	JiraServiceContext getContext() {
96  		User user = authContext.getUser();
97  		ErrorCollection errorCollection = new SimpleErrorCollection();
98  		return new JiraServiceContextImpl(user, errorCollection);
99  	}
100 
101 	/**
102 	 * Translates a given key using i18n bean
103 	 * 
104 	 * @param key
105 	 *           key to translate
106 	 * @return i18n string for given key
107 	 */
108 	String getText(String key) {
109 		return authContext.getI18nBean().getText(key);
110 	}
111 
112 	/**
113 	 * Translates a given key using i18n bean, passing in param
114 	 * 
115 	 * @param key
116 	 *           key to translate
117 	 * @param param
118 	 *           param to insert into property
119 	 * @return i18n string for given key, with param inserted
120 	 */
121 	String getText(String key, Object param) {
122 		return authContext.getI18nBean().getText(key, param);
123 	}
124 
125 	/**
126 	 * Translates a given key using i18n bean, passing along the params.
127 	 * 
128 	 * @param key
129 	 *           key to translate
130 	 * @param param1
131 	 *           param to insert
132 	 * @param param2
133 	 *           param to insert
134 	 * @return translated string.
135 	 */
136 	String getText(String key, String param1, String param2) {
137 		return authContext.getI18nBean().getText(key, param1, param2);
138 	}
139 
140 	
141 }