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.io.*;
011: import java.net.*;
012: import javax.servlet.http.HttpServletRequest;
013:
014: import com.iplanet.jato.model.*;
015: import com.iplanet.jato.view.html.*;
016: import com.iplanet.jato.util.*;
017: import com.sun.portal.search.admin.CSConfig;
018: import com.sun.portal.search.admin.Rule;
019: import com.sun.portal.search.admin.CnConfig;
020: import com.sun.portal.search.admin.resources.SearchResource;
021:
022: /**
023: * This class supports the MenuListView by providing the data in the
024: * format expected by the view.
025: */
026: public class RobotClassRuleListModel extends DefaultModel implements
027: DatasetModel, RetrievingModel, DeletingModel {
028: public static final String MODEL_NAME = "RobotClassRuleListModel";
029: public static final String FIELD_SRC = "Src";
030: public static final String FIELD_METHOD = "Method";
031: public static final String FIELD_CRITERIA = "Criteria";
032: public static final String FIELD_CLASSIFICATION = "Classification";
033: public static final String FIELD_DELETE = "Delete";
034: static final String[] FIELDS = { FIELD_SRC, FIELD_METHOD,
035: FIELD_CRITERIA, FIELD_CLASSIFICATION };
036: public Locale userLocale = Locale.getDefault();
037: static final String[] methodValues = new String[] {
038: CnConfig.METHOD_EXACT, CnConfig.METHOD_SUBSTR,
039: CnConfig.METHOD_PREFIX, CnConfig.METHOD_SUFFIX,
040: CnConfig.METHOD_REGEX };
041:
042: private CnConfig cnconfig = new CnConfig(CSConfig.getServerRoot());
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 RobotClassRuleListModel() {
049: super ();
050: /* try {
051: retrieve(null);
052: }
053: catch (Exception e) {}*/
054: }
055:
056: private void ValidateList() throws ModelControlException {
057: ArrayList delObjs = new ArrayList();
058: beforeFirst();
059: while (next()) {
060: for (int i = 0; i < FIELDS.length; i++) {
061: if (getValue(FIELDS[i]) == null) {
062: delObjs.add(getCurrentContext().getValueList().get(
063: getRowIndex()));
064: break;
065: }
066: }
067: }
068: for (int i = 0; i < delObjs.size(); i++) {
069: getCurrentContext().getValueList().remove(delObjs.get(i));
070: }
071: return;
072: }
073:
074: // Model execution methods
075: ////////////////////////////////////////////////////////////////////////////////
076:
077: /**
078: *
079: *
080: */
081: public Object execute(ModelExecutionContext context)
082: throws ModelControlException {
083: String operationName = null;
084: if (context != null)
085: operationName = context.getOperationName();
086: else
087: operationName = ModelExecutionContext.OPERATION_RETRIEVE;
088:
089: Object result = null;
090: if (operationName
091: .equals(ModelExecutionContext.OPERATION_RETRIEVE)) {
092: result = retrieve(context);
093: } else if (operationName
094: .equals(ModelExecutionContext.OPERATION_DELETE)) {
095: result = delete(context);
096: }
097:
098: return result;
099: }
100:
101: /**
102: *
103: *
104: */
105: public Object delete(ModelExecutionContext context)
106: throws ModelControlException {
107: beforeFirst();
108: int deltaIndex = 0;
109: while (next()) {
110: String isDelete = (String) getValue(this .FIELD_DELETE);
111: if (isDelete != null && isDelete.compareTo("true") == 0) {
112: int index = getRowIndex();
113: cnconfig.delete(index - deltaIndex);
114: deltaIndex++;
115: }
116: }
117: /* checking any deletion happened */
118: if (deltaIndex > 0) {
119: cnconfig.save();
120: }
121: return null;
122: }
123:
124: public int getNumOfRules() {
125: return cnconfig.getNumberOfRules();
126: }
127:
128: /* used for forced reload*/
129: private void loadRules() {
130: String server_root = CSConfig.getServerRoot();
131: cnconfig = new CnConfig(server_root);
132: }
133:
134: /**
135: *
136: *
137: */
138: public Object retrieve(ModelExecutionContext context)
139: throws ModelControlException {
140: OptionList srcOptions = getSrcLst(userLocale);
141: OptionList methodOptions = getMethodLst(userLocale);
142:
143: clear();
144: ArrayList rules = cnconfig.getRulesList();
145: int n = rules.size();
146: for (int i = 0; i < n; i++) {
147: Rule rule = (Rule) rules.get(i);
148: appendRow();
149: String label = srcOptions.getValueLabel(rule.getSrc());
150: setValue(FIELD_SRC, (label == null) ? rule.getSrc() : label);
151: label = methodOptions.getValueLabel(rule.getMethod());
152: setValue(FIELD_METHOD, (label == null) ? rule.getMethod()
153: : label);
154: setValue(FIELD_CRITERIA, rule.getName());
155: setValue(FIELD_CLASSIFICATION, rule.getAction());
156: }
157: beforeFirst();
158:
159: // Nothing useful to return
160: return null;
161: }
162:
163: public static OptionList getSrcLst(Locale userLocale) {
164:
165: OptionList srcLst = new OptionList(SearchResource.geti18nArray(
166: "classrules.src.options", ",", userLocale),
167: SearchResource.geti18nArray("classrules.src.values",
168: ",", userLocale));
169:
170: String[] schLst = (new SchemaListModel()).getSchemaList();
171:
172: if (schLst != null) {
173: for (int i = 0; i < schLst.length; i++) {
174: srcLst.add(i, schLst[i], schLst[i]);
175: }
176: }
177: return srcLst;
178: }
179:
180: public static OptionList getMethodLst(Locale userLocale) {
181: OptionList methodLst = new OptionList(SearchResource
182: .geti18nArray("classrules.method.options", ",",
183: userLocale), methodValues);
184: return methodLst;
185: }
186:
187: }
|