View Javadoc

1   /* ==============================================================================
2    *   JDBCAdmin, data management software.
3    *   Copyright (C) 2005  Norsys S.A
4    *
5    *   This library is free software; you can redistribute it and/or
6    *   modify it under the terms of the GNU Lesser General Public
7    *   License as published by the Free Software Foundation; either
8    *   version 2.1 of the License, or (at your option) any later version.
9    *
10   *   This library is distributed in the hope that it will be useful,
11   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *   Lesser General Public License for more details.
14   *
15   *   You should have received a copy of the GNU Lesser General Public
16   *   License along with this library; if not, write to the Free Software
17   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * ==============================================================================
19   */
20  package jdbcadmin.web;
21  
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpSession;
27  
28  import jdbcadmin.core.access.IAccessAuthorization;
29  import jdbcadmin.core.data.ConnectionInfo;
30  import jdbcadmin.core.data.Schema;
31  import jdbcadmin.core.data.Table;
32  import jdbcadmin.core.exceptions.DataNotFoundException;
33  import jdbcadmin.core.exceptions.TechnicalException;
34  import jdbcadmin.core.tools.DataManager;
35  import jdbcadmin.core.tools.JdbcInspector;
36  
37  /***
38   * Context Manager : Used to manage teh "state" of a user session.
39   * @author Thomas Recloux
40   */
41  public class ContexteManager {
42  
43      /*** Session key of the conection informations */
44      public static final String CNX_INFOS_KEY = "cnx.infos";
45      /*** Session key of the access restritions */
46      public static final String RESTRICTION_KEY = "restrictions";
47      /*** Session key of the JDBC Inspector */
48      public static final String INSPECTOR_KEY = "inspector";
49      /*** Session key of the data manager */
50      public static final String MANAGER_KEY = "manager";
51      /*** Session key of the current schema*/
52      public static final String SCHEMA_KEY = "schema";
53      /*** Session key of the schema list */
54      public static final String SCHEMA_LIST_KEY = "schemas";
55      /*** Session key current table */
56      public static final String TAB_KEY = "table";
57      /*** Session key of the table collection */
58      public static final String TABLES_COLL_KEY = "tables";
59      /*** Session key of the table map */
60      public static final String TABLES_MAP_KEY = "tablesmap";
61      /*** Session key current table index (browsing table) */
62      public static final String IDX_DATA_KEY = "idxdata";
63      /*** Session key of the db label */
64      public static final String LABEL_KEY = "label";
65  
66      /* ----------------------------------------- Constructor */
67      /*** Constructor */
68      protected ContexteManager () {
69      }
70  
71      /* ----------------------------------------- public methods */
72      /***
73       * Indicates wether or not the session is initialized
74       * @param aReq the http request
75       * @return true if initializes, otherwise false
76       */
77      public static boolean isInitialized(HttpServletRequest aReq) {
78          HttpSession session = aReq.getSession(false);
79          if (session != null) {
80              return (session.getAttribute(CNX_INFOS_KEY) != null && session
81                      .getAttribute(RESTRICTION_KEY) != null);
82          } else {
83              return false;
84          }
85      }
86  
87      /***
88       * @return The database inspector.
89       * @param aReq the HTTP request
90       */
91      public static JdbcInspector getInspector(HttpServletRequest aReq) {
92          init(aReq);
93          return (JdbcInspector) aReq.getSession().getAttribute(INSPECTOR_KEY);
94      }
95      /***
96       * @return The data manager
97       * @param aReq the HTTP request
98       */
99      public static DataManager getManager(HttpServletRequest aReq) {
100         init(aReq);
101         return (DataManager) aReq.getSession().getAttribute(MANAGER_KEY);
102     }
103     /***
104      * Set the current schema
105      * @param aReq the HTTP request
106      * @param aSchemaName name of the schema
107      * @throws TechnicalException technical error
108      */
109     public static void setCurrentSchema(HttpServletRequest aReq,
110             String aSchemaName) throws TechnicalException {
111         JdbcInspector insp = getInspector(aReq);
112         Schema schema = new Schema(aSchemaName);
113         aReq.getSession().setAttribute(SCHEMA_KEY, schema);
114         Map tables = insp.getNakedTables(ContexteManager.getSchema(aReq));
115         aReq.getSession().setAttribute(TABLES_MAP_KEY, tables);
116         aReq.getSession().setAttribute(TABLES_COLL_KEY, tables.values());
117     }
118 
119     /***
120      * @return recupere le schema courant
121      * @param aReq the HTTP request.
122      */
123     public static Schema getSchema(HttpServletRequest aReq) {
124         return (Schema) aReq.getSession().getAttribute(SCHEMA_KEY);
125     }
126 
127     /***
128      * Affecte la table courante
129      * @param aReq the HTTP request
130      * @param aTableName nom de la table
131      * @throws TechnicalException technical error
132      */
133     public static void setCurrentTable(HttpServletRequest aReq,
134             String aTableName) throws TechnicalException {
135         JdbcInspector insp = getInspector(aReq);
136         Table table = getSchemaTable(aReq, aTableName);
137         if (!table.isFilled()) {
138             insp.fillTable(table);
139         }
140         aReq.getSession().setAttribute(TAB_KEY, table);
141     }
142 
143     /***
144      * @return la table courante
145      * @param aReq the HTTP request
146      */
147     public static Table getCurrentTable(HttpServletRequest aReq) {
148         return (Table) aReq.getSession().getAttribute(TAB_KEY);
149     }
150 
151     /***
152      * Recupere les lignes d'une table
153      * @param aReq the HTTP request
154      * @param aDebIdx index de début
155      * @param aNbLignes nombre d elignes
156      * @return les lignes
157      * @throws TechnicalException technical error
158      * @throws DataNotFoundException données non trouvées
159      */
160     public static List getLignes(HttpServletRequest aReq, int aDebIdx,
161             int aNbLignes) throws TechnicalException, DataNotFoundException {
162         return getManager(aReq).select(getCurrentTable(aReq), aDebIdx,
163                 aNbLignes);
164     }
165 
166     /***
167      * Sets the data browsing index
168      * @param aReq the HTTP request
169      * @param aIdxDonnees the index
170      */
171     public static void setIdxDonnees(HttpServletRequest aReq, int aIdxDonnees) {
172         aReq.getSession().setAttribute(IDX_DATA_KEY, new Integer(aIdxDonnees));
173     }
174 
175     /***
176      * Sets the connection informations
177      * @param aReq the HTTP request
178      * @param aCnxInfos connection informations
179      */
180     public static void setCnxInfos(HttpServletRequest aReq,
181             ConnectionInfo aCnxInfos) {
182         aReq.getSession().setAttribute(CNX_INFOS_KEY, aCnxInfos);
183     }
184 
185     /***
186      * Sets the access restrictions
187      * @param aReq the HTTP request.
188      * @param aAccessAuth access restrictions
189      */
190     public static void setRestrictions(HttpServletRequest aReq,
191             IAccessAuthorization aAccessAuth) {
192         aReq.getSession().setAttribute(RESTRICTION_KEY, aAccessAuth);
193     }
194 
195     /***
196      * @return the datat browsing index
197      * @param aReq the HTTP request
198      */
199     public static int getIdxDonnees(HttpServletRequest aReq) {
200         return ((Integer) aReq.getSession().getAttribute(IDX_DATA_KEY))
201                 .intValue();
202     }
203 
204     /***
205      * Gets a table in the current schema
206      * @param aReq the HTTP request
207      * @param aTableName name of the table
208      * @return the table
209      */
210     public static Table getSchemaTable(HttpServletRequest aReq,
211             String aTableName) {
212         Map tables = (Map) aReq.getSession().getAttribute(TABLES_MAP_KEY);
213         return (Table) tables.get(aTableName);
214     }
215 
216     /***
217      * Sets the schema list
218      * @param aReq the HTTP request
219      * @param aSchemas the schema list
220      */
221     public static void setSchemas(HttpServletRequest aReq, List aSchemas) {
222         aReq.getSession().setAttribute(SCHEMA_LIST_KEY, aSchemas);
223     }
224 
225     /***
226      * Sets the label
227      * @param aRequest the HTTP request
228      * @param aLabel the db label
229      */
230     public static void setLabel(HttpServletRequest aRequest, String aLabel) {
231         aRequest.getSession().setAttribute(LABEL_KEY, aLabel);
232     }
233 
234     /* ----------------------------------------- private methods */
235     /***
236      * initializes the session.
237      * @param aRequest requete HTTP
238      */
239     private static void init(HttpServletRequest aRequest) {
240         HttpSession session = aRequest.getSession();
241         if (session.getAttribute(INSPECTOR_KEY) == null) {
242 
243             ConnectionInfo cnxInfos = (ConnectionInfo) session
244                     .getAttribute(CNX_INFOS_KEY);
245             IAccessAuthorization accessAuth = (IAccessAuthorization) session
246                     .getAttribute(RESTRICTION_KEY);
247 
248             JdbcInspector inspector = new JdbcInspector(cnxInfos, accessAuth);
249             DataManager manager = new DataManager(cnxInfos, accessAuth);
250 
251             session.setAttribute(INSPECTOR_KEY, inspector);
252             session.setAttribute(MANAGER_KEY, manager);
253         }
254     }
255 }