Search in sources :

Example 1 with Persistence

use of com.manydesigns.portofino.persistence.Persistence in project Portofino by ManyDesigns.

the class SessionCleaner method closeSessions.

static void closeSessions(ApplicationContext applicationContext) {
    Persistence persistence = applicationContext.getBean(Persistence.class);
    persistence.closeSessions();
}
Also used : Persistence(com.manydesigns.portofino.persistence.Persistence)

Example 2 with Persistence

use of com.manydesigns.portofino.persistence.Persistence in project Portofino by ManyDesigns.

the class CrudAction method saveConfiguration.

@Override
protected boolean saveConfiguration(Object configuration) {
    CrudConfiguration crudConfiguration = (CrudConfiguration) configuration;
    List<SelectionProviderReference> sps = new ArrayList<>(crudConfiguration.getSelectionProviders());
    crudConfiguration.getSelectionProviders().clear();
    crudConfiguration.persistence = persistence;
    crudConfiguration.init();
    sps.forEach(sp -> {
        ForeignKey fk = DatabaseLogic.findForeignKeyByName(crudConfiguration.getActualTable(), sp.getSelectionProviderName());
        if (fk != null) {
            sp.setForeignKeyName(sp.getSelectionProviderName());
            sp.setSelectionProviderName(null);
        }
        if (sp.getSelectionProviderName() != null || sp.getForeignKeyName() != null) {
            crudConfiguration.getSelectionProviders().add(sp);
        }
    });
    List<CrudProperty> existingProperties = this.crudConfiguration.getProperties();
    List<CrudProperty> configuredProperties = crudConfiguration.getProperties();
    List<CrudProperty> newProperties = configuredProperties.stream().map(p1 -> {
        Optional<CrudProperty> maybeP2 = existingProperties.stream().filter(p2 -> p1.getName().equals(p2.getName())).findFirst();
        CrudProperty p2 = maybeP2.orElse(new CrudProperty());
        p2.setName(p1.getName());
        p2.setEnabled(p1.isEnabled());
        p2.setInsertable(p1.isInsertable());
        p2.setInSummary(p1.isInSummary());
        p2.setLabel(p1.getLabel());
        p2.setSearchable(p1.isSearchable());
        p2.setUpdatable(p1.isUpdatable());
        return p2;
    }).collect(Collectors.toList());
    crudConfiguration.setProperties(newProperties);
    return super.saveConfiguration(crudConfiguration);
}
Also used : Persistence(com.manydesigns.portofino.persistence.Persistence) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) TableCriteria(com.manydesigns.portofino.persistence.TableCriteria) ResourceActionName(com.manydesigns.portofino.resourceactions.ResourceActionName) CrudConfiguration(com.manydesigns.portofino.resourceactions.crud.configuration.database.CrudConfiguration) QueryStringWithParameters(com.manydesigns.elements.text.QueryStringWithParameters) LoggerFactory(org.slf4j.LoggerFactory) Session(org.hibernate.Session) Autowired(org.springframework.beans.factory.annotation.Autowired) Insertable(com.manydesigns.elements.annotations.Insertable) ConfigurationClass(com.manydesigns.portofino.resourceactions.annotations.ConfigurationClass) SupportsPermissions(com.manydesigns.portofino.security.SupportsPermissions) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) JSQLParserException(net.sf.jsqlparser.JSQLParserException) ElementsThreadLocals(com.manydesigns.elements.ElementsThreadLocals) DatabaseLogic(com.manydesigns.portofino.model.database.DatabaseLogic) TableAccessor(com.manydesigns.portofino.reflection.TableAccessor) ClassAccessor(com.manydesigns.elements.reflection.ClassAccessor) Function(net.sf.jsqlparser.expression.Function) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) Table(com.manydesigns.portofino.model.database.Table) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) QueryUtils(com.manydesigns.portofino.persistence.QueryUtils) SelectionProviderReference(com.manydesigns.portofino.resourceactions.crud.configuration.database.SelectionProviderReference) AccessLevel(com.manydesigns.portofino.security.AccessLevel) Updatable(com.manydesigns.elements.annotations.Updatable) Logger(org.slf4j.Logger) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) CrudProperty(com.manydesigns.portofino.resourceactions.crud.configuration.CrudProperty) RequiresPermissions(com.manydesigns.portofino.security.RequiresPermissions) Collectors(java.util.stream.Collectors) IdStrategy(com.manydesigns.portofino.persistence.IdStrategy) RequestMessages(com.manydesigns.elements.messages.RequestMessages) Serializable(java.io.Serializable) PkHelper(com.manydesigns.portofino.util.PkHelper) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) ScriptTemplate(com.manydesigns.portofino.resourceactions.annotations.ScriptTemplate) Database(com.manydesigns.portofino.model.database.Database) Select(net.sf.jsqlparser.statement.select.Select) ForeignKey(com.manydesigns.portofino.model.database.ForeignKey) StringReader(java.io.StringReader) ActionInstance(com.manydesigns.portofino.resourceactions.ActionInstance) NotNull(org.jetbrains.annotations.NotNull) CCJSqlParserManager(net.sf.jsqlparser.parser.CCJSqlParserManager) SelectionProviderReference(com.manydesigns.portofino.resourceactions.crud.configuration.database.SelectionProviderReference) CrudProperty(com.manydesigns.portofino.resourceactions.crud.configuration.CrudProperty) ForeignKey(com.manydesigns.portofino.model.database.ForeignKey) CrudConfiguration(com.manydesigns.portofino.resourceactions.crud.configuration.database.CrudConfiguration)

Example 3 with Persistence

use of com.manydesigns.portofino.persistence.Persistence in project Portofino by ManyDesigns.

the class CrudActionTest method setup.

protected void setup(FileObject appDir) throws Exception {
    Configuration configuration = new PropertiesConfiguration();
    DatabasePlatformsRegistry databasePlatformsRegistry = new DatabasePlatformsRegistry(configuration);
    databasePlatformsRegistry.addDatabasePlatform(new H2DatabasePlatform());
    persistence = new Persistence(appDir, new ConfigurationSource(configuration, null), databasePlatformsRegistry);
    persistence.start();
    setupJPetStore();
    persistence.initModel();
}
Also used : Persistence(com.manydesigns.portofino.persistence.Persistence) H2DatabasePlatform(com.manydesigns.portofino.database.platforms.H2DatabasePlatform) ConfigurationSource(com.manydesigns.portofino.config.ConfigurationSource) CrudConfiguration(com.manydesigns.portofino.resourceactions.crud.configuration.database.CrudConfiguration) Configuration(org.apache.commons.configuration2.Configuration) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) DatabasePlatformsRegistry(com.manydesigns.portofino.model.database.platforms.DatabasePlatformsRegistry) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration)

Example 4 with Persistence

use of com.manydesigns.portofino.persistence.Persistence in project Portofino by ManyDesigns.

the class DatabaseModule method getPersistence.

@Bean
public Persistence getPersistence(@Autowired DatabasePlatformsRegistry databasePlatformsRegistry, @Autowired CacheResetListenerRegistry cacheResetListenerRegistry) throws FileSystemException {
    Persistence persistence = new Persistence(applicationDirectory, configuration, databasePlatformsRegistry);
    persistence.cacheResetListenerRegistry = cacheResetListenerRegistry;
    if (applicationContext != null) {
        // We may want it to be null when testing
        applicationContext.getAutowireCapableBeanFactory().autowireBean(persistence);
    }
    FileObject generatedClassesRoot = applicationDirectory.resolveFile(GENERATED_CLASSES_DIRECTORY_NAME);
    generatedClassesRoot.createFolder();
    AllFileSelector allFileSelector = new AllFileSelector();
    // When the entity mode is POJO:
    // - make generated classes visible to shared classes and actions;
    // - write them in the application directory so the user's IDE and tools can know about them.
    subscription = persistence.databaseSetupEvents.subscribe(e -> {
        String databaseName = e.setup.getDatabase().getDatabaseName();
        FileObject inMemoryDatabaseDir = e.setup.getCodeBase().getRoot().resolveFile(databaseName);
        FileObject externalDatabaseDir = generatedClassesRoot.resolveFile(databaseName);
        externalDatabaseDir.deleteAll();
        switch(e.type) {
            case Persistence.DatabaseSetupEvent.ADDED:
                persistenceCodeBase.add(e.setup.getCodeBase());
                if (e.setup.getEntityMode() == EntityMode.POJO) {
                    externalDatabaseDir.copyFrom(inMemoryDatabaseDir, allFileSelector);
                }
                break;
            case Persistence.DatabaseSetupEvent.REMOVED:
                persistenceCodeBase.remove(e.setup.getCodeBase());
                externalDatabaseDir.deleteAll();
                inMemoryDatabaseDir.deleteAll();
                break;
            case Persistence.DatabaseSetupEvent.REPLACED:
                persistenceCodeBase.replace(e.oldSetup.getCodeBase(), e.setup.getCodeBase());
                externalDatabaseDir.deleteAll();
                if (e.setup.getEntityMode() == EntityMode.POJO) {
                    externalDatabaseDir.copyFrom(inMemoryDatabaseDir, allFileSelector);
                }
                break;
        }
    });
    return persistence;
}
Also used : Persistence(com.manydesigns.portofino.persistence.Persistence) Persistence(com.manydesigns.portofino.persistence.Persistence) FileBasedConfigurationBuilder(org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder) CodeBase(com.manydesigns.portofino.code.CodeBase) LoggerFactory(org.slf4j.LoggerFactory) DatabasePlatformsRegistry(com.manydesigns.portofino.model.database.platforms.DatabasePlatformsRegistry) Autowired(org.springframework.beans.factory.annotation.Autowired) AggregateCodeBase(com.manydesigns.portofino.code.AggregateCodeBase) PreDestroy(javax.annotation.PreDestroy) Qualifier(org.springframework.beans.factory.annotation.Qualifier) CacheResetListenerRegistry(com.manydesigns.portofino.cache.CacheResetListenerRegistry) Logger(org.slf4j.Logger) EntityMode(org.hibernate.EntityMode) FileSystemException(org.apache.commons.vfs2.FileSystemException) BeansException(org.springframework.beans.BeansException) ApplicationListener(org.springframework.context.ApplicationListener) FileObject(org.apache.commons.vfs2.FileObject) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurationSource(com.manydesigns.portofino.config.ConfigurationSource) Configuration(org.apache.commons.configuration2.Configuration) MultiTenancyImplementationFactory(com.manydesigns.portofino.persistence.hibernate.multitenancy.MultiTenancyImplementationFactory) Disposable(io.reactivex.disposables.Disposable) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) PortofinoSpringConfiguration(com.manydesigns.portofino.spring.PortofinoSpringConfiguration) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) PostConstruct(javax.annotation.PostConstruct) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) ServletContext(javax.servlet.ServletContext) Bean(org.springframework.context.annotation.Bean) NotNull(org.jetbrains.annotations.NotNull) ApplicationContextAware(org.springframework.context.ApplicationContextAware) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileObject(org.apache.commons.vfs2.FileObject) Bean(org.springframework.context.annotation.Bean)

Example 5 with Persistence

use of com.manydesigns.portofino.persistence.Persistence in project Portofino by ManyDesigns.

the class CloseSessionsFilter method doFilter.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    try {
        chain.doFilter(request, response);
    } finally {
        MDC.clear();
        ServletContext servletContext = request.getServletContext();
        WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        Persistence persistence = applicationContext.getBean(Persistence.class);
        if (persistence.getModel() != null) {
            persistence.closeSessions();
        }
    }
}
Also used : Persistence(com.manydesigns.portofino.persistence.Persistence) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Aggregations

Persistence (com.manydesigns.portofino.persistence.Persistence)6 ConfigurationSource (com.manydesigns.portofino.config.ConfigurationSource)2 DatabasePlatformsRegistry (com.manydesigns.portofino.model.database.platforms.DatabasePlatformsRegistry)2 CrudConfiguration (com.manydesigns.portofino.resourceactions.crud.configuration.database.CrudConfiguration)2 Configuration (org.apache.commons.configuration2.Configuration)2 PropertiesConfiguration (org.apache.commons.configuration2.PropertiesConfiguration)2 NotNull (org.jetbrains.annotations.NotNull)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 ElementsThreadLocals (com.manydesigns.elements.ElementsThreadLocals)1 Insertable (com.manydesigns.elements.annotations.Insertable)1 Updatable (com.manydesigns.elements.annotations.Updatable)1 RequestMessages (com.manydesigns.elements.messages.RequestMessages)1 ClassAccessor (com.manydesigns.elements.reflection.ClassAccessor)1 PropertyAccessor (com.manydesigns.elements.reflection.PropertyAccessor)1 QueryStringWithParameters (com.manydesigns.elements.text.QueryStringWithParameters)1 CacheResetListenerRegistry (com.manydesigns.portofino.cache.CacheResetListenerRegistry)1 AggregateCodeBase (com.manydesigns.portofino.code.AggregateCodeBase)1 CodeBase (com.manydesigns.portofino.code.CodeBase)1