com.omicronware.streamlet.Main.java Source code

Java tutorial

Introduction

Here is the source code for com.omicronware.streamlet.Main.java

Source

/*
 * Copyright 2014 Makis.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.omicronware.streamlet;

import com.omicronware.database.ConnectionListener;
import com.omicronware.database.mysql.MysqlDatabase;
import com.omicronware.database.util.ObjectFactory;
import com.omicronware.javafx.Navigation;
import com.omicronware.streamlet.managers.AccountManager;
import com.omicronware.streamlet.managers.DownloadManager;
import com.omicronware.streamlet.managers.StoreObjectManager;
import com.omicronware.streamlet.objects.Category;
import com.omicronware.streamlet.objects.File;
import com.omicronware.streamlet.objects.FileDownload;
import com.omicronware.streamlet.objects.FilePacket;
import com.omicronware.streamlet.objects.FileSearchTerm;
import com.omicronware.streamlet.objects.Packet;
import com.omicronware.streamlet.objects.SettingsModel;
import com.omicronware.streamlet.objects.User;
import com.omicronware.streamlet.views.MainView;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.apache.commons.net.ftp.FTPClient;

public class Main extends Application {

    /*
        
        
     1)sto streamlet, otan o xristis prospa9ei na vrei kati kai den uparxei na apothikeuete se enan pinaka tis vaseis 
     dedomenwn , o opoios 9a emfanizete kai dhmosia sta request
     alla 9a borw na to vlepw kai egw wste na to vazw KAI OTAN TO ANEVASW TOTE NA ERXETE MIA EIDOPOIISH STON XRHSTH OTI 
     TO ARXEIO POU ZHTHSE PLEON UPARXEI
     (SAN TRAY-NOTIFICATION ALLA KAI MESA APTO PROGRAMMA.) prototupia auto
        
     2)
        
     2.1) Ta arxeia na apothikeuonte ston FTP me to id tou arxeiou
     2.2) kata to anevasma enos arxeiou apo enan xristi/anonymo tha ton rwtaei to onoma tou arxeiou, epeita 9a ton 
     rwtaei an 9elei  na tou kanei export to arxeio .stl (STreamLet) h tha sunexisei to anevasma kai gia alla arxeia 
     wste sti sunexeia na ta epileksei san checkbox kai na ta kanei ena paketo
     2.2) to arxeio .stl borei na perilamvanei panw apo ena arxeio, diladi 9a einai san ena packet arxeiwn 
     2.3) kata to create packet action tha rwtaei apton xristi tin katigoria tou paketou px music , software ktlp kai 
     to onoma tou paketou
     2.4) h morfi tha einai se encoded xml file opou tha exei autin tin ierarxeia :
        
     <packet name="my drivers" category="Software Drivers">
     <file id="20" name="intel widi driver" extension="msi" filesize="21230212"/>
     <file id="21" name="nvidia driver 335" extension="exe" filesize="43212391" />
     </packet>
        
        
     2.5) to arxeio borei na einai kai se polla file_packets tautoxrona.
     */
    public static final Navigation<MainView> NAVIGATION = new Navigation<>(MainView.class);
    private static final String defaultDlFolder = System.getProperty("user.home") + java.io.File.separator
            + "Desktop" + java.io.File.separator + "Downloads";
    public static SettingsModel SETTINGS;

    Stage stage;
    private static FTPClient client;
    public static MysqlDatabase DATABASE;

    @Override
    public void start(Stage stage) throws Exception {
        //register the different type of lists on ObjectFactory

        stage.setTitle("StreamLET - Omicronware Software (C)");
        stage.getIcons().add(new Image(
                getClass().getResource("/com/omicronware/streamlet/fxml/images/icon_96_96.png").toExternalForm()));
        stage.setOnHidden((WindowEvent event) -> {
            if (DATABASE != null) {
                DATABASE.close();
            }
            if (SETTINGS != null) {
                StoreObjectManager.getInstance().storeObject(SETTINGS);
            }
            Platform.exit();
            System.exit(0);
        });
        this.stage = stage;
        openDatabase();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    public static void loadFactories() {
        //register
        ObjectFactory.register(User.class, Category.class, FileDownload.class, File.class, FilePacket.class,
                FileSearchTerm.class, Packet.class);

        //fill 
        ObjectFactory.fill(DATABASE);

        //map filled in factories to the right objects.
        ObjectFactory.map(User.class, FileSearchTerm.class);
        ObjectFactory.map(File.class, FileSearchTerm.class);

        //this is one to one map -> 
        ObjectFactory.map(User.class, File.class);
        ObjectFactory.map(Category.class, File.class);
        ObjectFactory.map(User.class, FileDownload.class);
        ObjectFactory.map(File.class, FilePacket.class);
        ObjectFactory.map(User.class, FilePacket.class);
        ObjectFactory.map(User.class, Packet.class);
        //end of one to one map <-

        ObjectFactory.setAutoRemove(true);
        ObjectFactory.map(File.class, FileDownload.class);

        //and the one and only many to many with table manual map...
        ObjectFactory.list(Packet.class).stream().forEach(packet -> {

            List<FilePacket> collect = ObjectFactory.list(FilePacket.class).stream()
                    .filter(filepacket -> filepacket.getPacketId() == packet.getId()).collect(Collectors.toList());

            for (Iterator<FilePacket> fpacket = collect.iterator(); fpacket.hasNext();) {
                packet.getFiles().add(fpacket.next().getFile());
                fpacket.remove();
            }
            //.forEach(rightFilePacket -> packet.getFiles().add(rightFilePacket.getFile()));
        });

    }

    public static String getDownloadFolder() {
        if (SETTINGS == null) {
            SETTINGS = new SettingsModel(defaultDlFolder, null, null);
        }
        return SETTINGS.getDownloadFolder();
    }

    public void openDatabase() {
        //connect to mysql db

        if (DATABASE == null || (DATABASE != null && !DATABASE.isOpen())) {
            DATABASE = new MysqlDatabase("192.168.1.2", "makis", "j-sql", "streamlet", 3306);

            DATABASE.openAsync(new ConnectionListener() {

                @Override
                public void databaseConnected() {
                    loadFactories();
                    DownloadManager.prepareStatements(DATABASE);
                    try {
                        SETTINGS = StoreObjectManager.getInstance().getStoredObject(SettingsModel.class);
                    } catch (IOException | ClassNotFoundException ex) {
                        SETTINGS = null;
                    }
                    if (SETTINGS != null) {
                        //file exists or just downloadFolder or/and username&pass
                        AccountManager.getInstance().setMe(SETTINGS);
                    } else {
                        //we initiliaze it because can be null from SETTINGS = StoreObect...getStoredObject();
                        SETTINGS = new SettingsModel(defaultDlFolder, null, null);
                    }

                    Platform.runLater(() -> {
                        NAVIGATION.setPrimaryStage(stage);
                        NAVIGATION.show();
                    });
                }

                @Override
                public void databaseError(String error) {
                    Platform.runLater(() -> {
                        System.out.println(error);
                    });
                }
            });
        }
    }

    public static FTPClient getFtp() {
        if (client == null) {
            client = new FTPClient();
            try {
                client.connect("192.168.1.2");
                client.login("streamlet", "");
            } catch (IOException ex) {
                Logger.getLogger(DownloadManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return client;
    }

}