| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
package com.buckosoft.PicMan.web; |
| 9 | |
|
| 10 | |
import java.util.Collections; |
| 11 | |
import java.util.HashMap; |
| 12 | |
import java.util.LinkedList; |
| 13 | |
import java.util.List; |
| 14 | |
import java.util.Map; |
| 15 | |
|
| 16 | |
import javax.servlet.http.HttpServletRequest; |
| 17 | |
import javax.servlet.http.HttpServletResponse; |
| 18 | |
|
| 19 | |
import org.apache.commons.logging.Log; |
| 20 | |
import org.apache.commons.logging.LogFactory; |
| 21 | |
import org.springframework.validation.BindException; |
| 22 | |
import org.springframework.web.servlet.ModelAndView; |
| 23 | |
|
| 24 | |
import com.buckosoft.BSAccount.web.BSAccountSimpleFormController; |
| 25 | |
import com.buckosoft.BSAccount.web.BSAccountUserWebSession; |
| 26 | |
import com.buckosoft.PicMan.business.PicManFacade; |
| 27 | |
import com.buckosoft.PicMan.db.DatabaseFacade; |
| 28 | |
import com.buckosoft.PicMan.domain.Mosaic; |
| 29 | |
import com.buckosoft.PicMan.domain.Poster; |
| 30 | |
import com.buckosoft.PicMan.domain.User; |
| 31 | |
import com.buckosoft.PicMan.web.PosterSetupForm.MosaicNameAndMid; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public class PosterSetupFormController extends BSAccountSimpleFormController { |
| 39 | |
private final static boolean DEBUG = true; |
| 40 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 41 | |
|
| 42 | |
private DatabaseFacade dbf; |
| 43 | |
private PicManFacade pmf; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 0 | public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; } |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 0 | public void setPicMan(PicManFacade pmf) { this.pmf = pmf; } |
| 55 | |
|
| 56 | |
|
| 57 | 0 | public PosterSetupFormController() { |
| 58 | 0 | setSessionForm(true); |
| 59 | 0 | setValidateOnBinding(false); |
| 60 | 0 | setCommandName("posterSetupForm"); |
| 61 | 0 | setFormView("PosterSetupForm"); |
| 62 | 0 | } |
| 63 | |
|
| 64 | |
protected Object formBackingObject(HttpServletRequest request) throws Exception { |
| 65 | 0 | BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request); |
| 66 | |
|
| 67 | |
if (DEBUG) |
| 68 | 0 | logger.info("formBackingObject posterForm with session " + userWebSession); |
| 69 | |
|
| 70 | 0 | PosterSetupForm psf = new PosterSetupForm(); |
| 71 | 0 | psf.setUserWebSession(userWebSession); |
| 72 | 0 | psf.setPicMan(pmf); |
| 73 | |
|
| 74 | 0 | psf.setPoster(new Poster()); |
| 75 | 0 | List<Poster> list = dbf.getPosters(); |
| 76 | 0 | psf.setPosters(list); |
| 77 | |
|
| 78 | 0 | List<Mosaic> mlist = dbf.getMosaics(); |
| 79 | 0 | LinkedList<MosaicNameAndMid> mmlist = new LinkedList<MosaicNameAndMid>(); |
| 80 | |
|
| 81 | 0 | for (Mosaic m : mlist) { |
| 82 | 0 | MosaicNameAndMid mm = psf.new MosaicNameAndMid(); |
| 83 | 0 | mm.setName(m.getName()); |
| 84 | 0 | mm.setMid(m.getMid()); |
| 85 | 0 | mmlist.add(mm); |
| 86 | 0 | } |
| 87 | 0 | Collections.sort(mmlist); |
| 88 | 0 | psf.setMosaics(mmlist); |
| 89 | 0 | return(psf); |
| 90 | |
} |
| 91 | |
|
| 92 | |
protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) |
| 93 | |
throws Exception { |
| 94 | 0 | PosterSetupForm msf = (PosterSetupForm)command; |
| 95 | 0 | if (request.getParameter("startBuilding") == null) { |
| 96 | 0 | msf.setStartBuilding(false); |
| 97 | |
} else |
| 98 | 0 | msf.setStartBuilding(true); |
| 99 | |
|
| 100 | 0 | } |
| 101 | |
|
| 102 | |
protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception { |
| 103 | 0 | BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request); |
| 104 | 0 | Map<String, Object> myModel = new HashMap<String, Object>(); |
| 105 | 0 | myModel.put("userWebSession", userWebSession); |
| 106 | |
if (DEBUG) |
| 107 | 0 | logger.info("referenceData posterForm with session " + userWebSession); |
| 108 | |
|
| 109 | 0 | return myModel; |
| 110 | |
} |
| 111 | |
|
| 112 | |
protected ModelAndView onSubmit( |
| 113 | |
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) |
| 114 | |
throws Exception { |
| 115 | |
|
| 116 | 0 | BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request); |
| 117 | 0 | User user = (User)userWebSession.getUser(); |
| 118 | 0 | PosterSetupForm psf = (PosterSetupForm)command; |
| 119 | 0 | boolean userSettingsModified = false; |
| 120 | 0 | boolean success = true; |
| 121 | |
|
| 122 | 0 | Poster poster = psf.getPoster(); |
| 123 | 0 | user.setPosterPid(poster.getPid()); |
| 124 | 0 | pmf.getDB().storePoster(poster); |
| 125 | 0 | if (userSettingsModified) |
| 126 | 0 | this.dbf.storeUser(user); |
| 127 | |
|
| 128 | 0 | if (psf.isCalibrationSheet()) { |
| 129 | 0 | startBuilding(poster, true); |
| 130 | |
} |
| 131 | 0 | if (psf.isStartBuilding()) { |
| 132 | 0 | startBuilding(poster, false); |
| 133 | |
} |
| 134 | 0 | if (success && !response.isCommitted()) { |
| 135 | 0 | response.sendRedirect("posterSetup.do"); |
| 136 | 0 | return(null); |
| 137 | |
} |
| 138 | |
else |
| 139 | 0 | return(showForm(request, response, errors)); |
| 140 | |
} |
| 141 | |
|
| 142 | |
private void startBuilding(Poster poster, boolean calibration) throws CloneNotSupportedException { |
| 143 | 0 | this.pmf.getPosterManager().buildPoster(poster, calibration); |
| 144 | 0 | } |
| 145 | |
} |