KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MAsset


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

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 /**
25  * Asset Model
26  *
27  * @author Jorg Janke
28  * @version $Id: MAsset.java,v 1.10 2003/10/07 05:24:09 jjanke Exp $
29  */

30 public class MAsset extends X_A_Asset
31 {
32     /**
33      * Asset Constructor
34      * @param ctx context
35      * @param A_Asset_ID asset
36      */

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     } // MAsset
52

53     /**
54      * Load Constructor
55      * @param ctx context
56      * @param rs result set record
57      */

58     public MAsset (Properties ctx, ResultSet rs)
59     {
60         super (ctx, rs);
61     } // MAsset
62

63     /** Product Download URL */
64     private String JavaDoc m_ProductDownloadURL = null;
65     private String JavaDoc m_ProductVersionNo = null;
66     private int m_ProductR_MailText_ID = -1;
67     /** Logger */
68     private static Logger s_log = Logger.getCLogger (MAsset.class);
69
70
71     /**
72      * String representation
73      * @return info
74      */

75     public String JavaDoc toString ()
76     {
77         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("MAsset[").append (getID ()).append ("]");
78         return sb.toString ();
79     } // toString
80

81     /*************************************************************************/
82
83     /**
84      * Get Deliveries
85      * @return deliveries
86      */

87     public MAsset_Delivery[] getDeliveries()
88     {
89         ArrayList list = new ArrayList();
90
91         String JavaDoc 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 JavaDoc 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 JavaDoc e)
116             {}
117             pstmt = null;
118         }
119         //
120
MAsset_Delivery[] retValue = new MAsset_Delivery[list.size()];
121         list.toArray(retValue);
122         return retValue;
123     } // getDeliveries
124

125     /**
126      * Get Delivery count
127      * @return delivery count
128      */

129     public int getDeliveryCount()
130     {
131         String JavaDoc sql = "SELECT COUNT(*) FROM A_Asset_Delivery WHERE A_Asset_ID=?";
132         return DB.getSQLValue(sql, getA_Asset_ID());
133     } // getDeliveries
134

135     /*************************************************************************/
136
137     /**
138      * Can we download.
139      * Based on guarantee date and availability of download
140      * @return true if downloadable
141      */

142     public boolean isDownloadable()
143     {
144         if (!isActive())
145             return false;
146         //
147
Timestamp guarantee = getGuaranteeDate();
148         if (guarantee == null)
149             return false;
150         guarantee = TimeUtil.getDay(guarantee);
151         Timestamp now = TimeUtil.getDay(System.currentTimeMillis());
152         // valid
153
if (!now.after(guarantee)) // not after guarantee date
154
{
155             String JavaDoc where = getProductDownloadURL(); // do we have a link
156
return (where != null && where.length() > 0);
157         }
158         //
159
return false;
160     } // isDownloadable
161

162     /**
163      * Find download url
164      * @param downloadURL file url
165      * @param directory optional directory
166      * @return file or null
167      */

168     public static File getDownloadFile (String JavaDoc downloadURL, String JavaDoc directory)
169     {
170         File file = new File (downloadURL); // absolute file
171
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 JavaDoc 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     } // getDownloadFile
189

190     /**
191      * Get Download Stream
192      * @param directory optional directory
193      * @return input stream
194      */

195     public InputStream getDownloadStream (String JavaDoc directory)
196     {
197         if (!isDownloadable())
198             return null;
199
200         InputStream in = null;
201         String JavaDoc where = getProductDownloadURL();
202         try
203         {
204             if (where.indexOf ("://") != -1)
205             {
206                 URL url = new URL (where);
207                 in = url.openStream();
208             }
209             else // file
210
{
211                 File file = getDownloadFile(where, directory);
212                 if (file == null)
213                     return null;
214                 in = new FileInputStream (file);
215             }
216         }
217         catch (Exception JavaDoc ex)
218         {
219             log.error("getDownloadStream - " + where, ex);
220             return null;
221         }
222         return in;
223     } // getDownloadStream
224

225     /**
226      * Get Download URL
227      * @param directory optional directory
228      * @return url
229      */

230     public URL getDownloadURL (String JavaDoc directory)
231     {
232         if (!isDownloadable())
233             return null;
234
235         URL url = null;
236         String JavaDoc 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 JavaDoc ex)
249         {
250             log.error("getDownloadURL - " + where, ex);
251             return null;
252         }
253         return url;
254     } // getDownloadURL
255

256     /**
257      * Get Download URL
258      * @return download URL
259      */

260     public String JavaDoc getDownloadName()
261     {
262         if (!isDownloadable())
263             return null;
264         String JavaDoc 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     } // getDownloadName
270

271     /*************************************************************************/
272
273     /**
274      * Get Product Download URL
275      * @return download URL string
276      */

277     public String JavaDoc getProductDownloadURL()
278     {
279         if (m_ProductDownloadURL == null)
280             getProductInfo();
281         //
282
return m_ProductDownloadURL;
283     } // getProductDownloadURL
284

285     /**
286      * Get Product Version No
287      * @return VersionNo
288      */

289     public String JavaDoc getProductVersionNo()
290     {
291         if (m_ProductVersionNo == null)
292             getProductInfo();
293         //
294
return m_ProductVersionNo;
295     } // getProductVersionNo
296

297     /**
298      * Get Product R_MailText_ID
299      * @return R_MailText_ID
300      */

301     public int getProductR_MailText_ID()
302     {
303         if (m_ProductR_MailText_ID < 0)
304             getProductInfo();
305         //
306
return m_ProductR_MailText_ID;
307     } // getProductR_MailText_ID
308

309     /**
310      * Get Product Info
311      */

312     private void getProductInfo()
313     {
314         String JavaDoc 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 JavaDoc 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 JavaDoc e)
352             {}
353             pstmt = null;
354         }
355     } // getProductInfo
356

357     /************************************************************************/
358
359     /**
360      * Confirm Asset Delivery
361      * @param email email sent
362      * @param AD_User_ID recipient
363      */

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     } // confirmDelivery
370

371 } // MAsset
372
Popular Tags