1
2
3
4
5
6
7 package org.thema.scriptware.controller;
8
9 import java.io.*;
10 import java.sql.Connection;
11 import java.sql.SQLException;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.Iterator;
16
17 import javax.servlet.*;
18 import javax.servlet.http.*;
19 import org.thema.scriptware.dao.DAOFactory;
20 import org.thema.scriptware.dao.DdlDAO;
21 import org.thema.scriptware.dao.DmlDAO;
22 import org.thema.scriptware.digester.Plugin;
23 import org.thema.scriptware.digester.Script;
24
25
26
27 /***
28 *
29 * @author Eduardo M . Sasso
30 * @version
31 */
32 public class ScriptServlet extends HttpServlet {
33
34 /*** Initializes the servlet.
35 */
36 String tipo;
37 String objeto;
38 String criterio;
39 Collection resultado;
40 DAOFactory daoFactory;
41
42 public void init(ServletConfig config) throws ServletException {
43 super.init(config);
44
45
46
47
48 }
49
50 /*** Destroys the servlet.
51 */
52 public void destroy() {
53
54
55 }
56
57 /*** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
58 * @param request servlet request
59 * @param response servlet response
60 */
61 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
62 throws ServletException, IOException {
63 String plugin = null;
64
65 tipo = request.getParameter("tipo");
66 objeto = request.getParameter("objeto");
67 criterio = request.getParameter("criterio");
68 resultado = new ArrayList();
69
70 HttpSession session = request.getSession();
71 Connection conn = (Connection)session.getAttribute("connection");
72
73 int whichFactory = 2;
74 daoFactory = DAOFactory.getDAOFactory(whichFactory);
75
76 if (request.getRequestURI().endsWith("/dmdl")){
77 processNormal(conn,request);
78 } else if (request.getRequestURI().endsWith("/plugin")){
79 plugin = request.getParameter("id");
80 proccessPlugin(plugin,conn,request);
81 } else if (request.getRequestURI().endsWith("/pluginParams")){
82 plugin = (String)request.getAttribute("id");
83 }
84
85 if (plugin != null) {
86 proccessPlugin(plugin,conn,request);
87 }
88
89 request.setAttribute("tipo",tipo);
90 request.setAttribute("objeto",objeto);
91 request.setAttribute("criterio",criterio);
92 request.setAttribute("resultado",resultado);
93
94
95 RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
96
97 dispatcher.forward(request,response);
98
99 }
100
101 private void processNormal(Connection conn,HttpServletRequest request) throws ServletException{
102 if (tipo.equalsIgnoreCase("dml")) {
103 DmlDAO dml = daoFactory.getDmlDAO(conn);
104 try {
105 if (criterio.equals("")){
106 resultado = dml.getInsertScript(objeto);
107 } else {
108 resultado = dml.getInsertScript(objeto, criterio);
109 }
110 }catch (SQLException e){
111 throw new ServletException(e.getMessage());
112 }
113 } else if (tipo.equalsIgnoreCase("ddl")){
114 DdlDAO ddl = daoFactory.getDdlDAO(conn);
115 try {
116 resultado = ddl.getScript(objeto);
117 } catch (SQLException e) {
118 throw new ServletException(e.getMessage());
119 }
120 }
121
122 }
123
124 private void proccessPlugin(String pluginID,Connection conn, HttpServletRequest request) throws ServletException {
125 HttpSession session = request.getSession();
126 HashMap plugins = (HashMap)session.getAttribute("pluginsMap");
127 Plugin plugin = (Plugin)plugins.get(pluginID);
128 ArrayList arrayList = plugin.getScripts();
129
130 Iterator iterator = arrayList.iterator();
131 Script script=null;
132 Collection resultadoAux = null;
133 DmlDAO dml = null;
134 DdlDAO ddl = null;
135 String sql = null;
136 while (iterator.hasNext()){
137 script = (Script) iterator.next();
138 sql = script.getSql();
139 if (!"".equalsIgnoreCase(script.getSqlParams())){
140 sql = script.getSqlParams();
141 }
142 if ("dml".equalsIgnoreCase(script.getType())){
143 dml = daoFactory.getDmlDAO(conn);
144 try {
145 resultadoAux = dml.getInsertScriptSQL(sql);
146 resultado.addAll(resultadoAux);
147 } catch (SQLException e) {
148 throw new ServletException(e.getMessage());
149 }
150 } else if ("ddl".equalsIgnoreCase(script.getType())){
151 ddl = daoFactory.getDdlDAO(conn);
152 try {
153 resultadoAux = ddl.getScriptSQL(sql);
154 resultado.addAll(resultadoAux);
155 } catch (SQLException e) {
156 throw new ServletException(e.getMessage());
157 }
158 }
159 }
160 }
161
162 /*** Handles the HTTP <code>GET</code> method.
163 * @param request servlet request
164 * @param response servlet response
165 */
166 protected void doGet(HttpServletRequest request, HttpServletResponse response)
167 throws ServletException, IOException {
168 processRequest(request, response);
169 }
170
171 /*** Handles the HTTP <code>POST</code> method.
172 * @param request servlet request
173 * @param response servlet response
174 */
175 protected void doPost(HttpServletRequest request, HttpServletResponse response)
176 throws ServletException, IOException {
177 processRequest(request, response);
178 }
179
180 /*** Returns a short description of the servlet.
181 */
182 public String getServletInfo() {
183 return "Short description";
184 }
185
186 }