All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.smartling.maven.plugins.smartlingapi.mojo.GetFilesListMojo Maven / Gradle / Ivy

The newest version!
package com.smartling.maven.plugins.smartlingapi.mojo;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import au.com.bytecode.opencsv.CSVWriter;

import com.smartling.api.sdk.dto.file.FileStatus;
import com.smartling.api.sdk.exceptions.ApiException;
import com.smartling.api.sdk.file.FileListSearchParams;
import com.smartling.maven.plugins.smartlingapi.SmartlingApiMavenPluginUtils;

/**
 * Downloads list of files from Smartling TMS.
 *
 * @version $Id$
 * @since 1.0.0
 */
@Mojo(name = "getFilesList", requiresOnline = true, threadSafe = true)
class GetFilesListMojo extends AbstractSmartlingFileApiMojo
{
    private static final String SAVE_FILES_LIST_INFO_MESSAGE =
            SmartlingApiMavenPluginUtils.SMARTLING_API_PLUGIN_NAME + " writing files list; destFileName=%s";
    private static final String SAVE_FILES_LIST_FAILED_ERROR_MESSAGE =
            SmartlingApiMavenPluginUtils.SMARTLING_API_PLUGIN_NAME + " failed writing files list; destFileName=%s";


    /**
     *  Target files filter.
     */
    @Parameter
    private FileListSearchParams filesFilter;

    /**
     * Destination property where to put the resulting files list.
     */
    @Parameter(defaultValue = SmartlingApiMavenPluginUtils.SMARTLING_WORK_DIRECTORY + "/filesList.csv")
    private String destFileName;


    @Override
    public String getGoalName()
    {
        return "getFilesList";
    }

    @Override
    public void doExecute() throws MojoExecutionException, MojoFailureException
    {
        getFilesList();
    }

    private void getFilesList() throws MojoFailureException
    {
        try
        {
            final List filesList = downloadFilesList(filesFilter);
            saveFilesListCsv(filesList);
        }
        catch (final ApiException e)
        {
            handleError(String.format(DOWNLOAD_FILES_LIST_FAILED_ERROR_MESSAGE), e);
        }
        catch (final IOException e)
        {
            handleError(String.format(SAVE_FILES_LIST_FAILED_ERROR_MESSAGE, destFileName), e);
        }
    }

    private void saveFilesListCsv(final List filesList) throws IOException
    {
        getLog().info(String.format(SAVE_FILES_LIST_INFO_MESSAGE, destFileName));

        final File destFile = new File(destFileName);
        SmartlingApiMavenPluginUtils.createParentDirIfNotExists(destFile);

        final CSVWriter writer = new CSVWriter(new FileWriter(destFile));
        for (final FileStatus fileStatus : filesList)
        {
            final String[] csvLine = new String[]{
                    fileStatus.getFileUri(),
                    String.valueOf(fileStatus.getStringCount()),
                    String.valueOf(fileStatus.getWordCount()),
                    String.valueOf(fileStatus.getApprovedStringCount()),
                    String.valueOf(fileStatus.getCompletedStringCount()),
                    fileStatus.getLastUploaded(),
                    fileStatus.getFileType(),
                    fileStatus.getCallbackUrl(),
                    };
            writer.writeNext(csvLine);
        }
       writer.close();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy