KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > history > SearchExecutor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss.ui.history;
21
22 import org.netbeans.lib.cvsclient.command.log.RlogCommand;
23 import org.netbeans.lib.cvsclient.command.log.LogInformation;
24 import org.netbeans.lib.cvsclient.command.log.LogCommand;
25 import org.netbeans.modules.versioning.system.cvss.ui.actions.log.RLogExecutor;
26 import org.netbeans.modules.versioning.system.cvss.ui.actions.log.LogExecutor;
27 import org.netbeans.modules.versioning.system.cvss.ExecutorGroup;
28 import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
29 import org.netbeans.modules.versioning.system.cvss.ClientRuntime;
30 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig;
31 import org.netbeans.modules.versioning.system.cvss.util.Utils;
32
33 import java.io.File JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.*;
36 import java.text.DateFormat JavaDoc;
37 import java.text.ParseException JavaDoc;
38 import java.text.SimpleDateFormat JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.net.MalformedURLException JavaDoc;
41
42 import org.openide.util.NbBundle;
43 import org.openide.windows.OutputListener;
44 import org.openide.windows.OutputEvent;
45 import org.openide.awt.HtmlBrowser;
46 import org.openide.awt.StatusDisplayer;
47
48 /**
49  * Executes searches in Search History panel.
50  *
51  * @author Maros Sandor
52  */

53 class SearchExecutor implements Runnable JavaDoc {
54
55     public static final SimpleDateFormat JavaDoc simpleDateFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm"); // NOI18N
56

57     private static final SimpleDateFormat JavaDoc fullDateFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss Z"); // NOI18N
58
private static final DateFormat JavaDoc [] dateFormats = new DateFormat JavaDoc[] {
59         fullDateFormat,
60         new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss"), // NOI18N
61
simpleDateFormat,
62         new SimpleDateFormat JavaDoc("yyyy-MM-dd"), // NOI18N
63
};
64     
65     /**
66      * Collection of CVSRoots that do not support rXXX commands.
67      */

68     private static Set misconfiguredServers = Collections.synchronizedSet(new HashSet());
69     
70     private final SearchHistoryPanel master;
71     private File JavaDoc[] folders;
72     private File JavaDoc[] files;
73     private final SearchCriteriaPanel criteria;
74     
75     private List results = new ArrayList();
76
77     public SearchExecutor(SearchHistoryPanel master) {
78         this.master = master;
79         File JavaDoc [] roots = master.getRoots();
80         
81         Set printedWarnings = new HashSet(1);
82         Set foldersSet = new HashSet(roots.length);
83         Set filesSet = new HashSet(roots.length);
84         for (int i = 0; i < roots.length; i++) {
85             File JavaDoc root = roots[i];
86             boolean isMisconfiguredServer = false;
87             try {
88                 String JavaDoc cvsRoot = Utils.getCVSRootFor(root);
89                 isMisconfiguredServer = misconfiguredServers.contains(cvsRoot);
90                 if (root.isDirectory() && isMisconfiguredServer && printedWarnings.add(cvsRoot)) {
91                     showMisconfiguredServerWarning(cvsRoot);
92                 }
93             } catch (IOException JavaDoc e) {
94                 // ignore
95
}
96             if (root.isFile() || isMisconfiguredServer) {
97                 filesSet.add(root);
98             } else {
99                 foldersSet.add(root);
100             }
101         }
102         files = (File JavaDoc[]) filesSet.toArray(new File JavaDoc[filesSet.size()]);
103         folders = (File JavaDoc[]) foldersSet.toArray(new File JavaDoc[foldersSet.size()]);
104         criteria = master.getCriteria();
105     }
106
107     public void run() {
108         String JavaDoc from = criteria.getFrom();
109         String JavaDoc to = criteria.getTo();
110         Date fromDate = parseDate(from);
111         Date toDate = parseDate(to);
112
113         RlogCommand rcmd = new RlogCommand();
114         LogCommand lcmd = new LogCommand();
115
116         if (fromDate != null || toDate != null) {
117             String JavaDoc dateFilter = ""; // NOI18N
118
if (fromDate != null) {
119                 dateFilter = fullDateFormat.format(fromDate);
120             }
121             dateFilter += "<="; // NOI18N
122
if (toDate != null) {
123                 dateFilter += fullDateFormat.format(toDate);
124             }
125             rcmd.setDateFilter(dateFilter);
126             lcmd.setDateFilter(dateFilter);
127         } else if (from != null || to != null) {
128             String JavaDoc revFilter = ""; // NOI18N
129
if (from != null) {
130                 revFilter = from;
131             }
132             revFilter += ":"; // NOI18N
133
if (to != null) {
134                 revFilter += to;
135             }
136             rcmd.setRevisionFilter(revFilter);
137             lcmd.setRevisionFilter(revFilter);
138         }
139         
140         rcmd.setNoTags(!CvsModuleConfig.getDefault().getPreferences().getBoolean(CvsModuleConfig.PROP_SEARCHHISTORY_FETCHTAGS, true));
141         lcmd.setNoTags(!CvsModuleConfig.getDefault().getPreferences().getBoolean(CvsModuleConfig.PROP_SEARCHHISTORY_FETCHTAGS, true));
142         rcmd.setUserFilter(criteria.getUsername());
143         lcmd.setUserFilter(criteria.getUsername());
144
145         ExecutorGroup group = new ExecutorGroup(NbBundle.getMessage(SearchExecutor.class, "BK0001"), false); // NOI18N
146
RLogExecutor [] rexecutors;
147         if (folders.length > 0) {
148             rexecutors = RLogExecutor.splitCommand(rcmd, folders, null);
149         } else {
150             rexecutors = new RLogExecutor[0];
151         }
152         group.addExecutors(rexecutors);
153
154         LogExecutor [] lexecutors;
155         if (files.length > 0) {
156             lcmd.setFiles(files);
157             lexecutors = LogExecutor.splitCommand(lcmd, null);
158         } else {
159             lexecutors = new LogExecutor[0];
160         }
161         group.addExecutors(lexecutors);
162
163         final RLogExecutor [] frexecutors = rexecutors;
164         final LogExecutor [] flexecutors = lexecutors;
165         Runnable JavaDoc action = new Runnable JavaDoc() {
166             public void run() {
167                 List newResults = processResults(frexecutors, flexecutors);
168                 results.addAll(newResults);
169                 if (testForRLogFailures(frexecutors)) {
170                     SearchExecutor.this.run();
171                     return;
172                 }
173                 master.setResults(results);
174             }
175         };
176         group.addBarrier(action);
177         group.execute();
178
179     }
180
181     private boolean testForRLogFailures(RLogExecutor[] executors) {
182         Set failedFiles = new HashSet();
183         Set printedWarnings = new HashSet(1);
184         for (int i = 0; i < executors.length; i++) {
185             RLogExecutor executor = executors[i];
186             if (executor.hasFailedOnSymbolicLink()) {
187                 try {
188                     String JavaDoc cvsRoot = Utils.getCVSRootFor(executor.getFile());
189                     if (printedWarnings.add(cvsRoot)) {
190                         showMisconfiguredServerWarning(cvsRoot);
191                     }
192                     misconfiguredServers.add(cvsRoot);
193                 } catch (IOException JavaDoc e) {
194                     // harmless + should never happen
195
}
196                 failedFiles.add(executor.getFile());
197             }
198         }
199         if (failedFiles.size() > 0) {
200             files = (File JavaDoc[]) failedFiles.toArray(new File JavaDoc[failedFiles.size()]);
201             folders = new File JavaDoc[0];
202             return true;
203         } else {
204             return false;
205         }
206     }
207
208     private void showMisconfiguredServerWarning(String JavaDoc cvsRoot) {
209         final String JavaDoc relNotesUrl = "http://javacvs.netbeans.org/release/5.0"; // NOI18N
210
ClientRuntime runtime = CvsVersioningSystem.getInstance().getClientRuntime(cvsRoot);
211         runtime.log(NbBundle.getMessage(SearchExecutor.class, "MSG_SymlinkWarning1") + "\n", null); // NOI18N
212
runtime.log(NbBundle.getMessage(SearchExecutor.class, "MSG_SymlinkWarning2", relNotesUrl) + "\n", new OutputListener() { // NOI18N
213
public void outputLineSelected(OutputEvent ev) {
214             }
215
216             public void outputLineAction(OutputEvent ev) {
217                 try {
218                     HtmlBrowser.URLDisplayer.getDefault().showURL(new URL JavaDoc(relNotesUrl));
219                 } catch (MalformedURLException JavaDoc e) {
220                     // never happens
221
}
222             }
223
224             public void outputLineCleared(OutputEvent ev) {
225             }
226         });
227         StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(SearchExecutor.class, "MSG_StatusSymlinkWarning")); // NOI18N
228
}
229
230     private List processResults(RLogExecutor[] rexecutors, LogExecutor[] lexecutors) {
231         List log = new ArrayList(200);
232         for (int i = 0; i < rexecutors.length; i++) {
233             RLogExecutor executor = rexecutors[i];
234             log.addAll(executor.getLogEntries());
235         }
236         for (int i = 0; i < lexecutors.length; i++) {
237             LogExecutor executor = lexecutors[i];
238             log.addAll(executor.getLogEntries());
239         }
240         String JavaDoc commitMessage = criteria.getCommitMessage();
241
242         List newResults = new ArrayList(log.size());
243         for (Iterator i = log.iterator(); i.hasNext();) {
244             LogInformation info = (LogInformation) i.next();
245             newResults.addAll(info.getRevisionList());
246         }
247
248         if (commitMessage != null) {
249             for (Iterator i = newResults.iterator(); i.hasNext();) {
250                 LogInformation.Revision revision = (LogInformation.Revision) i.next();
251                 String JavaDoc msg = revision.getMessage();
252                 if (msg.indexOf(commitMessage) == -1) {
253                     i.remove();
254                 }
255             }
256         }
257
258         return newResults;
259     }
260     
261     private Date parseDate(String JavaDoc s) {
262         if (s == null) return null;
263         for (int i = 0; i < dateFormats.length; i++) {
264             DateFormat JavaDoc dateformat = dateFormats[i];
265             try {
266                 return dateformat.parse(s);
267             } catch (ParseException JavaDoc e) {
268                 // try the next one
269
}
270         }
271         return null;
272     }
273
274 }
275
Popular Tags