Eclipseplugins
RunProgramInPa.java
1 package com.proalpha.pds.paconnector;
2 
3 import java.util.Enumeration;
4 import java.util.Hashtable;
5 
6 import org.eclipse.core.runtime.IPath;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 
10 import com.openedge.core.runtime.ProgressCommand;
11 import com.proalpha.pds.exception.ProALPHANotAvailableException;
12 import com.proalpha.pds.paconnector.utils.CharsetConverter;
13 import com.proalpha.pds.paconnector.utils.ExceptionUtils;
14 
15 public class RunProgramInPa {
16 
17  private final Logger logger = LoggerFactory.getLogger(RunProgramInPa.class);
18  private static final String S_SUBPATH = "adm/support/proc/";
19 
20  // S T A T I C S
21 
22  // Run Internal Method of a Persistent Procedure or run a .p
23 
24  private static final int P_INTERNALCALL = 1;
25  private static final int P_EXTERNALCALL = 2;
26  private static final int P_FINDRECORD = 3;
27  private static final int P_EXTERNALCALLPERSISTENT = 4;
28 
29  // If Internal, run a Function or a Procedure
30 
31  private static final int P_INTERNALFUNCTION = 1;
32  private static final int P_INTERNALPROCEDURE = 2;
33  private static final int P_UNDEFINEDMETHODTYPE = 0;
34 
42  protected final PaProject paProject;
43 
45 
46  this.paProject = paProject;
47 
48  IPath bridgePath = this.paProject.getOeProject().getPropath().search("paoidebr.p", true);
49 
50  if (bridgePath == null)
51  throw new ProALPHANotAvailableException("paoidebr.p not Found. Not a pA Project");
52 
53  this.paProject.setBridgePath(bridgePath);
54 
55  try {
56  this.paProject.setCodepage(runSupportProcedure("ds_oea36.p"));
57  this.paProject.setVersion(callFunctionInService("session", "pa_cOIDEProperty", "proALPHAVersion"));
58  this.paProject.setCustomLevel(getpAStartupParam("CustomLevel", ""));
59  this.paProject.setAvailable(true);
60  } catch (IllegalArgumentException e) {
61  this.paProject.setCodepage("");
62  }
63  }
64 
75  public String callProcedureInService(String service, String procedureName, String... parameters) {
76  return run(P_INTERNALCALL, P_INTERNALPROCEDURE, service, procedureName, parameters);
77  }
78 
90  public String callFunctionInService(String service, String functionName, String... parameters) {
91  return run(P_INTERNALCALL, P_INTERNALFUNCTION, service, functionName, parameters);
92  }
93 
102  public String runExternalProcedure(String procedure, String... parameters) {
103  return run(P_EXTERNALCALL, P_UNDEFINEDMETHODTYPE, procedure, "", parameters);
104  }
105 
114  public String runExternalProcedurePersistent(String procedure, String... parameters) {
115  return run(P_EXTERNALCALLPERSISTENT, P_UNDEFINEDMETHODTYPE, procedure, "", parameters);
116  }
117 
128  public String recordField(String tableName, Hashtable<String, String> selection, String fieldName) {
129 
130  String[] parameters = new String[(selection.size() * 2) + 2];
131  parameters[0] = tableName;
132  parameters[1] = fieldName;
133  int k = 2;
134  Enumeration<String> keys = selection.keys();
135  Enumeration<String> values = selection.elements();
136  for (int i = 0; i < selection.size(); i++) {
137  parameters[k++] = keys.nextElement();
138  parameters[k++] = values.nextElement();
139  }
140 
141  return run(P_FINDRECORD, P_UNDEFINEDMETHODTYPE, "", "", parameters);
142  }
143 
154  private synchronized String run(int serviceType, int methodType, String program, String methodName,
155  String... parameters) {
156  return run(serviceType, methodType, program, methodName, false, parameters);
157  }
158 
159  private synchronized String run(int serviceType, int methodType, String program, String methodName, boolean toTop,
160  String... parameters) {
161 
162  String commandString;
163 
164  if (this.paProject.getBridgePath() == null) {
165  Throwable e = new Throwable("Bridge to pA not available. cancel action " + program);
166  ExceptionUtils.showAndLogErrorMessage(Activator.getDefault(), e);
167  return "";
168  }
169  // Setup Command String
170 
171  commandString = "ServiceType=" + serviceType + ",MethodType=" + methodType + ",Program=" + program
172  + ",MethodName=" + methodName + ",NumberOfParameters=" + parameters.length;
173 
174  int i = 1;
175 
176  StringBuilder cmdStr = new StringBuilder(commandString);
177  for (String parameter : parameters) {
178  cmdStr.append(",ParameterValue");
179  cmdStr.append(i++);
180  cmdStr.append("=");
181  cmdStr.append(parameter);
182  }
183  commandString = cmdStr.toString();
184 
185  // Progress Command objects can be used to run a file with
186  // an option single character string parameter
187 
188  if (this.paProject.getCodepage() != null) {
189  commandString = CharsetConverter.encode(this.paProject.getCodepage(), commandString);
190  }
191 
192  ProgressCommand cmd = new ProgressCommand((this.paProject.getBridgePath()).toOSString(), commandString,
193  "EXTERNAL");
194 
195  // send the command
196  this.paProject.getOeProject().getRuntime().runProgressCommand(cmd);
197 
198  // Set focus to runtime and display it on top of all windows
199  if (toTop)
200  this.paProject.getOeProject().getRuntime().bringToTop();
201 
202  // wait for a response...it may take a while depending
203  // on the program there is another for of
204  // waitForResult() that accepts a timeout in ms, if you
205  // don't care to wait longer than a given amount of
206  // time
207 
208  try {
209  cmd.waitforResult();
210  } catch (InterruptedException e) {
211  Thread.currentThread().interrupt();
212  ExceptionUtils.showAndLogErrorMessage(Activator.getDefault(), e);
213  }
214 
215  if (!cmd.isFinished()) {
216  Throwable e = new Throwable("Openedge Command couldn't get finished!");
217  ExceptionUtils.showAndLogErrorMessage(Activator.getDefault(), e);
218  }
219 
220  // make sure it was actually run. If the status isn't
221  // completed, then maybe something failed
222 
223  if (cmd.getCompletionStatus() == ProgressCommand.STATUS_COMPLETED) {
224 
225  // fetch the result and return it
226  String result = cmd.getResult();
227  logger.debug("CommandString: {}", commandString);
228  logger.debug("Result: {}", result);
229 
230  return result;
231  }
232 
233  return "";
234  }
235 
243  public String runSupportProcedure(String supportProcedure, String... parameters) {
244 
245  String filePath = null;
246  IPath iPath = this.paProject.getOeProject().getPropath().search(S_SUBPATH + supportProcedure, true);
247 
248  if (iPath != null && !iPath.isEmpty())
249  filePath = iPath.toOSString();
250 
251  if (filePath == null) {
252  throw new IllegalArgumentException(
253  String.format("In this version, this feature is not supported. (%s)", supportProcedure));
254  } else {
255  String retVal = runExternalProcedure(filePath, parameters);
256 
257  if (retVal.startsWith("PROGRESS runtime error ")) {
258  StringBuilder errorMsg = new StringBuilder();
259  errorMsg.append(
260  "Support procedure " + supportProcedure + " returned\n" + retVal + "\n\nParameters given:\n");
261  for (int i = 0; i < parameters.length; i++)
262  errorMsg.append(parameters[i] + "\n");
263 
264  throw new IllegalArgumentException(errorMsg.toString());
265  }
266 
267  return retVal;
268  }
269 
270  }
271 
272  public String getpAStartupParam(String startupParam, String defaultValue) {
273 
274  // first check the parameter is available
275  // the result of ds_oea is a parameter List
276  String[] results = runSupportProcedure("ds_oea06.p", "", "%" + startupParam).split(",");
277 
278  // the information is in parameter 2.
279  // result can be be the value of the Parameter or the Text unknown startup
280  // parameter "PARMANAME"
281  for (String result : results) {
282 
283  if (result.split("=")[0].equals("2")) {
284 
285  if ((result.split("=").length == 1)
286  // do not check the parameter name is included in response.
287  // do not work with parameter temp witch ends with temp
288  || !result.split("=")[1].contains("Unknown"))
289  return runSupportProcedure("ds_oea15.p", startupParam);
290  else
291  return defaultValue;
292  }
293  }
294  // this code never within reach
295  return defaultValue;
296  }
297 
298 }
String runExternalProcedurePersistent(String procedure, String... parameters)
String runSupportProcedure(String supportProcedure, String... parameters)
String callFunctionInService(String service, String functionName, String... parameters)
String recordField(String tableName, Hashtable< String, String > selection, String fieldName)
String callProcedureInService(String service, String procedureName, String... parameters)
String runExternalProcedure(String procedure, String... parameters)