source: trunk/src/org/expeditee/agents/SearchAgent.java@ 1510

Last change on this file since 1510 was 1304, checked in by bln4, 5 years ago

MailBay and MessageBay instances are now encrypted with ProfileEncryption

File size: 5.5 KB
RevLine 
[919]1/**
2 * SearchAgent.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
[134]19package org.expeditee.agents;
20
[376]21import java.util.Collection;
22
[1102]23import org.expeditee.gui.DisplayController;
[134]24import org.expeditee.gui.Frame;
25import org.expeditee.gui.FrameCreator;
26import org.expeditee.gui.FrameIO;
27import org.expeditee.io.Conversion;
28import org.expeditee.items.Item;
29import org.expeditee.items.Text;
30
31public abstract class SearchAgent extends DefaultAgent {
32
[162]33 private static final String DEFAULT_RESULTS_FRAMESET = "SearchResults";
[294]34
[362]35 public static final int SURROGATE_LENGTH = 50;
36
[134]37 protected FrameCreator _results;
38
39 protected String _pattern;
40
41 protected String _replacementString;
[294]42
[134]43 protected String _startName;
44
[291]45 public SearchAgent(String searchText) {
[376]46 if (searchText != null)
47 _pattern = searchText.toLowerCase();
[291]48 }
[294]49
[134]50 @Override
51 public boolean initialise(Frame frame, Item item) {
[156]52 String pattern = item.getText();
[134]53 String resultFrameset = null;
[294]54
55 // TODO use a results frame specified on the profile frame
[134]56 if (item.getLink() == null) {
[162]57 resultFrameset = DEFAULT_RESULTS_FRAMESET;
[134]58 } else {
59 resultFrameset = Conversion.getFramesetName(item.getAbsoluteLink(),
60 false);
61 }
[376]62 return initialise(frame, item, getSearchDescription(frame), resultFrameset,
[294]63 null, pattern);
[134]64 }
65
[294]66 /**
[376]67 * @param frame
68 * @return
69 */
70 protected String getSearchDescription(Frame frame) {
71 return frame.getFramesetName();
72 }
73
74 /**
[294]75 *
76 * @param frame
77 * @param item
78 * @param startName
79 * @param resultsFrameset
80 * @param replacementString
81 * @param pattern
82 * is ignored if the pattern has already been set earlier.
83 * @return
84 */
85 public boolean initialise(Frame frame, Item item, String startName,
86 String resultsFrameset, String replacementString, String pattern) {
[376]87 // TODO: Put the init params in the constructor!! Dont want to be
88 // setting _pattern in two places!
89
[294]90 if (_pattern == null)
91 _pattern = pattern.toLowerCase();
[134]92 _replacementString = replacementString;
93 _startName = startName;
94
95 // Create a frame to put the results on with the search query
96 // and type as the title
[294]97 String title = this.getClass().getSimpleName() + " [" + startName
[376]98 + "]"+getResultsTitleSuffix();
99 _results = new FrameCreator(resultsFrameset, FrameIO.FRAME_PATH, title,
[1304]100 FrameCreator.ExistingFramesetOptions.AppendSegregatedFrames, true, null);
[134]101 // Set the frame to be displayed after running the agent
102 _end = _results.getFirstFrame();
[294]103
[156]104 return super.initialise(frame, item);
[134]105 }
[294]106
[376]107 protected String getResultsTitleSuffix() {
108 return " [" + _pattern + "]";
109 }
110
[134]111 public String getResultsFrameName() {
112 return _results.getName();
113 }
[294]114
[134]115 public static boolean searchItem(Text itemToSearch, String pattern,
116 String replacementString) {
[143]117 String searchStr = itemToSearch.getText().toLowerCase();
[176]118 boolean bFound = searchStr.contains(pattern.toLowerCase());
[134]119 // If it is a find and replace... then replace with the replacement
120 // string
121 if (bFound && replacementString != null) {
122 itemToSearch.setText(searchStr.replaceAll(pattern,
123 replacementString));
124 }
125 return bFound;
126 }
127
[1102]128 public static boolean searchFrame(FrameCreator results, String frameName, String pattern, String replacementString)
129 {
130 boolean wasXrayMode = DisplayController.isXRayMode();
131 if (!wasXrayMode) DisplayController.ToggleXRayMode();
[134]132 Frame frameToSearch = FrameIO.LoadFrame(frameName);
[1102]133 if (!wasXrayMode) DisplayController.ToggleXRayMode();
[134]134 if (frameToSearch == null)
135 return false;
[156]136 for (Text itemToSearch : frameToSearch.getTextItems()) {
[134]137 // Search for the item and add it to the results page if
138 // it is found
[294]139 if (searchItem(itemToSearch, pattern, replacementString)) {
[134]140 // Add a linked item to the results frame
141 results.addText(frameName, null, frameName, null, false);
142 }
143 }
[1102]144 DisplayController.requestRefresh(true);
[134]145 FrameIO.SaveFrame(frameToSearch, false);
146 return true;
147 }
[376]148
149 protected int addResults(String frameName, Collection<String> found) {
150 return addResults(frameName, frameName, found);
151 }
152
153 /**
154 * @param frameNumber
155 * @param frameName
156 * @param found
157 * @return
158 */
159 protected int addResults(String frameNumber, String frameName, Collection<String> found) {
160 int size = found == null ? 0 : found.size();
161 // If the frame exists
162 if (found != null)
163 _frameCount++;
164 if (size > 0) {
165 // String repeats = size > 1? ("("+ size+ ")") : "";
166 for (String s : found) {
167 StringBuffer surrogate = new StringBuffer();
168 surrogate.append("[").append(frameNumber).append("] ");
169 if (s.length() > SearchAgent.SURROGATE_LENGTH)
170 surrogate.append(
171 s.substring(0, SearchAgent.SURROGATE_LENGTH - 3))
172 .append("...");
173 else {
174 surrogate.append(s);
175 }
176
177 _results.addText(surrogate.toString(), null,
178 frameName, null, false);
[1102]179 DisplayController.requestRefresh(true);
[376]180 }
181 }
182 return size;
183 }
[134]184}
Note: See TracBrowser for help on using the repository browser.