r54 - in trunk/echobase-services/src/main: java/fr/ifremer/echobase/services java/fr/ifremer/echobase/services/models resources
Author: tchemit Date: 2011-11-13 23:20:37 +0100 (Sun, 13 Nov 2011) New Revision: 54 Url: http://forge.codelutin.com/repositories/revision/echobase/54 Log: - add createAdmin in userService - can duplicate s service context (needed when two db context at the same time) - implements the EmbeddedApplicationService - improvement in service api Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java trunk/echobase-services/src/main/resources/echobase-embedded.properties Removed: trunk/echobase-services/src/main/resources/echobase-db-h2.properties Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractEchoBaseService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContext.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContextImpl.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/UserService.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ImportModel.java Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractEchoBaseService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractEchoBaseService.java 2011-11-13 22:18:21 UTC (rev 53) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/AbstractEchoBaseService.java 2011-11-13 22:20:37 UTC (rev 54) @@ -23,6 +23,7 @@ */ package fr.ifremer.echobase.services; +import fr.ifremer.echobase.EchoBaseConfiguration; import fr.ifremer.echobase.entities.meta.DbMeta; import org.nuiton.topia.TopiaContext; @@ -53,5 +54,14 @@ return serviceContext.getDbMeta(); } + protected EchoBaseConfiguration getConfiguration() { + return serviceContext.getConfiguration(); + } + protected <E extends EchoBaseService> E newService(Class<E> serviceClass) { + E e = serviceContext.newService(serviceClass); + return e; + } + + } Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorService.java 2011-11-13 22:18:21 UTC (rev 53) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/DbEditorService.java 2011-11-13 22:20:37 UTC (rev 54) @@ -35,7 +35,7 @@ import fr.ifremer.echobase.entities.meta.DbMeta; import fr.ifremer.echobase.entities.meta.TableMeta; import org.apache.commons.beanutils.BeanUtils; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.topia.TopiaException; @@ -65,8 +65,7 @@ protected DecoratorService getDecoratorService() { if (decoratorService == null) { - - decoratorService = serviceContext.newService(DecoratorService.class); + decoratorService = newService(DecoratorService.class); } return decoratorService; } @@ -230,7 +229,8 @@ } } - protected Map<String, Object> loadRow(TableMeta tableMeta, TopiaEntity entity) { + protected Map<String, Object> loadRow(TableMeta tableMeta, + TopiaEntity entity) { Map<String, Object> row = Maps.newLinkedHashMap(); EntityOperator<TopiaEntity> operator = (EntityOperator<TopiaEntity>) tableMeta.getOperator(); Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContext.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContext.java 2011-11-13 22:18:21 UTC (rev 53) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContext.java 2011-11-13 22:20:37 UTC (rev 54) @@ -42,12 +42,16 @@ TopiaContext getTransaction(); + void setTransaction(TopiaContext transaction); + Locale getLocale(); EchoBaseConfiguration getConfiguration(); DbMeta getDbMeta(); + EchoBaseServiceFactory getServiceFactory(); + <E extends EchoBaseService> E newService(Class<E> clazz); } Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContextImpl.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContextImpl.java 2011-11-13 22:18:21 UTC (rev 53) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EchoBaseServiceContextImpl.java 2011-11-13 22:20:37 UTC (rev 54) @@ -48,11 +48,35 @@ protected DbMeta dbMeta; - public EchoBaseServiceContextImpl(Locale locale, - TopiaContext transaction, - EchoBaseConfiguration configuration, - DbMeta dbMeta, - EchoBaseServiceFactory serviceFactory) { + public static EchoBaseServiceContextImpl newContext( + EchoBaseServiceContext serviceContext, + TopiaContext transaction) { + return newContext(serviceContext.getLocale(), + transaction, + serviceContext.getConfiguration(), + serviceContext.getDbMeta(), + serviceContext.getServiceFactory() + ); + } + + public static EchoBaseServiceContextImpl newContext( + Locale locale, + TopiaContext transaction, + EchoBaseConfiguration configuration, + DbMeta dbMeta, + EchoBaseServiceFactory serviceFactory) { + return new EchoBaseServiceContextImpl(locale, + transaction, + configuration, + dbMeta, + serviceFactory); + } + + protected EchoBaseServiceContextImpl(Locale locale, + TopiaContext transaction, + EchoBaseConfiguration configuration, + DbMeta dbMeta, + EchoBaseServiceFactory serviceFactory) { this.locale = locale; this.transaction = transaction; this.configuration = configuration; @@ -67,6 +91,11 @@ } @Override + public void setTransaction(TopiaContext transaction) { + this.transaction = transaction; + } + + @Override public Locale getLocale() { return locale; } @@ -86,7 +115,8 @@ return dbMeta; } - protected EchoBaseServiceFactory getServiceFactory() { + @Override + public EchoBaseServiceFactory getServiceFactory() { return serviceFactory; } } Added: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java (rev 0) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java 2011-11-13 22:20:37 UTC (rev 54) @@ -0,0 +1,201 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services; + +import com.google.common.base.Supplier; +import com.google.common.collect.Lists; +import fr.ifremer.echobase.EchoBaseTechnicalException; +import fr.ifremer.echobase.EchoBaseTopiaRootContextSupplierFactory; +import fr.ifremer.echobase.entities.EchoBaseUser; +import fr.ifremer.echobase.entities.ExportQuery; +import fr.ifremer.echobase.entities.Voyage; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.util.FileUtil; +import org.nuiton.util.ZipUtil; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.List; + +/** + * To create embedded application. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.1 + */ +public class EmbeddedApplicationService extends AbstractEchoBaseService { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(EmbeddedApplicationService.class); + + + public File createEmbeddedApplication(String fileName, String... voyageIds) { + + File warLocation = getConfiguration().getWarLocation(); + + File tempDirectory = new File(FileUtils.getTempDirectory(), + "echobase-embedded_" + System.nanoTime()); + tempDirectory.deleteOnExit(); + + File zipDirectory = new File(tempDirectory, fileName); + + File zipFile = new File(tempDirectory, "echobase.zip"); + + if (log.isInfoEnabled()) { + log.info("Creates zip file [" + zipFile + "] from directory " + + zipDirectory); + } + try { + // create / + FileUtil.createDirectoryIfNecessary(zipDirectory); + + // copy configuration file to / + copyConfigurationFile(zipDirectory); + + // copy war to / + FileUtil.copy(warLocation, new File(zipDirectory, warLocation.getName())); + + // create h2 db in /db + createH2Batabase(zipDirectory, voyageIds); + + } catch (Exception eee) { + throw new EchoBaseTechnicalException( + "Could not create zip structure at location " + + zipDirectory, eee); + } + + + try { + ZipUtil.compress(zipFile, zipDirectory); + } catch (IOException eee) { + throw new EchoBaseTechnicalException( + "Can not create zip file " + zipFile, eee); + } finally { + FileUtil.deleteRecursively(zipDirectory); + } + + return zipFile; + } + + protected void copyConfigurationFile(File zipDirectory) throws IOException { + + File configurationfile = new File(zipDirectory, "echobase.properties"); + if (log.isInfoEnabled()) { + log.info("Copy configuration to " + configurationfile); + } + InputStream configInputStream = + getClass().getResourceAsStream("/echobase-embedded.properties"); + OutputStream configOutputStream = new FileOutputStream(configurationfile); + try { + IOUtils.copy(configInputStream, configOutputStream); + } finally { + IOUtils.closeQuietly(configInputStream); + configOutputStream.close(); + } + } + + protected void createH2Batabase(File zipDirectory, + String... voyageIds) throws IOException, TopiaException { + + // create new h2 db + Supplier<TopiaContext> topiaContextSupplier = + new EchoBaseTopiaRootContextSupplierFactory().newEmbeddedDatabase(zipDirectory); + + TopiaContext rootContext = topiaContextSupplier.get(); + + TopiaContext topiaContext = rootContext.beginTransaction(); + try { + + // create new service context (with new transaction) + EchoBaseServiceContext newServiceContext = + EchoBaseServiceContextImpl.newContext(serviceContext, + topiaContext); + + // get user service from h2 db + UserService userService = + newServiceContext.newService(UserService.class); + + // create admin user + userService.createDefaultAdminUser(); + + // get admin from h2 db + EchoBaseUser admin = userService.getUserByEmail( + UserService.DEFAULT_ADMIN_EMAIL); + + // get export query service + ExportSqlService exportSqlService = + newService(ExportSqlService.class); + + // get all export queries from application + List<ExportQuery> queries = exportSqlService.getQueries(); + + // remove link to application user + for (ExportQuery query : queries) { + query.setLastModifiedUser(null); + } + + // replicate queries + getTransaction().replicateEntities(topiaContext, queries); + + // create export sql service from h2 db + exportSqlService = + newServiceContext.newService(ExportSqlService.class); + + // get all queries from h2 db + queries = exportSqlService.getQueries(); + + // attach them to default created admin user in the db + for (ExportQuery query : queries) { + exportSqlService.createOrUpdate(query, admin); + } + + // replicate referentiel to new h2 db + //TODO + + if (voyageIds != null) { + // replicate selected voyage to new h2 db + VoyageService voyageService = newService(VoyageService.class); + List<Voyage> voyages = Lists.newArrayList(); + for (String voyageId : voyageIds) { + Voyage voyage = voyageService.getVoyageById(voyageId); + voyages.add(voyage); + } + getTransaction().replicateEntities(topiaContext, voyages); + } + } finally { + topiaContext.closeContext(); + } + + } + +} Property changes on: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/EmbeddedApplicationService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlService.java 2011-11-13 22:18:21 UTC (rev 53) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/ExportSqlService.java 2011-11-13 22:20:37 UTC (rev 54) @@ -28,7 +28,7 @@ import fr.ifremer.echobase.entities.EchoBaseUser; import fr.ifremer.echobase.entities.ExportQuery; import fr.ifremer.echobase.entities.ExportQueryDAO; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.topia.TopiaContext; @@ -40,7 +40,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Map; @@ -56,7 +55,7 @@ /** Logger. */ private static final Log log = LogFactory.getLog(ExportSqlService.class); - public Collection<ExportQuery> getQueries() { + public List<ExportQuery> getQueries() { try { List<ExportQuery> result = getDAO().findAll(); return result; @@ -65,7 +64,7 @@ } } - public ExportQuery getQuery(String queryId) { + public ExportQuery getQueryById(String queryId) { try { ExportQuery result = getDAO().findByTopiaId(queryId); @@ -114,10 +113,6 @@ } } - protected ExportQueryDAO getDAO() throws TopiaException { - return EchoBaseDAOHelper.getExportQueryDAO(getTransaction()); - } - public Map<String, Object>[] executeSql(String sql, Pager pager) { // get a query to count all rows for the request @@ -148,7 +143,7 @@ } } - public String createCsvStream(String sql, EchoBaseUser echoBaseUser) { + public String createCsvFileContent(String sql, EchoBaseUser echoBaseUser) { GenericSQLQuery sqlQuery = new GenericSQLQuery(sql, null); Map<String, Object>[] rows; @@ -161,7 +156,7 @@ } StringBuilder buffer = new StringBuilder(); int rowCount = rows.length; - buffer.append("# Created at ").append(new Date()).append(" by user ").append(echoBaseUser.getEmail()); + buffer.append("#Created at ").append(new Date()).append(" by user ").append(echoBaseUser.getEmail()); buffer.append("\n#").append(StringUtils.join(columnNames, ";")); for (int i = 0; i < rowCount; i++) { Map<String, Object> row = rows[i]; @@ -171,6 +166,10 @@ return buffer.toString(); } + protected ExportQueryDAO getDAO() throws TopiaException { + return EchoBaseDAOHelper.getExportQueryDAO(getTransaction()); + } + private static class GenericSQLQuery extends TopiaSQLQuery<Map<String, Object>> { protected String[] columnNames; @@ -188,6 +187,17 @@ return columnNames; } + public Map<String, Object>[] getResult(TopiaContext tx) throws TopiaException { + List<Map<String, Object>> rows = + findMultipleResult((TopiaContextImplementor) tx); + return rows.toArray(new Map[rows.size()]); + } + + public String[] getColumnNames(TopiaContext tx) throws TopiaException { + findSingleResult((TopiaContextImplementor) tx); + return columnNames; + } + @Override protected PreparedStatement prepareQuery(Connection connection) throws SQLException { return connection.prepareStatement( @@ -236,18 +246,5 @@ return result; } - public Map<String, Object>[] getResult(TopiaContext tx) throws TopiaException { - List<Map<String, Object>> rows = - findMultipleResult((TopiaContextImplementor) tx); - return rows.toArray(new Map[rows.size()]); - } - - - public String[] getColumnNames(TopiaContext tx) throws TopiaException { - findSingleResult((TopiaContextImplementor) tx); - return columnNames; - } - - } } Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/UserService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/UserService.java 2011-11-13 22:18:21 UTC (rev 53) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/UserService.java 2011-11-13 22:20:37 UTC (rev 54) @@ -29,6 +29,7 @@ import fr.ifremer.echobase.entities.EchoBaseUser; import fr.ifremer.echobase.entities.EchoBaseUserDAO; import fr.ifremer.echobase.entities.EchoBaseUserDTO; +import fr.ifremer.echobase.entities.EchoBaseUserDTOImpl; import org.apache.commons.lang3.StringUtils; import org.nuiton.topia.TopiaException; import org.nuiton.topia.framework.TopiaQuery; @@ -42,6 +43,10 @@ */ public class UserService extends AbstractEchoBaseService { + public static final String DEFAULT_ADMIN_EMAIL = "admin"; + + public static final String DEFAULT_ADMIN_PASSWORD = "admin"; + public List<EchoBaseUser> getUsers() { return getUsers(null); } @@ -131,6 +136,25 @@ return s.equals(user.getPassword()); } + public void createDefaultAdminUser() { + EchoBaseUserDTO userDTO; + userDTO = new EchoBaseUserDTOImpl(); + userDTO.setEmail(DEFAULT_ADMIN_EMAIL); + userDTO.setPassword(DEFAULT_ADMIN_PASSWORD); + userDTO.setAdmin(true); + createOrUpdate(userDTO); + + if (getConfiguration().getOptionAsBoolean("createAdmins")) { + for (int i = 0; i < 1000; i++) { + userDTO = new EchoBaseUserDTOImpl(); + userDTO.setEmail(DEFAULT_ADMIN_EMAIL + i); + userDTO.setPassword(DEFAULT_ADMIN_PASSWORD); + userDTO.setAdmin(i % 2 == 0); + createOrUpdate(userDTO); + } + } + } + public static String encodePassword(String password) { String encodedPassword = StringUtil.encodeMD5(password); return encodedPassword; Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ImportModel.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ImportModel.java 2011-11-13 22:18:21 UTC (rev 53) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/models/ImportModel.java 2011-11-13 22:20:37 UTC (rev 54) @@ -24,7 +24,7 @@ package fr.ifremer.echobase.services.models; import fr.ifremer.echobase.entities.Voyage; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import java.io.File; Deleted: trunk/echobase-services/src/main/resources/echobase-db-h2.properties =================================================================== --- trunk/echobase-services/src/main/resources/echobase-db-h2.properties 2011-11-13 22:18:21 UTC (rev 53) +++ trunk/echobase-services/src/main/resources/echobase-db-h2.properties 2011-11-13 22:20:37 UTC (rev 54) @@ -1,31 +0,0 @@ -### -# #%L -# EchoBase :: Services -# -# $Id$ -# $HeadURL$ -# %% -# Copyright (C) 2011 Ifremer, Codelutin -# %% -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. -# #L% -### -hibernate.hbm2ddl.auto=update -hibernate.show_sql=false - -hibernate.dialect=org.hibernate.dialect.H2Dialect -hibernate.connection.username=sa -hibernate.connection.password=sa -hibernate.connection.driver_class=org.h2.Driver -hibernate.connection.url=jdbc:h2:file:${internal.db.directory}/echobase-user Copied: trunk/echobase-services/src/main/resources/echobase-embedded.properties (from rev 51, trunk/echobase-services/src/main/resources/echobase-db-h2.properties) =================================================================== --- trunk/echobase-services/src/main/resources/echobase-embedded.properties (rev 0) +++ trunk/echobase-services/src/main/resources/echobase-embedded.properties 2011-11-13 22:20:37 UTC (rev 54) @@ -0,0 +1,29 @@ +### +# #%L +# EchoBase :: Services +# +# $Id$ +# $HeadURL$ +# %% +# Copyright (C) 2011 Ifremer, Codelutin +# %% +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# #L% +### + +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.connection.username=sa +hibernate.connection.password=sa +hibernate.connection.driver_class=org.h2.Driver +hibernate.connection.url=jdbc:h2:file:./db/echobase Property changes on: trunk/echobase-services/src/main/resources/echobase-embedded.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native
participants (1)
-
tchemit@users.forge.codelutin.com