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 protected final Log logger = LogFactory.getLog(getClass());
41
42 private DatabaseFacade dbf;
43 private PicManFacade pmf;
44
45
46
47
48 public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
49
50
51
52
53
54 public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
55
56
57 public PosterSetupFormController() {
58 setSessionForm(true);
59 setValidateOnBinding(false);
60 setCommandName("posterSetupForm");
61 setFormView("PosterSetupForm");
62 }
63
64 protected Object formBackingObject(HttpServletRequest request) throws Exception {
65 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
66
67 if (DEBUG)
68 logger.info("formBackingObject posterForm with session " + userWebSession);
69
70 PosterSetupForm psf = new PosterSetupForm();
71 psf.setUserWebSession(userWebSession);
72 psf.setPicMan(pmf);
73
74 psf.setPoster(new Poster());
75 List<Poster> list = dbf.getPosters();
76 psf.setPosters(list);
77
78 List<Mosaic> mlist = dbf.getMosaics();
79 LinkedList<MosaicNameAndMid> mmlist = new LinkedList<MosaicNameAndMid>();
80
81 for (Mosaic m : mlist) {
82 MosaicNameAndMid mm = psf.new MosaicNameAndMid();
83 mm.setName(m.getName());
84 mm.setMid(m.getMid());
85 mmlist.add(mm);
86 }
87 Collections.sort(mmlist);
88 psf.setMosaics(mmlist);
89 return(psf);
90 }
91
92 protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
93 throws Exception {
94 PosterSetupForm msf = (PosterSetupForm)command;
95 if (request.getParameter("startBuilding") == null) {
96 msf.setStartBuilding(false);
97 } else
98 msf.setStartBuilding(true);
99
100 }
101
102 protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
103 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
104 Map<String, Object> myModel = new HashMap<String, Object>();
105 myModel.put("userWebSession", userWebSession);
106 if (DEBUG)
107 logger.info("referenceData posterForm with session " + userWebSession);
108
109 return myModel;
110 }
111
112 protected ModelAndView onSubmit(
113 HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
114 throws Exception {
115
116 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
117 User user = (User)userWebSession.getUser();
118 PosterSetupForm psf = (PosterSetupForm)command;
119 boolean userSettingsModified = false;
120 boolean success = true;
121
122 Poster poster = psf.getPoster();
123 user.setPosterPid(poster.getPid());
124 pmf.getDB().storePoster(poster);
125 if (userSettingsModified)
126 this.dbf.storeUser(user);
127
128 if (psf.isCalibrationSheet()) {
129 startBuilding(poster, true);
130 }
131 if (psf.isStartBuilding()) {
132 startBuilding(poster, false);
133 }
134 if (success && !response.isCommitted()) {
135 response.sendRedirect("posterSetup.do");
136 return(null);
137 }
138 else
139 return(showForm(request, response, errors));
140 }
141
142 private void startBuilding(Poster poster, boolean calibration) throws CloneNotSupportedException {
143 this.pmf.getPosterManager().buildPoster(poster, calibration);
144 }
145 }