1 14 package org.compiere.model; 15 16 import java.util.*; 17 import java.sql.*; 18 import java.math.*; 19 import java.io.*; 20 import java.net.*; 21 22 import org.compiere.util.*; 23 24 30 public class MAsset extends X_A_Asset 31 { 32 37 public MAsset (Properties ctx, int A_Asset_ID) 38 { 39 super (ctx, A_Asset_ID); 40 if (A_Asset_ID == 0) 41 { 42 setIsDepreciated (false); 43 setIsFullyDepreciated (false); 44 setValue (null); 45 setName (null); 46 setIsInPosession (false); 47 setIsOwned (false); 48 setA_Asset_Group_ID (0); 49 setIsDisposed (false); 50 } 51 } 53 58 public MAsset (Properties ctx, ResultSet rs) 59 { 60 super (ctx, rs); 61 } 63 64 private String m_ProductDownloadURL = null; 65 private String m_ProductVersionNo = null; 66 private int m_ProductR_MailText_ID = -1; 67 68 private static Logger s_log = Logger.getCLogger (MAsset.class); 69 70 71 75 public String toString () 76 { 77 StringBuffer sb = new StringBuffer ("MAsset[").append (getID ()).append ("]"); 78 return sb.toString (); 79 } 81 82 83 87 public MAsset_Delivery[] getDeliveries() 88 { 89 ArrayList list = new ArrayList(); 90 91 String sql = "SELECT * FROM A_Asset_Delivery WHERE A_Asset_ID=?"; 92 PreparedStatement pstmt = null; 93 try 94 { 95 pstmt = DB.prepareStatement(sql); 96 pstmt.setInt(1, getA_Asset_ID()); 97 ResultSet rs = pstmt.executeQuery(); 98 while (rs.next()) 99 list.add(new MAsset_Delivery(getCtx(), rs)); 100 rs.close(); 101 pstmt.close(); 102 pstmt = null; 103 } 104 catch (Exception e) 105 { 106 log.error("getDeliveries", e); 107 } 108 finally 109 { 110 try 111 { 112 if (pstmt != null) 113 pstmt.close (); 114 } 115 catch (Exception e) 116 {} 117 pstmt = null; 118 } 119 MAsset_Delivery[] retValue = new MAsset_Delivery[list.size()]; 121 list.toArray(retValue); 122 return retValue; 123 } 125 129 public int getDeliveryCount() 130 { 131 String sql = "SELECT COUNT(*) FROM A_Asset_Delivery WHERE A_Asset_ID=?"; 132 return DB.getSQLValue(sql, getA_Asset_ID()); 133 } 135 136 137 142 public boolean isDownloadable() 143 { 144 if (!isActive()) 145 return false; 146 Timestamp guarantee = getGuaranteeDate(); 148 if (guarantee == null) 149 return false; 150 guarantee = TimeUtil.getDay(guarantee); 151 Timestamp now = TimeUtil.getDay(System.currentTimeMillis()); 152 if (!now.after(guarantee)) { 155 String where = getProductDownloadURL(); return (where != null && where.length() > 0); 157 } 158 return false; 160 } 162 168 public static File getDownloadFile (String downloadURL, String directory) 169 { 170 File file = new File (downloadURL); if (file.exists()) 172 return file; 173 if (directory == null || directory.length() == 0) 174 { 175 s_log.error("getDownloadFile - Not found " + downloadURL); 176 return null; 177 } 178 String downloadURL2 = directory; 179 if (!downloadURL2.endsWith(File.separator)) 180 downloadURL2 += File.separator; 181 downloadURL2 += downloadURL; 182 file = new File (downloadURL2); 183 if (file.exists()) 184 return file; 185 186 s_log.error("getDownloadFile - Not found " + downloadURL + " + " + downloadURL2); 187 return null; 188 } 190 195 public InputStream getDownloadStream (String directory) 196 { 197 if (!isDownloadable()) 198 return null; 199 200 InputStream in = null; 201 String where = getProductDownloadURL(); 202 try 203 { 204 if (where.indexOf ("://") != -1) 205 { 206 URL url = new URL (where); 207 in = url.openStream(); 208 } 209 else { 211 File file = getDownloadFile(where, directory); 212 if (file == null) 213 return null; 214 in = new FileInputStream (file); 215 } 216 } 217 catch (Exception ex) 218 { 219 log.error("getDownloadStream - " + where, ex); 220 return null; 221 } 222 return in; 223 } 225 230 public URL getDownloadURL (String directory) 231 { 232 if (!isDownloadable()) 233 return null; 234 235 URL url = null; 236 String where = getProductDownloadURL(); 237 try 238 { 239 if (where.indexOf ("://") != -1) 240 url = new URL (where); 241 else 242 { 243 File f = getDownloadFile (where, directory); 244 if (f != null) 245 url = f.toURL (); 246 } 247 } 248 catch (Exception ex) 249 { 250 log.error("getDownloadURL - " + where, ex); 251 return null; 252 } 253 return url; 254 } 256 260 public String getDownloadName() 261 { 262 if (!isDownloadable()) 263 return null; 264 String name = getProductDownloadURL(); 265 int pos = Math.max(name.lastIndexOf('/'), name.lastIndexOf('\\')); 266 if (pos != -1) 267 return name.substring(pos+1); 268 return name; 269 } 271 272 273 277 public String getProductDownloadURL() 278 { 279 if (m_ProductDownloadURL == null) 280 getProductInfo(); 281 return m_ProductDownloadURL; 283 } 285 289 public String getProductVersionNo() 290 { 291 if (m_ProductVersionNo == null) 292 getProductInfo(); 293 return m_ProductVersionNo; 295 } 297 301 public int getProductR_MailText_ID() 302 { 303 if (m_ProductR_MailText_ID < 0) 304 getProductInfo(); 305 return m_ProductR_MailText_ID; 307 } 309 312 private void getProductInfo() 313 { 314 String sql = "SELECT DownloadURL, VersionNo, R_MailText_ID " 315 + "FROM M_Product " 316 + "WHERE M_Product_ID=?"; 317 PreparedStatement pstmt = null; 318 try 319 { 320 pstmt = DB.prepareStatement (sql); 321 pstmt.setInt (1, getM_Product_ID ()); 322 ResultSet rs = pstmt.executeQuery (); 323 if (rs.next ()) 324 { 325 m_ProductDownloadURL = rs.getString (1); 326 m_ProductVersionNo = rs.getString(2); 327 m_ProductR_MailText_ID = rs.getInt(3); 328 } 329 else 330 { 331 log.error ("getProductInfo - Not Found M_Product_ID=" + getM_Product_ID()); 332 m_ProductDownloadURL = ""; 333 m_ProductVersionNo = ""; 334 m_ProductR_MailText_ID = 0; 335 } 336 rs.close (); 337 pstmt.close (); 338 pstmt = null; 339 } 340 catch (Exception e) 341 { 342 log.error ("getProductInfo", e); 343 } 344 finally 345 { 346 try 347 { 348 if (pstmt != null) 349 pstmt.close (); 350 } 351 catch (Exception e) 352 {} 353 pstmt = null; 354 } 355 } 357 358 359 364 public MAsset_Delivery confirmDelivery (EMail email, int AD_User_ID) 365 { 366 this.setVersionNo(getProductVersionNo()); 367 MAsset_Delivery ad = new MAsset_Delivery (this, email, AD_User_ID); 368 return ad; 369 } 371 } | Popular Tags |