1 5 package org.exoplatform.portlets.communication.sms.component; 6 7 import java.io.IOException ; 8 import java.util.ArrayList ; 9 import java.util.List ; 10 import java.util.Map ; 11 import java.util.ResourceBundle ; 12 13 import javax.faces.application.FacesMessage; 14 import javax.faces.context.ExternalContext; 15 import javax.faces.context.FacesContext; 16 import javax.faces.context.ResponseWriter; 17 import javax.portlet.ActionRequest; 18 19 import org.apache.commons.logging.Log; 20 import org.exoplatform.faces.core.component.InformationProvider; 21 import org.exoplatform.faces.core.component.UIFormWithInformationProvider; 22 import org.exoplatform.faces.core.component.UISelectBox; 23 import org.exoplatform.faces.core.component.UIStringInput; 24 import org.exoplatform.faces.core.component.UITextArea; 25 import org.exoplatform.faces.core.component.model.Cell; 26 import org.exoplatform.faces.core.component.model.ComponentCell; 27 import org.exoplatform.faces.core.component.model.FormButton; 28 import org.exoplatform.faces.core.component.model.HeaderRow; 29 import org.exoplatform.faces.core.component.model.LabelCell; 30 import org.exoplatform.faces.core.component.model.ListComponentCell; 31 import org.exoplatform.faces.core.component.model.Row; 32 import org.exoplatform.faces.core.component.model.SelectItem; 33 import org.exoplatform.faces.core.event.ExoActionEvent; 34 import org.exoplatform.faces.core.event.ExoActionListener; 35 import org.exoplatform.portlets.communication.sms.util.SmsPortletUtil; 36 import org.exoplatform.services.communication.sms.SmsService; 37 import org.exoplatform.services.communication.sms.encoder.MessageFormat; 38 import org.exoplatform.services.communication.sms.model.*; 39 import org.exoplatform.services.communication.sms.provider.Provider; 40 import org.exoplatform.services.communication.sms.provider.Sender; 41 import org.exoplatform.services.log.LogService; 42 43 44 48 public class UISmsForm extends UIFormWithInformationProvider { 49 50 public static final String SEND_SMS_ACTION = "SendSms"; 51 public static final String CLEAR_SMS_ACTION = "ClearSms"; 52 53 public static final String MSG_FROM_FIELD = "smsFromNumber"; 54 public static final String MSG_TO_FIELD = "smsToNumber"; 55 public static final String MSG_FORMAT_FIELD = "smsFormat"; 56 public static final String MSG_MESSAGE_FIELD = "smsMessage"; 57 public static final String MSG_MESSAGE_COUNTER_FIELD = "smsMessageCounter"; 58 59 private static final String JS_TEXT_COUNTER = 60 "<SCRIPT type='text/javascript'>\n"+ 61 "<!--\n"+ 62 "function textCounter(messagefield,cntfield,maxlimit) {\n"+ 63 " cntfield.value = maxlimit - messagefield.value.length;\n"+ 64 "}\n"+ 65 "//-->\n"+ 66 "</SCRIPT>\n"; 67 68 private static List MSG_FORMATS; 69 70 static { 71 MSG_FORMATS = new ArrayList (5) ; 72 MSG_FORMATS.add(new SelectItem("Plain Text", MessageFormat.PLAIN_TEXT.toString())) ; 73 MSG_FORMATS.add(new SelectItem("Unicode Text", MessageFormat.UNICODE_TEXT.toString())) ; 74 } 76 77 private Log _log; 78 private SmsService _sms; 79 private ResourceBundle _res; 80 81 private UIStringInput _uiSmsFrom; 82 private UIStringInput _uiSmsTo; 83 private UISmsRecipients _uiSmsRecipients; 84 private UISelectBox _uiSmsFormats; 85 private UITextArea _uiSmsMessage; 86 private UIStringInput _uiSmsMessageCounter; 87 88 public UISmsForm(LogService logService, SmsService smsService, ResourceBundle res) { 89 super("smsForm", "post", null); 90 setId("UISmsForm"); 91 setClazz("UISmsForm"); 92 93 _log = logService.getLog("org.exoplatform.portlets.communication.sms.component"); 94 _sms = smsService; 95 _res = res; 96 97 _uiSmsFrom = new UIStringInput(MSG_FROM_FIELD, ""); 98 _uiSmsFrom.setTitle(res.getString("input.sms-from-number-format")); 99 _uiSmsRecipients = new UISmsRecipients(logService, smsService); 100 _uiSmsTo = new UIStringInput(MSG_TO_FIELD, ""); 101 _uiSmsTo.setTitle(res.getString("input.sms-number-format")); 102 _uiSmsFormats = new UISelectBox(MSG_FORMAT_FIELD, MessageFormat.PLAIN_TEXT.toString(), MSG_FORMATS); 103 _uiSmsMessage = new UITextArea(MSG_MESSAGE_FIELD, "").setCols("80").setRows("24"); 104 _uiSmsMessage.setKeyDown("textCounter("+MSG_MESSAGE_FIELD+","+MSG_MESSAGE_COUNTER_FIELD+",804)"); 105 _uiSmsMessage.setKeyUp("textCounter("+MSG_MESSAGE_FIELD+","+MSG_MESSAGE_COUNTER_FIELD+",804)"); 106 _uiSmsMessageCounter = new UIStringInput(MSG_MESSAGE_COUNTER_FIELD, "804"); 107 _uiSmsMessageCounter.setClazz("counter"); 108 _uiSmsMessageCounter.setEditable(false); 109 110 addActionListener(SendSmsActionListener.class, SEND_SMS_ACTION); 111 addActionListener(ClearSmsActionListener.class, CLEAR_SMS_ACTION); 112 113 add(new HeaderRow(). 114 add(new Cell(res.getString("header.send-sms")). 115 addColspan("2"))); 116 add(new Row(). 117 add(new LabelCell(res.getString("label.sms-from"))). 118 add(new ComponentCell(this, _uiSmsFrom))); 119 122 add(new Row(). 123 add(new LabelCell(res.getString("label.sms-recipients"))). 124 add(new ComponentCell(this, _uiSmsTo))); 125 add(new Row(). 126 add(new LabelCell(res.getString("label.sms-message"))). 127 add(new ComponentCell(this, _uiSmsFormats))); 128 add(new Row(). 129 add(new ComponentCell(this, _uiSmsMessage).addColspan("2"))); 130 add(new Row(). 131 add(new ComponentCell(this, _uiSmsMessageCounter).addColspan("2").addAlign("right"))); 132 133 FormButton sendButton = new FormButton(res.getString("button.sms-send"), SEND_SMS_ACTION); 134 sendButton.setClass("send"); 135 FormButton clearButton = new FormButton(res.getString("button.sms-clear"), CLEAR_SMS_ACTION); 136 clearButton.setClass("clear"); 137 138 add(new Row().add(new ListComponentCell().add(sendButton).add(clearButton).addColspan("2"))); 139 } 140 141 public void encodeBegin(FacesContext context) throws IOException { 142 super.encodeBegin(context); 143 ExternalContext econtext = context.getExternalContext(); 144 boolean inSmsRole = (econtext.getUserPrincipal() != null); 148 if (!inSmsRole) { 149 _uiSmsFrom.setText("eXo platform"); 150 _uiSmsFrom.setEditable(false); 151 } else { 152 _uiSmsFrom.setEditable(true); 153 } 154 ResponseWriter w = context.getResponseWriter(); 155 Provider provider = SmsPortletUtil.getProvider(_sms); 156 w.write(JS_TEXT_COUNTER); 157 } 160 161 public void encodeEnd(FacesContext context) throws IOException { 162 super.encodeEnd(context); 163 } 164 165 166 static public class SendSmsActionListener extends ExoActionListener { 167 public void execute(ExoActionEvent event) throws Exception { 168 UISmsForm uiForm = (UISmsForm) event.getComponent(); 169 Provider provider = SmsPortletUtil.getProvider(uiForm._sms); 170 ExternalContext econtext = FacesContext.getCurrentInstance().getExternalContext(); 171 Map parameters = econtext.getRequestParameterValuesMap(); 172 String from = ((String []) parameters.get(MSG_FROM_FIELD))[0]; 173 String to = ((String []) parameters.get(MSG_TO_FIELD))[0]; 174 MessageFormat messageFormat = MessageFormat.parse( ((String []) parameters.get(MSG_FORMAT_FIELD))[0] ); 175 String message = ((String []) parameters.get(MSG_MESSAGE_FIELD))[0]; 176 177 InformationProvider iprovider = findInformationProvider(uiForm); 178 ResourceBundle res = event.getApplicationResourceBundle() ; 179 boolean hasError = false; 180 if (from == null || from.length() == 0) { 181 iprovider.addMessage(new FacesMessage(res.getString("msg.error-sms-from-field"))); 182 hasError = true; 183 } 184 if (to == null || to.length() == 0) { 185 iprovider.addMessage(new FacesMessage(res.getString("msg.error-sms-to-field"))); 186 hasError = true; 187 } 188 if (message == null || message.length() == 0) { 189 iprovider.addMessage(new FacesMessage(res.getString("msg.error-sms-message-field"))); 190 hasError = true; 191 } 192 193 if (hasError) return; 194 195 SmsService sms = uiForm._sms; 196 ActionRequest prequest = (ActionRequest) econtext.getRequest(); 197 String encoding = prequest.getCharacterEncoding(); 198 Messages messages = sms.createMessages(); 199 messages.setEncoding(encoding); 200 org.exoplatform.services.communication.sms.model.Message msg = messages.addMessage(); 201 msg.setFrom(from); 202 msg.addRecipient(to); 203 msg.setContent(message); 204 msg.setFormat(messageFormat); 205 206 Sender sender = sms.createSender(provider); 207 sender.prepare(messages); 208 uiForm._log.debug("Message: "+message); 209 String payload = sender.getProvider().getAdapter().getRequest().getPayload(); 210 uiForm._log.debug("PSWinCom Payload: "+payload); 211 hasError = sender.send().hasErrorOccured(); 212 213 if (!hasError) { 214 iprovider.addMessage(new FacesMessage(res.getString("msg.sms-success"))); 215 } else { 216 iprovider.addMessage(new FacesMessage(res.getString("msg.sms-failure")+"<br>")); 217 String errorMsg = ""; 218 if (LogonStatus.FAILED.equals(messages.getLogonStatus())) { 219 errorMsg = messages.getReason(); 220 } else { 221 Recipient r = (Recipient) msg.getRecipients().get(0); 222 errorMsg = r.getError(); 223 } 224 iprovider.addMessage(new FacesMessage(res.getString("msg.sms-failure-reason")+errorMsg)); 225 } 226 227 } 228 } 229 230 static public class ClearSmsActionListener extends ExoActionListener { 231 public void execute(ExoActionEvent event) throws Exception { 232 UISmsForm uiForm = (UISmsForm) event.getComponent(); 233 uiForm._uiSmsFrom.setValue(""); 234 uiForm._uiSmsTo.setValue(""); 235 uiForm._uiSmsMessage.setValue(""); 236 uiForm._uiSmsFormats.setValue(MessageFormat.PLAIN_TEXT.toString()); 237 uiForm._uiSmsMessageCounter.setValue("804"); 238 } 239 } 240 } | Popular Tags |