protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String targetPage = null;
String method = getInitParameter("method");
CompanyService companyService=null;
try {
companyService = (CompanyService) BusinessUtil
.findBusiness("CompanyService");
} catch (Exception e2) {
e2.printStackTrace();
}
if (TABLE_ACTION.equals(method)) {
targetPage = getInitParameter("page");
} else if (PRE_ADD_ACTION.equals(method)) {
//prepareDataForSelect(req);
targetPage = "/pages/company/companyForm.jsp";
} else if (DETAILS.equals(method)) {
try {
//prepareDataForSelect(req);
Company company = companyService.load(Integer.valueOf(req.getParameter("id")));
req.setAttribute("id", String.valueOf(company.getIdCompany()));
req.setAttribute("name", company.getName());
req.setAttribute("address", company.getAddress());
if (company.getUserLogin()!=null) {
req.setAttribute("userLogin", company.getUserLogin());
}
else {
req.setAttribute("userLogin", EMPTY);
}
//req.setAttribute("userPassword", company.getUserPassword());
if(company.getLastNameContact()!=null) {
req.setAttribute("lastNameContact", company.getLastNameContact());
} else {
req.setAttribute("lastNameContact", EMPTY);
}
if(company.getFirstNameContact()!=null) {
req.setAttribute("firstNameContact", company.getFirstNameContact());
} else {
req.setAttribute("firstNameContact", EMPTY);
}
if(company.getEmail()!=null) {
req.setAttribute("email", company.getEmail());
} else {
req.setAttribute("email", EMPTY);
}
if(company.getPhone()!=null) {
req.setAttribute("phone", company.getPhone());
} else {
req.setAttribute("phone", EMPTY);
}
req.setAttribute("status", String.valueOf(company.getStatus()));
req.setAttribute("registrationDate", company.getRegistrationDate().toString());
if (company.getAdministrator()!=null) {
req.setAttribute("administrator", String.valueOf(company.getAdministrator().getFirstName()+
" "+String.valueOf(company.getAdministrator().getLastName())));
} else {
req.setAttribute("administrator", EMPTY);
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//prepareDataForSelect(req);
targetPage = "/pages/company/companyDetails.jsp"; }
else if (ADD_ACTION.equals(method)) {
String name=prepareString(req.getParameter("name"));
//String administratorData=req.getParameter("administratorData");
String address=prepareString(req.getParameter("address"));
String userLogin=prepareString(req.getParameter("userLogin"));
String userPassword=prepareString(req.getParameter("userPassword"));
String lastNameContact=prepareString(req.getParameter("lastNameContact"));
String firstNameContact=prepareString(req.getParameter("firstNameContact"));
String email=prepareString(req.getParameter("email"));
String status=prepareString(req.getParameter("status"));
String phone=prepareString(req.getParameter("phone"));
Administrator administrator=null;
/*AdministratorService administratorService;
try {
administratorService = (AdministratorService)BusinessUtil.findBusiness("AdministratorService");
administrator=administratorService.load(1);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}*/
Company company=new Company();
company.setAddress(address);
company.setAdministrator(administrator);
company.setEmail(email);
company.setFirstNameContact(firstNameContact);
company.setLastNameContact(lastNameContact);
company.setName(name);
company.setPhone(phone);
company.setRegistrationDate(new Date());
company.setStatus(status.charAt(0));
company.setUserLogin(userLogin);
company.setUserPassword(userPassword);
try {
companyService.save(company);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
req.setAttribute("refreshTableFunction", "refreshCompanyTabele");
targetPage = "/pages/default/closeAndRefresh.jsp";
} else if(PRE_EDIT_ACTION.equals(method)) {
try {
//prepareDataForSelect(req);
Company company = companyService.load(Integer.valueOf(req.getParameter("id")));
req.setAttribute("id", String.valueOf(company.getIdCompany()));
req.setAttribute("name", company.getName());
req.setAttribute("address", company.getAddress());
req.setAttribute("userLogin", company.getUserLogin());
req.setAttribute("userPassword", company.getUserPassword());
req.setAttribute("lastNameContact", company.getLastNameContact());
req.setAttribute("firstNameContant", company.getFirstNameContact());
req.setAttribute("email", company.getEmail());
req.setAttribute("phone", company.getPhone());
req.setAttribute("status", String.valueOf(company.getStatus()));
req.setAttribute("registrationDate", company.getRegistrationDate().toString());
//if (company.getAdministrator()!=null) {
// req.setAttribute("selectedAdministrator", String.valueOf(company.getAdministrator().getIdAdministrator()));
//}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
targetPage = "/pages/company/companyForm.jsp";
req.setAttribute("isEdit", Boolean.TRUE);
} else if (EDIT_ACTION.equals(method)) {
String id=req.getParameter("id");
String name=req.getParameter("name");
//String administratorData=req.getParameter("administratorData");
String address=req.getParameter("address");
String userLogin=req.getParameter("userLogin");
String userPassword=req.getParameter("userPassword");
String lastNameContact=req.getParameter("lastNameContact");
String firstNameContact=req.getParameter("firstNameContact");
String email=req.getParameter("email");
String status=req.getParameter("status");
String phone=req.getParameter("phone");
String date=req.getParameter("registrationDate");
Administrator administrator=null;
/*AdministratorService administratorService;
try {
administratorService = (AdministratorService)BusinessUtil.findBusiness("AdministratorService");
administrator=administratorService.load(1);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}*/
Company company=new Company();
company.setIdCompany(Integer.valueOf(id));
company.setAddress(address);
company.setAdministrator(administrator);
company.setEmail(email);
company.setFirstNameContact(firstNameContact);
company.setLastNameContact(lastNameContact);
company.setName(name);
company.setPhone(phone);
company.setStatus(status.charAt(0));
//DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//Date today=null;
//try {
// today = df.parse(date);
//} catch (ParseException e1) {
// TODO Auto-generated catch block
// e1.printStackTrace();
//}
//company.setRegistrationDate(today);
company.setUserLogin(userLogin);
company.setUserPassword(userPassword);
try {
companyService.update(company);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
req.setAttribute("refreshTableFunction", "refreshCompanyTabele");