1 5 package org.exoplatform.portlets.communication.sms.component; 6 7 import java.io.IOException ; 8 import java.util.Map ; 9 import java.util.ResourceBundle ; 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 32 public class UISmsAdminForm extends UIFormWithInformationProvider { 33 34 public static final String SAVE_SMS_CONFIG_ACTION = "SaveSmsConfig"; 35 private static final String 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 _res; 40 41 private UIStringInput _uiUsername; 42 private UIStringInput _uiPassword; 43 44 public UISmsAdminForm(LogService logService, SmsService smsService, ResourceBundle 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 uid = prefs.getValue(SmsPortletUtil.PREF_USERNAME, ""); 54 String 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 { 84 super.encodeEnd(context); 85 } 86 87 static public class SaveSmsConfigActionListener extends ExoActionListener { 88 public void execute(ExoActionEvent event) throws Exception { 89 UISmsAdminForm uiForm = (UISmsAdminForm) event.getComponent(); 90 Provider provider = SmsPortletUtil.getProvider(uiForm._sms); 91 Map parameters = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap(); 92 String uid = ((String []) parameters.get(SmsPortletUtil.PREF_USERNAME))[0]; 93 String pwd = ((String []) 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 t) { 119 iprovider.addMessage(new FacesMessage("We got problems with storing the portlet preferences")); 120 t.printStackTrace(); 121 }; 122 } 123 } 124 125 } | Popular Tags |