0001: package com.sun.portal.subscriptions.providers;
0002:
0003: import java.io.UnsupportedEncodingException;
0004: import java.util.Hashtable;
0005: import java.util.HashMap;
0006: import java.util.ArrayList;
0007: import java.util.Locale;
0008: import java.util.Calendar;
0009: import java.util.GregorianCalendar;
0010: import java.util.logging.Level;
0011: import java.util.logging.Logger;
0012: import java.text.DateFormat;
0013: import java.text.SimpleDateFormat;
0014:
0015: import com.sun.portal.search.rdm.RDM;
0016: import com.sun.portal.search.providers.SearchContext;
0017: import com.sun.portal.search.soif.SOIFInputStream;
0018: import com.sun.portal.search.util.Decoder;
0019: import com.sun.portal.search.util.Encoder;
0020: import com.sun.portal.log.common.PortalLogger;
0021:
0022: /**
0023: * Description of the Class
0024: *
0025: *@author pm95875
0026: *@created September 15, 2003
0027: */
0028: public class SearchSubscription extends Subscription {
0029: private String server = null;
0030: private String defaultServer = null;
0031: private ArrayList critera = null;
0032: private String database = null;
0033: private String viewAttributes = null;
0034: private HashMap elements = null;
0035: private static Logger logger = PortalLogger
0036: .getLogger(SearchSubscription.class);
0037:
0038: private final String DEFAULT_RATING = "-1";
0039:
0040: /**
0041: * Constructor for the Subscription object
0042: *
0043: *@param str Description of Parameter
0044: *@param server Description of Parameter
0045: *@param va View Attribute
0046: *@since
0047: */
0048: public SearchSubscription(String server, String str, String va) {
0049: super (str);
0050: this .defaultServer = server;
0051: this .viewAttributes = va;
0052: this .elements = new HashMap();
0053: subType = SAVED_SRCH_SUB;
0054: }
0055:
0056: /**
0057: * Constructor for the Subscription object
0058: *
0059: *@param type Description of Parameter
0060: *@param server Description of Parameter
0061: *@param str Description of Parameter
0062: *@param va Description of Parameter
0063: *@since
0064: */
0065: public SearchSubscription(String server, String type, String str,
0066: String va) {
0067: super (str);
0068: this .defaultServer = server;
0069: this .subType = type;
0070: this .viewAttributes = va;
0071: this .elements = new HashMap();
0072: }
0073:
0074: /**
0075: * Constructor for the SearchSubscription object
0076: *
0077: *@param type Description of Parameter
0078: *@param str Description of Parameter
0079: *@param critera Description of Parameter
0080: *@param va View Attribute
0081: *@since
0082: */
0083: public SearchSubscription(String server, String type, String str,
0084: String va, ArrayList critera) {
0085: super (str);
0086: this .defaultServer = server;
0087: this .subType = type;
0088: this .viewAttributes = va;
0089: this .critera = critera;
0090: this .elements = new HashMap();
0091: }
0092:
0093: public Object execute() {
0094: try {
0095: SearchContext srchCtx = (SearchContext) this .parse();
0096: srchCtx.setRDMServer(this .server);
0097: srchCtx.execute();
0098: return srchCtx.getResultStream();
0099: } catch (Exception e) {
0100: if (getLogger().isLoggable(Level.SEVERE)) {
0101: getLogger().log(Level.SEVERE, "PSSS_CSPP0018", e);
0102: }
0103: return null;
0104: }
0105: }
0106:
0107: /**
0108: * Description of the Method
0109: *
0110: *@param server Description of Parameter
0111: *@return Description of the Returned Value
0112: *@since
0113: */
0114: public Object execute(String srv) {
0115: try {
0116: this .server = srv;
0117: return execute();
0118: } catch (NullPointerException npe) {
0119: if (getLogger().isLoggable(Level.SEVERE)) {
0120: getLogger().log(Level.SEVERE, "PSSS_CSPP0018", npe);
0121: }
0122: return null;
0123: } catch (Exception e) {
0124: if (getLogger().isLoggable(Level.SEVERE)) {
0125: getLogger().log(Level.SEVERE, "PSSS_CSPP0018", e);
0126: }
0127: return null;
0128: }
0129: }
0130:
0131: /**
0132: * Description of the Method
0133: *
0134: *@return Description of the Returned Value
0135: *@since
0136: */
0137: public Object parse() {
0138: SearchContext srchCtx = null;
0139: if (this .subType.equalsIgnoreCase(CATEGORY_SRCH_SUB)) {
0140: srchCtx = (SearchContext) parseCategorySrchSub();
0141: } else if (this .subType.equalsIgnoreCase(DISCUSSION_SRCH_SUB)) {
0142: srchCtx = (SearchContext) parseDiscussionSrchSub();
0143: } else if (this .subType.equalsIgnoreCase(SAVED_SRCH_SUB)) {
0144: srchCtx = (SearchContext) parseSavedSrchSub();
0145: } else {
0146: // invalid or undefined subscription
0147: if (getLogger().isLoggable(Level.SEVERE)) {
0148: getLogger().log(Level.SEVERE, "PSSS_CSPP0018");
0149: }
0150: srchCtx = (SearchContext) null;
0151: }
0152: return srchCtx;
0153: }
0154:
0155: /**
0156: * Description of the Method
0157: *
0158: *@return Description of the Returned Value
0159: *@since
0160: */
0161: public Object parseCategorySrchSub() {
0162: SearchContext srchCtx = new SearchContext();
0163: srchCtx.setRDMType("rd-request");
0164: srchCtx.setQueryLanguage("search");
0165: srchCtx.setViewAttributes(this .viewAttributes);
0166: srchCtx.setViewHits(-1);
0167: String catName = null;
0168: String catTarget = null;
0169: String catScope = null;
0170: String catSince = "0";
0171: String catRating = DEFAULT_RATING;
0172: String catStatus = DEFAULT_STATUS;
0173: try {
0174: int delimNdx = this .subStr.indexOf("|");
0175: int prevNdx = 0;
0176: if (delimNdx != -1) { // category subscription name
0177: catName = Decoder.urlDecode(subStr.substring(prevNdx,
0178: delimNdx), "UTF-8");
0179: prevNdx = delimNdx + 1;
0180: delimNdx = subStr.indexOf("|", prevNdx);
0181: }
0182: if (delimNdx != -1) { // target category
0183: catTarget = Decoder.urlDecode(subStr.substring(prevNdx,
0184: delimNdx), "UTF-8");
0185: prevNdx = delimNdx + 1;
0186: delimNdx = subStr.indexOf("|", prevNdx);
0187: } else if (prevNdx != 0) {
0188: catTarget = "ROOT";
0189: prevNdx = 0;
0190: }
0191: if (delimNdx != -1) { // scope
0192: catScope = Decoder.urlDecode(subStr.substring(prevNdx,
0193: delimNdx), "UTF-8");
0194: prevNdx = delimNdx + 1;
0195: delimNdx = subStr.indexOf("|", prevNdx);
0196: } else if (prevNdx != 0) {
0197: catScope = Decoder.urlDecode(subStr.substring(prevNdx),
0198: "UTF-8");
0199: prevNdx = 0;
0200: }
0201: if (delimNdx != -1) {
0202: catSince = Decoder.urlDecode(subStr.substring(prevNdx,
0203: delimNdx), "UTF-8");
0204: prevNdx = delimNdx + 1;
0205: delimNdx = subStr.indexOf("|", prevNdx);
0206: } else if (prevNdx != 0) {
0207: catSince = "0";
0208: prevNdx = 0;
0209: }
0210: if (delimNdx != -1) { // rating
0211: catRating = Decoder.urlDecode(subStr.substring(prevNdx,
0212: delimNdx), "UTF-8");
0213: prevNdx = delimNdx + 1;
0214: delimNdx = subStr.indexOf("|", prevNdx);
0215: } else if (prevNdx != 0) {
0216: catRating = DEFAULT_RATING; // all
0217: prevNdx = 0;
0218: }
0219: if (delimNdx != -1) { // server
0220: this .server = Decoder.urlDecode(subStr.substring(
0221: prevNdx, delimNdx), "UTF-8");
0222: prevNdx = delimNdx + 1;
0223: delimNdx = subStr.indexOf("|", prevNdx);
0224: }
0225: if (delimNdx != -1) { // database
0226: this .database = Decoder.urlDecode(subStr.substring(
0227: prevNdx, delimNdx), "UTF-8");
0228: prevNdx = delimNdx + 1;
0229: delimNdx = subStr.indexOf("|", prevNdx);
0230: }
0231: if (delimNdx != -1) { // status [on/off]
0232: catStatus = Decoder.urlDecode(subStr.substring(prevNdx,
0233: delimNdx), "UTF-8");
0234: prevNdx = delimNdx + 1;
0235: delimNdx = subStr.indexOf("|", prevNdx);
0236: } else if (prevNdx != 0) {
0237: catStatus = Decoder.urlDecode(
0238: subStr.substring(prevNdx), "UTF-8");
0239: prevNdx = 0;
0240: }
0241: if ((catName == null || catTarget == null)
0242: || (catName != null && catName.trim().length() == 0)
0243: || (catTarget != null && catTarget.trim().length() == 0)) {
0244: if (getLogger().isLoggable(Level.SEVERE)) {
0245: String param[] = { Subscription.CATEGORY_SRCH_SUB,
0246: this .subStr };
0247: getLogger().log(Level.SEVERE, "PSSS_CSPP0016",
0248: param);
0249: }
0250: // presumably the subscription entry is corrupted returning "null"
0251: return (SearchContext) null;
0252: }
0253: if (catScope == null) {
0254: catScope = "*";
0255: }
0256: if (this .server == null || this .server.trim().length() == 0) {
0257: this .server = this .defaultServer;
0258: }
0259: if (this .database == null
0260: || this .database.trim().length() == 0) {
0261: this .database = "default";
0262: }
0263: this .elements.put("name", catName);
0264: this .elements.put("target", catTarget);
0265: this .elements.put("scope", catScope);
0266: this .elements.put("since", catSince);
0267: this .elements.put("rating", catRating);
0268: this .elements.put("server", this .server);
0269: this .elements.put("database", this .database);
0270: this .elements.put("status", catStatus);
0271: ArrayList aL = new ArrayList();
0272: HashMap hM = new HashMap();
0273: hM.put(SearchContext.OPERAND, "");
0274: hM.put(SearchContext.OPERATION, "<AND>");
0275: hM.put(SearchContext.VALUE, catScope);
0276: aL.add(hM);
0277: srchCtx.setScope(aL);
0278: srchCtx.setCategory(catTarget);
0279: srchCtx.setSearchAllCategories(false);
0280: srchCtx.setDatabase(this .database);
0281: } catch (Exception e) {
0282: if (getLogger().isLoggable(Level.SEVERE)) {
0283: String param[] = { Subscription.CATEGORY_SRCH_SUB,
0284: this .subStr };
0285: getLogger().log(Level.SEVERE, "PSSS_CSPP0017", param);
0286: }
0287: srchCtx = (SearchContext) null;
0288: }
0289: return srchCtx;
0290: }
0291:
0292: /**
0293: * Description of the Method
0294: *
0295: *@return Description of the Returned Value
0296: *@since
0297: */
0298: public Object parseDiscussionSrchSub() {
0299: SearchContext srchCtx = new SearchContext();
0300: srchCtx.setRDMType("rd-request");
0301: srchCtx.setQueryLanguage("search");
0302: srchCtx.setViewAttributes(this .viewAttributes);
0303: srchCtx.setViewHits(-1);
0304: String discName = null;
0305: String discScope = null;
0306: String discTarget = null;
0307: String discRating = DEFAULT_RATING;
0308: String discSince = "0";
0309: String discStatus = DEFAULT_STATUS;
0310: try {
0311: int delimNdx = subStr.indexOf("|");
0312: int prevNdx = 0;
0313: if (delimNdx != -1) { // discussion subscriptions Name
0314: discName = Decoder.urlDecode(subStr.substring(prevNdx,
0315: delimNdx), "UTF-8");
0316: prevNdx = delimNdx + 1;
0317: delimNdx = subStr.indexOf("|", prevNdx);
0318: }
0319: if (delimNdx != -1) { // discussion subscriptions target (aka parent node)
0320: discTarget = Decoder.urlDecode(subStr.substring(
0321: prevNdx, delimNdx), "UTF-8");
0322: prevNdx = delimNdx + 1;
0323: delimNdx = subStr.indexOf("|", prevNdx);
0324: } else if (prevNdx != 0) {
0325: discTarget = "";
0326: prevNdx = 0;
0327: }
0328: if (delimNdx != -1) { // scope
0329: discScope = Decoder.urlDecode(subStr.substring(prevNdx,
0330: delimNdx), "UTF-8");
0331: prevNdx = delimNdx + 1;
0332: delimNdx = subStr.indexOf("|", prevNdx);
0333: } else if (prevNdx != 0) {
0334: discScope = Decoder.urlDecode(
0335: subStr.substring(prevNdx), "UTF-8");
0336: prevNdx = 0;
0337: }
0338: if (delimNdx != -1) { // search from date...
0339: discSince = Decoder.urlDecode(subStr.substring(prevNdx,
0340: delimNdx), "UTF-8");
0341: prevNdx = delimNdx + 1;
0342: delimNdx = subStr.indexOf("|", prevNdx);
0343: } else if (prevNdx != 0) {
0344: discSince = "0";
0345: prevNdx = 0;
0346: }
0347: if (delimNdx != -1) { // minimum rating
0348: discRating = Decoder.urlDecode(subStr.substring(
0349: prevNdx, delimNdx), "UTF-8");
0350: prevNdx = delimNdx + 1;
0351: delimNdx = subStr.indexOf("|", prevNdx);
0352: } else if (prevNdx != 0) {
0353: discRating = DEFAULT_RATING;
0354: prevNdx = 0;
0355: }
0356: if (delimNdx != -1) {
0357: this .server = Decoder.urlDecode(subStr.substring(
0358: prevNdx, delimNdx), "UTF-8");
0359: prevNdx = delimNdx + 1;
0360: delimNdx = subStr.indexOf("|", prevNdx);
0361: }
0362: if (delimNdx != -1) { // database
0363: this .database = Decoder.urlDecode(subStr.substring(
0364: prevNdx, delimNdx), "UTF-8");
0365: prevNdx = delimNdx + 1;
0366: delimNdx = subStr.indexOf("|", prevNdx);
0367: } else if (prevNdx != 0) {
0368: this .database = "discussion";
0369: prevNdx = 0;
0370: }
0371: if (delimNdx != -1) { // status [on/off]
0372: discStatus = Decoder.urlDecode(subStr.substring(
0373: prevNdx, delimNdx), "UTF-8");
0374: prevNdx = delimNdx + 1;
0375: delimNdx = subStr.indexOf("|", prevNdx);
0376: } else if (prevNdx != 0) {
0377: discStatus = Decoder.urlDecode(subStr
0378: .substring(prevNdx), "UTF-8");
0379: prevNdx = 0;
0380: }
0381: if ((discName == null || discTarget == null)
0382: || (discName != null && discName.trim().length() == 0)
0383: || (discTarget != null && discTarget.trim()
0384: .length() == 0)) {
0385: if (getLogger().isLoggable(Level.SEVERE)) {
0386: String param[] = {
0387: Subscription.DISCUSSION_SRCH_SUB,
0388: this .subStr };
0389: getLogger().log(Level.SEVERE, "PSSS_CSPP0016",
0390: param);
0391: }
0392: // presumably the subscription entry is corrupted returning "null"
0393: return (SearchContext) null;
0394: }
0395: if (discScope == null) {
0396: discScope = "*";
0397: }
0398: if (discRating == null) {
0399: discRating = "-1";
0400: }
0401: if (this .server == null || this .server.trim().length() == 0) {
0402: this .server = this .defaultServer;
0403: }
0404: if (this .database == null
0405: || this .database.trim().length() == 0) {
0406: this .database = "discussion";
0407: }
0408: this .elements.put("name", discName);
0409: this .elements.put("target", discTarget);
0410: this .elements.put("scope", discScope);
0411: this .elements.put("since", discSince);
0412: this .elements.put("rating", discRating);
0413: this .elements.put("server", this .server);
0414: this .elements.put("database", this .database);
0415: this .elements.put("status", discStatus);
0416: ArrayList aL = new ArrayList();
0417: HashMap hM = null;
0418: if (!discSince.equals("0")) {
0419: hM = new HashMap();
0420: hM.put(SearchContext.OPERAND, "RD-Last-Changed");
0421: hM.put(SearchContext.OPERATION, SearchContext.GREATER);
0422: Calendar c1 = new GregorianCalendar();
0423: if (discSince.equals("1")) {
0424: c1.add(Calendar.DATE, -1);
0425: }
0426: if (discSince.equals("7")) {
0427: c1.add(Calendar.DATE, -7);
0428: }
0429: if (discSince.equals("30")) {
0430: c1.add(Calendar.MONTH, -1);
0431: }
0432: if (discSince.equals("180")) {
0433: c1.add(Calendar.MONTH, -6);
0434: }
0435: if (discSince.equals("365")) {
0436: c1.add(Calendar.YEAR, -1);
0437: }
0438: DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
0439: String d1 = df.format(c1.getTime());
0440: hM.put(SearchContext.VALUE, d1);
0441: aL.add(hM);
0442: }
0443: hM = new HashMap();
0444: hM.put(SearchContext.OPERAND, "");
0445: hM.put(SearchContext.OPERATION, "<AND>");
0446: hM.put(SearchContext.VALUE, discScope);
0447: aL.add(hM);
0448: hM = new HashMap();
0449: hM.put(SearchContext.OPERAND, "RD-Reference-id");
0450: hM.put(SearchContext.OPERATION, SearchContext.CONTAIN);
0451: hM.put(SearchContext.VALUE, discTarget);
0452: aL.add(hM);
0453: int rtg = -1;
0454: try {
0455: rtg = new Integer(discRating).intValue();
0456: } catch (NumberFormatException e) {
0457: rtg = -1;
0458: }
0459: if (rtg > -1) {
0460: hM = new HashMap();
0461: hM.put(SearchContext.OPERAND, "RD-Rating");
0462: hM.put(SearchContext.OPERATION, SearchContext.GREATER);
0463: hM.put(SearchContext.VALUE, Integer.toString(rtg - 1));
0464: aL.add(hM);
0465: }
0466: srchCtx.setScope(aL);
0467: srchCtx.setDatabase(this .database);
0468: } catch (Exception e) {
0469: if (getLogger().isLoggable(Level.SEVERE)) {
0470: String param[] = { Subscription.DISCUSSION_SRCH_SUB,
0471: this .subStr };
0472: getLogger().log(Level.SEVERE, "PSSS_CSPP0017", param);
0473: }
0474: srchCtx = (SearchContext) null;
0475: }
0476: return srchCtx;
0477: }
0478:
0479: /**
0480: * Description of the Method
0481: *
0482: *@return Description of the Returned Value
0483: *@since
0484: */
0485: public Object parseSavedSrchSub() {
0486: SearchContext srchCtx = new SearchContext();
0487: srchCtx.setRDMType("rd-request");
0488: srchCtx.setQueryLanguage("search");
0489: srchCtx.setViewAttributes(this .viewAttributes);
0490: srchCtx.setViewHits(-1);
0491: String savedSearchName = null;
0492: String savedSearchScope = null;
0493: String savedSearchSince = "0";
0494: String savedSearchRating = DEFAULT_RATING;
0495: String savedSearchStatus = DEFAULT_STATUS;
0496: try {
0497: int delimNdx = subStr.indexOf("|");
0498: int prevNdx = 0;
0499: if (delimNdx != -1) { // subs name
0500: savedSearchName = Decoder.urlDecode(subStr.substring(
0501: prevNdx, delimNdx), "UTF-8");
0502: prevNdx = delimNdx + 1;
0503: delimNdx = subStr.indexOf("|", prevNdx);
0504: }
0505: if (delimNdx != -1) { // subs scope
0506: savedSearchScope = Decoder.urlDecode(subStr.substring(
0507: prevNdx, delimNdx), "UTF-8");
0508: prevNdx = delimNdx + 1;
0509: delimNdx = subStr.indexOf("|", prevNdx);
0510: } else if (prevNdx != 0) {
0511: savedSearchScope = Decoder.urlDecode(subStr
0512: .substring(prevNdx), "UTF-8");
0513: prevNdx = 0;
0514: }
0515: if (delimNdx != -1) { // subs since
0516: savedSearchSince = Decoder.urlDecode(subStr.substring(
0517: prevNdx, delimNdx), "UTF-8");
0518: prevNdx = delimNdx + 1;
0519: delimNdx = subStr.indexOf("|", prevNdx);
0520: } else if (prevNdx != 0) {
0521: savedSearchSince = Decoder.urlDecode(subStr
0522: .substring(prevNdx), "UTF-8");
0523: prevNdx = 0;
0524: }
0525: if (delimNdx != -1) { // subs rating
0526: savedSearchRating = Decoder.urlDecode(subStr.substring(
0527: prevNdx, delimNdx), "UTF-8");
0528: prevNdx = delimNdx + 1;
0529: delimNdx = subStr.indexOf("|", prevNdx);
0530: } else if (prevNdx != 0) {
0531: savedSearchRating = DEFAULT_RATING;
0532: prevNdx = 0;
0533: }
0534: if (delimNdx != -1) { // subs server
0535: this .server = Decoder.urlDecode(subStr.substring(
0536: prevNdx, delimNdx), "UTF-8");
0537: prevNdx = delimNdx + 1;
0538: delimNdx = subStr.indexOf("|", prevNdx);
0539: }
0540: if (delimNdx != -1) { // database
0541: this .database = Decoder.urlDecode(subStr.substring(
0542: prevNdx, delimNdx), "UTF-8");
0543: prevNdx = delimNdx + 1;
0544: delimNdx = subStr.indexOf("|", prevNdx);
0545: } else if (prevNdx != 0) {
0546: this .database = Decoder.urlDecode(subStr
0547: .substring(prevNdx), "UTF-8");
0548: prevNdx = 0;
0549: }
0550: if (delimNdx != -1) { // status
0551: savedSearchStatus = Decoder.urlDecode(subStr.substring(
0552: prevNdx, delimNdx), "UTF-8");
0553: prevNdx = delimNdx + 1;
0554: delimNdx = subStr.indexOf("|", prevNdx);
0555: } else if (prevNdx != 0) {
0556: savedSearchStatus = Decoder.urlDecode(subStr
0557: .substring(prevNdx), "UTF-8");
0558: prevNdx = 0;
0559: }
0560: if (savedSearchName == null
0561: || (savedSearchName != null && savedSearchName
0562: .trim().length() == 0)) {
0563: if (getLogger().isLoggable(Level.SEVERE)) {
0564: String param[] = { Subscription.SAVED_SRCH_SUB,
0565: this .subStr };
0566: getLogger().log(Level.SEVERE, "PSSS_CSPP0016",
0567: param);
0568: }
0569: // presumably the subscription entry is corrupted returning "null"
0570: return (SearchContext) null;
0571: }
0572: if (savedSearchScope == null) {
0573: savedSearchScope = "*";
0574: }
0575: if (this .server == null || this .server.trim().length() == 0) {
0576: this .server = this .defaultServer;
0577: }
0578: if (this .database == null
0579: || this .database.trim().length() == 0) {
0580: this .database = "default";
0581: }
0582: this .elements.put("name", savedSearchName);
0583: this .elements.put("scope", savedSearchScope);
0584: this .elements.put("since", savedSearchSince);
0585: this .elements.put("rating", savedSearchRating);
0586: this .elements.put("server", this .server);
0587: this .elements.put("database", this .database);
0588: this .elements.put("status", savedSearchStatus);
0589: ArrayList aL = new ArrayList();
0590: HashMap hM = new HashMap();
0591: hM.put(SearchContext.OPERAND, "");
0592: hM.put(SearchContext.OPERATION, "<AND>");
0593: hM.put(SearchContext.VALUE, savedSearchScope);
0594: aL.add(hM);
0595: srchCtx.setScope(aL);
0596: srchCtx.setDatabase(this .database);
0597: } catch (Exception e) {
0598: if (getLogger().isLoggable(Level.SEVERE)) {
0599: String param[] = { Subscription.SAVED_SRCH_SUB,
0600: this .subStr };
0601: getLogger().log(Level.SEVERE, "PSSS_CSPP0017", param);
0602: }
0603: srchCtx = (SearchContext) null;
0604: }
0605: return srchCtx;
0606: }
0607:
0608: public HashMap getElements() {
0609: return this .elements;
0610: }
0611:
0612: public String getAdvQryParam(String subScopeStr) {
0613: String ret = "&scope=";
0614: if (subScopeStr != null && subScopeStr.trim().length() != 0) {
0615: try {
0616: AdvSrchSubscription advSrchSub = this .new AdvSrchSubscription(
0617: subScopeStr);
0618: advSrchSub.parse();
0619: ret = advSrchSub.getUrlQryParams();
0620: } catch (Exception e) {
0621: }
0622: }
0623: return ret;
0624: }
0625:
0626: public static void main(String[] args) {
0627: SearchSubscription a = new SearchSubscription("", "", "");
0628: if (args.length < 1) {
0629: a.usage();
0630: } else {
0631: System.out.println("urlQryParams="
0632: + a.getAdvQryParam(args[0]));
0633: }
0634: }
0635:
0636: public void usage() {
0637: System.err.println("SearchSubscription \"query string\"");
0638: }
0639:
0640: public class AdvSrchSubscription {
0641:
0642: private String queryString;
0643:
0644: private ArrayList fields;
0645:
0646: private boolean adv = false;
0647:
0648: private String wordsOp = null;
0649:
0650: private String wordsVal = null;
0651:
0652: private String noWordsOp = null;
0653:
0654: private String noWordsVal = null;
0655:
0656: private String expiresOp = null;
0657:
0658: private String expiresVal = null;
0659:
0660: private String lastModifiedOp = null;
0661:
0662: private String lastModifiedVal = null;
0663:
0664: private String authorOp = null;
0665:
0666: private String authorVal = null;
0667:
0668: private String keywordsOp = null;
0669:
0670: private String keywordsVal = null;
0671:
0672: private String titleOp = null;
0673:
0674: private String titleVal = null;
0675:
0676: private String urlOp = null;
0677:
0678: private String urlVal = null;
0679:
0680: public AdvSrchSubscription(String qryStr) {
0681: setQueryString(qryStr);
0682: fields = new ArrayList();
0683: fields.add("author");
0684: fields.add("title");
0685: fields.add("url");
0686: fields.add("keywords");
0687: fields.add("last-modified");
0688: fields.add("expires");
0689: wordsOp = null;
0690: wordsVal = "";
0691: noWordsOp = null;
0692: noWordsVal = "";
0693: expiresOp = null;
0694: expiresVal = null;
0695: lastModifiedOp = null;
0696: lastModifiedVal = null;
0697: authorOp = null;
0698: authorVal = null;
0699: keywordsOp = null;
0700: keywordsVal = null;
0701: titleOp = null;
0702: titleVal = null;
0703: urlOp = null;
0704: urlVal = null;
0705: }
0706:
0707: public void parse() {
0708: boolean and = false;
0709: boolean andNot = false;
0710: boolean broken = false;
0711: boolean done = false;
0712: String str = getQueryString().trim();
0713: while (!done && !broken && str.length() != 0) {
0714: int lpi = str.indexOf('(');
0715: if (lpi != -1) {
0716: // check for booleans
0717: if (lpi > 0) {
0718: boolean operBroken = false;
0719: boolean operDone = false;
0720: String operStr = str.substring(0, lpi).trim();
0721: if (operStr.equalsIgnoreCase("<and>")) {
0722: and = true;
0723: andNot = false;
0724: } else if (operStr
0725: .equalsIgnoreCase("<and> <not>")) {
0726: and = false;
0727: andNot = true;
0728: } else if (operStr.trim().length() == 0) {
0729: and = true;
0730: andNot = false;
0731: wordsVal = wordsVal + " " + str;
0732: done = true;
0733: } else {
0734: and = false;
0735: andNot = false;
0736: wordsVal = wordsVal + " " + str;
0737: operBroken = true;
0738: broken = true;
0739: }
0740: if (!operBroken) {
0741: str = str.substring(lpi);
0742: }
0743: } else {
0744: // get the content of parenthesis
0745: int rpi = str.indexOf(')', lpi);
0746: if (rpi != -1 && rpi > lpi + 1) {
0747: String tupleStr = str.substring(lpi + 1,
0748: rpi).trim();
0749: if ((tupleStr.indexOf('"') == 0)
0750: && tupleStr.endsWith("\"")) {
0751: // this is an exact string
0752: wordsOp = "exact";
0753: wordsVal = tupleStr.substring(1,
0754: tupleStr.lastIndexOf('"'));
0755: setAdv(true);
0756: } else {
0757: int fieldSep = tupleStr.indexOf(" ");
0758: if (fieldSep != -1) {
0759: String field = tupleStr.substring(
0760: 0, fieldSep).trim();
0761: if (field.toLowerCase().startsWith(
0762: "<or>")) {
0763: //special case of the noWords
0764: if (andNot) {
0765: noWordsOp = "not";
0766: noWordsVal = tupleStr
0767: .substring("<or>"
0768: .length());
0769: } else {
0770: wordsVal = wordsVal + " "
0771: + tupleStr;
0772: }
0773: } else if (field.startsWith("(")) {
0774: wordsVal = wordsVal + " " + str;
0775: done = true;
0776: } else if (field.startsWith("<")) {
0777: // a potential wordsOp
0778: if (field.endsWith(">")) {
0779: int rOi = field
0780: .indexOf(">");
0781: wordsOp = field.substring(
0782: 1, rOi)
0783: .toLowerCase();
0784: if (wordsOp.equals("sor")) {
0785: wordsOp = "any"; //SearchContext.ANY
0786: } else if (wordsOp
0787: .equals("passage")) {
0788: wordsOp = "passage"; //SearchContext.PASSAGE
0789: } else {
0790: //unexpected wordsOp
0791: }
0792: if (rOi < tupleStr.length()) {
0793: wordsVal = tupleStr
0794: .substring(
0795: rOi + 1)
0796: .trim();
0797: }
0798: setAdv(true);
0799: } else {
0800: //broken
0801: wordsVal = wordsVal + " "
0802: + tupleStr;
0803: broken = true;
0804: }
0805: } else if (fieldSep < tupleStr
0806: .length()) {
0807: String opAndValStr = tupleStr
0808: .substring(fieldSep);
0809: if (fields.contains(field)) {
0810: // an advanced Search field
0811: if (field.equals("expires")
0812: || field
0813: .equals("last-modified")) {
0814: // time based field
0815: String dateOp = "";
0816: String dateVal = "";
0817: if (opAndValStr
0818: .indexOf(" < ") != -1) {
0819: dateOp = "less";
0820: dateVal = opAndValStr
0821: .substring(" < "
0822: .length());
0823: } else if (opAndValStr
0824: .indexOf(" > ") != -1) {
0825: dateOp = "greater";
0826: dateVal = opAndValStr
0827: .substring(" > "
0828: .length());
0829: } else if (opAndValStr
0830: .indexOf(" = ") != -1) {
0831: dateOp = "equal";
0832: dateVal = opAndValStr
0833: .substring(" = "
0834: .length());
0835: } else if (opAndValStr
0836: .indexOf(" != ") != -1) {
0837: dateOp = "notequal";
0838: dateVal = opAndValStr
0839: .substring(" != "
0840: .length());
0841: } else {
0842: broken = true;
0843: }
0844: if (!broken) {
0845: if (field
0846: .equals("expires")) {
0847: expiresOp = dateOp;
0848: expiresVal = dateVal;
0849: } else {
0850: lastModifiedOp = dateOp;
0851: lastModifiedVal = dateVal;
0852: }
0853: }
0854: } else {
0855: // string based field
0856: int ci = tupleStr
0857: .toLowerCase()
0858: .indexOf(
0859: "<contains>");
0860: if (ci != -1) {
0861: String value = tupleStr
0862: .substring(
0863: ci
0864: + "<contains>"
0865: .length())
0866: .trim();
0867: if (value.length() == -1) {
0868: broken = true;
0869: } else {
0870: if (field
0871: .equalsIgnoreCase("author")) {
0872: authorOp = (andNot) ? "notcontain"
0873: : "contain";
0874: authorVal = value;
0875: } else if (field
0876: .equalsIgnoreCase("title")) {
0877: titleOp = (andNot) ? "notcontain"
0878: : "contain";
0879: titleVal = value;
0880: } else if (field
0881: .equalsIgnoreCase("url")) {
0882: urlOp = (andNot) ? "notcontain"
0883: : "contain";
0884: urlVal = value;
0885: } else if (field
0886: .equalsIgnoreCase("keywords")) {
0887: keywordsOp = (andNot) ? "notcontain"
0888: : "contain";
0889: keywordsVal = value;
0890: } else {
0891: wordsVal = wordsVal
0892: + " "
0893: + field
0894: + " <contains> "
0895: + value;
0896: }
0897: }
0898: }
0899: }
0900: } else {
0901: // not an advanced Search field
0902: wordsVal = wordsVal + "("
0903: + tupleStr + ")";
0904: if (wordsOp == null)
0905: wordsOp = "all";
0906: setAdv(true);
0907: }
0908: } else {
0909: // not an advanced Search field
0910: wordsVal = wordsVal + "("
0911: + tupleStr + ")";
0912: if (wordsOp == null)
0913: wordsOp = "all";
0914: setAdv(true);
0915: }
0916: } else {
0917: and = false;
0918: andNot = false;
0919: wordsVal = wordsVal + "("
0920: + tupleStr + ")";
0921: if (wordsOp == null)
0922: wordsOp = "all";
0923: setAdv(true);
0924: }
0925: }
0926: } else {
0927: wordsVal = wordsVal + str;
0928: broken = true;
0929: }
0930: str = str.substring(rpi + 1);
0931: }
0932: } else {
0933: if (broken != true) {
0934: wordsVal = wordsVal + str;
0935: } else {
0936: wordsVal = getQueryString().trim();
0937: }
0938: done = true;
0939: }
0940: } // end while
0941: if (broken == true) {
0942: setAdv(false);
0943: }
0944: }
0945:
0946: public String getUrlQryParams() {
0947: if (!isAdv()) {
0948: return "&mode=basic&scope=" + getQueryString();
0949: } else {
0950: try {
0951: StringBuffer sb = new StringBuffer();
0952: sb.append("&mode=adv");
0953: if (getWordsOp() != null) {
0954: sb.append("&wordsOp="
0955: + Encoder.urlEncode(getWordsOp(),
0956: "UTF-8"));
0957: sb.append("&wordsVal=");
0958: String wv = getWordsVal();
0959: if (wv != null) {
0960: wv = wv.trim();
0961: if (wv.startsWith("(") && wv.endsWith(")")) {
0962: sb.append(Encoder.urlEncode(wv
0963: .substring(1, wv.length() - 1),
0964: "UTF-8"));
0965: } else {
0966: sb.append(Encoder
0967: .urlEncode(wv, "UTF-8"));
0968: }
0969: }
0970: }
0971: if (getNoWordsOp() != null) {
0972: sb.append("&noWordsOp="
0973: + Encoder.urlEncode(getNoWordsOp(),
0974: "UTF-8"));
0975: sb.append("&noWordsVal=");
0976: if (getNoWordsVal() != null)
0977: sb.append(Encoder.urlEncode(
0978: getNoWordsVal(), "UTF-8"));
0979: }
0980: if (getExpiresOp() != null) {
0981: sb.append("&expiresOp="
0982: + Encoder.urlEncode(getExpiresOp(),
0983: "UTF-8"));
0984: sb.append("&expriesVal=");
0985: if (getExpiresVal() != null)
0986: sb.append(Encoder.urlEncode(
0987: getExpiresVal(), "UTF-8"));
0988: }
0989: if (getKeywordsOp() != null) {
0990: sb.append("&keywordsOp="
0991: + Encoder.urlEncode(getKeywordsOp(),
0992: "UTF-8"));
0993: sb.append("&keywordsVal=");
0994: if (getKeywordsVal() != null)
0995: sb.append(Encoder.urlEncode(
0996: getKeywordsVal(), "UTF-8"));
0997: }
0998: if (getTitleOp() != null) {
0999: sb.append("&titleOp="
1000: + Encoder.urlEncode(getTitleOp(),
1001: "UTF-8"));
1002: sb.append("&titleVal=");
1003: if (getTitleVal() != null)
1004: sb.append(Encoder.urlEncode(getTitleVal(),
1005: "UTF-8"));
1006: }
1007: if (getAuthorOp() != null) {
1008: sb.append("&authorOp="
1009: + Encoder.urlEncode(getAuthorOp(),
1010: "UTF-8"));
1011: sb.append("&authorVal=");
1012: if (getAuthorVal() != null)
1013: sb.append(Encoder.urlEncode(getAuthorVal(),
1014: "UTF-8"));
1015: }
1016: if (getUrlOp() != null) {
1017: sb
1018: .append("&urlOp="
1019: + Encoder.urlEncode(getUrlOp(),
1020: "UTF-8"));
1021: sb.append("&urlVal=");
1022: if (getUrlVal() != null)
1023: sb.append(Encoder.urlEncode(getUrlVal(),
1024: "UTF-8"));
1025: }
1026: return sb.toString();
1027: } catch (UnsupportedEncodingException e) {
1028: if (logger.isLoggable(Level.FINEST)) {
1029: logger
1030: .finest("failed getting advance url query params");
1031: }
1032: return null;
1033: }
1034: }
1035: }
1036:
1037: /**
1038: * Getter for property authorOp.
1039: * @return Value of property authorOp.
1040: */
1041: public java.lang.String getAuthorOp() {
1042: return authorOp;
1043: }
1044:
1045: /**
1046: * Setter for property authorOp.
1047: * @param authorOp New value of property authorOp.
1048: */
1049: public void setAuthorOp(java.lang.String authorOp) {
1050: this .authorOp = authorOp;
1051: }
1052:
1053: /**
1054: * Getter for property authorVal.
1055: * @return Value of property authorVal.
1056: */
1057: public java.lang.String getAuthorVal() {
1058: return authorVal;
1059: }
1060:
1061: /**
1062: * Setter for property authorVal.
1063: * @param authorVal New value of property authorVal.
1064: */
1065: public void setAuthorVal(java.lang.String authorVal) {
1066: this .authorVal = authorVal;
1067: }
1068:
1069: /**
1070: * Getter for property expiresOp.
1071: * @return Value of property expiresOp.
1072: */
1073: public java.lang.String getExpiresOp() {
1074: return expiresOp;
1075: }
1076:
1077: /**
1078: * Setter for property expiresOp.
1079: * @param expiresOp New value of property expiresOp.
1080: */
1081: public void setExpiresOp(java.lang.String expiresOp) {
1082: this .expiresOp = expiresOp;
1083: }
1084:
1085: /**
1086: * Getter for property expiresVal.
1087: * @return Value of property expiresVal.
1088: */
1089: public java.lang.String getExpiresVal() {
1090: return expiresVal;
1091: }
1092:
1093: /**
1094: * Setter for property expiresVal.
1095: * @param expiresVal New value of property expiresVal.
1096: */
1097: public void setExpiresVal(java.lang.String expiresVal) {
1098: this .expiresVal = expiresVal;
1099: }
1100:
1101: /**
1102: * Getter for property keywordsOp.
1103: * @return Value of property keywordsOp.
1104: */
1105: public java.lang.String getKeywordsOp() {
1106: return keywordsOp;
1107: }
1108:
1109: /**
1110: * Setter for property keywordsOp.
1111: * @param keywordsOp New value of property keywordsOp.
1112: */
1113: public void setKeywordsOp(java.lang.String keywordsOp) {
1114: this .keywordsOp = keywordsOp;
1115: }
1116:
1117: /**
1118: * Getter for property keywordsVal.
1119: * @return Value of property keywordsVal.
1120: */
1121: public java.lang.String getKeywordsVal() {
1122: return keywordsVal;
1123: }
1124:
1125: /**
1126: * Setter for property keywordsVal.
1127: * @param keywordsVal New value of property keywordsVal.
1128: */
1129: public void setKeywordsVal(java.lang.String keywordsVal) {
1130: this .keywordsVal = keywordsVal;
1131: }
1132:
1133: /**
1134: * Getter for property lastModifiedOp.
1135: * @return Value of property lastModifiedOp.
1136: */
1137: public java.lang.String getLastModifiedOp() {
1138: return lastModifiedOp;
1139: }
1140:
1141: /**
1142: * Setter for property lastModifiedOp.
1143: * @param lastModifiedOp New value of property lastModifiedOp.
1144: */
1145: public void setLastModifiedOp(java.lang.String lastModifiedOp) {
1146: this .lastModifiedOp = lastModifiedOp;
1147: }
1148:
1149: /**
1150: * Getter for property lastModifiedVal.
1151: * @return Value of property lastModifiedVal.
1152: */
1153: public java.lang.String getLastModifiedVal() {
1154: return lastModifiedVal;
1155: }
1156:
1157: /**
1158: * Setter for property lastModifiedVal.
1159: * @param lastModifiedVal New value of property lastModifiedVal.
1160: */
1161: public void setLastModifiedVal(java.lang.String lastModifiedVal) {
1162: this .lastModifiedVal = lastModifiedVal;
1163: }
1164:
1165: /**
1166: * Getter for property noWordsOp.
1167: * @return Value of property noWordsOp.
1168: */
1169: public java.lang.String getNoWordsOp() {
1170: return noWordsOp;
1171: }
1172:
1173: /**
1174: * Setter for property noWordsOp.
1175: * @param noWordsOp New value of property noWordsOp.
1176: */
1177: public void setNoWordsOp(java.lang.String noWordsOp) {
1178: this .noWordsOp = noWordsOp;
1179: }
1180:
1181: /**
1182: * Getter for property noWordsVal.
1183: * @return Value of property noWordsVal.
1184: */
1185: public java.lang.String getNoWordsVal() {
1186: return noWordsVal;
1187: }
1188:
1189: /**
1190: * Setter for property noWordsVal.
1191: * @param noWordsVal New value of property noWordsVal.
1192: */
1193: public void setNoWordsVal(java.lang.String noWordsVal) {
1194: this .noWordsVal = noWordsVal;
1195: }
1196:
1197: /**
1198: * Getter for property queryString.
1199: * @return Value of property queryString.
1200: */
1201: public java.lang.String getQueryString() {
1202: return queryString;
1203: }
1204:
1205: /**
1206: * Setter for property queryString.
1207: * @param queryString New value of property queryString.
1208: */
1209: public void setQueryString(java.lang.String queryString) {
1210: this .queryString = queryString;
1211: }
1212:
1213: /**
1214: * Getter for property titleOp.
1215: * @return Value of property titleOp.
1216: */
1217: public java.lang.String getTitleOp() {
1218: return titleOp;
1219: }
1220:
1221: /**
1222: * Setter for property titleOp.
1223: * @param titleOp New value of property titleOp.
1224: */
1225: public void setTitleOp(java.lang.String titleOp) {
1226: this .titleOp = titleOp;
1227: }
1228:
1229: /**
1230: * Getter for property titleVal.
1231: * @return Value of property titleVal.
1232: */
1233: public java.lang.String getTitleVal() {
1234: return titleVal;
1235: }
1236:
1237: /**
1238: * Setter for property titleVal.
1239: * @param titleVal New value of property titleVal.
1240: */
1241: public void setTitleVal(java.lang.String titleVal) {
1242: this .titleVal = titleVal;
1243: }
1244:
1245: /**
1246: * Getter for property urlOp.
1247: * @return Value of property urlOp.
1248: */
1249: public java.lang.String getUrlOp() {
1250: return urlOp;
1251: }
1252:
1253: /**
1254: * Setter for property urlOp.
1255: * @param urlOp New value of property urlOp.
1256: */
1257: public void setUrlOp(java.lang.String urlOp) {
1258: this .urlOp = urlOp;
1259: }
1260:
1261: /**
1262: * Getter for property urlVal.
1263: * @return Value of property urlVal.
1264: */
1265: public java.lang.String getUrlVal() {
1266: return urlVal;
1267: }
1268:
1269: /**
1270: * Setter for property urlVal.
1271: * @param urlVal New value of property urlVal.
1272: */
1273: public void setUrlVal(java.lang.String urlVal) {
1274: this .urlVal = urlVal;
1275: }
1276:
1277: /**
1278: * Getter for property wordsOp.
1279: * @return Value of property wordsOp.
1280: */
1281: public java.lang.String getWordsOp() {
1282: return wordsOp;
1283: }
1284:
1285: /**
1286: * Setter for property wordsOp.
1287: * @param wordsOp New value of property wordsOp.
1288: */
1289: public void setWordsOp(java.lang.String wordsOp) {
1290: this .wordsOp = wordsOp;
1291: }
1292:
1293: /**
1294: * Getter for property wordsVal.
1295: * @return Value of property wordsVal.
1296: */
1297: public java.lang.String getWordsVal() {
1298: return wordsVal;
1299: }
1300:
1301: /**
1302: * Setter for property wordsVal.
1303: * @param wordsVal New value of property wordsVal.
1304: */
1305: public void setWordsVal(java.lang.String wordsVal) {
1306: this .wordsVal = wordsVal;
1307: }
1308:
1309: /**
1310: * Getter for property adv.
1311: * @return Value of property adv.
1312: */
1313: public boolean isAdv() {
1314: return adv;
1315: }
1316:
1317: /**
1318: * Setter for property adv.
1319: * @param adv New value of property adv.
1320: */
1321: public void setAdv(boolean adv) {
1322: this .adv = adv;
1323: }
1324:
1325: }
1326:
1327: public Logger getLogger() {
1328: return logger;
1329: }
1330:
1331: public void setLogger(Logger logger) {
1332: this.logger = logger;
1333: }
1334:
1335: }
|