1 package de.tivsource.page.admin.actions.locations.reservation;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.Calendar;
8 import java.util.Date;
9 import java.util.List;
10 import java.util.Properties;
11
12 import javax.mail.MessagingException;
13
14 import org.apache.logging.log4j.LogManager;
15 import org.apache.logging.log4j.Logger;
16 import org.apache.pdfbox.exceptions.COSVisitorException;
17 import org.apache.struts2.ServletActionContext;
18 import org.apache.struts2.convention.annotation.Action;
19 import org.apache.struts2.convention.annotation.Actions;
20 import org.apache.struts2.convention.annotation.Result;
21
22 import de.tivsource.ejb3plugin.InjectEJB;
23 import de.tivsource.page.admin.actions.EmptyAction;
24 import de.tivsource.page.dao.administration.UserDaoLocal;
25 import de.tivsource.page.dao.event.EventDaoLocal;
26 import de.tivsource.page.dao.picture.PictureDaoLocal;
27 import de.tivsource.page.dao.property.PropertyDaoLocal;
28 import de.tivsource.page.dao.reservation.ReservationDaoLocal;
29 import de.tivsource.page.entity.administration.User;
30 import de.tivsource.page.entity.reservation.Reservation;
31 import de.tivsource.page.enumeration.UrlType;
32 import de.tivsource.page.helper.sender.ReservationMail;
33
34
35
36
37
38
39 public class ConfirmAction extends EmptyAction {
40
41
42
43
44 private static final long serialVersionUID = 7464881886808879895L;
45
46
47
48
49 private static final Logger LOGGER = LogManager.getLogger(ConfirmAction.class);
50
51 private static final String HTDOCS = "/var/www/html";
52
53 @InjectEJB(name="UserDao")
54 private UserDaoLocal userDaoLocal;
55
56 @InjectEJB(name="PropertyDao")
57 private PropertyDaoLocal propertyDaoLocal;
58
59 @InjectEJB(name="EventDao")
60 private EventDaoLocal eventDaoLocal;
61
62 @InjectEJB(name="ReservationDao")
63 private ReservationDaoLocal reservationDaoLocal;
64
65 @InjectEJB(name="PictureDao")
66 private PictureDaoLocal pictureDaoLocal;
67
68 private String redirect;
69
70 private Reservation reservation;
71
72 public Reservation getReservation() {
73 return reservation;
74 }
75
76 public void setReservation(Reservation reservation) {
77 this.reservation = reservation;
78 }
79
80 public String getRedirect() {
81 return redirect;
82 }
83
84 @Override
85 @Actions({
86 @Action(
87 value = "confirm",
88 results = {
89 @Result(name = "success", type = "redirectAction", location = "queue.html"),
90 @Result(name = "input", type = "tiles", location = "reservationConfirmForm"),
91 @Result(name = "error", type = "tiles", location = "reservationConfirmError")
92 }
93 )
94 })
95 public String execute() throws Exception {
96 LOGGER.info("execute() aufgerufen.");
97
98 String remoteUser = ServletActionContext.getRequest().getRemoteUser();
99 String remoteAddress = ServletActionContext.getRequest().getRemoteAddr();
100
101 if(reservation != null) {
102 Reservation dbReservation = reservationDaoLocal.findByUuid(reservation.getUuid());
103 dbReservation.setConfirmed(true);
104 dbReservation.setConfirmedAddress(remoteAddress);
105 dbReservation.setConfirmedDate(new Date());
106 dbReservation.setConfirmedBy(remoteUser);
107 reservationDaoLocal.merge(dbReservation);
108
109
110 String urlAds = HTDOCS + pictureDaoLocal.findByUuid(
111 propertyDaoLocal.findByKey("reservation.ads").getValue()
112 ).getPictureUrl(UrlType.NORMAL.toString());
113
114 URL templatePath = new URL(propertyDaoLocal.findByKey("reservation.confirm.template.path").getValue());
115 URL logoPath = new URL(propertyDaoLocal.findByKey("reservation.confirm.logo.path").getValue());
116 URL fontPath = new URL(propertyDaoLocal.findByKey("reservation.confirm.font.path").getValue());
117
118
119 User user = userDaoLocal.findByUsername(remoteUser);
120
121 LOGGER.info("Pfad der Logo Datei " + logoPath.getFile());
122 LOGGER.info("Pfad der Ads Datei " + urlAds);
123 LOGGER.info("Pfad der Font Datei " + fontPath.getFile());
124
125 ReservationMail reservationMail = new ReservationMail(
126 propertyDaoLocal.findByKey("mail.user").getValue(),
127 propertyDaoLocal.findByKey("mail.password").getValue(),
128 templatePath,
129 dbReservation,
130 new File(logoPath.getFile()),
131 new File(urlAds),
132 getProperties(),
133 new File(fontPath.getFile()),
134 propertyDaoLocal.findByKey("reservation.formAddress").getValue(),
135 propertyDaoLocal.findByKey("reservation.fromName").getValue(),
136 propertyDaoLocal.findByKey("reservation.replyToAddress").getValue(),
137 user.getFirstname() + " " + user.getLastname(),
138 propertyDaoLocal.findByKey("reservation.bccAddress").getValue()
139 );
140
141 new Thread(new Runnable() {
142 public void run(){
143 try {
144 reservationMail.send();
145 } catch (COSVisitorException | MessagingException | IOException e) {
146
147 e.printStackTrace();
148 }
149 return;
150 }
151 }).start();
152
153 return SUCCESS;
154 }
155 else {
156 return ERROR;
157 }
158
159
160 }
161
162 public List<Date> getTimes() {
163 LOGGER.info("getTimes() aufgerufen.");
164
165 List<Date> times = new ArrayList<Date>();
166
167
168 Calendar calendarStart = Calendar.getInstance();
169 calendarStart.setTime(reservation.getEvent().getBeginning());
170 times.add(calendarStart.getTime());
171
172
173 Calendar calendar = Calendar.getInstance();
174 calendar.setTime(reservation.getEvent().getEnding());
175 calendar.add(Calendar.MINUTE, -30);
176 Date end = calendar.getTime();
177
178
179 Date time = reservation.getEvent().getBeginning();
180 while (time.before(end)) {
181 Calendar calendarTime = Calendar.getInstance();
182 calendarTime.setTime(time);
183 calendarTime.add(Calendar.MINUTE, 15);
184 time = calendarTime.getTime();
185 times.add(time);
186 }
187
188 LOGGER.info("Inhalt der Liste: " + times.size());
189
190 return times;
191 }
192
193 private Properties getProperties() {
194 LOGGER.info("getProperties() aufgerufen.");
195
196
197 Properties props = System.getProperties();
198
199
200 props.put("mail.transport.protocol",
201 propertyDaoLocal.findByKey("mail.transport.protocol").getValue());
202 props.put("mail.host",
203 propertyDaoLocal.findByKey("mail.host").getValue());
204 props.put("mail.smtp.auth",
205 propertyDaoLocal.findByKey("mail.smtp.auth").getValue());
206 props.put("mail.smtp.tls",
207 propertyDaoLocal.findByKey("mail.smtp.tls").getValue());
208 props.put("mail.smtp.starttls.enable",
209 "true");
210 props.put("mail.smtp.localhost",
211 propertyDaoLocal.findByKey("mail.smtp.localhost").getValue());
212 props.put("mail.user",
213 propertyDaoLocal.findByKey("mail.user").getValue());
214 props.put("mail.password",
215 propertyDaoLocal.findByKey("mail.password").getValue());
216 props.put("mail.mime.charset",
217 propertyDaoLocal.findByKey("mail.mime.charset").getValue());
218 props.put("mail.use8bit",
219 propertyDaoLocal.findByKey("mail.use8bit").getValue());
220
221 return props;
222 }
223
224 }