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 java.io.*;
013: import java.net.*;
014: import javax.servlet.http.HttpServletRequest;
015:
016: import com.iplanet.jato.model.*;
017: import com.iplanet.jato.view.html.*;
018: import com.iplanet.jato.util.Encoder;
019: import com.sun.portal.search.admin.CSConfig;
020: import com.sun.portal.search.util.SearchConfig;
021: import com.sun.portal.search.admin.resources.SearchResource;
022: import com.sun.portal.search.soif.*;
023: import com.sun.portal.search.rdm.*;
024: import com.sun.portal.log.common.PortalLogger;
025:
026: /**
027: * This class supports the MenuListView by providing the data in the
028: * format expected by the view.
029: */
030: public class SchemaListModel extends DefaultModel implements
031: DatasetModel, RetrievingModel, DeletingModel {
032: public static final String FIELD_NAME = "Name";
033: public static final String FIELD_DELNAME = "DelName";
034: public static final String FIELD_DESC = "Desc";
035: public static final String FIELD_ALIAS = "Alias";
036: public static final String FIELD_DATATYPE = "Datatype";
037: public static final String FIELD_EDITABLE = "Editable";
038: public static final String FIELD_INDEXABLE = "Indexable";
039: public static final String FIELD_DELETE = "Delete";
040: public static final String FIELD_ENCODED_NAME = "EncodedName";
041: static final String[] FIELDS = { FIELD_NAME, FIELD_DESC,
042: FIELD_ALIAS };
043: public Locale userLocale = Locale.getDefault();
044: public static final String[] DATA_TYPES = { "date", "int", "string" };
045: public boolean showInternal = false;
046:
047: private String schfn = "";
048:
049: // Create a Logger for this class
050: private static Logger debugLogger = PortalLogger
051: .getLogger(SchemaListModel.class);
052:
053: /**
054: * @param req The HttpServletRequest object passed to the super class.
055: * @param rbName The name of the resource bundle.
056: */
057: public SchemaListModel() {
058: super ();
059: /*
060: try {
061: retrieve(null);
062: }
063: catch (Exception e) {}
064: */
065: }
066:
067: // Model execution methods
068: ////////////////////////////////////////////////////////////////////////////////
069:
070: /**
071: *
072: *
073: */
074: public Object execute(ModelExecutionContext context)
075: throws ModelControlException {
076: String operationName = null;
077: if (context != null)
078: operationName = context.getOperationName();
079: else
080: operationName = ModelExecutionContext.OPERATION_RETRIEVE;
081:
082: Object result = null;
083: if (operationName
084: .equals(ModelExecutionContext.OPERATION_RETRIEVE)) {
085: result = retrieve(context);
086: } else if (operationName
087: .equals(ModelExecutionContext.OPERATION_DELETE)) {
088: result = delete(context);
089: }
090:
091: return result;
092: }
093:
094: /**
095: *
096: *
097: */
098: public Object delete(ModelExecutionContext context)
099: throws ModelControlException {
100: RDMSchema schema = null;
101: try {
102: schema = loadSchema();
103: } catch (Exception e) {
104: throw new ModelControlException(e);
105: }
106:
107: if (schema == null) {
108: throw new ModelControlException("Error on loading Schema");
109: }
110:
111: beforeFirst();
112: while (next()) {
113: String isDelete = (String) getValue(FIELD_DELETE);
114: if (isDelete != null && isDelete.equalsIgnoreCase("true")) {
115: String delName = (String) getValue(FIELD_DELNAME);
116: debugLogger
117: .log(Level.FINER, "PSSH_CSPSAM0040", delName);
118: if (delName != null) {
119: String name = null;
120: try {
121: name = new String(
122: Encoder.decodeBase64(delName), "UTF-8");
123: schema.deleteColumn(name);
124: } catch (Exception e) {
125: throw new ModelControlException(
126: "Schema failed to delete column: "
127: + name + ": " + e);
128: }
129: }
130: }
131: }
132: try {
133: SOIFOutputStream outss = new SOIFOutputStream(schfn);
134: outss.write(schema.getSOIF());
135: outss.close();
136: } catch (Exception e) {
137: throw new ModelControlException("Unable to write SOIF to "
138: + schfn + ": " + e);
139: }
140: return null;
141: }
142:
143: private RDMSchema loadSchema() throws Exception {
144: RDMSchema schema = null;
145: String csidfn = CSConfig.getServerRoot() + File.separator
146: + SearchConfig.CONFDIR + File.separator
147: + SearchConfig.SEARCH_CONF;
148: try {
149: SearchConfig.init(csidfn);
150: } catch (Exception e) {
151: throw new Exception("Unable to initialize SearchConfig: "
152: + csidfn);
153: }
154:
155: if ((schfn = SearchConfig.getValue(SearchConfig.SCHEMA)) == null) {
156: throw new Exception("Missing SearchConfig.SCHEMA parameter");
157: }
158:
159: // Load the schema
160: debugLogger.log(Level.FINER, "PSSH_CSPSAM0041", schfn);
161: SOIFInputStream ss = null;
162: try {
163: ss = new SOIFInputStream(schfn);
164: schema = new RDMSchema(ss.readSOIF());
165: } catch (Exception e) {
166: debugLogger.log(Level.INFO, "PSSH_CSPSAM0042", schfn);
167: }
168: return schema;
169: }
170:
171: public String[] getSchemaList() {
172: try {
173: RDMSchema schema = loadSchema();
174: return schema.schema_nonInternal_attrs();
175: } catch (Exception e) {
176: }
177: return null;
178: }
179:
180: /**
181: *
182: *
183: */
184: public Object retrieve(ModelExecutionContext context)
185: throws ModelControlException {
186: clear();
187: try {
188: RDMSchema schema = loadSchema();
189: String[] arr = {};
190: if (showInternal) {
191: int i, j, n, nmv = 0;
192: List attrs = new ArrayList();
193: String attr;
194: String iflg = null;
195:
196: n = schema.getMaxIndex();
197: for (i = 0; i <= n; i++) {
198: if ((attr = schema.getSOIFAttribute(i)) != null) {
199: attrs.add(attr);
200: }
201: }
202: arr = (String[]) attrs.toArray(new String[0]);
203: } else {
204: arr = schema.schema_nonInternal_attrs();
205: }
206:
207: for (int i = 0; i < arr.length; i++) {
208: appendRow();
209: String name = arr[i];
210: setValue(FIELD_NAME, name);
211:
212: try {
213: String encodedName = Encoder.encodeBase64(name
214: .getBytes("UTF-8"));
215: setValue(FIELD_ENCODED_NAME, encodedName);
216: setValue(FIELD_DELNAME, encodedName);
217: } catch (Exception e) {
218: debugLogger.log(Level.INFO, "PSSH_CSPSAM0012", e
219: .getMessage());
220: setValue(FIELD_ENCODED_NAME, name);
221: setValue(FIELD_DELNAME, name);
222: }
223:
224: int col = schema.getColumnNumber(name);
225: setValue(FIELD_DESC, schema.getDescription(col));
226: String value = schema.getAliases(col);
227: if (value != null) {
228: setValue(FIELD_ALIAS, value);
229: }
230: value = schema.getDataType(col);
231: if (value != null) {
232: setValue(FIELD_DATATYPE, value);
233: }
234: value = schema.getEditAttribute(col);
235: if (value != null && value.equals("1")) {
236: setValue(FIELD_EDITABLE, "true");
237: } else {
238: setValue(FIELD_EDITABLE, "false");
239: }
240: value = schema.getIndexAttribute(col);
241: if (value != null && value.equals("1")) {
242: setValue(FIELD_INDEXABLE, "true");
243: } else {
244: setValue(FIELD_INDEXABLE, "false");
245: }
246: }
247:
248: } catch (Exception e) {
249: throw new ModelControlException(e);
250: }
251: beforeFirst();
252:
253: // Nothing useful to return
254: return null;
255: }
256:
257: }
|