PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/compiere-330/base/src/org/compiere/process/EntityTypeRegister.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 271 lines | 186 code | 26 blank | 59 comment | 45 complexity | 7b95e148716fe1b42a408a7164a37ab5 MD5 | raw file
  1. /******************************************************************************
  2. * Product: Compiere ERP & CRM Smart Business Solution *
  3. * Copyright (C) 1999-2007 ComPiere, Inc. All Rights Reserved. *
  4. * This program is free software, you can redistribute it and/or modify it *
  5. * under the terms version 2 of the GNU General Public License as published *
  6. * by the Free Software Foundation. This program is distributed in the hope *
  7. * that it will be useful, but WITHOUT ANY WARRANTY, without even the implied *
  8. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
  9. * See the GNU General Public License for more details. *
  10. * You should have received a copy of the GNU General Public License along *
  11. * with this program, if not, write to the Free Software Foundation, Inc., *
  12. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
  13. * For the text or an alternative of this public license, you may reach us *
  14. * ComPiere, Inc., 3600 Bridge Parkway #102, Redwood City, CA 94065, USA *
  15. * or via info@compiere.org or http://www.compiere.org/license.html *
  16. *****************************************************************************/
  17. package org.compiere.process;
  18. import java.io.*;
  19. import java.math.*;
  20. import java.net.*;
  21. import java.util.logging.*;
  22. import org.apache.commons.httpclient.*;
  23. import org.apache.commons.httpclient.methods.*;
  24. import org.apache.commons.httpclient.methods.multipart.*;
  25. import org.apache.commons.httpclient.params.*;
  26. import org.compiere.*;
  27. import org.compiere.model.*;
  28. import org.compiere.util.*;
  29. /**
  30. * Register Entity Type
  31. *
  32. * @author Jorg Janke
  33. * @version $Id: EntityTypeRegister.java,v 1.2 2006/07/30 00:51:02 jjanke Exp $
  34. */
  35. public class EntityTypeRegister extends SvrProcess
  36. {
  37. /** Register Entity Type */
  38. protected int p_AD_EntityType_ID = 0;
  39. /** File Name to upload */
  40. private String p_FileName = null;
  41. /** Suggested Price */
  42. private BigDecimal p_SuggestedPrice = null;
  43. /** Stream Reader */
  44. private InputStreamReader m_in = null;
  45. /** Encoding */
  46. static private final String ENC = "UTF-8";
  47. /** URL */
  48. static private final String URLSTRING = "http://www.compiere.com/migrateApps/RegisterComponent";
  49. // static private final String URLSTRING = "http://ws-jj/migrateApps/RegisterComponent";
  50. /**
  51. * Prepare
  52. */
  53. @Override
  54. protected void prepare ()
  55. {
  56. ProcessInfoParameter[] para = getParameter();
  57. for (ProcessInfoParameter element : para) {
  58. String name = element.getParameterName();
  59. if (element.getParameter() == null)
  60. ;
  61. else if (name.equals("FileName"))
  62. p_FileName = (String)element.getParameter();
  63. else if (name.equals("SuggestedPrice"))
  64. p_SuggestedPrice = (BigDecimal)element.getParameter();
  65. }
  66. p_AD_EntityType_ID = getRecord_ID();
  67. } // prepare
  68. /**
  69. * Process
  70. * @return summary
  71. * @throws Exception
  72. */
  73. @Override
  74. protected String doIt() throws Exception
  75. {
  76. log.info("AD_EntityType_ID=" + p_AD_EntityType_ID
  77. + ", FileName=" + p_FileName);
  78. MEntityType et = MEntityType.getEntityType(getCtx(), p_AD_EntityType_ID);
  79. if (et.isSystemMaintained())
  80. throw new CompiereUserException("You cannot register a System maintained entity");
  81. String EntityType = et.getEntityType();
  82. if (EntityType.toUpperCase().startsWith("C") || EntityType.toUpperCase().startsWith("X"))
  83. throw new CompiereUserException("Entity Types starting with C or X are reserved.");
  84. boolean ok = sendRegistration(et);
  85. if (!ok)
  86. throw new CompiereSystemException("Could not contact Compiere - Try later");
  87. String response = getResponse();
  88. if (response == null)
  89. throw new CompiereSystemException("No Response - Try later");
  90. String info = et.processRegistrationResponse(response);
  91. et.save();
  92. if (Util.isEmpty(p_FileName) || et.getRecord_ID() == 0)
  93. return info;
  94. info += " - " + uploadFile(et);
  95. return info;
  96. } // doIt
  97. /**
  98. * Upload the deployment as attachment
  99. * @param et
  100. * @return
  101. */
  102. private String uploadFile(MEntityType et)
  103. {
  104. File file = new File(p_FileName);
  105. if (!file.exists())
  106. return "File not exist: " + p_FileName;
  107. if (file.length() <= 0)
  108. return "Empty file: " + p_FileName;
  109. PostMethod filePost = new PostMethod(URLSTRING);
  110. filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
  111. try
  112. {
  113. MSystem system = MSystem.get(getCtx());
  114. Part[] parts = {
  115. new StringPart("USER", system.getUserName(), ENC),
  116. new StringPart("EntityType", et.getEntityType(), ENC),
  117. new StringPart("RecordID", String.valueOf(et.getRecord_ID()), ENC),
  118. new FilePart(file.getName(), file)
  119. };
  120. filePost.setRequestEntity(
  121. new MultipartRequestEntity(parts, filePost.getParams())
  122. );
  123. HttpClient client = new HttpClient();
  124. client.getHttpConnectionManager().
  125. getParams().setConnectionTimeout(5000);
  126. int status = client.executeMethod(filePost);
  127. if (status == HttpStatus.SC_OK)
  128. {
  129. return "Upload complete, response=" + filePost.getResponseBodyAsString();
  130. }
  131. else
  132. {
  133. return "Upload failed, response=" + HttpStatus.getStatusText(status);
  134. }
  135. }
  136. catch (Exception ex)
  137. {
  138. log.log(Level.SEVERE, "ERROR: " + ex.getClass().getName(), ex);
  139. }
  140. finally
  141. {
  142. filePost.releaseConnection();
  143. }
  144. return "Upload complete";
  145. } // uploadFile()
  146. /**
  147. * Send Registration
  148. * @param et entity type
  149. * @return true if sent
  150. */
  151. private boolean sendRegistration(MEntityType et)
  152. {
  153. URL url = null;
  154. // Assemble request
  155. try
  156. {
  157. String tRelease = Compiere.MAIN_VERSION.substring(8);
  158. String tVersion = Compiere.DATE_VERSION;
  159. String from = InetAddress.getLocalHost().toString();
  160. StringBuffer urlString = new StringBuffer (URLSTRING);
  161. // Sender
  162. MSystem system = MSystem.get(getCtx());
  163. urlString.append("?NAME=").append(URLEncoder.encode(system.getName(), ENC))
  164. .append("&USER=").append(URLEncoder.encode(system.getUserName(), ENC))
  165. .append("&PASSWORD=").append(URLEncoder.encode(system.getPassword(), ENC))
  166. .append("&FROM=").append(URLEncoder.encode(from, ENC));
  167. //
  168. urlString.append("&TRELEASENO=").append(URLEncoder.encode(tRelease, ENC))
  169. .append("&TVERSION=").append(URLEncoder.encode(tVersion, ENC));
  170. //
  171. urlString.append("&EntityType=").append(URLEncoder.encode(et.getEntityType(), ENC))
  172. .append("&ETName=").append(URLEncoder.encode(et.getName(), ENC));
  173. if (et.getRecord_ID() != 0)
  174. urlString.append("&RecordID=").append(et.getRecord_ID());
  175. //
  176. if (et.getDescription() != null)
  177. urlString.append("&Description=").append(URLEncoder.encode(et.getDescription(), ENC));
  178. if (et.getHelp() != null)
  179. urlString.append("&Help=").append(URLEncoder.encode(et.getHelp(), ENC));
  180. if (et.getVersion() != null)
  181. urlString.append("&Version=").append(URLEncoder.encode(et.getVersion(), ENC));
  182. if (et.getRequireCompiereVersion() != null)
  183. urlString.append("&RequireCompiereVersion=").append(URLEncoder.encode(et.getRequireCompiereVersion(), ENC));
  184. if (et.getRequireComponentVersion() != null)
  185. urlString.append("&RequireComponentVersion=").append(URLEncoder.encode(et.getRequireComponentVersion(), ENC));
  186. if (et.getDocumentationText() != null)
  187. urlString.append("&DocumentationText=").append(URLEncoder.encode(et.getDocumentationText(), ENC));
  188. if (p_SuggestedPrice != null)
  189. urlString.append("&SuggestedPrice=").append(p_SuggestedPrice);
  190. //
  191. url = new URL (urlString.toString());
  192. log.config(url.toString());
  193. }
  194. catch (Exception e)
  195. {
  196. if (log != null)
  197. log.log(Level.SEVERE, "--", e);
  198. return false;
  199. }
  200. // Send it
  201. try
  202. {
  203. URLConnection uc = url.openConnection();
  204. m_in = new InputStreamReader(uc.getInputStream());
  205. }
  206. catch (FileNotFoundException ex)
  207. {
  208. log.log(Level.WARNING, " Could not connect - Try later");
  209. return false;
  210. }
  211. catch (Exception e)
  212. {
  213. log.log(Level.SEVERE, "->", e);
  214. return false;
  215. }
  216. return true;
  217. } // sendRegistration
  218. /**
  219. * Get Response
  220. * @return response
  221. */
  222. private String getResponse()
  223. {
  224. if (m_in == null)
  225. return null;
  226. //
  227. StringBuffer sb = new StringBuffer();
  228. try // Get Summary
  229. {
  230. int c;
  231. while ((c = m_in.read()) != -1)
  232. sb.append((char)c);
  233. m_in.close();
  234. if (log != null)
  235. log.fine("(" + sb.length() + ") " + sb);
  236. }
  237. catch (Exception ex)
  238. {
  239. log.log(Level.WARNING, "<-", ex);
  240. return null;
  241. }
  242. log.config(sb.toString());
  243. return sb.toString();
  244. } // getResponse
  245. } // EntityTypeRegister