View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.atlassian.theplugin.jira.model;
17  
18  import com.atlassian.theplugin.configuration.IssueRecentlyOpenBean;
19  import com.atlassian.theplugin.jira.api.JIRAIssue;
20  import org.jetbrains.annotations.NotNull;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.Set;
26  
27  public class SearchingJIRAIssueListModel extends JIRAIssueListModelListenerHolder {
28  	private String searchTerm;
29  
30  	public SearchingJIRAIssueListModel(JIRAIssueListModel parent) {
31  		super(parent);
32  		searchTerm = "";
33  	}
34  
35  	public void setSearchTerm(@NotNull String searchTerm) {
36  
37  		if (this.searchTerm.equals(searchTerm)) {
38  			return;
39  		}
40  		this.searchTerm = searchTerm.toLowerCase();
41  		fireModelChanged();
42  	}
43  
44  	public Collection<JIRAIssue> search(Collection<JIRAIssue> col) {
45  		if (searchTerm.length() == 0) {
46  			return col;
47  		}
48  		List<JIRAIssue> list = new ArrayList<JIRAIssue>();
49  		for (JIRAIssue i : col) {
50  			if (isMatch(i)) {
51  				list.add(i);
52  			}
53  		}
54  		return list;
55  	}
56  
57  	private boolean isMatch(JIRAIssue issue) {
58  		return issue.getKey().toLowerCase().indexOf(searchTerm) > -1
59  				|| issue.getSummary().toLowerCase().indexOf(searchTerm) > -1;
60  	}
61  
62  	public Collection<JIRAIssue> getIssues() {
63  		return search(parent.getIssues());
64  	}
65  
66  	/*
67  	 * this version of the routine also returns issues that have subtasks matching the search term
68  	 */
69  	public Collection<JIRAIssue> getIssuesNoSubtasks() {
70  		List<JIRAIssue> result = new ArrayList<JIRAIssue>();
71  
72  		Collection<JIRAIssue> issues = parent.getIssues();
73  		for (JIRAIssue i : issues) {
74  			if (!i.isSubTask()) {
75  				if (isMatch(i)) {
76  					result.add(i);
77  				} else {
78  					for (String subKey : i.getSubTaskKeys()) {
79  						JIRAIssue sub = parent.findIssue(subKey);
80  						if (sub != null && isMatch(sub)) {
81  							result.add(i);
82  							break;
83  						}
84  					}
85  				}
86  			}
87  		}
88  		return result;
89  	}
90  
91  	@NotNull
92  	public Collection<JIRAIssue> getSubtasks(JIRAIssue p) {
93  		return search(parent.getSubtasks(p));
94  	}
95  
96  	public JIRAIssue findIssue(String key) {
97  		return parent.findIssue(key);
98  	}
99  
100 	public void clearCache() {
101 	}
102 
103 	public Set<JIRAIssue> getIssuesCache() {
104 		return null;
105 	}
106 
107 	public JIRAIssue getIssueFromCache(final IssueRecentlyOpenBean recentIssue) {
108 		return null;
109 	}
110 
111 	public void setActiveJiraIssue(final ActiveJiraIssueBean issue) {
112 	}
113 
114 	public ActiveJiraIssueBean getActiveJiraIssue() {
115 		return null;
116 	}
117 }