001: /**
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.admin.console.search;
013:
014: import java.util.*;
015: import java.util.logging.Level;
016: import java.io.*;
017:
018: import javax.faces.context.FacesContext;
019: import javax.faces.el.ValueBinding;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.management.*;
022:
023: import com.sun.web.ui.component.*;
024: import com.sun.web.ui.model.*;
025: import com.sun.web.ui.event.*;
026:
027: import com.sun.data.provider.*;
028: import com.sun.data.provider.impl.ObjectListDataProvider;
029:
030: import com.sun.cacao.agent.JmxClient;
031:
032: import com.sun.portal.admin.common.util.AdminClientUtil;
033: import com.sun.portal.admin.console.common.PSBaseBean;
034:
035: public class EditRobotFilterBean extends PSBaseBean {
036:
037: private String filterName = "No Name";
038: private String filterDesc = "";
039: private String newSiteDefault = "true";
040: private String documentMatch = "exclude";
041: private DataProvider rules = null;
042:
043: private TableRowGroup tableRowGroup = null;
044: private TableSelectPhaseListener tspl = null;
045:
046: private Option[] documentMatchOption;
047: private Option[] sourceOption;
048: private Option[] methodOption;
049: private Option[] patternOption;
050:
051: private String filterId = "";
052:
053: public EditRobotFilterBean() {
054: tspl = new TableSelectPhaseListener();
055: }
056:
057: /*
058: * Button Actions
059: */
060: public String save() {
061: try {
062: LinkedList path = new LinkedList();
063: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
064: path
065: .addFirst((String) getSessionAttribute("search.server.selected"));
066: path.addFirst("robot");
067: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
068: AdminClientUtil.SEARCH_ROBOT_MBEAN_TYPE, path);
069:
070: Boolean include, isdefault;
071: if (documentMatch.equalsIgnoreCase("exclude"))
072: include = Boolean.FALSE;
073: else
074: include = Boolean.TRUE;
075:
076: if (newSiteDefault.equalsIgnoreCase("true"))
077: isdefault = Boolean.TRUE;
078: else
079: isdefault = Boolean.FALSE;
080:
081: ArrayList al = new ArrayList();
082: ObjectListDataProvider oldp = (ObjectListDataProvider) rules;
083: oldp.commitChanges();
084: List l = oldp.getList();
085: for (int i = 0; i < l.size(); i++) {
086: FilterMatchRuleBean fmrb = (FilterMatchRuleBean) l
087: .get(i);
088: HashMap matchRule = new HashMap();
089: matchRule.put("FilterSource", fmrb.source);
090: matchRule.put("FilterMethod", fmrb.method);
091: matchRule.put("FilterString", fmrb.pattern);
092: al.add(matchRule);
093: }
094:
095: Object[] params = { filterId, filterName, filterDesc,
096: isdefault, include, al };
097: String[] signature = { "java.lang.String",
098: "java.lang.String", "java.lang.String",
099: "java.lang.Boolean", "java.lang.Boolean",
100: "java.util.ArrayList" };
101: getMBeanServerConnection().invoke(on, "editFilter", params,
102: signature);
103: } catch (Exception e) {
104: log(Level.SEVERE,
105: "EditRobotFilterBean.save() failed to edit the robot filter Id "
106: + filterId, e);
107: }
108:
109: filterId = "";
110: tspl.clear();
111: return "gotoRobotFiltersHome";
112: }
113:
114: public String cancel() {
115: filterId = "";
116: tspl.clear();
117: return "gotoRobotFiltersHome";
118: }
119:
120: public String create() { //create a row to the filter rule table
121: ObjectListDataProvider oldp = (ObjectListDataProvider) rules;
122: oldp.addObject(new FilterMatchRuleBean("url", "is", ""));
123: oldp.commitChanges();
124: return "gotoEditRobotFilter";
125: }
126:
127: public String delete() { //delete a row from the filter rule table
128: ObjectListDataProvider oldp = (ObjectListDataProvider) rules;
129:
130: RowKey[] rowKeys = tableRowGroup.getRenderedRowKeys();
131: for (int index = 0; index < rowKeys.length; index++) {
132: RowKey rowKey = rowKeys[index];
133: if (tspl.isSelected(rowKey)) {
134: oldp.removeRow(rowKey);
135: }
136: }
137:
138: oldp.commitChanges();
139: tspl.clear();
140: return "gotoEditRobotFilter";
141: }
142:
143: /*
144: * Methods for UI options
145: */
146: public Option[] getDocumentMatchOption() {
147: documentMatchOption = new Option[2];
148: documentMatchOption[0] = new Option("exclude",
149: getLocalizedString("robot.filter.exclude"));
150: documentMatchOption[1] = new Option("include",
151: getLocalizedString("robot.filter.include"));
152: return documentMatchOption;
153: }
154:
155: public Option[] getSourceOption() {
156: sourceOption = new Option[5];
157: ArrayList sources = getStringList(getLocalizedString("robot.filter.source.listlabel"));
158: sourceOption[0] = new Option("url", (String) sources.get(0));
159: sourceOption[1] = new Option("protocol", (String) sources
160: .get(1));
161: sourceOption[2] = new Option("host", (String) sources.get(2));
162: sourceOption[3] = new Option("path", (String) sources.get(3));
163: sourceOption[4] = new Option("MIME", (String) sources.get(4));
164: return sourceOption;
165: }
166:
167: public Option[] getMethodOption() {
168: methodOption = new Option[5];
169: ArrayList methods = getStringList(getLocalizedString("robot.filter.method.listlabel"));
170: methodOption[0] = new Option("is", (String) methods.get(0));
171: methodOption[1] = new Option("contains", (String) methods
172: .get(1));
173: methodOption[2] = new Option("begins with", (String) methods
174: .get(2));
175: methodOption[3] = new Option("ends with", (String) methods
176: .get(3));
177: methodOption[4] = new Option("regular", (String) methods.get(4));
178: return methodOption;
179: }
180:
181: /*
182: * Methods to Get/Set Bean elements
183: */
184: public String getFilterName() {
185: if (!filterId
186: .equals((String) getSessionAttribute("robot.filter.selected"))) {
187: generateRobotFilter();
188: tspl.clear();
189: }
190: return filterName;
191: }
192:
193: public void setFilterName(String s) {
194: this .filterName = s;
195: }
196:
197: public String getFilterDesc() {
198: if (!filterId
199: .equals((String) getSessionAttribute("robot.filter.selected"))) {
200: generateRobotFilter();
201: tspl.clear();
202: }
203: return filterDesc;
204: }
205:
206: public void setFilterDesc(String s) {
207: this .filterDesc = s;
208: }
209:
210: public String getNewSiteDefault() {
211: if (!filterId
212: .equals((String) getSessionAttribute("robot.filter.selected"))) {
213: generateRobotFilter();
214: tspl.clear();
215: }
216: return newSiteDefault;
217: }
218:
219: public void setNewSiteDefault(String s) {
220: if (s == null)
221: this .newSiteDefault = "false";
222: else
223: this .newSiteDefault = "true";
224: }
225:
226: public String getDocumentMatch() {
227: if (!filterId
228: .equals((String) getSessionAttribute("robot.filter.selected"))) {
229: generateRobotFilter();
230: tspl.clear();
231: }
232: return documentMatch;
233: }
234:
235: public void setDocumentMatch(String s) {
236: this .documentMatch = s;
237: }
238:
239: public DataProvider getRules() {
240: if (!filterId
241: .equals((String) getSessionAttribute("robot.filter.selected"))) {
242: generateRobotFilter();
243: tspl.clear();
244: }
245: return rules;
246: }
247:
248: public void setRules(DataProvider rules) {
249: this .rules = rules;
250: }
251:
252: public TableRowGroup getTableRowGroup() {
253: return tableRowGroup;
254: }
255:
256: public void setTableRowGroup(TableRowGroup tableRowGroup) {
257: this .tableRowGroup = tableRowGroup;
258: }
259:
260: public Object getSelected() {
261: return tspl.getSelected(getTableRow());
262: }
263:
264: public void setSelected(Object object) {
265: tspl.setSelected(getTableRow(), object);
266: }
267:
268: /*
269: * Private Methods
270: */
271: private Option[] getOptionList(ArrayList values, ArrayList labels) {
272:
273: if (!values.isEmpty() && !labels.isEmpty()) {
274:
275: Option[] options = new Option[values.size()];
276:
277: for (int i = 0; i < values.size(); i++) {
278: options[i] = new Option((String) values.get(i),
279: (String) labels.get(i));
280: }
281:
282: return options;
283: }
284: return null;
285: }
286:
287: private ArrayList getStringList(String s) {
288:
289: ArrayList s_list = new ArrayList();
290: StringTokenizer st = new StringTokenizer(s, ",");
291: while (st.hasMoreTokens()) {
292: String label = st.nextToken();
293: s_list.add(label);
294: }
295: return s_list;
296: }
297:
298: private String getLocalizedString(String key) {
299: return super .getLocalizedString("search", key);
300: }
301:
302: private RowKey getTableRow() {
303: FacesContext context = FacesContext.getCurrentInstance();
304: ValueBinding vb = context.getApplication().createValueBinding(
305: "#{filterMatchRule.tableRow}");
306: return (RowKey) vb.getValue(context);
307: }
308:
309: private void generateRobotFilter() {
310: filterId = (String) getSessionAttribute("robot.filter.selected");
311:
312: ArrayList filterlist = null;
313: if (filterId != null && !filterId.equals("")) {
314: try {
315: LinkedList path = new LinkedList();
316: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
317: path
318: .addFirst((String) getSessionAttribute("search.server.selected"));
319: path.addFirst("robot");
320: ObjectName on = AdminClientUtil
321: .getResourceMBeanObjectName(
322: AdminClientUtil.SEARCH_ROBOT_MBEAN_TYPE,
323: path);
324:
325: Object[] params = { filterId };
326: String[] signature = { "java.lang.String" };
327:
328: filterlist = (ArrayList) getMBeanServerConnection()
329: .invoke(on, "listFilters", params, signature);
330:
331: } catch (Exception e) {
332: log(Level.SEVERE,
333: "EditRobotFilterBean.generateRobotFilter() failed to get robot Filter Id "
334: + filterId, e);
335: }
336:
337: ArrayList al = new ArrayList();
338: String source, method, pattern;
339:
340: if (filterlist != null) {
341:
342: HashMap fr = (HashMap) filterlist.get(0); //the only one
343:
344: if (fr != null) {
345:
346: filterName = (String) fr.get("FilterName");
347: if (filterName.startsWith("filter.default")) {
348: filterName = getLocalizedString((String) fr
349: .get("FilterName"));
350: }
351:
352: filterDesc = (String) fr.get("FilterDesc");
353:
354: if (((Boolean) fr.get("NewSite")).booleanValue()) {
355: newSiteDefault = "true";
356: } else {
357: newSiteDefault = "false";
358: }
359:
360: if (((Boolean) fr.get("Include")).booleanValue()) {
361: documentMatch = "include";
362: } else {
363: documentMatch = "exclude";
364: }
365:
366: ArrayList defs = (ArrayList) fr.get("FilterDef");
367: for (int i = 0; i < defs.size(); i++) {
368: HashMap def = (HashMap) defs.get(i);
369: source = (String) def.get("FilterSource");
370: method = (String) def.get("FilterMethod");
371: pattern = (String) def.get("FilterString");
372: al.add(new FilterMatchRuleBean(source, method,
373: pattern));
374: }
375: rules = new ObjectListDataProvider(al);
376: } // fr != null
377:
378: } // filterlist != null
379:
380: }
381:
382: }
383:
384: }
|