package com.casamind.adware.server.servlet.test;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.casamind.adware.server.domain.Company;
import com.casamind.adware.server.domain.Publisher;
import com.casamind.adware.server.domain.Slot;
import com.casamind.adware.server.proxy.DatastoreProxy;
import com.casamind.adware.shared.SlotStatus;
@SuppressWarnings("serial")
public class TESTCalendarHandler extends HttpServlet {
private static final Logger log = Logger.getLogger(TESTCalendarHandler.class.getName());
public static String startDate, endDate, attendee;
public static int duration, price;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
startDate = getInitParameter("dtstart");
endDate = getInitParameter("dtend");
attendee = getInitParameter("attendee");
duration = Integer.parseInt(getInitParameter("duration"));
price = Integer.parseInt(getInitParameter("price"));
log.info("Extracted attendee: " + attendee);
SimpleDateFormat icalFormatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
SimpleDateFormat logFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm Z");
Company company = DatastoreProxy.getCompanyByLogin(attendee);
if (company != null) {
log.info("Found company: " + company.getLastname());
Slot slot = new Slot();
slot.setOwnerId(company.getId());
log.info("DTSTART :" + startDate);
Date parsedStartDate = icalFormatter.parse(startDate);
log.info("Parsed start date :" + logFormatter.format(parsedStartDate));
log.info("DTEND :" + endDate);
Date parsedEndDate = icalFormatter.parse(endDate);
log.info("Parsed end date :" + logFormatter.format(parsedEndDate));
slot.setStartDate(parsedStartDate);
slot.setEndDate(parsedEndDate);
slot.setStatus(SlotStatus.Ordered);
long slotInterval = parsedEndDate.getTime() - parsedStartDate.getTime();
log.info("Interval in miliseconds: " + slotInterval);
long slotDurationInMilisecondes = (long) duration * 60 * 1000;
log.info("Default slot duration in miliseconds: " + slotDurationInMilisecondes);
long nbSlots = slotInterval / slotDurationInMilisecondes;
log.info("Number of slots (Long): " + nbSlots);
slot.setNbSlots((int) nbSlots);
slot.setPrice(price);
Slot entity = DatastoreProxy.createSlot(slot);
log.info("Created new slot for company:" + company.getLastname());
log.info(entity.toString());
} else {
log.warning("Did not find a company with attendee: " + attendee);
log.info("Trying to a find a publisher with attendee: " + attendee);
Publisher publisher = DatastoreProxy.getPublisherByLogin(attendee);
if (publisher != null) {
log.info("Found publisher with attendee: " + publisher.getFirstname() + " " + publisher.getLastname());
Slot slot = new Slot();
slot.setOwnerId(publisher.getId());
log.info("DTSTART :" + startDate);
Date parsedStartDate = icalFormatter.parse(startDate);
log.info("Parsed start date :" + logFormatter.format(parsedStartDate));
log.info("DTEND :" + endDate);
Date parsedEndDate = icalFormatter.parse(endDate);
log.info("Parsed end date :" + logFormatter.format(parsedEndDate));
slot.setStartDate(parsedStartDate);
slot.setEndDate(parsedEndDate);
slot.setStatus(SlotStatus.Ordered);
long slotInterval = parsedEndDate.getTime() - parsedStartDate.getTime();
log.info("Interval in miliseconds: " + slotInterval);
long slotDurationInMilisecondes = (long) duration * 60 * 1000;
log.info("Default slot duration in miliseconds: " + slotDurationInMilisecondes);
long nbSlots = slotInterval / slotDurationInMilisecondes;
log.info("Number of slots (Long): " + nbSlots);
slot.setNbSlots((int) nbSlots);
slot.setPrice(price);
Slot entity = DatastoreProxy.createSlot(slot);
log.info("Created new slot for publisher:" + publisher.getFirstname() + " " + publisher.getLastname());
log.info(entity.toString());
} else {
log.warning("Could not find publisher: " + attendee);
}
response.getWriter().println(logFormatter.format(new Date()) + " : Finshed! See the server logs for debug.");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}