1 14 package org.compiere.process; 15 16 import java.sql.*; 17 import java.util.*; 18 import java.math.*; 19 import java.lang.reflect.*; 20 21 import org.compiere.util.*; 22 23 29 public abstract class SvrProcess implements ProcessCall 30 { 31 34 public SvrProcess() 35 { 36 } 38 private Properties m_ctx; 39 private ProcessInfo m_pi; 40 41 42 protected Logger log = Logger.getCLogger (getClass()); 43 44 45 protected static String MSG_SaveErrorRowNotFound = "@SaveErrorRowNotFound@"; 46 protected static String MSG_InvalidArguments = "@InvalidArguments@"; 47 48 49 59 public final boolean startProcess (Properties ctx, ProcessInfo pi) 60 { 61 m_ctx = ctx == null ? Env.getCtx() : ctx; 63 m_pi = pi; 64 lock(); 65 process(); 67 unlock(); 68 return !m_pi.isError(); 69 } 71 72 73 76 protected void process() 77 { 78 prepare(); 79 String msg = null; 80 boolean error = false; 81 try 82 { 83 msg = doIt(); 84 } 85 catch (Exception e) 86 { 87 msg = e.getMessage(); 88 if (msg == null) 89 msg = e.toString(); 90 if (e.getCause() != null) 91 log.error("process - " + msg, e.getCause()); 92 else 93 log.error("process - " + msg); 94 error = true; 95 } 96 msg = Msg.parseTranslation(m_ctx, msg); 97 m_pi.setSummary(msg, error); 98 ProcessInfoUtil.saveLogToDB(m_pi); 99 } 101 104 abstract protected void prepare(); 105 106 111 abstract protected String doIt() throws Exception ; 112 113 114 115 119 public ProcessInfo getProcessInfo() 120 { 121 return m_pi; 122 } 124 128 public Properties getCtx() 129 { 130 return m_ctx; 131 } 133 137 protected String getName() 138 { 139 return m_pi.getTitle(); 140 } 142 146 protected int getAD_PInstance_ID() 147 { 148 return m_pi.getAD_PInstance_ID(); 149 } 151 155 protected int getRecord_ID() 156 { 157 return m_pi.getRecord_ID(); 158 } 160 164 protected int getAD_User_ID() 165 { 166 if (m_pi.getAD_User_ID() == null) 167 { 168 String sql = "SELECT AD_User_ID, AD_Client_ID FROM AD_PInstance WHERE AD_PInstance_ID=?"; 169 try 170 { 171 PreparedStatement pstmt = DB.prepareStatement(sql); 172 pstmt.setInt(1, m_pi.getAD_PInstance_ID()); 173 ResultSet rs = pstmt.executeQuery(); 174 if (rs.next()) 175 { 176 m_pi.setAD_User_ID (rs.getInt (1)); 177 m_pi.setAD_Client_ID (rs.getInt(2)); 178 } 179 rs.close(); 180 pstmt.close(); 181 } 182 catch (SQLException e) 183 { 184 log.error("getAD_User_ID", e); 185 } 186 } 187 if (m_pi.getAD_User_ID() == null) 188 return 0; 189 return m_pi.getAD_User_ID().intValue(); 190 } 192 196 protected int getAD_Client_ID() 197 { 198 if (m_pi.getAD_Client_ID() == null) 199 { 200 getAD_User_ID(); if (m_pi.getAD_Client_ID() == null) 202 return 0; 203 } 204 return m_pi.getAD_Client_ID().intValue(); 205 } 207 208 209 213 protected ProcessInfoParameter[] getParameter() 214 { 215 ProcessInfoParameter[] retValue = m_pi.getParameter(); 216 if (retValue == null) 217 { 218 ProcessInfoUtil.setParameterFromDB(m_pi); 219 retValue = m_pi.getParameter(); 220 } 221 return retValue; 222 } 224 225 226 233 public void addLog (int id, Timestamp date, BigDecimal number, String msg) 234 { 235 m_pi.addLog(id, date, number, msg); 236 } 238 239 240 247 public Object doIt (String className, String methodName, Object args[]) 248 { 249 try 250 { 251 Class clazz = Class.forName(className); 252 Object object = clazz.newInstance(); 253 Method[] methods = clazz.getMethods(); 254 for (int i = 0; i < methods.length; i++) 255 { 256 if (methods[i].getName().equals(methodName)) 257 return methods[i].invoke(object, args); 258 } 259 } 260 catch (Exception ex) 261 { 262 log.error("doIt", ex); 263 } 264 return null; 265 } 267 268 269 272 private void lock() 273 { 274 log.debug("lock"); 275 DB.executeUpdate("UPDATE AD_PInstance SET IsProcessing='Y' WHERE AD_PInstance_ID=" + m_pi.getAD_PInstance_ID()); 276 } 278 282 private void unlock () 283 { 284 StringBuffer sql = new StringBuffer ("UPDATE AD_PInstance SET IsProcessing='N',Updated=SysDate,Result="); 285 sql.append(m_pi.isError() ? "0" : "1"); 286 String msg = m_pi.getSummary(); 287 if (msg != null && msg.length() > 0) 288 sql.append(", ErrorMsg=").append(DB.TO_STRING(msg, 2000)); 289 sql.append(" WHERE AD_PInstance_ID=").append(m_pi.getAD_PInstance_ID()); 290 DB.executeUpdate(sql.toString()); 291 log.debug("unlock - Error=" + m_pi.isError() + " - " + msg); 292 } 294 } | Popular Tags |