| 1 | package net.sourceforge.finmodel.ui; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.io.PrintWriter; |
| 5 | import java.io.UnsupportedEncodingException; |
| 6 | import java.net.URLEncoder; |
| 7 | import java.util.logging.Logger; |
| 8 | |
| 9 | import javax.servlet.ServletException; |
| 10 | import javax.servlet.http.HttpServlet; |
| 11 | import javax.servlet.http.HttpServletRequest; |
| 12 | import javax.servlet.http.HttpServletResponse; |
| 13 | |
| 14 | import net.sourceforge.finmodel.account.Account; |
| 15 | import net.sourceforge.finmodel.plan.FinancialPlan; |
| 16 | import net.sourceforge.finmodel.rules.Rule; |
| 17 | |
| 18 | public class PlanInfoServlet extends HttpServlet { |
| 19 | private static Logger log = Logger.getLogger("net.sourceforge.finmodel.ui.PlanInfoServlet"); |
| 20 | public static final long serialVersionUID = 1L; |
| 21 | |
| 22 | public void doGet(HttpServletRequest request, HttpServletResponse response ) |
| 23 | throws IOException, ServletException |
| 24 | { |
| 25 | // TODO: Implement the XML better. |
| 26 | PrintWriter out = response.getWriter(); |
| 27 | |
| 28 | out.println("<?xml version='1.0'?>"); |
| 29 | String planIdStr = request.getParameter("planId"); |
| 30 | long planId = Long.parseLong(planIdStr); |
| 31 | |
| 32 | FinancialPlan plan = FinancialPlan.load(planId); |
| 33 | out.println("<plan id='" + plan.getId() + "' name='" + encode(plan.getName()) + "'>"); |
| 34 | |
| 35 | out.println("<accounts>"); |
| 36 | for (Account acct: plan.getAccounts()) { |
| 37 | out.println("<account name='" + acct.getName() + "' id='" + acct.getId() + "' />"); |
| 38 | } |
| 39 | out.println("</accounts>"); |
| 40 | |
| 41 | out.println("<rules>"); |
| 42 | for (Rule rule: plan.getRules()) { |
| 43 | out.println("<rule id='" + rule.getId() + ">"); |
| 44 | out.println("<acctSource id='" + rule.getSource().getId() + "' name='" + |
| 45 | encode(rule.getSource().getName()) + "' />"); |
| 46 | out.println("<acctDest id='" + rule.getDestination().getId() + "' name='" + |
| 47 | encode(rule.getDestination().getName()) + "' />"); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | private String encode( String str ) |
| 52 | { |
| 53 | try { |
| 54 | return URLEncoder.encode(str, "UTF-8"); |
| 55 | } catch (UnsupportedEncodingException uee) { |
| 56 | log.info("UnsupportedEncodingException: " + uee.getMessage()); |
| 57 | return str; |
| 58 | } |
| 59 | } |
| 60 | } |