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

com.smartling.api.sdk.file.parameters.FileUploadParameterBuilder Maven / Gradle / Ivy

There is a newer version: 4.0.25
Show newest version
package com.smartling.api.sdk.file.parameters;

import com.google.gson.JsonObject;
import com.smartling.api.sdk.LibNameVersionHolder;
import com.smartling.api.sdk.file.FileType;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Uploads a file for translation to the Smartling Translation API params
 */
public class FileUploadParameterBuilder implements ParameterBuilder
{
    private static final String CLIENT_KEY = "client";
    private static final String VERSION_KEY = "version";

    private FileType fileType;
    private String fileUri;
    private Boolean authorize;
    private String callbackUrl;
    private List localeIdsToAuthorize;
    private Boolean overwriteAuthorizedLocales;
    private Map directives;
    private String charset;

    public FileUploadParameterBuilder (FileType fileType, String fileUri)
    {
        this.fileType = fileType;
        this.fileUri = fileUri;
    }

    /**
     * @param directives Directives a Map of smartling directives. Can be null
     * @return the current instance of a builder
     */
    public FileUploadParameterBuilder directives(final Map directives)
    {
        this.directives = directives;
        return this;
    }

    /**
     * TRUE by default, it means that all locales in list will be approved everything
     * else excluded. If set to FALSE - no locales will be excluded, only existing and
     * locales which in the list will be approved. Can be null
     * @param   overwriteAuthorizedLocales whether to overwrite locales
     * @return  the current instance of a builder
     */
    public FileUploadParameterBuilder overwriteAuthorizedLocales(final Boolean overwriteAuthorizedLocales)
    {
        this.overwriteAuthorizedLocales = overwriteAuthorizedLocales;
        return this;
    }

    /**
     * List of locales to be approved after uploading the file. Can be null
     * @param localesToApprove list of locales to approve
     * @return the current instance of a builder
     */
    public FileUploadParameterBuilder localeIdsToAuthorize(final List localesToApprove)
    {
        this.localeIdsToAuthorize = localesToApprove;
        return this;
    }

    public List getLocaleIdsToAuthorize()
    {
        return null != localeIdsToAuthorize ? localeIdsToAuthorize : new ArrayList();
    }

    /**
     * Callback url. Can be null
     * @param callbackUrl callback URL
     * @return the current instance of a builder
     */
    public FileUploadParameterBuilder callbackUrl(final String callbackUrl)
    {
        this.callbackUrl = callbackUrl;
        return this;
    }

    /**
     * TRUE if the file contents should be approved after uploading the file. Can be NULL.
     * NULL uses fileApi default of FALSE.
     * @param approveContent whether to approve content
     * @return the current instance of a builder
     */
    public FileUploadParameterBuilder approveContent(final Boolean approveContent)
    {
        this.authorize = approveContent;
        return this;
    }

    public String getFileUri()
    {
        return fileUri;
    }


    public FileUploadParameterBuilder charset(final String charset)
    {
        this.charset = charset;
        return this;
    }

    public String getCharset()
    {
        return charset;
    }

    public FileType getFileType()
    {
        return fileType;
    }

    @Override
    public List getNameValueList()
    {
        final List paramsList = new LinkedList();

        paramsList.add(new BasicNameValuePair(FileApiParameter.FILE_URI, fileUri));
        paramsList.add(new BasicNameValuePair(FileApiParameter.FILE_TYPE, fileType.getIdentifier()));
        paramsList.add(new BasicNameValuePair(FileApiParameter.AUTHORIZE, null == authorize ? null : Boolean.toString(authorize)));
        paramsList.add(new BasicNameValuePair(FileApiParameter.CALLBACK_URL, callbackUrl));
        if (localeIdsToAuthorize != null && !localeIdsToAuthorize.isEmpty())
            paramsList.addAll(convertLocalesBasedApproveParams(FileApiParameter.LOCALES_ID_TO_AUTHORIZE, localeIdsToAuthorize));
        if (overwriteAuthorizedLocales != null)
            paramsList.add(new BasicNameValuePair(FileApiParameter.OVERWRITE_AUTHORIZED_LOCALES, overwriteAuthorizedLocales.toString()));
        paramsList.add(new BasicNameValuePair(FileApiParameter.CLIENT_LIB_ID, getClientUidDirective()));
        paramsList.addAll(convertMapParams(directives));

        return paramsList;
    }

    private List convertLocalesBasedApproveParams(final String prefix, final List values)
    {
        if (values == null || values.isEmpty())
            return Collections.emptyList();

        final List nameValuePairs = new ArrayList();

        for (int index = 0; index < values.size(); index++) {
            nameValuePairs.add(new BasicNameValuePair(prefix + "[]", values.get(index)));
        }

        return nameValuePairs;
    }

    private List convertMapParams(final Map paramMap)
    {
        if (paramMap != null && !paramMap.isEmpty())
        {
            final List nameValuePairs = new LinkedList();
            for (final String key : paramMap.keySet())
                nameValuePairs.add(new BasicNameValuePair(key, paramMap.get(key)));

            return nameValuePairs;
        }
        return Collections.emptyList();
    }

    private String getClientUidDirective()
    {
        JsonObject object = new JsonObject();
        object.addProperty(CLIENT_KEY, LibNameVersionHolder.getClientLibName());
        object.addProperty(VERSION_KEY, LibNameVersionHolder.getClientLibVersion());
        return object.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy