ru.develgame.jflickrorganizer.MainClass.java Source code

Java tutorial

Introduction

Here is the source code for ru.develgame.jflickrorganizer.MainClass.java

Source

/* This Source Code Form is subject to the terms of the Mozilla
 * Public License, v. 2.0. If a copy of the MPL was not distributed
 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * Copyright 2017 Ilya Zemskov */

package ru.develgame.jflickrorganizer;

import com.flickr4java.flickr.photos.Photo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import ru.develgame.jflickrorganizer.Common.LocaleMessages;
import ru.develgame.jflickrorganizer.DataModel.TreeAlbumsDataModel;
import ru.develgame.jflickrorganizer.Threads.BackupRunnable;
import ru.develgame.jflickrorganizer.Threads.BackupSetsRunnable;
import ru.develgame.jflickrorganizer.Threads.GetPhotoListRunnable;

/**
 *
 * @author Ilya Zemskov
 */
@Configuration
@ComponentScan("ru.develgame.jflickrorganizer")
@EnableTransactionManagement
@EnableJpaRepositories
public class MainClass {
    @Autowired
    @Qualifier("jFlickrOrganizerProperties")
    private Properties properties;

    @Bean
    @Scope("prototype")
    public GetPhotoListRunnable getPhotoListRunnable() {
        return new GetPhotoListRunnable();
    }

    @Bean
    @Scope("prototype")
    public BackupRunnable backupRunnable(List<Photo> photos, File backupFolder) {
        return new BackupRunnable(photos, backupFolder);
    }

    @Bean
    @Scope("prototype")
    public BackupSetsRunnable backupSetsRunnable() {
        return new BackupSetsRunnable();
    }

    @Bean
    @Scope("prototype")
    public TreeAlbumsDataModel treeAlbumsDataModel() {
        return new TreeAlbumsDataModel(null);
    }

    @Bean
    @Scope("prototype")
    public MainForm mainForm() {
        return new MainForm() {
            @Override
            protected GetPhotoListRunnable getGetPhotoListRunnable() {
                return getPhotoListRunnable();
            }

            @Override
            protected BackupRunnable getBackupRunnable(List<Photo> photos, File backupFolder) {
                return backupRunnable(photos, backupFolder);
            }

            @Override
            protected BackupSetsRunnable getBackupSetsRunnable() {
                return backupSetsRunnable();
            }
        };
    }

    @Bean
    @Scope("prototype")
    public LoginForm loginForm() {
        return new LoginForm() {
            @Override
            protected MainForm getMainForm() {
                return mainForm();
            }
        };
    }

    @Bean
    public Properties jFlickrOrganizerProperties() {
        String propertiesFile = "src/main/resources/setup.properties";
        Properties properties = null;
        try (FileInputStream fis = new FileInputStream(propertiesFile)) {
            properties = new Properties();
            properties.load(new FileInputStream(propertiesFile));
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, LocaleMessages.getMessage("Authorizer.Error.PropertyFileNotFound"),
                    LocaleMessages.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, LocaleMessages.getMessage("Authorizer.Error.PropertyFileNotFound"),
                    LocaleMessages.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
        }

        return properties;
    }

    // Database    
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource basicDataSource = new DriverManagerDataSource();
        basicDataSource.setDriverClassName("org.sqlite.JDBC");
        basicDataSource.setUrl("jdbc:sqlite:" + properties.getProperty("pathToDB", ""));
        basicDataSource.setUsername("");
        basicDataSource.setPassword("");
        return basicDataSource;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setShowSql(false);
        jpaVendorAdapter.setGenerateDdl(true);
        jpaVendorAdapter.setDatabasePlatform("ru.develgame.jflickrorganizer.Common.SQLiteDialect");
        return jpaVendorAdapter;
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(dataSource());
        emf.setJpaVendorAdapter(jpaVendorAdapter());
        emf.setPackagesToScan("ru.develgame.jflickrorganizer.entities");
        emf.setJpaProperties(additionalProperties());
        emf.afterPropertiesSet();

        return emf.getObject();
    }

    private Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        return properties;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        return new JpaTransactionManager(entityManagerFactory());
    }

    public static void main(String args[]) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainClass.class);

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | UnsupportedLookAndFeelException ex) {
        }

        context.getBean(LoginForm.class).setVisible(true);
    }
}