package vu.de.npolke.myexpenses.servlets;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vu.de.npolke.myexpenses.model.Account;
import vu.de.npolke.myexpenses.model.Category;
import vu.de.npolke.myexpenses.model.Expense;
import vu.de.npolke.myexpenses.services.CategoryDAO;
import vu.de.npolke.myexpenses.services.DAOFactory;
import vu.de.npolke.myexpenses.services.ExpenseDAO;

/**
 * Copyright 2015 Niklas Polke
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * @author Niklas Polke
 */
@WebServlet("/addexpense")
public class AddExpenseServlet extends AbstractBasicServlet {

	private static final long serialVersionUID = 1L;

	private ExpenseDAO	expenseDAO	= (ExpenseDAO) DAOFactory.getDAO(Expense.class);
	private CategoryDAO	categoryDAO	= (CategoryDAO) DAOFactory.getDAO(Category.class);

	@Override
	protected void doGet(final HttpServletRequest request, final HttpServletResponse response,
			final HttpSession session, Account account) throws ServletException, IOException {
		
		List<Category> categories = categoryDAO.readByAccountId(account.getId());
		Calendar now = Calendar.getInstance(Locale.GERMANY);
		now.setTimeInMillis(System.currentTimeMillis());

		try {
			long id = Long.parseLong(request.getParameter("id"));
			Expense expense = expenseDAO.read(id);
			session.setAttribute("expense", expense);
		} catch (NumberFormatException nfe) {
			Expense defaultExpense = new Expense();
			defaultExpense.setDay(now.getTime());
			session.setAttribute("expense", defaultExpense);
		}
		
		session.setAttribute("categories", categories);
		response.sendRedirect("addexpense.jsp");
	}

	@Override
	public void doPost(final HttpServletRequest request, final HttpServletResponse response, final HttpSession session,
			Account account) throws ServletException, IOException {

		double amount = Double.parseDouble(request.getParameter("amount").replaceAll(",", "."));
		String reason = request.getParameter("reason");
		String day = request.getParameter("day");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		long categoryId = Long.valueOf(request.getParameter("category"));

		String readableDate = day + "." + month + "." + year;
		expenseDAO.create(readableDate, amount, reason, categoryId, account.getId());

		response.sendRedirect("listexpenses");
	}
}

@@ -2061,16 +2061,71 @@
 ad(id);%0A
+%09%09%09expense.setId(0);%0A%09%09%09expense.setDay(now.getTime());%0A
 %09%09%09sessi

Set time to current day when cloning expense