| | |
| | | |
| | | package de.uhilger.um.web; |
| | | |
| | | import de.uhilger.baselink.GenericRecord; |
| | | import de.uhilger.baselink.PersistenceManager; |
| | | import de.uhilger.baselink.Record; |
| | | import de.uhilger.um.api.UserMgr; |
| | | import de.uhilger.um.daten.User; |
| | | import de.uhilger.um.daten.UserData; |
| | | import de.uhilger.um.daten.UserRole; |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | |
| | | PersistenceManager db = new PersistenceManager(); |
| | | db.setDataSourceName(servletContext.getInitParameter(P_DSNAME)); |
| | | servletContext.setAttribute(UserMgr.UM_DB, db); |
| | | initMapper(servletContext); |
| | | Properties sql = (Properties) servletContext.getAttribute(UserMgr.UM_SQL_PROPERTIES); |
| | | if(!dbVorhanden(db, sql.getProperty(SQL_DB_VORHANDEN))) { |
| | | logger.info("Datenbank ist nicht vorhanden"); |
| | | int[] ergebnis = db.executeScript(getSqlSkript()); |
| | | User admin = new User(); |
| | | admin.setId("admin"); |
| | | admin.setPw("admin"); |
| | | admin.setFirstName("admin"); |
| | | admin.setLastName("admin"); |
| | | admin.setEmail("none"); |
| | | UserMgr um = new UserMgr(); |
| | | um.setServletContext(servletContext); |
| | | um.createUser(admin); |
| | | } |
| | | } catch(Exception ex) { |
| | | logger.log(Level.INFO, ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Mapper erzeugen |
| | | * @param servletContext der ServletContext dieser Webanwendung |
| | | */ |
| | | /* |
| | | TODO: das noch auf on demand anlegen: GenericRecord wird erst angelegt |
| | | wenn er erstmals benoetigt wird. Ggf. auch einen Mechanismus zum entfernen |
| | | laenger nicht benoetigter Mapper hinzufuegen |
| | | */ |
| | | private void initMapper(ServletContext servletContext) { |
| | | servletContext.setAttribute(UserMgr.MP_USER, new GenericRecord(User.class)); |
| | | servletContext.setAttribute(UserMgr.MP_USER_DATA, new GenericRecord(UserData.class)); |
| | | servletContext.setAttribute(UserMgr.MP_USER_ROLE, new GenericRecord(UserRole.class)); |
| | | } |
| | | |
| | | @SuppressWarnings("rawtypes") |
| | | private boolean dbVorhanden(PersistenceManager pm, String sql) { |
| | | boolean istVorhanden = false; |
| | | List<List<String>> list = pm.select(sql, Record.WITHOUT_BLOBS); |
| | | if(list.size() > 1) { |
| | | if(list != null && list.size() > 1) { |
| | | istVorhanden = true; |
| | | logger.fine("Datenbank ist vorhanden"); |
| | | } |
| | |
| | | @Override |
| | | public void contextDestroyed(ServletContextEvent sce) { |
| | | // destroy whatever |
| | | ServletContext ctx = sce.getServletContext(); |
| | | ctx.removeAttribute(UserMgr.MP_USER); |
| | | ctx.removeAttribute(UserMgr.MP_USER_DATA); |
| | | ctx.removeAttribute(UserMgr.MP_USER_ROLE); |
| | | ctx.removeAttribute(UserMgr.UM_SQL_PROPERTIES); |
| | | ctx.removeAttribute(UserMgr.UM_DB); |
| | | } |
| | | |
| | | } |