/api-sdk/src/main/java/com/smartling/api/sdk/file/parameters/FileUploadParameterBuilder.java

http://github.com/Smartling/api-sdk-java · Java · 175 lines · 119 code · 26 blank · 30 comment · 15 complexity · 6c26a4b37ad51b83d472a12abbd792d3 MD5 · raw file

  1. package com.smartling.api.sdk.file.parameters;
  2. import com.google.gson.JsonObject;
  3. import com.smartling.api.sdk.LibNameVersionHolder;
  4. import com.smartling.api.sdk.file.FileType;
  5. import org.apache.http.NameValuePair;
  6. import org.apache.http.message.BasicNameValuePair;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Map;
  12. /**
  13. * Uploads a file for translation to the Smartling Translation API params
  14. */
  15. public class FileUploadParameterBuilder implements ParameterBuilder
  16. {
  17. private static final String CLIENT_KEY = "client";
  18. private static final String VERSION_KEY = "version";
  19. private FileType fileType;
  20. private String fileUri;
  21. private Boolean authorize;
  22. private String callbackUrl;
  23. private List<String> localeIdsToAuthorize;
  24. private Boolean overwriteAuthorizedLocales;
  25. private Map<String, String> directives;
  26. private String charset;
  27. public FileUploadParameterBuilder (FileType fileType, String fileUri)
  28. {
  29. this.fileType = fileType;
  30. this.fileUri = fileUri;
  31. }
  32. /**
  33. * @param directives Directives a Map of smartling directives. Can be null
  34. * @return the current instance of a builder
  35. */
  36. public FileUploadParameterBuilder directives(final Map<String, String> directives)
  37. {
  38. this.directives = directives;
  39. return this;
  40. }
  41. /**
  42. * TRUE by default, it means that all locales in list will be approved everything
  43. * else excluded. If set to FALSE - no locales will be excluded, only existing and
  44. * locales which in the list will be approved. Can be null
  45. * @param overwriteAuthorizedLocales whether to overwrite locales
  46. * @return the current instance of a builder
  47. */
  48. public FileUploadParameterBuilder overwriteAuthorizedLocales(final Boolean overwriteAuthorizedLocales)
  49. {
  50. this.overwriteAuthorizedLocales = overwriteAuthorizedLocales;
  51. return this;
  52. }
  53. /**
  54. * List of locales to be approved after uploading the file. Can be null
  55. * @param localesToApprove list of locales to approve
  56. * @return the current instance of a builder
  57. */
  58. public FileUploadParameterBuilder localeIdsToAuthorize(final List<String> localesToApprove)
  59. {
  60. this.localeIdsToAuthorize = localesToApprove;
  61. return this;
  62. }
  63. public List<String> getLocaleIdsToAuthorize()
  64. {
  65. return null != localeIdsToAuthorize ? localeIdsToAuthorize : new ArrayList<String>();
  66. }
  67. /**
  68. * Callback url. Can be null
  69. * @param callbackUrl callback URL
  70. * @return the current instance of a builder
  71. */
  72. public FileUploadParameterBuilder callbackUrl(final String callbackUrl)
  73. {
  74. this.callbackUrl = callbackUrl;
  75. return this;
  76. }
  77. /**
  78. * TRUE if the file contents should be approved after uploading the file. Can be NULL.
  79. * NULL uses fileApi default of FALSE.
  80. * @param approveContent whether to approve content
  81. * @return the current instance of a builder
  82. */
  83. public FileUploadParameterBuilder approveContent(final Boolean approveContent)
  84. {
  85. this.authorize = approveContent;
  86. return this;
  87. }
  88. public String getFileUri()
  89. {
  90. return fileUri;
  91. }
  92. public FileUploadParameterBuilder charset(final String charset)
  93. {
  94. this.charset = charset;
  95. return this;
  96. }
  97. public String getCharset()
  98. {
  99. return charset;
  100. }
  101. public FileType getFileType()
  102. {
  103. return fileType;
  104. }
  105. @Override
  106. public List<NameValuePair> getNameValueList()
  107. {
  108. final List<NameValuePair> paramsList = new LinkedList<NameValuePair>();
  109. paramsList.add(new BasicNameValuePair(FileApiParameter.FILE_URI, fileUri));
  110. paramsList.add(new BasicNameValuePair(FileApiParameter.FILE_TYPE, fileType.getIdentifier()));
  111. paramsList.add(new BasicNameValuePair(FileApiParameter.AUTHORIZE, null == authorize ? null : Boolean.toString(authorize)));
  112. paramsList.add(new BasicNameValuePair(FileApiParameter.CALLBACK_URL, callbackUrl));
  113. if (localeIdsToAuthorize != null && !localeIdsToAuthorize.isEmpty())
  114. paramsList.addAll(convertLocalesBasedApproveParams(FileApiParameter.LOCALES_ID_TO_AUTHORIZE, localeIdsToAuthorize));
  115. if (overwriteAuthorizedLocales != null)
  116. paramsList.add(new BasicNameValuePair(FileApiParameter.OVERWRITE_AUTHORIZED_LOCALES, overwriteAuthorizedLocales.toString()));
  117. paramsList.add(new BasicNameValuePair(FileApiParameter.CLIENT_LIB_ID, getClientUidDirective()));
  118. paramsList.addAll(convertMapParams(directives));
  119. return paramsList;
  120. }
  121. private List<NameValuePair> convertLocalesBasedApproveParams(final String prefix, final List<String> values)
  122. {
  123. if (values == null || values.isEmpty())
  124. return Collections.emptyList();
  125. final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  126. for (int index = 0; index < values.size(); index++) {
  127. nameValuePairs.add(new BasicNameValuePair(prefix + "[]", values.get(index)));
  128. }
  129. return nameValuePairs;
  130. }
  131. private List<NameValuePair> convertMapParams(final Map<String, String> paramMap)
  132. {
  133. if (paramMap != null && !paramMap.isEmpty())
  134. {
  135. final List<NameValuePair> nameValuePairs = new LinkedList<NameValuePair>();
  136. for (final String key : paramMap.keySet())
  137. nameValuePairs.add(new BasicNameValuePair(key, paramMap.get(key)));
  138. return nameValuePairs;
  139. }
  140. return Collections.emptyList();
  141. }
  142. private String getClientUidDirective()
  143. {
  144. JsonObject object = new JsonObject();
  145. object.addProperty(CLIENT_KEY, LibNameVersionHolder.getClientLibName());
  146. object.addProperty(VERSION_KEY, LibNameVersionHolder.getClientLibVersion());
  147. return object.toString();
  148. }
  149. }