001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.admin.model;
007:
008: import java.util.*;
009: import java.net.*;
010:
011: import com.iplanet.jato.model.*;
012: import com.iplanet.jato.view.html.OptionList;
013: import com.sun.portal.search.admin.CSConfig;
014: import com.sun.portal.search.robot.FilterRuleset;
015:
016: /**
017: * This class supports the MenuListView by providing the data in the
018: * format expected by the view.
019: */
020: public class StartingGroupListModel extends DefaultModel implements
021: DatasetModel, RetrievingModel, UpdatingModel, InsertingModel,
022: DeletingModel {
023: private String rulesetID = null;
024: private int delIndex = -1;
025: public static final String FIELD_URL = "URL";
026: public static final String FIELD_DEPTH = "Depth";
027: public static final String FIELD_DELETE = "Delete";
028:
029: /**
030: * @param req The HttpServletRequest object passed to the super class.
031: * @param rbName The name of the resource bundle.
032: */
033: public StartingGroupListModel() {
034: super ();
035: }
036:
037: public void setID(String id) {
038: rulesetID = id;
039: }
040:
041: public void setDelIndex(int index) {
042: delIndex = index;
043: }
044:
045: /*
046: * when tiledView has an checkBox field, it will leave a row with null in model
047: * after deleted all row.
048: *
049: */
050: private void removeNullList() throws ModelControlException {
051: if (getSize() == 1 && getValue(this .FIELD_URL) == null) {
052: this .getCurrentContext().getValueList().clear();
053: }
054: }
055:
056: // Model execution methods
057: ////////////////////////////////////////////////////////////////////////////////
058:
059: /**
060: *
061: *
062: */
063: public Object execute(ModelExecutionContext context)
064: throws ModelControlException {
065: String operationName = null;
066: if (context != null)
067: operationName = context.getOperationName();
068: else
069: operationName = ModelExecutionContext.OPERATION_RETRIEVE;
070:
071: Object result = null;
072: if (operationName
073: .equals(ModelExecutionContext.OPERATION_RETRIEVE)) {
074: result = retrieve(context);
075: } else if (operationName
076: .equals(ModelExecutionContext.OPERATION_INSERT)) {
077: result = insert(context);
078: } else if (operationName
079: .equals(ModelExecutionContext.OPERATION_UPDATE)) {
080: result = update(context);
081: } else if (operationName
082: .equals(ModelExecutionContext.OPERATION_DELETE)) {
083: result = delete(context);
084: }
085:
086: return result;
087: }
088:
089: /**
090: *
091: *
092: */
093: public Object update(ModelExecutionContext context)
094: throws ModelControlException {
095: removeNullList();
096: beforeFirst();
097: FilterRuleset frs = null;
098: if (rulesetID != null) {
099: frs = CSConfig.getRobotConfig().GetRuleSet(rulesetID);
100: }
101: if (frs != null) {
102: frs.removeStartingPoints();
103: while (this .next()) {
104: /*String isDelete = (String) getValue(this.FIELD_DELETE);
105: if (isDelete != null && isDelete.compareTo("true")==0) {
106: continue;
107: }*/
108: String url = (String) getValue(FIELD_URL);
109: String depth = (String) getValue(FIELD_DEPTH);
110: if (url != null && depth != null
111: && url.trim().length() > 0) {
112: try {
113: URL newUrl = new URL(url);
114: frs.addStartingPoint(newUrl, Integer
115: .parseInt(depth));
116: } catch (Exception e) {
117: }
118:
119: }
120: }
121: }
122: return null;
123: }
124:
125: /**
126: *
127: *
128: */
129: public Object insert(ModelExecutionContext context)
130: throws ModelControlException {
131: removeNullList();
132: this .appendRow();
133: String defaultDepth = (String) CSConfig.getRobotConfig().processConf
134: .get("depth");
135: setValue(this .FIELD_DEPTH, defaultDepth == null ? "-1"
136: : defaultDepth);
137: return null;
138: }
139:
140: /**
141: *
142: *
143: */
144: public Object delete(ModelExecutionContext context)
145: throws ModelControlException {
146: removeNullList();
147: if (delIndex != -1) {
148: getCurrentContext().getValueList().remove(delIndex);
149: return null;
150: }
151:
152: ArrayList delObjs = new ArrayList();
153: beforeFirst();
154: while (next()) {
155: String isDelete = (String) getValue(this .FIELD_DELETE);
156: if (isDelete != null && isDelete.compareTo("true") == 0) {
157: int index = getRowIndex();
158: delObjs.add(getCurrentContext().getValueList().get(
159: index));
160: }
161: }
162: for (int i = 0; i < delObjs.size(); i++) {
163: getCurrentContext().getValueList().remove(delObjs.get(i));
164: }
165: return null;
166: }
167:
168: /**
169: *
170: *
171: */
172: public Object retrieve(ModelExecutionContext context)
173: throws ModelControlException {
174: // Perform a dummy retrieve operation. We ignore the
175: // execution context for now.
176: FilterRuleset frs = null;
177: if (rulesetID != null) {
178: frs = CSConfig.getRobotConfig().GetRuleSet(rulesetID);
179: }
180: if (frs != null) {
181: clear();
182: int n = frs.numOfStartingPoints();
183: for (int i = 0; i < n; i++) {
184: String sp = frs.getStartingPointByIndex(i);
185: if (sp != null) {
186: String urlStr = null;
187: String depthStr = null;
188: StringTokenizer st = new StringTokenizer(sp);
189: if (st.hasMoreTokens()) {
190: urlStr = st.nextToken();
191: }
192: if (st.hasMoreTokens()) {
193: String ds = st.nextToken();
194: int ndx = ds.indexOf("=");
195: if (ndx != -1) {
196: depthStr = ds.substring(ndx + 1);
197: }
198: }
199: if (urlStr != null && depthStr != null) {
200: appendRow();
201: setValue(this .FIELD_URL, urlStr);
202: setValue(this .FIELD_DEPTH, depthStr);
203: }
204: }
205: }
206: }
207: beforeFirst();
208:
209: // Nothing useful to return
210: return null;
211: }
212:
213: }
|