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.lang.*;
009: import java.util.Map;
010: import java.util.Set;
011: import java.util.Collections;
012: import java.util.List;
013: import java.util.ArrayList;
014: import java.util.logging.Logger;
015: import java.util.logging.Level;
016: import java.net.*;
017: import javax.servlet.http.HttpServletRequest;
018:
019: import com.iplanet.jato.model.*;
020: import com.iplanet.jato.view.html.OptionList;
021: import com.sun.portal.search.admin.CSConfig;
022: import com.sun.portal.search.robot.FilterRuleset;
023: import com.sun.portal.log.common.PortalLogger;
024:
025: /**
026: * This class supports the MenuListView by providing the data in the
027: * format expected by the view.
028: */
029: public class ServerGroupListModel extends DefaultModel implements
030: DatasetModel, RetrievingModel, UpdatingModel, InsertingModel,
031: DeletingModel {
032: private String rulesetID = null;
033: private int delIndex = -1;
034: public static final String FIELD_HOST = "Host";
035: public static final String FIELD_VERIFIED_TEXT = "VerifiedText";
036: public static final String FIELD_PROTOCOL = "Protocol";
037: public static final String FIELD_PORT = "Port";
038: public static final String FIELD_DELETE = "Delete";
039:
040: // Create a Logger for this class
041: private static Logger debugLogger = PortalLogger
042: .getLogger(ServerGroupListModel.class);
043:
044: /**
045: * @param req The HttpServletRequest object passed to the super class.
046: * @param rbName The name of the resource bundle.
047: */
048: public ServerGroupListModel() {
049: super ();
050: }
051:
052: public void setID(String id) {
053: this .rulesetID = id;
054: }
055:
056: public void setDelIndex(int index) {
057: delIndex = index;
058: }
059:
060: // Model execution methods
061: ////////////////////////////////////////////////////////////////////////////////
062:
063: /**
064: *
065: *
066: */
067: public Object execute(ModelExecutionContext context)
068: throws ModelControlException {
069: String operationName = null;
070: if (context != null)
071: operationName = context.getOperationName();
072: else
073: operationName = ModelExecutionContext.OPERATION_RETRIEVE;
074:
075: Object result = null;
076: if (operationName
077: .equals(ModelExecutionContext.OPERATION_RETRIEVE)) {
078: result = retrieve(context);
079: } else if (operationName
080: .equals(ModelExecutionContext.OPERATION_INSERT)) {
081: result = insert(context);
082: } else if (operationName
083: .equals(ModelExecutionContext.OPERATION_UPDATE)) {
084: result = update(context);
085: } else if (operationName
086: .equals(ModelExecutionContext.OPERATION_DELETE)) {
087: result = delete(context);
088: }
089:
090: return result;
091: }
092:
093: /**
094: *
095: *
096: */
097: public Object update(ModelExecutionContext context)
098: throws ModelControlException {
099: removeNullList();
100: beforeFirst();
101: FilterRuleset frs = null;
102: if (rulesetID != null) {
103: frs = CSConfig.getRobotConfig().GetRuleSet(rulesetID);
104: }
105: if (frs != null) {
106: frs.removeServers();
107: while (this .next()) {
108: //String isDelete = (String) getValue(this.FIELD_DELETE);
109: //if (isDelete != null && isDelete.compareTo("true")==0) {
110: // continue;
111: //}
112: String host = (String) getValue(FIELD_HOST);
113: String protocol = (String) getValue(FIELD_PROTOCOL);
114: String port = (String) getValue(FIELD_PORT);
115: if (host != null && protocol != null && port != null
116: && host.trim().length() > 0) {
117: try {
118: int portno = -1;
119: try {
120: if (!protocol.equalsIgnoreCase("file")) {
121: portno = Integer.parseInt(port);
122: }
123: } catch (Exception e) {
124: portno = -1;
125: }
126: URL url = new URL(protocol, host, portno, "/");
127: frs.addServer(url.toString());
128: } catch (java.net.MalformedURLException e) {
129: debugLogger.log(Level.INFO, "PSSH_CSPSAM0052",
130: new String[] { host, protocol, port });
131: }
132: }
133: }
134: }
135: return null;
136: }
137:
138: /*
139: * when tiledView has an checkBox field, it will leave a row with null in model
140: * after deleted all row.
141: *
142: */
143: private void removeNullList() throws ModelControlException {
144: if (getSize() == 1 && getValue(this .FIELD_HOST) == null) {
145: this .getCurrentContext().getValueList().clear();
146: }
147: }
148:
149: /**
150: *
151: *
152: */
153: public Object insert(ModelExecutionContext context)
154: throws ModelControlException {
155: removeNullList();
156: this .appendRow();
157: setValue(FIELD_PROTOCOL, FilterRuleset.ProtocolTypes[0]);
158: setValue(FIELD_PORT, "");
159: return null;
160: }
161:
162: /**
163: *
164: *
165: */
166: public Object delete(ModelExecutionContext context)
167: throws ModelControlException {
168: removeNullList();
169: if (delIndex != -1) {
170: getCurrentContext().getValueList().remove(delIndex);
171: return null;
172: }
173:
174: ArrayList delObjs = new ArrayList();
175: beforeFirst();
176: while (next()) {
177: String isDelete = (String) getValue(this .FIELD_DELETE);
178: if (isDelete != null && isDelete.compareTo("true") == 0) {
179: int index = getRowIndex();
180: delObjs.add(getCurrentContext().getValueList().get(
181: index));
182: }
183: }
184: for (int i = 0; i < delObjs.size(); i++) {
185: getCurrentContext().getValueList().remove(delObjs.get(i));
186: }
187: return null;
188: }
189:
190: /**
191: *
192: *
193: */
194: public Object retrieve(ModelExecutionContext context)
195: throws ModelControlException {
196: // Perform a dummy retrieve operation. We ignore the
197: // execution context for now.
198: FilterRuleset frs = null;
199: if (rulesetID != null) {
200: frs = CSConfig.getRobotConfig().GetRuleSet(rulesetID);
201: }
202: if (frs != null) {
203: clear();
204: int n = frs.numOfServers();
205: for (int i = 0; i < n; i++) {
206: String server = frs.getServerByIndex(i);
207: if (server != null) {
208: try {
209: URL url = new URL(server);
210: appendRow();
211: setValue(FIELD_HOST, url.getHost());
212: // XXX need a function to find the cname here
213: setValue(FIELD_VERIFIED_TEXT, url.getHost());
214: setValue(FIELD_PROTOCOL, url.getProtocol());
215: if (!url.getProtocol().equalsIgnoreCase("file")
216: && url.getPort() > 0) {
217:
218: setValue(FIELD_PORT, Integer.toString(url
219: .getPort()));
220: } else {
221: setValue(FIELD_PORT, "");
222: }
223: setValue(FIELD_DELETE, "false");
224: } catch (java.net.MalformedURLException e) {
225: debugLogger.log(Level.INFO, "PSSH_CSPSAM0053",
226: server);
227: }
228: }
229: }
230: }
231: beforeFirst();
232:
233: // Nothing useful to return
234: return null;
235: }
236:
237: }
|