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 CreateRobotFilterBean extends PSBaseBean implements
036: WizardEventListener {
037:
038: public String filterName = "";
039: public String filterDesc = "";
040: public String newSiteDefault = "true";
041: public String documentMatch = "exclude";
042:
043: private DataProvider rules = null;
044: private TableRowGroup tableRowGroup = null;
045: private TableSelectPhaseListener tspl = null;
046:
047: public Option[] documentMatchOption;
048: public Option[] sourceOption;
049: public Option[] methodOption;
050: public Option[] patternOption;
051:
052: public boolean handleEvent(WizardEvent event) {
053: int eventCode = event.getNavigationEvent();
054: if (eventCode == WizardEvent.NEXT) {
055: ObjectListDataProvider oldp = (ObjectListDataProvider) rules;
056: oldp.commitChanges();
057: } else {
058: if (eventCode == WizardEvent.FINISH) {
059: createRobotFilter();
060: filterName = "";
061: filterDesc = "";
062: newSiteDefault = "true";
063: documentMatch = "exclude";
064: rules = null;
065: }
066: if (eventCode == WizardEvent.CANCEL) {
067: filterName = "";
068: filterDesc = "";
069: newSiteDefault = "true";
070: documentMatch = "exclude";
071: rules = null;
072: }
073: }
074: return true;
075: }
076:
077: public boolean isTransient() {
078: return false;
079: }
080:
081: public void setTransient(boolean flag) {
082: }
083:
084: public Object saveState(FacesContext context) {
085: return null;
086: }
087:
088: public void restoreState(FacesContext context, Object state) {
089: }
090:
091: /*
092: * Button Actions
093: */
094: public String create() { //create a row to the filter rule table
095: ObjectListDataProvider oldp = (ObjectListDataProvider) rules;
096: oldp.addObject(new FilterMatchRuleBean("url", "is", ""));
097: oldp.commitChanges();
098: return null;
099: }
100:
101: public String delete() { //delete a row from the filter rule table
102: ObjectListDataProvider oldp = (ObjectListDataProvider) rules;
103:
104: RowKey[] rowKeys = tableRowGroup.getRenderedRowKeys();
105: for (int index = 0; index < rowKeys.length; index++) {
106: RowKey rowKey = rowKeys[index];
107: if (tspl.isSelected(rowKey)) {
108: oldp.removeRow(rowKey);
109: }
110: }
111:
112: oldp.commitChanges();
113: tspl.clear();
114: return null;
115: }
116:
117: /*
118: * Methods for UI options
119: */
120: public Option[] getDocumentMatchOption() {
121: documentMatchOption = new Option[2];
122: documentMatchOption[0] = new Option("exclude",
123: getLocalizedString("robot.filter.exclude"));
124: documentMatchOption[1] = new Option("include",
125: getLocalizedString("robot.filter.include"));
126: return documentMatchOption;
127: }
128:
129: public Option[] getSourceOption() {
130: sourceOption = new Option[5];
131: ArrayList sources = getStringList(getLocalizedString("robot.filter.source.listlabel"));
132: sourceOption[0] = new Option("url", (String) sources.get(0));
133: sourceOption[1] = new Option("protocol", (String) sources
134: .get(1));
135: sourceOption[2] = new Option("host", (String) sources.get(2));
136: sourceOption[3] = new Option("path", (String) sources.get(3));
137: sourceOption[4] = new Option("MIME", (String) sources.get(4));
138: return sourceOption;
139: }
140:
141: public Option[] getMethodOption() {
142: methodOption = new Option[5];
143: ArrayList methods = getStringList(getLocalizedString("robot.filter.method.listlabel"));
144: methodOption[0] = new Option("is", (String) methods.get(0));
145: methodOption[1] = new Option("contains", (String) methods
146: .get(1));
147: methodOption[2] = new Option("begins with", (String) methods
148: .get(2));
149: methodOption[3] = new Option("ends with", (String) methods
150: .get(3));
151: methodOption[4] = new Option("regular", (String) methods.get(4));
152: return methodOption;
153: }
154:
155: /*
156: * Methods to Get/Set Bean elements
157: */
158: public String getFilterName() {
159: return filterName;
160: }
161:
162: public void setFilterName(String s) {
163: this .filterName = s;
164: }
165:
166: public String getFilterDesc() {
167: return filterDesc;
168: }
169:
170: public void setFilterDesc(String s) {
171: this .filterDesc = s;
172: }
173:
174: public String getNewSiteDefault() {
175: return newSiteDefault;
176: }
177:
178: public void setNewSiteDefault(String s) {
179: if (s == null)
180: this .newSiteDefault = "false";
181: else
182: this .newSiteDefault = "true";
183: }
184:
185: public String getDocumentMatch() {
186: return documentMatch;
187: }
188:
189: public void setDocumentMatch(String s) {
190: this .documentMatch = s;
191: }
192:
193: public DataProvider getRules() {
194: if (rules == null) {
195: ArrayList al = new ArrayList();
196: FilterMatchRuleBean fb = new FilterMatchRuleBean("url",
197: "is", "");
198: al.add(fb);
199: rules = new ObjectListDataProvider(al);
200: }
201: if (tspl == null) {
202: tspl = new TableSelectPhaseListener();
203: }
204: return rules;
205: }
206:
207: public void setRules(DataProvider rules) {
208: this .rules = rules;
209: }
210:
211: public TableRowGroup getTableRowGroup() {
212: return tableRowGroup;
213: }
214:
215: public void setTableRowGroup(TableRowGroup tableRowGroup) {
216: this .tableRowGroup = tableRowGroup;
217: }
218:
219: public Object getSelected() {
220: return tspl.getSelected(getTableRow());
221: }
222:
223: public void setSelected(Object object) {
224: tspl.setSelected(getTableRow(), object);
225: }
226:
227: /*
228: * Private Methods
229: */
230: private Option[] getOptionList(ArrayList values, ArrayList labels) {
231:
232: if (!values.isEmpty() && !labels.isEmpty()) {
233:
234: Option[] options = new Option[values.size()];
235:
236: for (int i = 0; i < values.size(); i++) {
237: options[i] = new Option((String) values.get(i),
238: (String) labels.get(i));
239: }
240:
241: return options;
242: }
243: return null;
244: }
245:
246: private ArrayList getStringList(String s) {
247:
248: ArrayList s_list = new ArrayList();
249: StringTokenizer st = new StringTokenizer(s, ",");
250: while (st.hasMoreTokens()) {
251: String label = st.nextToken();
252: s_list.add(label);
253: }
254: return s_list;
255: }
256:
257: private String getLocalizedString(String key) {
258: return super .getLocalizedString("search", key);
259: }
260:
261: private RowKey getTableRow() {
262: FacesContext context = FacesContext.getCurrentInstance();
263: ValueBinding vb = context.getApplication().createValueBinding(
264: "#{filterMatchRule.tableRow}");
265: return (RowKey) vb.getValue(context);
266: }
267:
268: private void createRobotFilter() {
269: try {
270: LinkedList path = new LinkedList();
271: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
272: path
273: .addFirst((String) getSessionAttribute("search.server.selected"));
274: path.addFirst("robot");
275: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
276: AdminClientUtil.SEARCH_ROBOT_MBEAN_TYPE, path);
277:
278: Boolean include, isdefault;
279: if (documentMatch.equalsIgnoreCase("exclude"))
280: include = Boolean.FALSE;
281: else
282: include = Boolean.TRUE;
283:
284: if (newSiteDefault.equalsIgnoreCase("true"))
285: isdefault = Boolean.TRUE;
286: else
287: isdefault = Boolean.FALSE;
288:
289: ArrayList al = new ArrayList();
290: ObjectListDataProvider oldp = (ObjectListDataProvider) rules;
291: oldp.commitChanges();
292: List l = oldp.getList();
293: for (int i = 0; i < l.size(); i++) {
294: FilterMatchRuleBean fmrb = (FilterMatchRuleBean) l
295: .get(i);
296: HashMap matchRule = new HashMap();
297: matchRule.put("FilterSource", fmrb.source);
298: matchRule.put("FilterMethod", fmrb.method);
299: matchRule.put("FilterString", fmrb.pattern);
300: al.add(matchRule);
301: }
302:
303: Object[] params = { filterName, filterDesc, isdefault,
304: include, al };
305: String[] signature = { "java.lang.String",
306: "java.lang.String", "java.lang.Boolean",
307: "java.lang.Boolean", "java.util.ArrayList" };
308: String filterId = (String) getMBeanServerConnection()
309: .invoke(on, "createFilter", params, signature);
310:
311: } catch (Exception e) {
312: log(
313: Level.SEVERE,
314: "CreateRobotFilterBean.createRobotFilter() failed to create a robot filter",
315: e);
316: }
317: }
318:
319: }
|