001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * any nuclear facility.
035: */
036:
037: package com.sun.portal.app.communityportlets.search.plugins;
038:
039: import com.sun.portal.community.CommunityException;
040: import com.sun.portal.community.CommunityFactory;
041: import com.sun.portal.log.common.PortalLogger;
042: import com.sun.portal.search.soif.SOIF;
043: import java.util.logging.Logger;
044: import com.sun.portal.community.CommunityId;
045: import com.sun.portal.community.CommunityUser;
046: import com.sun.portal.community.RoleId;
047: import com.sun.portal.community.urlmanager.CommunityURLManagerFactory;
048: import com.sun.portal.desktop.DesktopParameters;
049: import com.sun.portal.portlet.impl.PortletRequestConstants;
050: import com.sun.portal.search.providers.faces.SearchHit;
051: import com.sun.portal.search.providers.faces.SearchHitFactory;
052: import java.text.MessageFormat;
053: import java.util.StringTokenizer;
054: import java.util.logging.Level;
055: import javax.faces.context.ExternalContext;
056: import javax.faces.context.FacesContext;
057: import javax.portlet.ActionRequest;
058: import javax.portlet.PortletRequest;
059: import javax.servlet.http.HttpServletRequest;
060:
061: /**
062: *
063: * @author #ac120954
064: */
065: public class CommunityContentHit extends SearchHit {
066: private static final String[] attributes = new String[] {
067: "rd-display-url", "hl-title", "title", "hl-description",
068: "description", "classification", "author",
069: "rd-last-changed", "virtual-db", "channelname" };
070: private static Logger logger = PortalLogger
071: .getLogger(CommunityContentHit.class);
072: private static final String infoPortletUrl = "{0}?dt.isPortletRequest=true&dt.action=process&dt.provider=PortletWindowProcessChannel&dt.windowProvider.targetPortletChannel={1}_Container/CommunityInfo&dt.containerName={1}_Container&dt.windowProvider.currentChannelMode=VIEW&dt.window.portletAction=ACTION&{2}&SearchRequestUrl={3}&RedirectUrl={4}";
073: private static final String wikiPortletUrl = "{0}?dt.isPortletRequest=true&dt.action=process&dt.provider=PortletWindowProcessChannel&dt.windowProvider.targetPortletChannel={1}_Container/Wiki&dt.containerName={1}_Container&dt.windowProvider.currentChannelMode=VIEW&dt.window.portletAction=ACTION&{2}&wikipage={2}&wikijsp=Wiki.jsp";
074: private static final String[] supportType = { SearchHitFactory.HIT_COMMUNITY_CONTENT };
075:
076: private CommunityId communityId = null;
077:
078: public CommunityContentHit() {
079: super ();
080: };
081:
082: public CommunityContentHit(SOIF soif) {
083: super (soif);
084: parseCommunityId(soif);
085:
086: }
087:
088: private void parseCommunityId(SOIF soif) {
089: String vdb = soif.getValue("virtual-db");
090: if (vdb != null) {
091: StringTokenizer tokenizer = new StringTokenizer(vdb, ".");
092: if (tokenizer.hasMoreTokens()) {
093: String id = tokenizer.nextToken();
094: try {
095: communityId = new CommunityId(id);
096: } catch (Exception e) {
097: logger.log(Level.FINEST,
098: "Fail to create CommunityId by :" + id
099: + " Exception:" + e);
100: }
101: }
102: }
103: }
104:
105: public static String getSupportAttribute() {
106: StringBuffer sb = new StringBuffer();
107: for (int i = 0; i < attributes.length; i++) {
108: sb.append(attributes[i]);
109: if (i + 1 < attributes.length) {
110: sb.append(",");
111: }
112: }
113: return sb.toString();
114: }
115:
116: private String communityCurrentMembershipParameter(String url) {
117: StringTokenizer tokenizer = new StringTokenizer(url, "&");
118: while (tokenizer.hasMoreTokens()) {
119: String p = tokenizer.nextToken();
120: if (p.startsWith(DesktopParameters.CURRENT_MEMBERSHIP)) {
121: return p;
122: }
123: }
124: return null;
125: }
126:
127: private String communityContainerSwitchParameter(String url) {
128: String p = "";
129: int i = url.indexOf('?');
130: if (i > 0) {
131: StringTokenizer tokenizer = new StringTokenizer(url
132: .substring(i + 1), "&");
133: while (tokenizer.hasMoreTokens()) {
134: String ps = tokenizer.nextToken();
135: if (!ps
136: .startsWith(DesktopParameters.CURRENT_MEMBERSHIP)) {
137: if (p.length() > 0) {
138: p += "&";
139: }
140: p += ps;
141: }
142: }
143: }
144: return p;
145: }
146:
147: public void showParent() {
148:
149: String vdb = soif.getValue("virtual-db");
150: StringTokenizer tokenizer = new StringTokenizer(vdb, ".");
151: String id = tokenizer.nextToken();
152: try {
153: String ctyUrl = CommunityURLManagerFactory.getInstance()
154: .getCommunityURLManager().getCommunityAccessURL(
155: getHttpServletRequest(),
156: new CommunityId(id));
157: sendRedirect(ctyUrl);
158: } catch (Exception e) {
159: logger.log(Level.WARNING, "community_gotoCommunity_failed",
160: e);
161: }
162:
163: }
164:
165: protected static HttpServletRequest getHttpServletRequest() {
166:
167: ExternalContext eContext = FacesContext.getCurrentInstance()
168: .getExternalContext();
169: PortletRequest pReq = (PortletRequest) eContext.getRequest();
170:
171: HttpServletRequest req = (HttpServletRequest) pReq
172: .getAttribute(PortletRequestConstants.HTTP_SERVLET_REQUEST);
173:
174: return req;
175: }
176:
177: protected CommunityUser getCommunityUser() {
178: HttpServletRequest req = getHttpServletRequest();
179:
180: CommunityUser cu = null;
181: try {
182:
183: cu = CommunityFactory.getInstance().getCommunityUser(req);
184:
185: } catch (CommunityException ce) {
186: //
187:
188: }
189:
190: return cu;
191: }
192:
193: public String gotoDocument() {
194: String url = null;
195: String vdb = soif.getValue("virtual-db");
196: if (vdb == null) {
197: url = soif.getValue("rd-display-url");
198: if (url == null) {
199: url = soif.getURL();
200: }
201: } else {
202: StringTokenizer tokenizer = new StringTokenizer(vdb, ".");
203: String id = tokenizer.nextToken();
204: try {
205: CommunityId cid = new CommunityId(id);
206: } catch (Exception cide) {
207: url = soif.getValue("rd-display-url");
208: if (url == null) {
209: url = soif.getURL();
210: }
211: }
212: if (url == null) {
213:
214: try {
215: ExternalContext eContext = FacesContext
216: .getCurrentInstance().getExternalContext();
217: ActionRequest aReq = (ActionRequest) eContext
218: .getRequest();
219: HttpServletRequest httpReq = (HttpServletRequest) aReq
220: .getAttribute(PortletRequestConstants.HTTP_SERVLET_REQUEST);
221:
222: String ctyUrl = CommunityURLManagerFactory
223: .getInstance().getCommunityURLManager()
224: .getCommunityURL(getHttpServletRequest(),
225: new CommunityId(id));
226: String ctyParameter = communityCurrentMembershipParameter(ctyUrl);
227: String ctyContainerParameter = communityContainerSwitchParameter(ctyUrl);
228: //String tmpParameter = communityTempMembershipParameter(ctyUrl);
229: CommunityId cid = new CommunityId(id);
230: String type = getContentType();
231: CommunityUser cUser = getCommunityUser();
232: if (cUser != null
233: && cUser.hasRole(cid, RoleId.MEMBER_ROLE)
234: && !cUser.hasRole(cid, RoleId.BANNED_ROLE)) {
235: url = soif.getValue("rd-display-url");
236:
237: if ("discussions".equals(type)) {
238: String channelName = soif
239: .getValue("channelname");
240: StringTokenizer tokenizer1 = new StringTokenizer(
241: channelName, "/");
242: String conatinerName = tokenizer1
243: .nextToken();
244: url = ctyUrl
245: + "&"
246: + conatinerName
247: + ".setSelected="
248: + channelName
249: + "&"
250: + soif.getValue("channelname")
251: + "_dmode=vl&did="
252: + com.sun.portal.search.util.Encoder
253: .urlEncode(soif.getURL(),
254: "UTF-8");
255: } else if ("wiki".equals(type)) {
256: url = soif.getURL();
257: int i = url.indexOf("page=");
258: if (i > 0) {
259: String page = com.sun.portal.search.util.Encoder
260: .urlEncode(
261: url.substring(i + 5),
262: "UTF-8");
263: url = MessageFormat
264: .format(
265: this .wikiPortletUrl,
266: new String[] {
267: httpReq
268: .getRequestURI(),
269: cid.toString(),
270: page });
271: String redirectUrl = cid.toString()
272: + "_Container.setSelected="
273: + cid.toString()
274: + "_Container/Wiki&"
275: + ctyContainerParameter;
276:
277: url += "&"
278: + ctyParameter
279: + "&dt.newargs="
280: + com.sun.portal.search.util.Encoder
281: .urlEncode(redirectUrl,
282: "UTF-8");
283: }
284: } else {
285:
286: if (url == null) {
287: url = soif.getURL();
288: } else if (ctyParameter != null) {
289: url += "&"
290: + ctyParameter
291: + "&RedirectUrl="
292: + com.sun.portal.search.util.Encoder
293: .urlEncode(ctyUrl,
294: "UTF-8");
295: }
296: }
297:
298: } else {
299: String[] args = new String[5];
300: args[0] = httpReq.getRequestURI();
301: args[1] = cid.toString();
302: //args[2] = ctyParameter + "&" + tmpParameter;
303: args[2] = ctyParameter;
304: if ("discussions".equals(type)) {
305: String channelName = soif
306: .getValue("channelname");
307: StringTokenizer tokenizer1 = new StringTokenizer(
308: channelName, "/");
309: String conatinerName = tokenizer1
310: .nextToken();
311: String durl = args[0]
312: + "?"
313: + conatinerName
314: + ".setSelected="
315: + channelName
316: + "&"
317: + soif.getValue("channelname")
318: + "_dmode=vl&did="
319: + com.sun.portal.search.util.Encoder
320: .urlEncode(soif.getURL(),
321: "UTF-8");
322:
323: args[3] = com.sun.portal.search.util.Encoder
324: .urlEncode(durl, "UTF-8");
325: } else if ("wiki".equals(type)) {
326: String u = soif.getURL();
327: int i = u.indexOf("page=");
328: if (i > 0) {
329: String page = com.sun.portal.search.util.Encoder
330: .urlEncode(u.substring(i + 5),
331: "UTF-8");
332: u = MessageFormat
333: .format(
334: this .wikiPortletUrl,
335: new String[] {
336: httpReq
337: .getRequestURI(),
338: cid.toString(),
339: page });
340: String redirectUrl = cid.toString()
341: + "_Container.setSelected="
342: + cid.toString()
343: + "_Container/Wiki";
344: u += "&dt.newargs="
345: + com.sun.portal.search.util.Encoder
346: .urlEncode(redirectUrl,
347: "UTF-8");
348: args[3] = com.sun.portal.search.util.Encoder
349: .urlEncode(u, "UTF-8");
350: }
351:
352: } else {
353: String u = soif.getValue("rd-display-url");
354: if (u == null) {
355: u = soif.getURL();
356: }
357: args[3] = com.sun.portal.search.util.Encoder
358: .urlEncode(u, "UTF-8");
359: }
360: args[4] = com.sun.portal.search.util.Encoder
361: .urlEncode(ctyUrl, "UTF-8");
362: url = MessageFormat.format(this .infoPortletUrl,
363: args);
364: }
365: } catch (Exception e) {
366: logger.log(Level.WARNING,
367: "community_showrd_failed", e);
368: return null;
369: }
370: }
371: }
372: if (url != null) {
373: try {
374: sendRedirect(url);
375: } catch (Exception e) {
376: logger.log(Level.WARNING, "sendRedirect_failed", e);
377:
378: }
379: }
380: return null;
381:
382: }
383:
384: public String getParentName() {
385: if (this .communityId != null) {
386: return communityId.getName();
387: }
388: return "";
389: }
390:
391: public String getIconUrl() {
392: String type = getContentType();
393: if (type != null && type.length() > 0) {
394: return "/images/" + type + "_icon.jpg";
395: }
396: return "/images/blank.jpg";
397:
398: }
399:
400: /*
401: * expect virtualdb tag with value of communityId.contentType
402: */
403: public String getContentType7_0() {
404: String vdb = soif.getValue("virtual-db");
405: if (vdb != null) {
406: int i = vdb.lastIndexOf('.');
407: if (i > 0) {
408: return vdb.substring(i + 1);
409: }
410: }
411: return null;
412: }
413:
414: public String getContentType() {
415: String contentType = getContentType7_0();
416: if (contentType != null) {
417: return contentType;
418: }
419: String dbname = getSourceDatabaseName();
420: if (dbname != null) {
421: String[] parts = dbname.split(".");
422: if (parts.length > 1) {
423: dbname = parts[0];
424: }
425: /* expect to get communityPrefix_dbname_portalID in parts[0] */
426: int s = dbname.indexOf('_');
427: int e = dbname.lastIndexOf('_');
428: if (s > 0 && e > 0 && e > s) {
429: return dbname.substring(s + 1, e);
430: }
431: }
432: return "unknown";
433: }
434:
435: public String getType() {
436: return SearchHitFactory.HIT_COMMUNITY_CONTENT;
437: }
438:
439: public SearchHit createInstance(SOIF soif, String defaultType) {
440: for (int i = 0; i < this .supportType.length; i++) {
441: if (supportType[i].equals(defaultType)) {
442: return new CommunityContentHit(soif);
443: }
444: }
445: return null;
446: }
447:
448: public String gotoParent() {
449: try {
450: String url = CommunityURLManagerFactory.getInstance()
451: .getCommunityURLManager().getCommunityAccessURL(
452: getHttpServletRequest(), communityId);
453: this .sendRedirect(url);
454: } catch (Exception e) {
455: logger.log(Level.FINEST, "gotoCommunity_failed", e);
456: }
457: return null;
458: }
459:
460: public boolean isHasParent() {
461: return (communityId != null);
462: }
463: }
|