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.admin.RDSearch;
021: import com.sun.portal.search.util.SearchConfig;
022: import com.sun.portal.search.admin.resources.SearchResource;
023: import com.sun.portal.search.soif.*;
024: import com.sun.portal.search.rdm.*;
025: import com.sun.portal.search.db.SToken;
026: import com.sun.portal.log.common.PortalLogger;
027:
028: import com.sun.portal.util.Platform;
029:
030: /**
031: * This class supports the MenuListView by providing the data in the
032: * format expected by the view.
033: */
034: public class RDListModel extends DefaultModel implements DatasetModel,
035: RetrievingModel, DeletingModel {
036: public static final String MODEL_NAME = "RDListModel";
037: public static final String FIELD_URL = "Url";
038: public static final String FIELD_TITLE = "Title";
039: public static final String FIELD_DESC = "Desc";
040: public static final String FIELD_DELETE = "Delete";
041: public static final String FIELD_ENCODED_URL = "EncodedName";
042: public Locale userLocale = Locale.getDefault();
043:
044: public int numPerPage = 25;
045: public int currPage = 1;
046: public int totalHits = 0;
047: String queryString = "*";
048: String database = null;
049: int searchOption = 1;
050: private SToken st = null;
051:
052: public int RDM_Results = 0;
053: public int RDM_Hits = 0;
054: public int RDM_Documents = 0;
055: private boolean isRetrieved = false;
056:
057: private String schfn = "";
058:
059: // Create a Logger for this class
060: private static Logger debugLogger = PortalLogger
061: .getLogger(RDListModel.class);
062:
063: /**
064: * @param req The HttpServletRequest object passed to the super class.
065: * @param rbName The name of the resource bundle.
066: */
067: public RDListModel() {
068: super ();
069: }
070:
071: // Model execution methods
072: ////////////////////////////////////////////////////////////////////////////////
073:
074: /**
075: *
076: *
077: */
078: public Object execute(ModelExecutionContext context)
079: throws ModelControlException {
080: String operationName = null;
081: if (context != null)
082: operationName = context.getOperationName();
083: else
084: operationName = ModelExecutionContext.OPERATION_RETRIEVE;
085:
086: Object result = null;
087: if (operationName
088: .equals(ModelExecutionContext.OPERATION_RETRIEVE)) {
089: result = retrieve(context);
090: } else if (operationName
091: .equals(ModelExecutionContext.OPERATION_DELETE)) {
092: result = delete(context);
093: }
094:
095: return result;
096: }
097:
098: /**
099: *
100: *
101: */
102: public Object delete(ModelExecutionContext context)
103: throws ModelControlException {
104: String tmpFile = CSConfig.getServerRoot() + File.separator
105: + "tmp" + File.separator + "tmpRDEditor."
106: + Long.toString(System.currentTimeMillis());
107: SOIFOutputStream so;
108: try {
109: so = new SOIFOutputStream(tmpFile);
110:
111: beforeFirst();
112: while (next()) {
113: String isDelete = (String) getValue(FIELD_DELETE);
114: if (isDelete != null
115: && isDelete.equalsIgnoreCase("true")) {
116: try {
117: String delURL = new String(
118: Encoder
119: .decodeBase64((String) getValue(FIELD_ENCODED_URL)),
120: "UTF-8");
121: debugLogger.log(Level.FINER, "PSSH_CSPSAM0035",
122: delURL);
123: SOIF delRD = new SOIF("DOCUMENT", delURL);
124: so.write(delRD);
125: } catch (Exception e) {
126: debugLogger.log(Level.INFO, "PSSH_CSPSAM0012",
127: e.getMessage());
128: }
129: }
130: }
131: so.close();
132: Runtime rt = Runtime.getRuntime();
133: String db_option = (database != null) ? "-y " + database
134: + " " : " ";
135: String runCsCli = null;
136: runCsCli = Platform.getCommand("run-cs-cli");
137: String rdmgr = CSConfig.getServerRoot() + File.separator
138: + runCsCli + " rdmgr -d -q " + db_option + tmpFile;
139:
140: debugLogger.log(Level.FINER, "PSSH_CSPSAM0033", rdmgr);
141: Process p = rt.exec(rdmgr);
142: try {
143: p.waitFor();
144: } catch (java.lang.InterruptedException e) {
145: // Might need to do something here
146: }
147: int exitValue = p.exitValue();
148: if (exitValue == 1) {
149: debugLogger.log(Level.FINER, "PSSH_CSPSAM0033", Integer
150: .toString(exitValue));
151: }
152:
153: } catch (Exception e) {
154: throw new ModelControlException(e);
155: }
156:
157: return null;
158: }
159:
160: private ArrayList actionSearch() {
161: debugLogger.log(Level.FINER, "PSSH_CSPSAM0037", new String[] {
162: Integer.toString(searchOption), queryString });
163:
164: // XXX try to init SearchServlet in order to use RDMUIBridge
165: //HttpServletRequest req = getRequestContext().getRequest();
166: //CSConfig.initSearch((req.isSecure()? "https":"http") + "://" + req.getServerName() + ":" + req.getServerPort()
167: // + CSConfig.getDynamicURI() + "/search");
168: try {
169: RDSearch rdSearch = new RDSearch();
170: ArrayList resultList = rdSearch.search(database,
171: queryString, searchOption, (currPage - 1)
172: * numPerPage + 1, numPerPage, st);
173: RDM_Results = rdSearch.RDM_Results;
174: if (searchOption == 5 && RDM_Results == 1) {
175: RDM_Hits = 1;
176: RDM_Documents = 1;
177: } else {
178: RDM_Hits = rdSearch.RDM_Hits;
179: RDM_Documents = rdSearch.RDM_Documents;
180: }
181: return resultList;
182:
183: } catch (Exception e) {
184: return null;
185: }
186: }
187:
188: /**
189: *
190: *
191: */
192: public Object retrieve(ModelExecutionContext context)
193: throws ModelControlException {
194: if (isRetrieved) {
195: return null;
196: }
197: clear();
198: ArrayList hits = actionSearch();
199: if (hits != null) {
200: /*adjust currpage to last page if it's out of range*/
201: for (int i = 0; i < hits.size(); i++) {
202: SOIF s = (SOIF) hits.get(i);
203: appendRow();
204: String url = s.getURL();
205: String base64url = url;
206: try {
207: base64url = new String(Encoder.encodeBase64(url
208: .getBytes("UTF-8")));
209: } catch (Exception e) {
210: }
211:
212: setValue(FIELD_URL, url);
213: setValue(FIELD_TITLE, s.getValue("title"));
214: setValue(FIELD_ENCODED_URL, base64url);
215: }
216: }
217: beforeFirst();
218: isRetrieved = true;
219:
220: // Nothing useful to return
221: return null;
222: }
223:
224: public void setDatabase(String database) {
225: this .database = database;
226: }
227:
228: /* the search is actually done in model retrieve function */
229: public int doSearch(String database, int numPerPage, int currPage,
230: String queryString, int searchOption, SToken st) {
231: this .numPerPage = numPerPage;
232: this .currPage = currPage;
233: this .queryString = queryString;
234: this .searchOption = searchOption;
235: this .database = database;
236: this .st = st;
237: try {
238: this .retrieve(null);
239: } catch (Exception e) {
240: }
241: return totalHits;
242: }
243:
244: static public boolean isWindows() {
245: if (System.getProperty("os.name").startsWith("Windows")) {
246: return true;
247: } else {
248: return false;
249: }
250: }
251: }
|