package com.jeffdouglas;
import java.io.IOException;
import javax.servlet.http.*;
import java.util.Date;
import java.util.List;
import java.text.DateFormat;
import javax.servlet.*;
import javax.jdo.PersistenceManager;
import com.jeffdouglas.entity.*;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
@SuppressWarnings("serial")
public class TelesalesServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// create the persistence manager instance
PersistenceManager pm = PMF.get().getPersistenceManager();
// display the lookup form
if(request.getParameter("action").equals("accountLookup")) {
// query for the entities by name
String query = "select from " + Account.class.getName() + " where name == '"+request.getParameter("accountName")+"'";
List<Account> accounts = (List<Account>) pm.newQuery(query).execute();
// pass the list to the jsp
request.setAttribute("accounts", accounts);
// forward the request to the jsp
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/accountLookup.jsp");
dispatcher.forward(request, response);
// display the create new account form
} else if(request.getParameter("action").equals("accountCreate")) {
response.sendRedirect("/accountCreate.jsp");
// process the new account creation and send them to the account display page
} else if(request.getParameter("action").equals("accountCreateDo")) {
// create the new account
Account a = new Account(
request.getParameter("name"),
request.getParameter("billingCity"),
request.getParameter("billingState"),
request.getParameter("phone"),
request.getParameter("website")
);
// persist the entity
try {
pm.makePersistent(a);
} finally {
pm.close();
}
response.sendRedirect("telesales?action=accountDisplay&accountId="+a.getId());
// display the account details and opportunities
}
else if(request.getParameter("action").equals("accountDisplay")) {
// fetch the account
Key k = KeyFactory.createKey(Account.class.getSimpleName(), new Integer(request.getParameter("accountId")).intValue());
Account a = pm.getObjectById(Account.class, k);
// query for the opportunities
String query = "select from " + Opportunity.class.getName() + " where accountId == "+request.getParameter("accountId");
List<Opportunity> opportunities = (List<Opportunity>) pm.newQuery(query).execute();
// pass the list to the jsp
request.setAttribute("account", a);
// pass the list to the jsp
request.setAttribute("opportunities", opportunities);
// forward the request to the jsp
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/accountDisplay.jsp");
dispatcher.forward(request, response);
}
else if(request.getParameter("action").equals("opportunityCreate")) {
Key k = KeyFactory.createKey(Account.class.getSimpleName(), new Integer(request.getParameter("accountId")).intValue());
Account a = pm.getObjectById(Account.class, k);
// pass the account name to the jsp
request.setAttribute("accountName", a.getName());
// forward the request to the jsp
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/opportunityCreate.jsp");
dispatcher.forward(request, response);
// process the new opportunity creation and send them to the account display page
} else if(request.getParameter("action").equals("opportunityCreateDo"))
{
Date closeDate = new Date();
// try and parse the date
try {
DateFormat df = DateFormat.getDateInstance(3);
closeDate = df.parse(request.getParameter("closeDate"));
} catch(java.text.ParseException pe) {
System.out.println("Exception " + pe);
}
// create the new opportunity
Opportunity opp = new Opportunity(
request.getParameter("name"),
new Double(request.getParameter("amount")).doubleValue(),
request.getParameter("stageName"),
new Integer(request.getParameter("probability")).intValue(),
closeDate,
new Integer(request.getParameter("orderNumber")).intValue(),
new Long(request.getParameter("accountId"))
);
// persist the entity
try {
pm.makePersistent(opp);
} finally {
pm.close();
}
response.sendRedirect("telesales?action=accountDisplay&accountId="+request.getParameter("accountId"));
}
else
{
Key k = KeyFactory.createKey(Account.class.getSimpleName(), new Integer(request.getParameter("accountId").trim()).intValue());
Account a = pm.getObjectById(Account.class, k);
if(request.getParameter("action").equals("accountUpdate")) {
//response.sendRedirect("/accountCreate.jsp");
System.out.println("dfhdsjfh");
// // fetch the account
// Key k = KeyFactory.createKey(Account.class.getSimpleName(), new Integer(request.getParameter("accountId")).intValue());
// Account a = pm.getObjectById(Account.class, k);
//
// // query for the opportunities
// String query = "select from " + Opportunity.class.getName() + " where accountId == "+request.getParameter("accountId");
// List<Opportunity> opportunities = (List<Opportunity>) pm.newQuery(query).execute();
//
// // pass the list to the jsp
// request.setAttribute("account", a);
// // pass the list to the jsp
// request.setAttribute("opportunities", opportunities);
//
// // forward the request to the jsp
// RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/accountUpdate.jsp");
// dispatcher.forward(request, response);
// String query = "select from " + Account.class.getName() + " where name == '"+request.getParameter("accountName")+"'";
// List<Account> accounts = (List<Account>) pm.newQuery(query).execute();
// Account b = (Account)accounts.get(0);
System.out.println(request.getParameter("NewPhone"));
System.out.println("get it");
try{
//Account c=pm.getObjectById(Account.class, request.getParameter("accountId").trim());
a.setPhone(request.getParameter("NewPhone"));
}
finally{
pm.close();
}
}
else if(request.getParameter("action").equals("accountDelete")) {
//response.sendRedirect("/accountCreate.jsp");
System.out.println("delete");
// // fetch the account
// Key k = KeyFactory.createKey(Account.class.getSimpleName(), new Integer(request.getParameter("accountId")).intValue());
// Account a = pm.getObjectById(Account.class, k);
//
// // query for the opportunities
// String query = "select from " + Opportunity.class.getName() + " where accountId == "+request.getParameter("accountId");
// List<Opportunity> opportunities = (List<Opportunity>) pm.newQuery(query).execute();
//
// // pass the list to the jsp
// request.setAttribute("account", a);
// // pass the list to the jsp
// request.setAttribute("opportunities", opportunities);
//
// // forward the request to the jsp
// RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/accountUpdate.jsp");
// dispatcher.forward(request, response);
// String query = "select from " + Account.class.getName() + " where name == '"+request.getParameter("accountName")+"'";
// List<Account> accounts = (List<Account>) pm.newQuery(query).execute();
// Account b = (Account)accounts.get(0);
//Key k = KeyFactory.createKey(Account.class.getSimpleName(), new Integer(request.getParameter("accountId").trim()).intValue());
//Account b = pm.getObjectById(Account.class, k);
//try{
//Account c=pm.getOb