com.daarons.transfer.DownloadTask.java Source code

Java tutorial

Introduction

Here is the source code for com.daarons.transfer.DownloadTask.java

Source

/*
 * Copyright 2016 David Aarons.
 *
 * 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.daarons.transfer;

import com.daarons.controller.TransferSettingsController;
import com.daarons.dao.TransferRecordDAO;
import com.daarons.dao.TransferRecordDAOCsvImpl;
import com.daarons.model.DownloadTransferObject;
import com.daarons.model.TransferRecord;
import com.daarons.springconfig.SpringConfig;
import java.io.File;
import java.sql.Timestamp;
import java.util.Iterator;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 *
 * @author David
 */
public class DownloadTask extends Task {

    private static final Logger log = LogManager.getLogger(DownloadTask.class);
    private WebDriver driver;
    private WebDriverWait longWait, smallWait;
    private ObservableList<DownloadTransferObject> downloadList;
    TransferRecordDAO dao;

    public DownloadTask(ObservableList<DownloadTransferObject> downloadList) {
        this.downloadList = downloadList;
        dao = new TransferRecordDAOCsvImpl();
    }

    @Override
    protected Object call() throws Exception {
        initDownloadTask();

        acceptToS();

        Iterator<DownloadTransferObject> i = downloadList.iterator();
        while (i.hasNext()) {
            DownloadTransferObject downloadTransferObject = i.next();
            try {
                driver.get(downloadTransferObject.getUrl());
            } catch (Exception e) {
                log.error(e);
            }
            boolean doesPasswordExist = downloadTransferObject.getPassword() != null;
            if (doesPasswordExist) {
                enterPassword(downloadTransferObject.getPassword());
            }
            String fileName = getFileName();
            String downloadFolder = getDownloadFolderPath();
            clickDownloadButton();
            File downloadFile = new File(downloadFolder + fileName);
            while (!downloadFile.exists()) {
                if (downloadFile.exists()) {
                    break;
                }
            }

            TransferRecord transferRecord = new TransferRecord();
            transferRecord.setTimestamp(new Timestamp(System.currentTimeMillis()));
            transferRecord.setOperation("download");
            transferRecord.setLocalFile(downloadFile.getAbsolutePath());
            transferRecord.setLink(downloadTransferObject.getUrl());

            dao.addTransferRecord(transferRecord);
        }

        getTransferSettingsController().clearTable("download");
        driver.quit();
        return null;
    }

    private TransferSettingsController getTransferSettingsController() {
        return SpringConfig.getApplicationContext().getBean(TransferSettingsController.class);
    }

    private void initDownloadTask() {
        initChromeDriver();
        longWait = new WebDriverWait(driver, 60);
        smallWait = new WebDriverWait(driver, 3);
        driver.get("https://wetransfer.com/sign-in");
    }

    private void initChromeDriver() {
        System.setProperty("webdriver.chrome.driver", "chromedriver_win32/chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        driver = new ChromeDriver(options);
    }

    private void acceptToS() {
        try {
            WebElement acceptButton = smallWait
                    .until(ExpectedConditions.presenceOfElementLocated(By.className("transfer__button")));
            acceptButton.click();
        } catch (Exception ex) {
            log.error("Couldn't accept ToS", ex);
        }
    }

    private void enterPassword(String password) {
        WebElement passwordInput = driver.findElement(
                By.cssSelector("body > div > div > div.transfer > div > div.transfer__contents > div > input"));
        passwordInput.sendKeys(password);
        WebElement enterPasswordBtn = driver.findElement(By.className("transfer__button"));
        enterPasswordBtn.click();
    }

    private String getFileName() {
        String fileName = null;
        try {
            WebElement fileNameElement = longWait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(
                    "body > div > div > div.transfer > div > div.scrollable.transfer__contents > div.scrollable__content > ul > li > span.filelist__name")));
            fileName = fileNameElement.getText();
        } catch (Exception e) {
            log.error(e);
        }
        return fileName;
    }

    private String getDownloadFolderPath() {
        String downloadFolderPath = null;
        File downloadFolder = getTransferSettingsController().getDownloadFolder();
        if (downloadFolder == null) {
            downloadFolderPath = "C:\\Users\\" + System.getProperty("user.name") + "\\Downloads\\";
        } else {
            downloadFolderPath = downloadFolder.getAbsolutePath() + File.separatorChar;
        }
        return downloadFolderPath;
    }

    private void clickDownloadButton() {
        WebElement downloadButton = smallWait
                .until(ExpectedConditions.presenceOfElementLocated(By.className("transfer__button")));
        downloadButton.click();
    }
}