|
| 1 | +package hudson.plugins.nested_view.search; |
| 2 | + |
| 3 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 4 | +import hudson.plugins.nested_view.NestedViewsSearch; |
| 5 | +import jenkins.model.Jenkins; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.text.SimpleDateFormat; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.Date; |
| 14 | +import java.util.List; |
| 15 | +import java.util.logging.Level; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +public class HistoryItem { |
| 19 | + |
| 20 | + private static final int MAX_HISTORY = 500; |
| 21 | + private static final File historyCache = new File(Jenkins.get().root, ".nested-view-serch.cache"); |
| 22 | + private static final List<HistoryItem> history = HistoryItem.load(); |
| 23 | + |
| 24 | + private final String query; |
| 25 | + private final int size; |
| 26 | + private final Date date; |
| 27 | + |
| 28 | + @SuppressFBWarnings(value = {"EI_EXPOSE_REP2"}, justification = "date is not cared") |
| 29 | + public HistoryItem(String query, int size, Date date) { |
| 30 | + this.query = query; |
| 31 | + this.size = size; |
| 32 | + this.date = date; |
| 33 | + } |
| 34 | + |
| 35 | + public static void save() { |
| 36 | + try { |
| 37 | + saveImp(); |
| 38 | + } catch (Exception ex) { |
| 39 | + NestedViewsSearch.LOGGER.log(Level.SEVERE, "saving nested view search history failed", ex); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public static void saveImp() throws IOException { |
| 44 | + Files.write(historyCache.toPath(), history.stream().map(a -> a.toSave()).collect(Collectors.toList())); |
| 45 | + } |
| 46 | + |
| 47 | + public static List<HistoryItem> load() { |
| 48 | + List<String> l = new ArrayList<>(0); |
| 49 | + if (historyCache.exists()) try { |
| 50 | + l = Files.readAllLines(historyCache.toPath()); |
| 51 | + } catch (Exception ex) { |
| 52 | + NestedViewsSearch.LOGGER.log(Level.SEVERE, "loading nested view search history failed", ex); |
| 53 | + } |
| 54 | + ArrayList<HistoryItem> r = new ArrayList(MAX_HISTORY); |
| 55 | + for (String s : l) { |
| 56 | + try { |
| 57 | + r.add(new HistoryItem(s)); |
| 58 | + } catch (Exception ex) { |
| 59 | + NestedViewsSearch.LOGGER.log(Level.SEVERE, "reading nested view search history " + s + " failed", ex); |
| 60 | + } |
| 61 | + } |
| 62 | + return Collections.synchronizedList(r); |
| 63 | + } |
| 64 | + |
| 65 | + public static List<HistoryItem> get() { |
| 66 | + return Collections.unmodifiableList(history); |
| 67 | + } |
| 68 | + |
| 69 | + public static void add(HistoryItem his) { |
| 70 | + history.removeAll(Collections.singleton(his)); |
| 71 | + history.add(0, his); |
| 72 | + while (history.size() > MAX_HISTORY) { |
| 73 | + history.remove(history.size() - 1); |
| 74 | + } |
| 75 | + save(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean equals(Object o) { |
| 80 | + if (this == o) return true; |
| 81 | + if (o == null || getClass() != o.getClass()) return false; |
| 82 | + HistoryItem that = (HistoryItem) o; |
| 83 | + return query.equals(that.query); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public int hashCode() { |
| 88 | + return query.hashCode(); |
| 89 | + } |
| 90 | + |
| 91 | + public String getQuery() { |
| 92 | + return query; |
| 93 | + } |
| 94 | + |
| 95 | + public String getUrl() { |
| 96 | + return BuildDetails.getJenkinsUrl() + "/search/?q=" + getQuery(); |
| 97 | + } |
| 98 | + |
| 99 | + public int getSize() { |
| 100 | + return size; |
| 101 | + } |
| 102 | + |
| 103 | + public String getDate() { |
| 104 | + return new SimpleDateFormat("HH:mm:ss dd/MM").format(date); |
| 105 | + } |
| 106 | + |
| 107 | + public String toSave() { |
| 108 | + return size + " " + date.getTime() + " " + query; |
| 109 | + } |
| 110 | + |
| 111 | + public HistoryItem(String saved) { |
| 112 | + String[] sd = saved.split(" "); |
| 113 | + size = Integer.parseInt(sd[0]); |
| 114 | + date = new Date(Long.parseLong(sd[1])); |
| 115 | + query = saved.replace(size + " " + date.getTime() + " ", ""); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments