001: package com.sun.portal.app.collab.survey.util;
002:
003: import java.util.Locale;
004:
005: import javax.faces.context.FacesContext;
006: import javax.faces.context.ExternalContext;
007: import com.iplanet.sso.SSOToken;
008: import com.iplanet.sso.SSOException;
009: import javax.servlet.ServletContext;
010: import javax.portlet.PortletRequest;
011: import javax.portlet.PortletContext;
012: import com.sun.portal.community.CommunityFactory;
013: import com.sun.portal.community.CommunityId;
014: import javax.portlet.PortletPreferences;
015:
016: import com.sun.portal.log.common.PortalLogger;
017: import java.util.logging.Logger;
018:
019: import com.sun.portal.app.collabcommon.LocalizedResourceManager;
020: import com.sun.portal.search.demo.SearchDatabase;
021:
022: public class SurveyUtil {
023:
024: public static String APPTYPE = "application_type";
025: public static String SURVEY_HELPFILE = "/surveys//html/help/surveys.html";
026: public static String POLL_HELPFILE = "/surveys/html/help/polls.html";
027: public static String CAN_DELETE = "canDelete";
028:
029: private static Logger logger = PortalLogger
030: .getLogger(SurveyUtil.class);
031:
032: public boolean findAppType() {
033: boolean isSurvey = true;
034: String appType = "";
035: try {
036: Object obj = FacesContext.getCurrentInstance()
037: .getExternalContext().getRequest();
038: if (obj instanceof PortletRequest) {
039: PortletRequest req = (PortletRequest) obj;
040: PortletPreferences pp = req.getPreferences();
041: appType = pp.getValue(APPTYPE, null);
042: if (appType != null
043: && appType.equalsIgnoreCase("SURVEY")) {
044: isSurvey = true;
045: } else {
046: isSurvey = false;
047: }
048: }
049: } catch (Exception ex) {
050: ex.printStackTrace();
051: }
052: return isSurvey;
053: }
054:
055: public boolean isCanDelete() {
056: boolean canDelete = false;
057: String canDeleteProp = "false";
058: try {
059: Object obj = FacesContext.getCurrentInstance()
060: .getExternalContext().getRequest();
061: if (obj instanceof PortletRequest) {
062: PortletRequest req = (PortletRequest) obj;
063: PortletPreferences pp = req.getPreferences();
064: canDeleteProp = pp.getValue(CAN_DELETE, null);
065: if (canDeleteProp != null
066: && canDeleteProp.equalsIgnoreCase("true")) {
067: canDelete = true;
068: } else {
069: canDelete = false;
070: }
071: }
072: } catch (Exception ex) {
073: ex.printStackTrace();
074: }
075: return canDelete;
076: }
077:
078: public boolean getIsSurvey() {
079: return findAppType();
080: }
081:
082: public boolean getIsPoll() {
083: return !findAppType();
084: }
085:
086: public boolean isSurvey() {
087: return findAppType();
088: }
089:
090: public boolean isPoll() {
091: return !findAppType();
092: }
093:
094: public String getUserId() {
095: String userId = null;
096: try {
097: Object obj = FacesContext.getCurrentInstance()
098: .getExternalContext().getRequest();
099: if (obj instanceof PortletRequest) {
100: PortletRequest req = (PortletRequest) obj;
101: SSOToken ssoToken = (SSOToken) req
102: .getAttribute("javax.portlet.portletc.ssotoken");
103: String userDN = ssoToken.getPrincipal().getName();
104: userId = userDN.substring((userDN.indexOf("=") + 1),
105: userDN.indexOf(","));
106: }
107: } catch (SSOException ssoexp) {
108: ssoexp.printStackTrace();
109: }
110: logger.fine("User ID = " + userId);
111: return userId;
112: }
113:
114: public SearchDatabase getSearchDatabase() {
115: SearchDatabase searchdb = null;
116: try {
117: Object obj = FacesContext.getCurrentInstance()
118: .getExternalContext().getRequest();
119: if (obj instanceof PortletRequest) {
120: PortletRequest req = (PortletRequest) obj;
121: SSOToken ssoToken = (SSOToken) req
122: .getAttribute("javax.portlet.portletc.ssotoken");
123: PortletPreferences pp = req.getPreferences();
124: String searchserverurl = pp.getValue(
125: "ps.searchserverurl", null);
126: String dbName = pp.getValue("ps.searchdb", null);
127: logger.fine("create search database:url="
128: + searchserverurl + " database=" + dbName);
129: searchdb = new SearchDatabase(searchserverurl, dbName);
130: if (ssoToken != null) {
131: searchdb.setSessionID(ssoToken.getTokenID()
132: .toString());
133: }
134: }
135: } catch (Exception ex) {
136: ex.printStackTrace();
137: }
138: return searchdb;
139: }
140:
141: public String getRedirctUrl() {
142: try {
143: Object obj = FacesContext.getCurrentInstance()
144: .getExternalContext().getRequest();
145: if (obj instanceof PortletRequest) {
146: PortletRequest req = (PortletRequest) obj;
147: PortletPreferences pp = req.getPreferences();
148: return pp.getValue("ps.visibleurl", null);
149: }
150: } catch (Exception ex) {
151: ex.printStackTrace();
152: }
153: return null;
154: }
155:
156: public String getCommunityId() {
157: CommunityId communityId = null;
158: try {
159: Object obj = FacesContext.getCurrentInstance()
160: .getExternalContext().getRequest();
161: if (obj instanceof PortletRequest) {
162: PortletRequest req = (PortletRequest) obj;
163: PortletPreferences pp = req.getPreferences();
164: String communityIdString = pp.getValue(
165: CommunityFactory.COMMUNITY_ID_PREF, null);
166: communityId = new CommunityId(communityIdString);
167: }
168: } catch (Exception ex) {
169: ex.printStackTrace();
170: }
171: logger.fine("CommunityID = " + communityId);
172: return communityId.toString();
173: }
174:
175: public String getHelpFileName() {
176: String helpFileName = null;
177: ExternalContext externalContext = FacesContext
178: .getCurrentInstance().getExternalContext();
179: Object context = externalContext.getContext();
180: Object req = externalContext.getRequest();
181: Locale locale = externalContext.getRequestLocale();
182: if (context instanceof PortletContext) {
183: helpFileName = LocalizedResourceManager.getLocalizedFile(
184: (PortletContext) context, (isPoll() ? POLL_HELPFILE
185: : SURVEY_HELPFILE), locale);
186: } else if (context instanceof ServletContext) {
187: helpFileName = LocalizedResourceManager.getLocalizedFile(
188: (ServletContext) context, (isPoll() ? POLL_HELPFILE
189: : SURVEY_HELPFILE), locale);
190: }
191: return helpFileName;
192: }
193:
194: }
|