PseudoTranslate.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2013
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP
  6. Schedule Contract with IBM Corp.
  7. */
  8. import java.io.BufferedReader;
  9. import java.io.BufferedWriter;
  10. import java.io.DataOutputStream;
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.io.InputStreamReader;
  16. import java.io.OutputStream;
  17. import java.io.OutputStreamWriter;
  18. import java.net.HttpURLConnection;
  19. import java.net.MalformedURLException;
  20. import java.net.URL;
  21. import java.util.Iterator;
  22. import com.ibm.json.java.JSONArray;
  23. import com.ibm.json.java.JSONObject;
  24. /**
  25. * This code demonstrates model modifications. It finds an English name of a cube
  26. * and creates a pseudo-translated name adding lead-in and lead-out character.
  27. */
  28. public class PseudoTranslate {
  29. /**
  30. * URL to the server with the FMD SDK installed.
  31. */
  32. final private static String BI_SERVER_URL = "http://localhost:9300/p2pd/servlet/dispatch/";
  33. /**
  34. * The URL part that directs requests to the FMD SDK.
  35. */
  36. final private static String BASE_URL = BI_SERVER_URL + "FmCommand/";
  37. /**
  38. *
  39. */
  40. final private static String LOCALE_FROM = "EN";
  41. /**
  42. *
  43. */
  44. final private static String LOCALE_TO = "zh";
  45. /**
  46. *
  47. */
  48. final private static String PSEUDO_TRANSLATE_LEAD_IN = "入口";
  49. /**
  50. *
  51. */
  52. final private static String PSEUDO_TRANSLATE_LEAD_OUT = "出口";
  53. /**
  54. * POJO code that creates HTTP request passing data in JSON format.
  55. * <p>Should the request return a error or a warning, they are written out to the system error stream.</p>
  56. * @param keyword The URL path segment used to identify the request
  57. * @param method The HTTP method to use ("GET", "POST", "PUT", "DELETE").
  58. * @param json The data to be passed with the request, might be <code>null</code>
  59. * @param modelObjectId The Id of the model object targeted by this request, may be <code>null</code>
  60. * @return The JSON data returned by the SDK. Might be null.
  61. */
  62. static protected JSONObject callFMD(String keyword, String method, JSONObject json, String modelObjectId) {
  63. OutputStream os = null;
  64. BufferedReader br = null;
  65. HttpURLConnection conn = null;
  66. try {
  67. URL url = (modelObjectId == null) ? new URL(BASE_URL + keyword) : new URL(BASE_URL + keyword + "/"+modelObjectId);
  68. conn = (HttpURLConnection) url.openConnection();
  69. conn.setDoOutput(true);
  70. conn.setRequestMethod(method);
  71. // By default, server only allows "GET" and "POST" requests. Use X-header
  72. // to pass other verbs.
  73. if ("GET".equals(method) || "POST".equals(method))
  74. conn.setRequestMethod(method);
  75. else if ("PUT".equals(method) || "DELETE".equals(method)) {
  76. conn.setRequestMethod("POST");
  77. conn.setRequestProperty("X-HTTP-Method-Override", method);
  78. }
  79. conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
  80. if (json != null) {
  81. os = conn.getOutputStream();
  82. os.write(json.serialize().getBytes());
  83. os.flush();
  84. }
  85. br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
  86. String output;
  87. StringBuffer tmp = new StringBuffer();
  88. while ((output = br.readLine()) != null) {
  89. tmp.append(output);
  90. }
  91. JSONObject result = JSONObject.parse(tmp.toString());
  92. JSONArray errorsArray = (JSONArray) result.get("errors");
  93. if (errorsArray != null)
  94. System.err.println("Errors for request \"" + keyword + "\": " + errorsArray.toString());
  95. JSONArray warningsArray = (JSONArray) result.get("warnings");
  96. if (warningsArray != null)
  97. System.err.println("Warnings for request \"" + keyword + "\": " + warningsArray.toString());
  98. return result;
  99. } catch (MalformedURLException e) {
  100. e.printStackTrace();
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. } finally {
  104. if (json != null)
  105. json.clear();
  106. try {
  107. if (os != null)
  108. os.close();
  109. if (br != null)
  110. br.close();
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. }
  114. if (conn != null)
  115. conn.disconnect();
  116. }
  117. return null;
  118. }
  119. /**
  120. * A convenience function wrapping "save model to a stream" request.
  121. * @param file The local file in which to save the stream contents
  122. * @param modelObjectId The Id of the model object targeted by this request
  123. * @return <code>true</code> if model was saved successfully
  124. */
  125. static protected boolean saveToFile(File file, String modelObjectId) {
  126. BufferedReader replyReader = null;
  127. HttpURLConnection conn = null;
  128. BufferedWriter fileWriter = null;
  129. try {
  130. URL url = new URL(BASE_URL + "model_save_stream/" + modelObjectId);
  131. conn = (HttpURLConnection) url.openConnection();
  132. conn.setRequestMethod("POST");
  133. conn.setUseCaches(false);
  134. conn.setRequestProperty("Cache-Control", "no-cache");
  135. conn.setRequestProperty("Connection", "Keep-Alive");
  136. conn.setRequestProperty("Content-Type", "text/plain");
  137. replyReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
  138. String output;
  139. fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
  140. while ((output = replyReader.readLine()) != null) {
  141. fileWriter.write(output);
  142. fileWriter.newLine();
  143. }
  144. fileWriter.flush();
  145. return true;
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. } finally {
  149. try {
  150. if (replyReader != null)
  151. replyReader.close();
  152. if (fileWriter != null)
  153. fileWriter.close();
  154. if (conn != null)
  155. conn.disconnect();
  156. } catch (IOException ex) {
  157. ex.printStackTrace();
  158. }
  159. }
  160. return false;
  161. }
  162. /**
  163. * A convenience function wrapping "open model to a stream" request.
  164. * @param file The local file from which to read model contents
  165. * @return The model Id, if model was successfully opened. May return <code>null</code> if
  166. * model opening failed.
  167. */
  168. static protected String openFile(File file) {
  169. BufferedReader fileReader = null;
  170. BufferedReader replyReader = null;
  171. HttpURLConnection conn = null;
  172. try {
  173. URL url = new URL(BASE_URL + "model_open_stream");
  174. conn = (HttpURLConnection) url.openConnection();
  175. conn.setRequestMethod("POST");
  176. conn.setDoOutput(true);
  177. conn.setUseCaches(false);
  178. conn.setRequestProperty("Cache-Control", "no-cache");
  179. conn.setRequestProperty("Connection", "Keep-Alive");
  180. conn.setRequestProperty("Content-Type", "text/plain");
  181. DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
  182. fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
  183. String inLine;
  184. while ((inLine = fileReader.readLine()) != null) {
  185. outStream.write(inLine.getBytes("UTF-8"));
  186. }
  187. outStream.flush();
  188. outStream.close();
  189. replyReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
  190. String output;
  191. StringBuffer tmp = new StringBuffer();
  192. while ((output = replyReader.readLine()) != null) {
  193. tmp.append(output);
  194. }
  195. JSONObject result = JSONObject.parse(tmp.toString());
  196. JSONArray errorsArray = (JSONArray) result.get("errors");
  197. if (errorsArray != null)
  198. System.err.println("Errors for request \"model_open_stream\": " + errorsArray.toString());
  199. JSONArray warningsArray = (JSONArray) result.get("warnings");
  200. if (warningsArray != null)
  201. System.err.println("Warnings for request \"model_open_stream\": " + warningsArray.toString());
  202. return (String) result.get("id");
  203. } catch (IOException e) {
  204. e.printStackTrace();
  205. } finally {
  206. try {
  207. if (fileReader != null)
  208. fileReader.close();
  209. if (replyReader != null)
  210. replyReader.close();
  211. if (conn != null)
  212. conn.disconnect();
  213. } catch (IOException ex) {
  214. ex.printStackTrace();
  215. }
  216. }
  217. return null;
  218. }
  219. /**
  220. * Convenience method creating a pair of a text and locale.
  221. */
  222. static protected JSONArray localizedText(String text, String locale) {
  223. JSONObject tmp = new JSONObject();
  224. tmp.put("text", text);
  225. tmp.put("locale", locale);
  226. JSONArray nameArray = new JSONArray(1);
  227. nameArray.add(tmp);
  228. return nameArray;
  229. }
  230. public static void main(String[] args) throws IOException {
  231. if (args.length != 2) {
  232. System.out.println("Please supply arguments: <input_file> <output_file>");
  233. return;
  234. }
  235. // open the model
  236. File file = new File(args[0]);
  237. String modelId = (String) openFile(file);
  238. // find cubes
  239. JSONObject model = callFMD("model", "GET", null, modelId);
  240. JSONArray cubes = (JSONArray) model.get("cubes");
  241. for(Iterator<?> i = cubes.iterator(); i.hasNext(); ) {
  242. String cubeId = (String) i.next();
  243. JSONObject cube = callFMD("cube", "GET", null, cubeId);
  244. JSONArray names = (JSONArray) cube.get("name");
  245. // get a source name
  246. for(Iterator<?> j = names.iterator(); j.hasNext(); ) {
  247. JSONObject localizedName = (JSONObject) j.next();
  248. if (LOCALE_FROM.equals(localizedName.get("locale"))) {
  249. // construct pseudo-translation with double-byte lead-in and lead-out characters
  250. String sourceName = (String) localizedName.get("text");
  251. String targetName = PSEUDO_TRANSLATE_LEAD_IN + sourceName + PSEUDO_TRANSLATE_LEAD_OUT;
  252. // update the cube
  253. JSONObject updateData = new JSONObject();
  254. JSONArray newNames = localizedText(targetName, LOCALE_TO);
  255. updateData.put("name", newNames);
  256. callFMD("cube", "PUT", updateData, cubeId);
  257. break;
  258. }
  259. }
  260. }
  261. // save model
  262. File outputFile = new File(args[1]);
  263. saveToFile(outputFile, modelId);
  264. System.out.println("Created updated model at \"" + args[1] + "\".");
  265. }
  266. }