001: /*
002: * $Id: MailPrefsContext.java,v 1.6 2005/09/21 10:51:51 dg154973 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and iPlanet
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */
014:
015: package com.sun.portal.wireless.taglibs.commprefs;
016:
017: import com.iplanet.sso.SSOToken;
018: import com.sun.portal.log.common.PortalLogger;
019: import com.sun.portal.wireless.taglibs.base.Context;
020: import com.sun.portal.wireless.taglibs.base.ContextCache;
021: import com.sun.portal.wireless.taglibs.mail.MailContext;
022: import com.sun.portal.wireless.util.AppConfigConstants;
023: import com.sun.portal.wireless.util.SSOAdapterPrivate;
024: import com.sun.ssoadapter.SSOAdapter;
025: import com.sun.ssoadapter.SSOAdapterSession;
026: import com.sun.ssoadapter.config.Configuration;
027: import com.sun.ssoadapter.config.ConfigurationException;
028: import com.sun.ssoadapter.config.ConfigurationFactory;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.jsp.PageContext;
032: import java.util.*;
033: import java.util.logging.Level;
034: import java.util.logging.Logger;
035:
036: /**
037: * MailPrefsContext - set up a useful mail context
038: * for mail preferences editing.
039: *
040: * @version 1.0
041: * @see ContextCache
042: */
043:
044: public class MailPrefsContext extends MailContext {
045:
046: public static final String CONTEXT_CACHE_CLASS_NAME = "com.sun.portal.wireless.taglibs.commprefs.MailPrefsContextCache";
047: public static final String CONTEXT_CLASS_NAME = "com.sun.portal.wireless.taglibs.commprefs.MailPrefsContext";
048: public static final String CONTEXT_TYPE = "prefscontext";
049: private static final int NUMBER_OF_PRESET_MSGS = 9;
050: private static final String DEVICE_NAMES = "DeviceNames";
051:
052: // TODO: replace with providerContext.getCollectionProperty(...);
053: private static final Vector clientTypes = new Vector();
054: static {
055: clientTypes.add("Nokia6310i_1.0");
056: clientTypes
057: .add("Nokia6310i_1.0_(4.06)_Profile_MIDP-1.0_Configuration_CLDC-1.");
058: clientTypes
059: .add("Nokia6310i_1.0_(4.07)_Profile_MIDP-1.0_Configuration_CLDC-1.");
060: }
061:
062: private static Logger logger = PortalLogger
063: .getLogger(MailPrefsContext.class);
064: private Vector rules = new Vector();
065: // The presentation name of the device types (look up in device names bundle)
066: private Vector clientNames = new Vector();
067:
068: /**
069: * If properties are set on the mail app prefs config, then set this 'dirty bit',
070: * so that config will be written out (at end of MailPrefsContextTag, or end of session).
071: */
072: private boolean mailAppConfigWillUpdate = false;
073:
074: private ConfigurationFactory ssoConfigFactory = null;
075:
076: public void init(HttpServletRequest request, SSOToken session,
077: SSOAdapter ssoAdapter) throws Exception {
078: super .init(request, session,
079: AppConfigConstants.MAIL_APP_SERVICE_NAME, ssoAdapter);
080:
081: ruleFactory = ConfigurationFactory.getInstance(
082: AppConfigConstants.MAIL_APP_SERVICE_NAME,
083: AppConfigConstants.MAIL_APP_RULE_LIST);
084:
085: viewFactory = ConfigurationFactory.getInstance(
086: AppConfigConstants.MAIL_APP_SERVICE_NAME,
087: AppConfigConstants.MAIL_APP_VIEW_LIST);
088:
089: initAppConfig(request);
090: initPresetMsgs();
091: initPopConfigs(request);
092: initRuleCollection(request);
093: initViewCollection(request);
094:
095: /**
096: * Initialize the presentable client names from client types.
097: */
098: //deviceNamesBundle = provider.getResourceBundle(PROPERTIES_DEV);
099: ResourceBundle deviceNamesBundle = ResourceBundle
100: .getBundle(DEVICE_NAMES);
101: Iterator itr = clientTypes.iterator();
102: while (itr.hasNext()) {
103: String type = (String) itr.next();
104: String name = type;
105: if (deviceNamesBundle != null) {
106: try {
107: deviceNamesBundle.getString(type);
108: } catch (Exception e) {
109: logger.log(Level.INFO, "PSMA_CSPWTP0003", type);
110: }
111: }
112: clientNames.add(name);
113: }
114:
115: }
116:
117: private void initAppConfig(HttpServletRequest request) {
118: //
119: // Read "per-account" application preferences...
120: //
121: mailAppConfigFactory = ConfigurationFactory.getInstance(
122: AppConfigConstants.MAIL_APP_SERVICE_NAME,
123: AppConfigConstants.MAIL_APP_ATTR_CONFIGURATIONS_NAME,
124: AppConfigConstants.MAIL_APP_ATTR_TEMPLATES_NAME);
125:
126: try {
127: String configName = ssoAdapter.getName();
128: mailAppConfig = mailAppConfigFactory.readConfiguration(
129: configName, true, new SSOAdapterSession(request));
130: if (mailAppConfig == null) {
131: int i = configName.indexOf('_');
132: if (i >= 0) {
133: mailAppConfig = mailAppConfigFactory
134: .readConfiguration(configName.substring(0,
135: i), true, new SSOAdapterSession(
136: request));
137: }
138: if (mailAppConfig == null) {
139: mailAppConfig = new Configuration();
140: }
141: }
142: } catch (Exception e) {
143: mailAppConfig = new Configuration();
144: }
145:
146: }
147:
148: // Get all the pop configurations
149: protected void initPopConfigs(HttpServletRequest request)
150: throws Exception {
151:
152: ssoConfigFactory = ConfigurationFactory.getInstance(
153: SSOAdapterPrivate.SSO_SERVICE_NAME,
154: SSOAdapterPrivate.SSO_ATTR_CONFIGURATIONS_NAME,
155: SSOAdapterPrivate.SSO_ATTR_TEMPLATES_NAME);
156:
157: //
158: // TODO: This is bogus. We should only be collecting POP configurations here and the "config" tag
159: // be changed to something like the "popcollectionsconfig" tag...
160: //
161: popConfigs = new Vector();
162: Enumeration configEnum = ssoConfigFactory
163: .getConfigurationNames(new SSOAdapterSession(request));
164: if (configEnum != null) {
165:
166: // iterate through each configuration and add it to the ConfigBean list
167: while (configEnum.hasMoreElements()) {
168: String configName = (String) configEnum.nextElement();
169: Configuration configProps = ssoConfigFactory
170: .readConfiguration(configName, true,
171: new SSOAdapterSession(request));
172:
173: if (configProps != null
174: && configProps.getConfigurationDescription() != null
175: && configProps
176: .getConfigurationDescription()
177: .equals(
178: AppConfigConstants.POP_COLLECTIONS_TEMPLATE_NAME)) {
179: popConfigs.add(new PopConfigBean(configName,
180: configProps));
181: }
182: }
183: }
184:
185: }
186:
187: private void initRuleCollection(HttpServletRequest request) {
188:
189: Enumeration ruleNames = ruleFactory
190: .getConfigurationNames(new SSOAdapterSession(request));
191: rules = new Vector();
192: while (ruleNames.hasMoreElements()) {
193: String ruleName = String.valueOf(ruleNames.nextElement());
194: Configuration ruleConfig = null;
195: try {
196: ruleConfig = ruleFactory.readConfiguration(ruleName,
197: false, new SSOAdapterSession(request));
198: rules.add(new RuleBean(ruleName, ruleConfig));
199: } catch (ConfigurationException e) {
200: logger.log(Level.WARNING, "PSMA_CSPWTP0004", e);
201: }
202: }
203: }
204:
205: protected void initViewCollection(HttpServletRequest request) {
206:
207: views = new Vector();
208: Enumeration viewNames = viewFactory
209: .getConfigurationNames(new SSOAdapterSession(request));
210: while (viewNames.hasMoreElements()) {
211: String viewName = (String) viewNames.nextElement();
212: Configuration conf = null;
213: try {
214: conf = viewFactory.readConfiguration(viewName, false,
215: new SSOAdapterSession(request));
216: } catch (ConfigurationException e) {
217: logger.log(Level.WARNING, "PSMA_CSPWTP0005", e);
218: }
219: views.add(new ViewBean(viewName, conf));
220: }
221: }
222:
223: /**
224: * Get the preset messages from user's and profile and encapsulate
225: * them in PresetMsg beans.
226: *
227: */
228: public void initPresetMsgs() {
229:
230: // Collection of preset messages to return
231: presetMessages = new Vector();
232:
233: // Read the predefined message list from the Profile
234: Vector profilePresetMsgs = new Vector(
235: getAttributeSet("sunMobileAppMailPredefinedReplies"));
236:
237: int numProfPresetMsgs = profilePresetMsgs.size();
238: for (int i = 0; i < NUMBER_OF_PRESET_MSGS; i++) {
239:
240: /** The profile preset msgs are a set of Strings of form:
241: * preset7=Hi There
242: * Extract these values (while they exist) and construct 9 (possibly empty)
243: * PresetMsg beans.
244: */
245: String value = "";
246: if (i < numProfPresetMsgs) {
247: // full string is 'preset5=foobar'
248: String nameValue = profilePresetMsgs.elementAt(i)
249: .toString();
250: // Extract part after '=' e.g. foobar
251: int delimPos = nameValue.indexOf('=');
252: // if no '=' ignore this prof preset
253: if (delimPos != -1) {
254: value = nameValue.substring(delimPos + 1, nameValue
255: .length());
256: }
257: }
258: presetMessages.add(new PresetMsg(value, i + 1));
259: }
260:
261: }
262:
263: /**
264: * Returns a MailPrefsContext for this session. Assumes that the MailContext
265: * found is a MailPrefsContext.
266: *
267: * @param pageContext
268: */
269: public static MailPrefsContext getPrefsContext(
270: PageContext pageContext) throws Exception {
271: return (MailPrefsContext) Context.getContext(pageContext,
272: CONTEXT_CLASS_NAME, CONTEXT_CACHE_CLASS_NAME,
273: CONTEXT_TYPE);
274: }
275:
276: public void setSignature(String value) {
277: setAttributeString("sunMobileAppMailSignature", value);
278: }
279:
280: public boolean sentFolderCopy() {
281: if ((getSaveSent() == null) || (getSaveSent().equals(""))) {
282: return false;
283: } else {
284: Boolean b = new Boolean(getSaveSent());
285: return b.booleanValue();
286: }
287: }
288:
289: public String getSaveSent() {
290: String value = mailAppConfig.getProperty("sentFolderCopy");
291: return value;
292: }
293:
294: public void setSaveSent(String saveSent) {
295: mailAppConfig.setProperty("sentFolderCopy", saveSent);
296: mailAppConfigWillUpdate = true;
297: }
298:
299: /**
300: * Get the fromAddress value stored in the mail app preferences.
301: * @return The fromAddress pref value.
302: */
303: public String getFromAddress() {
304: return mailAppConfig.getProperty("fromAddress");
305: }
306:
307: public void setFromAddress(String value) {
308: mailAppConfig.setProperty("fromAddress", value);
309: mailAppConfigWillUpdate = true;
310: }
311:
312: public void addConfig(PopConfigBean config) {
313: popConfigs.add(config);
314: }
315:
316: public void addView(ViewBean view) {
317: views.add(view);
318: }
319:
320: public void addRule(RuleBean rule) {
321: rules.add(rule);
322: }
323:
324: public Vector getRulesCollection() {
325: return rules;
326: }
327:
328: // TODO: get these from providerContext.getCollectionProperty(...);
329: public Vector getClientTypes() {
330: return clientTypes;
331: }
332:
333: // Short, presentable version of client types
334: public Vector getClientNames() {
335: return clientNames;
336: }
337:
338: /**
339: * clientType: this should be set prior to calling getClientVews or setClientViews
340: */
341: private String clientType = null;
342:
343: public String getClientType() {
344: return clientType;
345: }
346:
347: public void setClientType(String clientType) {
348: this .clientType = clientType;
349: }
350:
351: /**
352: * Get the views for a specifc client (set via setClientType)
353: */
354: public Vector getClientViews() {
355: if (clientType == null)
356: return new Vector();
357: Set valSet = getAttributeSet(
358: AppConfigConstants.MAIL_APP_VIEW_CLIENTS_LIST,
359: clientType);
360: if (valSet == null)
361: return new Vector();
362: return new Vector(valSet);
363: }
364:
365: // Cache the added views prior to saving to user context
366: private Vector clientViews = new Vector();
367:
368: /**
369: * Add to the cached clientView set. null will clear the set.
370: */
371: public void setClientView(String clientView) {
372: if (clientView == null) {
373: clientViews = new Vector();
374: } else {
375: clientViews.add(clientView);
376: }
377: }
378:
379: /**
380: * If the mail apps prefs config has been updated, then write config.
381: */
382: public void maybeWriteConfiguration(HttpServletRequest request) {
383: if (mailAppConfigWillUpdate) {
384: try {
385: mailAppConfigFactory.writeConfiguration(mailAppConfig,
386: new SSOAdapterSession(request));
387: mailAppConfigWillUpdate = false;
388: } catch (ConfigurationException e) {
389: //TODO: handle this more gracefully
390: e.printStackTrace();
391: }
392: }
393: maybeSaveConfigBeans(popConfigs, ssoConfigFactory, request);
394: maybeSaveConfigBeans(rules, ruleFactory, request);
395: maybeSaveConfigBeans(views, viewFactory, request);
396: maybeSavePresetMsgs();
397:
398: // If there is a clientType and clientViews, save them
399: if ((clientType != null) && (clientViews.size()) != 0) {
400: setAttribute(clientType,
401: AppConfigConstants.MAIL_APP_VIEW_CLIENTS_LIST,
402: new HashSet(clientViews));
403: }
404:
405: }
406:
407: /**
408: * For each bean in a collection of sso configuration backed (SSOConfigurationBean) beans:
409: * if the bean should be deleted, delete from configuration
410: * if the bean should be renamed delete config with old name, and save under new name
411: * if the bean should be saved, save.
412: * @param request
413: */
414: private void maybeSaveConfigBeans(Vector beans,
415: ConfigurationFactory factory, HttpServletRequest request) {
416: Enumeration e = beans.elements();
417: while (e.hasMoreElements()) {
418: SSOConfigurationBean bean = (SSOConfigurationBean) e
419: .nextElement();
420: if ((bean.getName()) == null) {
421: // If no name, delete this bean, as it is an ephemeral bean created during add
422: beans.remove(bean);
423: } else if (bean.needsSaving()) {
424: try {
425: factory.writeConfiguration(bean.getConfiguration(),
426: new SSOAdapterSession(request));
427: String oldName = bean.getOldName();
428: if ((oldName != null)
429: && !(oldName.equals(bean.getName()))) {
430: // rename: delete old bean
431: factory.removeConfiguration(oldName,
432: new SSOAdapterSession(request));
433: Enumeration o = beans.elements();
434: // find the bean with the old name, and remove it from beans
435: while (o.hasMoreElements()) {
436: SSOConfigurationBean c = (SSOConfigurationBean) o
437: .nextElement();
438: if (oldName.equals(c.getName())) {
439: beans.remove(c);
440: }
441: }
442: }
443: bean.upToDate();
444: } catch (ConfigurationException ex) {
445: //TODO: handle this more gracefully
446: logger.log(Level.WARNING, "PSMA_CSPWTP0006", ex);
447: }
448: } else if (bean.isDelete()) {
449: // TODO: handle renamed bean
450: try {
451: factory.removeConfiguration(bean.getName(),
452: new SSOAdapterSession(request));
453: beans.remove(bean);
454: } catch (ConfigurationException ex) {
455: //TODO: handle this more gracefully
456: logger.log(Level.WARNING, "PSMA_CSPWTP0007", ex);
457: }
458: }
459: }
460:
461: }
462:
463: /**
464: * If any of the preset msgs have been updated, save them
465: */
466: private void maybeSavePresetMsgs() {
467: if (presetMessages != null) {
468: Enumeration msgs = presetMessages.elements();
469: boolean save = false;
470: while (msgs.hasMoreElements()) {
471: PresetMsg msg = (PresetMsg) msgs.nextElement();
472: if (msg.needsSaving()) {
473: save = true;
474: }
475: }
476: if (save) {
477: Vector msgStrings = new Vector();
478: for (int i = 0; i < presetMessages.size(); i++) {
479: PresetMsg msg = (PresetMsg) presetMessages
480: .elementAt(i);
481: msg.upToDate();
482: msgStrings.addElement("preset" + (i + 1) + "="
483: + msg.getMsg());
484: }
485: setAttribute("sunMobileAppMailPredefinedReplies",
486: new HashSet(msgStrings));
487: }
488: }
489: }
490:
491: }
|