123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- /**
- Licensed Materials - Property of IBM
- IBM Cognos Products: DOCS
- (C) Copyright IBM Corp. 2013
- US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP
- Schedule Contract with IBM Corp.
- */
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Iterator;
- import com.ibm.json.java.JSONArray;
- import com.ibm.json.java.JSONObject;
- /**
- * This code demonstrates model modifications. It finds an English name of a cube
- * and creates a pseudo-translated name adding lead-in and lead-out character.
- */
- public class PseudoTranslate {
-
- /**
- * URL to the server with the FMD SDK installed.
- */
- final private static String BI_SERVER_URL = "http://localhost:9300/p2pd/servlet/dispatch/";
-
- /**
- * The URL part that directs requests to the FMD SDK.
- */
- final private static String BASE_URL = BI_SERVER_URL + "FmCommand/";
-
- /**
- *
- */
- final private static String LOCALE_FROM = "EN";
- /**
- *
- */
- final private static String LOCALE_TO = "zh";
- /**
- *
- */
- final private static String PSEUDO_TRANSLATE_LEAD_IN = "入口";
- /**
- *
- */
- final private static String PSEUDO_TRANSLATE_LEAD_OUT = "出口";
- /**
- * POJO code that creates HTTP request passing data in JSON format.
- * <p>Should the request return a error or a warning, they are written out to the system error stream.</p>
- * @param keyword The URL path segment used to identify the request
- * @param method The HTTP method to use ("GET", "POST", "PUT", "DELETE").
- * @param json The data to be passed with the request, might be <code>null</code>
- * @param modelObjectId The Id of the model object targeted by this request, may be <code>null</code>
- * @return The JSON data returned by the SDK. Might be null.
- */
- static protected JSONObject callFMD(String keyword, String method, JSONObject json, String modelObjectId) {
- OutputStream os = null;
- BufferedReader br = null;
- HttpURLConnection conn = null;
- try {
- URL url = (modelObjectId == null) ? new URL(BASE_URL + keyword) : new URL(BASE_URL + keyword + "/"+modelObjectId);
- conn = (HttpURLConnection) url.openConnection();
- conn.setDoOutput(true);
- conn.setRequestMethod(method);
- // By default, server only allows "GET" and "POST" requests. Use X-header
- // to pass other verbs.
- if ("GET".equals(method) || "POST".equals(method))
- conn.setRequestMethod(method);
- else if ("PUT".equals(method) || "DELETE".equals(method)) {
- conn.setRequestMethod("POST");
- conn.setRequestProperty("X-HTTP-Method-Override", method);
- }
- conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
-
- if (json != null) {
- os = conn.getOutputStream();
- os.write(json.serialize().getBytes());
- os.flush();
- }
-
- br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
- String output;
- StringBuffer tmp = new StringBuffer();
- while ((output = br.readLine()) != null) {
- tmp.append(output);
- }
-
- JSONObject result = JSONObject.parse(tmp.toString());
- JSONArray errorsArray = (JSONArray) result.get("errors");
- if (errorsArray != null)
- System.err.println("Errors for request \"" + keyword + "\": " + errorsArray.toString());
- JSONArray warningsArray = (JSONArray) result.get("warnings");
- if (warningsArray != null)
- System.err.println("Warnings for request \"" + keyword + "\": " + warningsArray.toString());
-
- return result;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (json != null)
- json.clear();
- try {
- if (os != null)
- os.close();
- if (br != null)
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (conn != null)
- conn.disconnect();
- }
- return null;
- }
-
- /**
- * A convenience function wrapping "save model to a stream" request.
- * @param file The local file in which to save the stream contents
- * @param modelObjectId The Id of the model object targeted by this request
- * @return <code>true</code> if model was saved successfully
- */
- static protected boolean saveToFile(File file, String modelObjectId) {
- BufferedReader replyReader = null;
- HttpURLConnection conn = null;
- BufferedWriter fileWriter = null;
- try {
- URL url = new URL(BASE_URL + "model_save_stream/" + modelObjectId);
- conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("POST");
- conn.setUseCaches(false);
- conn.setRequestProperty("Cache-Control", "no-cache");
- conn.setRequestProperty("Connection", "Keep-Alive");
- conn.setRequestProperty("Content-Type", "text/plain");
-
- replyReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
- String output;
- fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
- while ((output = replyReader.readLine()) != null) {
- fileWriter.write(output);
- fileWriter.newLine();
- }
- fileWriter.flush();
- return true;
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (replyReader != null)
- replyReader.close();
- if (fileWriter != null)
- fileWriter.close();
- if (conn != null)
- conn.disconnect();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- return false;
- }
- /**
- * A convenience function wrapping "open model to a stream" request.
- * @param file The local file from which to read model contents
- * @return The model Id, if model was successfully opened. May return <code>null</code> if
- * model opening failed.
- */
- static protected String openFile(File file) {
- BufferedReader fileReader = null;
- BufferedReader replyReader = null;
- HttpURLConnection conn = null;
- try {
- URL url = new URL(BASE_URL + "model_open_stream");
- conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("POST");
- conn.setDoOutput(true);
- conn.setUseCaches(false);
- conn.setRequestProperty("Cache-Control", "no-cache");
- conn.setRequestProperty("Connection", "Keep-Alive");
- conn.setRequestProperty("Content-Type", "text/plain");
-
- DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
- fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
- String inLine;
- while ((inLine = fileReader.readLine()) != null) {
- outStream.write(inLine.getBytes("UTF-8"));
- }
-
- outStream.flush();
- outStream.close();
-
- replyReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
- String output;
- StringBuffer tmp = new StringBuffer();
- while ((output = replyReader.readLine()) != null) {
- tmp.append(output);
- }
- JSONObject result = JSONObject.parse(tmp.toString());
- JSONArray errorsArray = (JSONArray) result.get("errors");
- if (errorsArray != null)
- System.err.println("Errors for request \"model_open_stream\": " + errorsArray.toString());
- JSONArray warningsArray = (JSONArray) result.get("warnings");
- if (warningsArray != null)
- System.err.println("Warnings for request \"model_open_stream\": " + warningsArray.toString());
-
- return (String) result.get("id");
-
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (fileReader != null)
- fileReader.close();
- if (replyReader != null)
- replyReader.close();
- if (conn != null)
- conn.disconnect();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- return null;
- }
-
- /**
- * Convenience method creating a pair of a text and locale.
- */
- static protected JSONArray localizedText(String text, String locale) {
- JSONObject tmp = new JSONObject();
- tmp.put("text", text);
- tmp.put("locale", locale);
- JSONArray nameArray = new JSONArray(1);
- nameArray.add(tmp);
- return nameArray;
- }
- public static void main(String[] args) throws IOException {
- if (args.length != 2) {
- System.out.println("Please supply arguments: <input_file> <output_file>");
- return;
- }
-
- // open the model
- File file = new File(args[0]);
- String modelId = (String) openFile(file);
-
- // find cubes
- JSONObject model = callFMD("model", "GET", null, modelId);
- JSONArray cubes = (JSONArray) model.get("cubes");
- for(Iterator<?> i = cubes.iterator(); i.hasNext(); ) {
- String cubeId = (String) i.next();
- JSONObject cube = callFMD("cube", "GET", null, cubeId);
- JSONArray names = (JSONArray) cube.get("name");
- // get a source name
- for(Iterator<?> j = names.iterator(); j.hasNext(); ) {
- JSONObject localizedName = (JSONObject) j.next();
- if (LOCALE_FROM.equals(localizedName.get("locale"))) {
- // construct pseudo-translation with double-byte lead-in and lead-out characters
- String sourceName = (String) localizedName.get("text");
- String targetName = PSEUDO_TRANSLATE_LEAD_IN + sourceName + PSEUDO_TRANSLATE_LEAD_OUT;
-
- // update the cube
- JSONObject updateData = new JSONObject();
- JSONArray newNames = localizedText(targetName, LOCALE_TO);
- updateData.put("name", newNames);
- callFMD("cube", "PUT", updateData, cubeId);
- break;
- }
- }
- }
- // save model
- File outputFile = new File(args[1]);
- saveToFile(outputFile, modelId);
- System.out.println("Created updated model at \"" + args[1] + "\".");
- }
- }
|