/*
* Copyright Massimiliano Dessi' (desmax74@yahoo.it)
*
* Licensed for non-commercial use,
* under Apache License Version 2.0
* (http://www.apache.org/licenses/LICENSE-2.0),
*
* for commercial use, under
* GNU General Public License Version 2 or later (the "GPL")
* http://www.gnu.org/licenses/gpl.html
*/
package org.magicbox.controller;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javolution.util.FastMap;
import org.magicbox.controller.util.WebUtils;
import org.magicbox.domain.AnnuncioImpl;
import org.magicbox.dto.Envelope;
import org.magicbox.service.GruppiService;
import org.magicbox.service.PubblicazioniFacade;
import org.magicbox.util.Constant;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractWizardFormController;
/**
* Wizard Controller per invio sms
*
* @author Massimiliano Dessì (desmax74@yahoo.it)
* @since jdk 1.6.0
* @version 3.0
*/
public class SmsWizardController extends AbstractWizardFormController {
public SmsWizardController() {
String[] pages = new String[2];
pages[0] = "comunicazioni/smsEditor";
pages[1] = "comunicazioni/smsGruppi";
setPages(pages);
setCommandClass(AnnuncioImpl.class);
setCommandName(Constant.SMS);
}
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
StringTrimmerEditor trimmerEditor = new StringTrimmerEditor(false);
binder.registerCustomEditor(String.class, trimmerEditor);
}
protected Object formBackingObject(HttpServletRequest req) throws Exception {
return new AnnuncioImpl();
}
@SuppressWarnings("unchecked")
protected Map referenceData(HttpServletRequest req, int page) throws Exception {
if (page == 1) {
Map model = new FastMap();
model.put(Constant.FREE_LIST, gruppiService.getGruppiCentro(WebUtils.getIdCentro(req)));
return model;
}
return null;
}
public ModelAndView processFinish(HttpServletRequest req, HttpServletResponse res, Object command, BindException errors) throws Exception {
pubblicazioniFacade.inviaSms(getEnvelope(req, command));
return new ModelAndView(Constant.REDIRECT_WELCOME_USER);
}
protected void validatePage(Object command, Errors err, int pageN) {
switch (pageN) {
case 0:
getValidator().validate(command, err);
break;
case 1:
break;
}
}
private Envelope getEnvelope(HttpServletRequest req, Object command) throws Exception {
Envelope busta = (Envelope) command;
busta.setCentro(WebUtils.getIdCentro(req));
busta.setData(new Date());
busta.setTipo(Constant.TYPE_SMS);
busta.setTesto(busta.getTesto());
busta.setTitolo(busta.getOggetto());
return busta;
}
public void setPubblicazioniFacade(PubblicazioniFacade pubblicazioniFacade) {
this.pubblicazioniFacade = pubblicazioniFacade;
}
public void setGruppiService(GruppiService gruppiService) {
this.gruppiService = gruppiService;
}
private GruppiService gruppiService;
private PubblicazioniFacade pubblicazioniFacade;
}