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.*;
010: import java.util.logging.Logger;
011: import java.util.logging.Level;
012: import javax.servlet.http.HttpServletRequest;
013:
014: import com.iplanet.jato.model.*;
015: import com.sun.portal.search.admin.CSConfig;
016: import com.sun.portal.search.admin.resources.SearchResource;
017: import com.sun.portal.search.robot.*;
018: import com.sun.portal.log.common.PortalLogger;
019:
020: /**
021: * This class supports the MenuListView by providing the data in the
022: * format expected by the view.
023: */
024: public class FiltersListModel extends DefaultModel implements
025: DatasetModel, RetrievingModel, UpdatingModel, DeletingModel {
026: private RobotConfig rc = null;
027: public static final String FIELD_ENABLE = "Enable";
028: public static final String FIELD_ID = "FilterID";
029: public static final String FIELD_NICKNAME = "NickName";
030: public static final String FIELD_DEFAULT = "Default";
031: public static final String FIELD_DELETE = "Delete";
032: public static final String FIELD_USEDINSITES = "UsedInSites";
033: public static final String ENABLE_VALUES[] = { "true", "false" };
034: public Locale userLocale = Locale.getDefault();
035:
036: // Create a Logger for this class
037: private static Logger debugLogger = PortalLogger
038: .getLogger(FiltersListModel.class);
039:
040: /**
041: * @param req The HttpServletRequest object passed to the super class.
042: * @param rbName The name of the resource bundle.
043: */
044: public FiltersListModel() {
045: super ();
046: rc = CSConfig.getRobotConfig();
047:
048: }
049:
050: // Model execution methods
051: ////////////////////////////////////////////////////////////////////////////////
052:
053: /**
054: *
055: *
056: */
057: public Object execute(ModelExecutionContext context)
058: throws ModelControlException {
059: String operationName = null;
060: if (context != null)
061: operationName = context.getOperationName();
062: else
063: operationName = ModelExecutionContext.OPERATION_RETRIEVE;
064:
065: Object result = null;
066: if (operationName
067: .equals(ModelExecutionContext.OPERATION_RETRIEVE)) {
068: result = retrieve(context);
069: } else if (operationName
070: .equals(ModelExecutionContext.OPERATION_UPDATE)) {
071: result = update(context);
072: } else if (operationName
073: .equals(ModelExecutionContext.OPERATION_DELETE)) {
074: result = delete(context);
075: }
076:
077: return result;
078: }
079:
080: /**
081: *
082: *
083: */
084: public Object update(ModelExecutionContext context)
085: throws ModelControlException {
086: beforeFirst();
087: while (next()) {
088: FilterRule fr = rc.GetRuleByIndex(getLocation());
089: if (fr != null) {
090: String enable = (String) getValue(FIELD_ENABLE);
091: fr.enable = (enable.compareTo(ENABLE_VALUES[0]) == 0);
092: }
093: }
094: rc.updateFile();
095: return null;
096: }
097:
098: /**
099: *
100: *
101: */
102: public Object delete(ModelExecutionContext context)
103: throws ModelControlException {
104: beforeFirst();
105: String[] ids = new String[getSize()]; //record ruleset id
106: while (next()) {
107: int index = getLocation();
108: String isDelete = (String) getValue(FIELD_DELETE);
109: ids[index] = null;
110: if (isDelete.compareTo("true") == 0) {
111:
112: FilterRule fr = rc.GetRuleByIndex(index);
113: if (fr != null) {
114: ids[index] = fr.id;
115: }
116: }
117: }
118:
119: for (int i = 0; i < ids.length; i++) {
120: if (ids[i] != null) {
121: rc.delRule(ids[i]);
122: }
123: }
124: rc.updateFile();
125: return null;
126: }
127:
128: /**
129: *
130: *
131: */
132: public Object retrieve(ModelExecutionContext context)
133: throws ModelControlException {
134: clear();
135: int n = rc.numOfRules();
136: for (int i = 0; i < n; i++) {
137: FilterRule fr = rc.GetRuleByIndex(i);
138: if (fr != null) {
139: appendRow();
140: setValue(FIELD_ENABLE, fr.enable ? ENABLE_VALUES[0]
141: : ENABLE_VALUES[1]);
142: setValue(FIELD_ID, fr.id);
143: setValue(this .FIELD_DEFAULT,
144: fr.quickcf ? SearchResource.geti18nString(
145: "filter.quikcf", userLocale) : "");
146: String localizedValue = SearchResource.geti18nString(
147: fr.nickname, userLocale, false);
148: if (localizedValue != null) {
149: setValue(FIELD_NICKNAME, localizedValue);
150: } else {
151: setValue(FIELD_NICKNAME, fr.nickname);
152: }
153: setValue(FIELD_DELETE, "false");
154: String[] ids = rc
155: .getFiletrRulesetIDsForFilterRule(fr.id);
156: if (ids != null) {
157: StringBuffer buf = new StringBuffer();
158: for (int j = 0; j < ids.length; j++) {
159: FilterRuleset frs = rc.GetRuleSet(ids[j]);
160: buf.append("<a href=\"Site?id=" + frs.id
161: + "\">" + frs.nickname + "</a>");
162: if (j < ids.length - 1) {
163: buf.append(", ");
164: }
165: }
166: debugLogger.log(Level.FINER, "PSSH_CSPSAM0003",
167: new String[] { FIELD_USEDINSITES,
168: buf.toString() });
169: setValue(FIELD_USEDINSITES, buf.toString());
170: } else
171: setValue(FIELD_USEDINSITES, "");
172:
173: }
174: }
175: beforeFirst();
176:
177: // Nothing useful to return
178: return null;
179: }
180:
181: }
|