KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > communication > sms > component > UISmsAdminForm


1 /**
2  * Copyright 2001-2004 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.portlets.communication.sms.component;
6
7 import java.io.IOException JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.ResourceBundle JavaDoc;
10 import javax.faces.application.FacesMessage;
11 import javax.faces.context.FacesContext;
12 import javax.portlet.PortletPreferences;
13 import org.apache.commons.logging.Log;
14 import org.exoplatform.faces.FacesUtil;
15 import org.exoplatform.faces.application.ExoFacesMessage;
16 import org.exoplatform.faces.core.component.InformationProvider;
17 import org.exoplatform.faces.core.component.UIFormWithInformationProvider;
18 import org.exoplatform.faces.core.component.UIStringInput;
19 import org.exoplatform.faces.core.component.model.*;
20 import org.exoplatform.faces.core.event.ExoActionEvent;
21 import org.exoplatform.faces.core.event.ExoActionListener;
22 import org.exoplatform.portlets.communication.sms.util.SmsPortletUtil;
23 import org.exoplatform.services.communication.sms.SmsService;
24 import org.exoplatform.services.communication.sms.provider.Provider;
25 import org.exoplatform.services.log.LogService;
26
27
28 /**
29  * @author Ove Ranheim (oranheim@yahoo.no)
30  * @since Jun 21, 2004 12:45:24 PM
31  */

32 public class UISmsAdminForm extends UIFormWithInformationProvider {
33
34     public static final String JavaDoc SAVE_SMS_CONFIG_ACTION = "SaveSmsConfig";
35     private static final String JavaDoc PSWINCOM_IMAGE = "<p><a target='_new' HREF='http://www.pswin.com'><img border='0' SRC='/content/images/PSWin3.jpg' align='left' valign'top'></a>";
36
37     private Log _log;
38     private SmsService _sms;
39     private ResourceBundle JavaDoc _res;
40
41     private UIStringInput _uiUsername;
42     private UIStringInput _uiPassword;
43
44     public UISmsAdminForm(LogService logService, SmsService smsService, ResourceBundle JavaDoc res) {
45         super("smsAdminForm", "post", null);
46         setId("UISmsAdminForm");
47         setClazz("UISmsForm");
48         _log = logService.getLog("org.exoplatform.portlets.communication.sms.component");
49         _sms = smsService;
50         _res = res;
51
52         PortletPreferences prefs = FacesUtil.getPortletPreferences();
53         String JavaDoc uid = prefs.getValue(SmsPortletUtil.PREF_USERNAME, "");
54         String JavaDoc pwd = prefs.getValue(SmsPortletUtil.PREF_PASSWORD, "");
55
56         _uiUsername = new UIStringInput(SmsPortletUtil.PREF_USERNAME, uid);
57         _uiPassword = new UIStringInput(SmsPortletUtil.PREF_PASSWORD, "******");
58         _uiPassword.setType(UIStringInput.PASSWORD);
59
60         addActionListener(SaveSmsConfigActionListener.class, SAVE_SMS_CONFIG_ACTION);
61
62         add(new HeaderRow().
63             add(new Cell(res.getString("header.sms-config")).
64             addColspan("2")));
65         add(new Row().
66             add(new Cell(PSWINCOM_IMAGE+res.getString("header.sms-info")+res.getString("header.sms-info-site")+"<br><br><br>").
67             addColspan("2"))).
68         add(new Row().
69             add(new LabelCell(res.getString("label.sms-config-username"))).
70             add(new ComponentCell(this, _uiUsername))).
71         add(new Row().
72             add(new LabelCell(res.getString("label.sms-config-password"))).
73             add(new ComponentCell(this, _uiPassword)));
74         
75         FormButton saveButton = new FormButton(res.getString("button.sms-config-save-config"), SAVE_SMS_CONFIG_ACTION);
76         saveButton.setClass("save");
77
78         add(new Row().add(new ListComponentCell().
79             add(saveButton).
80             addColspan("2")));
81     }
82
83     public void encodeEnd(FacesContext context) throws IOException JavaDoc {
84         super.encodeEnd(context);
85     }
86
87     static public class SaveSmsConfigActionListener extends ExoActionListener {
88       public void execute(ExoActionEvent event) throws Exception JavaDoc {
89         UISmsAdminForm uiForm = (UISmsAdminForm) event.getComponent();
90         Provider provider = SmsPortletUtil.getProvider(uiForm._sms);
91         Map JavaDoc parameters = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap();
92         String JavaDoc uid = ((String JavaDoc[]) parameters.get(SmsPortletUtil.PREF_USERNAME))[0];
93         String JavaDoc pwd = ((String JavaDoc[]) parameters.get(SmsPortletUtil.PREF_PASSWORD))[0];
94         
95         InformationProvider iprovider = findInformationProvider(uiForm);
96         boolean hasError = false;
97         if (uid == null || uid.length() == 0) {
98           iprovider.addMessage(new ExoFacesMessage("msg.error-no-username"));
99           hasError = true;
100         }
101         
102         if (pwd == null || pwd.length() == 0 || ("******".equals(pwd))) {
103           iprovider.addMessage(new ExoFacesMessage("msg.error-no-password"));
104           hasError = true;
105         }
106         
107         if (hasError) return;
108         
109         provider.getOperator().setUsername(uid);
110         provider.getOperator().setPassword(pwd);
111         
112         PortletPreferences prefs = FacesUtil.getPortletPreferences();
113         
114         prefs.setValue(SmsPortletUtil.PREF_USERNAME, provider.getOperator().getUsername());
115         prefs.setValue(SmsPortletUtil.PREF_PASSWORD, provider.getOperator().getPassword());
116         try {
117           prefs.store();
118         } catch(Throwable JavaDoc t) {
119           iprovider.addMessage(new FacesMessage("We got problems with storing the portlet preferences"));
120           t.printStackTrace();
121         };
122       }
123     }
124     
125 }
Popular Tags