KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > ProductLocation


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.hipergate;
34
35 import java.lang.System JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.File JavaDoc;
38
39 import java.sql.PreparedStatement JavaDoc;
40 import java.sql.ResultSet JavaDoc;
41 import java.sql.SQLException JavaDoc;
42
43 import java.net.URL JavaDoc;
44 import java.net.MalformedURLException JavaDoc;
45
46 import com.knowgate.debug.DebugFile;
47 import com.knowgate.jdc.JDCConnection;
48 import com.knowgate.dataobjs.DB;
49 import com.knowgate.dataobjs.DBPersist;
50 import com.knowgate.dfs.FileSystem;
51 import com.knowgate.misc.Gadgets;
52
53 /**
54  * <p>Product Location</p>
55  * Location semantics depend upon what Product is used for.<br>
56  * <ul>
57  * <li>For Downloadable Products, ProductLocations represent mirror download URLs.
58  * <li>For Versioned Products, ProductLocations represent different versions of the same File.
59  * <li>For Compound Products, ProductLocations represent parts of the Product each one being a File.
60  * <li>For Physical Products, ProductLocations represent stock of Product at different warehouses.
61  * </ul>
62  * @author Sergio Montoro Ten
63  * @version 2.2
64  */

65 public class ProductLocation extends DBPersist {
66
67   public ProductLocation() {
68     super(DB.k_prod_locats, "ProductLocation");
69   }
70
71   /**
72    * Load ProductLocation from database
73    * @param oConn Database Conenction
74    * @param iIdLocation GUID of ProductLocation to be loaded
75    * @throws SQLException
76    */

77   public ProductLocation(JDCConnection oConn, String JavaDoc iIdLocation) throws SQLException JavaDoc {
78     super(DB.k_prod_locats, "ProductLocation");
79
80     Object JavaDoc aProd[] = { iIdLocation };
81
82     load(oConn, aProd);
83   }
84
85   // ----------------------------------------------------------
86

87   /**
88    * <p>Get Path to URL or file</p>
89    * @return xprotocol [xhost] [:xport] / xpath<br>
90    * Example 1: file:///opt/knowgate/storage/userdata/<br>
91    * Example 2: http://www.hipergate.org/es/<br>
92    * Example 3: http://localhost:8080/
93    */

94   public String JavaDoc getPath() {
95     String JavaDoc sPath;
96     String JavaDoc sXProtocol = getString(DB.xprotocol);
97     String JavaDoc sXPath = getStringNull(DB.xpath, "");
98     String JavaDoc sFileSep = sXProtocol.startsWith("file") ? System.getProperty("file.separator") : "/";
99
100     if (!sXProtocol.endsWith("://")) sXProtocol += "://";
101
102     sPath = sXProtocol;
103
104     if (!sXProtocol.startsWith("file")) sPath += getString("xhost");
105
106     if (!isNull(DB.xport)) sPath += ":" + getShort(DB.xport);
107
108     if (!sPath.endsWith(sFileSep) && !sXPath.startsWith(sFileSep)) sPath += sFileSep;
109
110     sPath += sXPath;
111
112     return sPath;
113   } // getPath()
114

115   // ----------------------------------------------------------
116

117   /**
118    * Get URL for ProductLocation
119    * @return getPath() [xfile] [#xanchor]
120    */

121
122   public String JavaDoc getURL() {
123     String JavaDoc sURL;
124     String JavaDoc sXProtocol = getString(DB.xprotocol);
125     String JavaDoc sFileSep = sXProtocol.startsWith("file") ? System.getProperty("file.separator") : "/";
126
127     sURL = getPath();
128
129     if (null!=sURL) {
130       sURL = sURL.trim();
131
132       if (!sURL.endsWith(sFileSep) && !isNull(DB.xfile)) sURL += sFileSep;
133
134       if (!isNull(DB.xfile)) sURL += getString(DB.xfile);
135       if (!isNull(DB.xanchor)) sURL += "#" + getString(DB.xanchor);
136     }
137
138   return sURL;
139   } // getURL()
140

141   // ----------------------------------------------------------
142

143   /**
144    * <p>Get numeric identifier for container type.</p>
145    * Usually, container type is computed from protocol.<br>
146    * <table border=1 cellpadding=4>
147    * <tr><td><b>Protocol</b></td><td><b>Container Type</b></td></tr>
148    * <tr><td align=middle>file://</td><td align=middle>CONTAINER_FILE</td></tr>
149    * <tr><td align=middle>http://</td><td align=middle>CONTAINER_HTTP</td></tr>
150    * <tr><td align=middle>https://</td><td align=middle>CONTAINER_HTTPS</td></tr>
151    * <tr><td align=middle>ftp://</td><td align=middle>CONTAINER_FTP</td></tr>
152    * <tr><td align=middle>odbc://</td><td align=middle>CONTAINER_ODBC</td></tr>
153    * <tr><td align=middle>lotus://</td><td align=middle>CONTAINER_LOTUS</td></tr>
154    * <tr><td align=middle>jdbc://</td><td align=middle>CONTAINER_JDBC</td></tr>
155    * <tr><td align=middle>ware://</td><td align=middle>CONTAINER_WARE</td></tr>
156    * </table>
157    * @return Container Type. One of { CONTAINER_FILE, CONTAINER_HTTP, CONTAINER_HTTPS, CONTAINER_FTP, CONTAINER_ODBC, CONTAINER_JDBC, CONTAINER_LOTUS, CONTAINER_WARE }
158    */

159   public int getContainerType() {
160
161     if (DebugFile.trace) {
162       DebugFile.writeln("Begin ProductLocation.getContainerType()");
163       DebugFile.incIdent();
164       DebugFile.writeln("protocol="+getStringNull(DB.xprotocol,"null").toLowerCase());
165     }
166
167     String JavaDoc sProtocol = getString(DB.xprotocol).toLowerCase();
168     int iProdType = 0;
169
170     if (sProtocol.startsWith("file:"))
171       iProdType = 1;
172     else if (sProtocol.startsWith("http:"))
173       iProdType = 2;
174     else if (sProtocol.startsWith("https:"))
175       iProdType = 3;
176     else if (sProtocol.startsWith("ftp:"))
177       iProdType = 4;
178     else if (sProtocol.startsWith("odbc:"))
179       iProdType = 5;
180     else if (sProtocol.startsWith("lotus:"))
181       iProdType = 6;
182     else if (sProtocol.startsWith("jdbc:"))
183       iProdType = 7;
184     else if (sProtocol.startsWith("ware:"))
185       iProdType = 100;
186
187     if (DebugFile.trace) {
188       DebugFile.decIdent();
189       DebugFile.writeln("End ProductLocation.getContainerType() : " + String.valueOf(iProdType));
190     }
191
192     return iProdType;
193   } // getContainerType()
194

195   // ----------------------------------------------------------
196

197   /**
198    * <p>Remove querystring from a file name</p>
199    * Used for separating a file extension from the query string in a URL.<br>
200    * The tactic used is finding the first character of a the set {'?', '#', '/', '&' }
201    * and cut file extension at that point.
202    * @param sType File Name and query string
203    * @return File Name without query string
204    */

205   private String JavaDoc removeQueryString(String JavaDoc sType) {
206     int iLastQuery, iNextQuery;
207
208     // If 4 characters or less, then return as is.
209
if (sType.length()<=4) return sType;
210
211     iLastQuery = iNextQuery = sType.indexOf("?");
212
213     iNextQuery = sType.indexOf("#");
214     if (iNextQuery>0 && (iNextQuery<iLastQuery || iLastQuery==-1))
215       iLastQuery = iNextQuery;
216
217     iNextQuery = sType.indexOf(",");
218     if (iNextQuery>0 && (iNextQuery<iLastQuery || iLastQuery==-1))
219       iLastQuery = iNextQuery;
220
221     iNextQuery = sType.indexOf("/");
222     if (iNextQuery>0 && (iNextQuery<iLastQuery || iLastQuery==-1))
223       iLastQuery = iNextQuery;
224
225     iNextQuery = sType.indexOf("&");
226     if (iNextQuery>0 && (iNextQuery<iLastQuery || iLastQuery==-1))
227       iLastQuery = iNextQuery;
228
229     if (iLastQuery<0) iLastQuery=4;
230
231     return sType.substring(0, iLastQuery);
232   } // removeQueryString()
233

234   // ----------------------------------------------------------
235

236   /**
237    * <p>Get product type from file path.</p>
238    * @return Product Type as listed in k_lu_prod_types table computed following these rules:<br>
239    * If xfile is <b>null</b> and xpath is <b>null</b> and id_cont_type is not CONTAINER_HTTP or CONTAINER_HTTPS then "?" is returned.<br>
240    * If xfile is <b>null</b> and xpath is <b>null</b> and id_cont_type is CONTAINER_HTTP or CONTAINER_HTTPS then "HTML" is returned.<br>
241    * If xfile is not <b>null</b> and file name has at least one dot ('.') then last dot is considered the extension delimiter.<br>
242    * <b>Example 1</b>:<br>
243    * ProductLocation oLoca = new ProductLocation();<br>
244    * oLoca.setURL("http://www.hipergate.org/index.jsp?lang=en");<br>
245    * String sProdType = oLoca.getProductType();<br>
246    * // sProdType == "JSP"<br><br>
247    * <b>Example 2</b>:<br>
248    * ProductLocation oLoca = new ProductLocation();<br>
249    * oLoca.put(DB.id_cont_type, ProductLocation.CONTAINER_HTTP);<br>
250    * oLoca.setURL("http://www.hipergate.org/");<br>
251    * // sProdType == "HTML"
252    */

253
254   public String JavaDoc getProductType() {
255     String JavaDoc sFile;
256     String JavaDoc sType;
257     int iLast;
258
259     sFile = isNull(DB.xfile) ? getStringNull(DB.xpath,"") : getStringNull(DB.xfile,"");
260
261     if (sFile.length()==0) {
262       if (isNull(DB.id_cont_type))
263         return "?";
264       else
265         switch (getInt(DB.id_cont_type)) {
266           case CONTAINER_HTTP :
267           case CONTAINER_HTTPS:
268             return "HTML";
269           default:
270             return "?";
271         }
272     }
273     else {
274       iLast = sFile.lastIndexOf(".");
275       if (iLast > 0) {
276         sType = removeQueryString(sFile.substring(++iLast).toUpperCase());
277
278         return sType;
279       }
280       else {
281         if (isNull(DB.id_cont_type))
282           return "?";
283         else
284           switch (getInt(DB.id_cont_type)) {
285             case CONTAINER_HTTP:
286             case CONTAINER_HTTPS:
287               return "HTML";
288             default:
289               return "?";
290           }
291       }
292     }
293   } // getProductType()
294

295   // ----------------------------------------------------------
296

297   /**
298    * <p>Set URL for ProductLocation</p>
299    * @throws MalformedURLException
300    */

301
302   public void setURL (String JavaDoc sURL) throws MalformedURLException JavaDoc {
303     String JavaDoc sURI = sURL.toLowerCase();
304     URL JavaDoc oURL;
305
306     if (DebugFile.trace) {
307       DebugFile.writeln("Begin ProductLocation.setURL(" + sURL + ")" );
308       DebugFile.incIdent();
309     }
310
311     if (!sURI.startsWith("http://") && !sURI.startsWith("https://") && !sURI.startsWith("ftp://") &&
312         !sURI.startsWith("file://") && !sURI.startsWith("odbc:") && !sURI.startsWith("jdbc:") &&
313         !sURI.startsWith("lotus://") && !sURI.startsWith("ware://"))
314       sURI = "http://" + sURI;
315
316     if (sURI.startsWith("https://"))
317       oURL = new URL JavaDoc("http" + sURI.substring(5));
318     else
319       oURL = new URL JavaDoc(sURI);
320
321     replace(DB.xprotocol, sURI.substring(0, sURI.indexOf("://")+3));
322
323     replace(DB.xhost, oURL.getHost());
324
325     if (-1!=oURL.getPort())
326       replace(DB.xport, oURL.getPort());
327     else
328       remove(DB.xport);
329
330     String JavaDoc sPath = oURL.getPath();
331     if (null!=sPath) {
332       if (sPath.equals("/"))
333         remove(DB.xpath);
334       else
335         replace(DB.xpath, sPath);
336     }
337     else
338       remove(DB.xpath);
339
340     String JavaDoc sFile = oURL.getFile();
341     if (null!=sFile) {
342       if (sPath.equals("/")) {
343         remove(DB.xfile);
344         remove(DB.xoriginalfile);
345       }
346       else {
347         replace(DB.xfile, sFile);
348         replace(DB.xoriginalfile, sFile);
349       }
350     }
351     else {
352       remove(DB.xfile);
353       remove(DB.xoriginalfile);
354     }
355
356     if (null!=oURL.getRef())
357       replace(DB.xanchor, oURL.getRef());
358     else
359       remove(DB.xanchor);
360
361     if (DebugFile.trace) {
362       DebugFile.decIdent();
363       DebugFile.writeln("End ProductLocation.setURL() : " + oURL.toExternalForm());
364     }
365
366     } // setURL()
367

368     // ----------------------------------------------------------
369

370     /**
371      * <p>Set file length in bytes</p>
372      * Max file length is 2147483647 bytes
373      * @param lLen Length of file pointed by ProductLocation URL
374      * @throws IllegalArgumentException If lLen > 2147483647
375      */

376     public void setLength (long lLen) throws IllegalArgumentException JavaDoc {
377
378       if (lLen>(long)2147483647)
379         throw new IllegalArgumentException JavaDoc("Maximum allowed file length is 2Gb");
380
381       replace (DB.len_file, new Long JavaDoc(lLen).intValue());
382     }
383
384     // ----------------------------------------------------------
385

386     /**
387      * <p>Set file length in bytes</p>
388      * @param lLen Length of file pointed by ProductLocation URL
389      */

390     public void setLength (int iLen) {
391       replace (DB.len_file, iLen);
392     }
393
394     // ----------------------------------------------------------
395

396     /**
397      * <p>Set File Path</p>
398      * @param sProtocol Protocol. One of { "file://", "http://", "https://", "ftp://", "odbc://", "jdbc://", "ware://" }
399      * @param sHost Host Name for electronic Products or WareHouse Name for physical Products. Example: "www.hipergate.org"
400      * @param sPath Absolute Access Path. Example: "/opt/knowgate/users/"
401      * @param sFile Uploaded File Name
402      * @param sOriginalFile Original File Name before uploading.
403      * In some cases, files may be renamed upon upload and then renamed again to their original names when downloaded.
404      */

405     public void setPath (String JavaDoc sProtocol, String JavaDoc sHost, String JavaDoc sPath, String JavaDoc sFile, String JavaDoc sOriginalFile) {
406       if (DebugFile.trace) {
407         DebugFile.writeln("Begin ProductLocation.setPath(" + sProtocol + "," + sHost + "," + sPath + "," + sFile + "," + sOriginalFile + ")");
408         DebugFile.incIdent();
409       }
410
411       replace(DB.xprotocol,sProtocol);
412       replace(DB.xhost,sHost);
413       replace(DB.xpath,sPath);
414       replace(DB.xfile,sFile);
415       replace(DB.xoriginalfile,sOriginalFile);
416
417       if (DebugFile.trace) {
418         DebugFile.decIdent();
419         DebugFile.writeln("End ProductLocation.setPath()");
420       }
421     } // setPath
422

423     // ----------------------------------------------------------
424

425     /**
426      * <p>Set File Path</p>
427      * @param sProtocol Protocol. One of { "file://", "http://", "https://", "ftp://", "odbc://", "jdbc://", "ware://" }
428      * @param sHost Host Name for electronic Products or WareHouse Name for physical Products. Example: "www.hipergate.org"
429      * @param sPath Absolute Access Path. Example: "/opt/knowgate/users/"
430      * @param sFile Uploaded File Name
431      * @param sOriginalFile Original File Name before uploading.
432      * In some cases, files may be renamed upon upload and then renamed again to their original names when downloaded.
433      * @param sAnchor Anchor
434      */

435     public void setPath (String JavaDoc sProtocol, String JavaDoc sHost, String JavaDoc sPath, String JavaDoc sFile, String JavaDoc sOriginalFile, String JavaDoc sAnchor) {
436       if (DebugFile.trace) {
437         DebugFile.writeln("Begin ProductLocation.setPath(" + sProtocol + "," + sHost + "," + sPath + "," + sFile + "," + sOriginalFile + "," + sAnchor + ")");
438         DebugFile.incIdent();
439       }
440
441       replace(DB.xprotocol,sProtocol);
442       replace(DB.xhost,sHost);
443       replace(DB.xpath,sPath);
444       replace(DB.xfile,sFile);
445       replace(DB.xoriginalfile,sOriginalFile);
446       replace(DB.xanchor,sAnchor);
447
448       if (DebugFile.trace) {
449         DebugFile.decIdent();
450         DebugFile.writeln("End ProductLocation.setPath()");
451       }
452     } // setPath
453

454   // ----------------------------------------------------------
455

456   /**
457    * <p>Set Local Path for ProductLocation</p>
458    * File Separator is taken from System.getProperty("file.separator") method.
459    * @param sHost Host Name
460    * @param sLocalPath Full Local Path and File Name.<br>
461    * Example 1: "file:///opt/knowgate/userfiles/logo.gif"<br>
462    * Example 2: "file://C:\\TEMP\\UserGuide.doc"<br>
463    */

464
465   public void setPath (String JavaDoc sHost, String JavaDoc sLocalPath) {
466     final String JavaDoc sFileSep = System.getProperty("file.separator");
467     String JavaDoc sPath = sLocalPath;
468     int iLast;
469
470     if (DebugFile.trace) {
471       DebugFile.writeln("Begin ProductLocation.setPath(" + sHost + "," + sLocalPath + ")");
472       DebugFile.incIdent();
473     }
474
475     if (!sPath.startsWith("file://")) sPath = "file://" + sPath;
476
477     iLast = sPath.lastIndexOf(sFileSep);
478
479     replace(DB.xprotocol, "file");
480     replace(DB.xhost, sHost);
481
482     if (DebugFile.trace) DebugFile.writeln(DB.xpath + "=" + sPath.substring(7, iLast));
483
484     replace(DB.xpath, sPath.substring(7, iLast));
485
486     if (iLast<sPath.length()-1) {
487       if (DebugFile.trace) DebugFile.writeln(DB.xfile + "=" + sPath.substring(iLast+1));
488
489       replace(DB.xfile, sPath.substring(iLast+1));
490
491       if (DebugFile.trace) DebugFile.writeln(DB.xoriginalfile + "=" + sPath.substring(iLast+1));
492
493       replace(DB.xoriginalfile, sPath.substring(iLast+1));
494     }
495     else {
496       remove(DB.xfile);
497       remove(DB.xoriginalfile);
498     }
499
500     remove(DB.xport);
501     remove(DB.xanchor);
502
503     if (DebugFile.trace) {
504       DebugFile.decIdent();
505       DebugFile.writeln("End ProductLocation.setPath()");
506     }
507   } // setPath()
508

509   // ----------------------------------------------------------
510

511   /**
512    * <p>Store productLocation</p>
513    * Properties gu_location, dt_uploaded, dt_modified, id_cont_type,
514    * id_prod_type, pg_prod_locat and status are automatically set if not present
515    * at internal properties colelction when calling store().
516    * @param oConn Database Connection
517    * @throws SQLException
518    */

519   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
520     boolean iRetVal;
521     String JavaDoc sProtocol;
522     String JavaDoc sProdType;
523     PreparedStatement JavaDoc oStmt;
524     ResultSet JavaDoc oRSet;
525     Object JavaDoc oNextLoca;
526     final String JavaDoc EmptyStr = "";
527     java.util.Date JavaDoc dtNow = new java.util.Date JavaDoc();
528     java.sql.Timestamp JavaDoc dtSQL = new java.sql.Timestamp JavaDoc(dtNow.getTime());
529
530     if (DebugFile.trace) {
531       DebugFile.writeln("Begin ProductLocation.store([Connection])" );
532       DebugFile.incIdent();
533     }
534
535     if (!AllVals.containsKey(DB.gu_location))
536       put(DB.gu_location, Gadgets.generateUUID());
537
538     if (!AllVals.containsKey(DB.dt_uploaded))
539       put (DB.dt_uploaded, dtSQL);
540
541     if (!AllVals.containsKey(DB.dt_modified))
542       put (DB.dt_modified, dtSQL);
543
544     if (!AllVals.containsKey(DB.id_cont_type)) {
545       if (DebugFile.trace) DebugFile.writeln(DB.id_cont_type + "=" + String.valueOf(getContainerType()));
546
547       put (DB.id_cont_type, new Integer JavaDoc(getContainerType()));
548     }
549
550     if (!AllVals.containsKey(DB.id_prod_type)) {
551       sProdType = getProductType();
552
553       if (DebugFile.trace) DebugFile.writeln(DB.id_prod_type + "=" + sProdType);
554
555       put(DB.id_prod_type, sProdType.length()>0 ? sProdType : "?");
556     }
557     else {
558       sProdType = (String JavaDoc) AllVals.get(DB.id_prod_type);
559     }
560
561     if (!AllVals.containsKey(DB.xhost))
562       put (DB.xhost, "localhost");
563     else if (EmptyStr.equals(AllVals.get(DB.xhost)))
564       put (DB.xhost, "localhost");
565
566     PreparedStatement JavaDoc oStTp = oConn.prepareStatement("SELECT NULL FROM "+DB.k_lu_prod_types+" WHERE "+DB.id_prod_type+"=?",ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
567     oStTp.setString(1, sProdType.length()>0 ? sProdType : "?");
568     ResultSet JavaDoc oRsTp = oStTp.executeQuery();
569     boolean bRegistredType = oRsTp.next();
570     oRsTp.close();
571     oStTp.close();
572     if (!bRegistredType) {
573       oStTp = oConn.prepareStatement("INSERT INTO "+DB.k_lu_prod_types+" ("+DB.id_prod_type+","+DB.de_prod_type+","+DB.nm_icon+") VALUES (?,'"+sProdType+" file',NULL)");
574       oStTp.setString(1, sProdType);
575       oStTp.executeUpdate();
576       oStTp.close();
577     }
578
579     if (AllVals.containsKey(DB.xprotocol))
580       replace(DB.xprotocol, getString(DB.xprotocol).toLowerCase());
581
582     if (!AllVals.containsKey(DB.pg_prod_locat)) {
583       if (DebugFile.trace)
584          DebugFile.writeln("Connection.prepareStatement(SELECT MAX(" + DB.pg_prod_locat + ")+1 FROM " + DB.k_prod_locats + " WHERE " + DB.gu_product + "='" + getStringNull(DB.gu_product, "") + "')");
585
586       oStmt = oConn.prepareStatement("SELECT MAX(" + DB.pg_prod_locat + ")+1 FROM " + DB.k_prod_locats + " WHERE " + DB.gu_product + "=?");
587       oStmt.setString(1, getString(DB.gu_product));
588
589       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeQuery()");
590
591       oRSet = oStmt.executeQuery();
592       if (oRSet.next()) {
593         oNextLoca = oRSet.getObject(1);
594         if (oRSet.wasNull())
595           put(DB.pg_prod_locat, new Integer JavaDoc(1));
596         else
597           put(DB.pg_prod_locat, new Integer JavaDoc(oNextLoca.toString()));
598       }
599       else
600         put(DB.pg_prod_locat, new Integer JavaDoc(1));
601       oRSet.close();
602       oStmt.close();
603       oStmt = null;
604
605       if (DebugFile.trace)
606         DebugFile.writeln(DB.pg_prod_locat + "=" + String.valueOf(getInt(DB.pg_prod_locat)));
607     }
608
609     if (AllVals.containsKey(DB.status))
610       replace(DB.status, 1);
611
612     iRetVal = super.store(oConn);
613
614     if (DebugFile.trace) {
615       DebugFile.decIdent();
616       DebugFile.writeln("End ProductLocation.store() : " + String.valueOf(iRetVal));
617     }
618
619     return iRetVal;
620   } // store()
621

622   // ----------------------------------------------------------
623

624   /**
625    * <p>Rename a Product Location</p>
626    * This method updates the xfile value from table k_prod_locats and changes the physical file name accordingly
627    * @param oConn Database connection
628    * @param oFileSys FileSystem Object for accessing physical file
629    * @param sNewFile New file name
630    * @return <b>true</b> if file was successfully renamed
631    * @since 2.1
632    * @throws Exception
633    * @throws IOException
634    * @throws SQLException
635    * @throws SecurityException
636    */

637   public boolean rename(JDCConnection oConn, FileSystem oFileSys, String JavaDoc sNewFile)
638     throws Exception JavaDoc,IOException JavaDoc,SQLException JavaDoc,SecurityException JavaDoc {
639     String JavaDoc sPath;
640     String JavaDoc sOldFile;
641     java.util.Date JavaDoc dtNow = new java.util.Date JavaDoc();
642     java.sql.Timestamp JavaDoc dtSQL = new java.sql.Timestamp JavaDoc(dtNow.getTime());
643
644     sOldFile = getString(DB.xfile);
645
646     if (getString(DB.xprotocol).equalsIgnoreCase("file://")) {
647       sPath = Gadgets.chomp (getString(DB.xprotocol)+getString(DB.xpath), File.separator);
648     }
649     else {
650       if (isNull(DB.xport))
651         sPath = Gadgets.chomp (getString(DB.xprotocol)+getString(DB.xhost)+"/"+getString(DB.xpath), '/');
652       else
653         sPath = Gadgets.chomp (getString(DB.xprotocol)+getString(DB.xhost)+":"+String.valueOf(getInt(DB.xport))+"/"+getString(DB.xpath), '/');
654     }
655
656     replace(DB.xfile, sNewFile);
657     replace(DB.id_prod_type, getProductType());
658     replace(DB.dt_modified, dtSQL);
659
660     store(oConn);
661
662     return oFileSys.rename(sPath+sOldFile, sPath+sNewFile);
663   } // rename
664

665   // ----------------------------------------------------------
666

667   /**
668    * Move a File from a temporary directory up to the final location referenced
669    * by ProductLocation URL.
670    * @param oConn Database Connection
671    * @param oFileSys FileSystem object used for moving the file.<br>
672    * If FileSystem requieres additional parameters (such as user authentification for FTP)
673    * it will be taken from hipergate.cnf file by using Environment singleton.
674    * @param sSourcePath Source Directory. For example: "file:///tmp/archivos/"
675    * @param sSourceFile Original File Name. For example: "notes.pdf"
676    * @param sTargetPath Target Directory. For example: "ftp://saturno/opt/storage/"
677    * @param sTargetFile Final File Name. For Example: "notes-13-05-03.pdf"
678    * @throws Exception
679    * @throws IOException
680    * @throws SQLException
681    * @see com.knowgate.misc.Environment
682    */

683   public void upload(JDCConnection oConn, FileSystem oFileSys, String JavaDoc sSourcePath, String JavaDoc sSourceFile, String JavaDoc sTargetPath, String JavaDoc sTargetFile) throws Exception JavaDoc,IOException JavaDoc,SQLException JavaDoc {
684     String JavaDoc sFileSep = System.getProperty("file.separator");
685
686     if (DebugFile.trace) {
687       DebugFile.writeln("Begin ProductLocation.upload([Connection], [FileSystem], " + sSourcePath + "," + sSourceFile + sTargetPath + "," + sTargetFile + ")" );
688       DebugFile.incIdent();
689     }
690
691     if (!sSourcePath.endsWith(sFileSep)) sSourcePath += sFileSep;
692     if (!sTargetPath.endsWith(sFileSep)) sTargetPath += sFileSep;
693
694     oFileSys.move(sSourcePath+sSourceFile, sTargetPath+sTargetFile);
695
696     if (DebugFile.trace) {
697       DebugFile.decIdent();
698       DebugFile.writeln("End ProductLocation.upload()");
699     }
700   } // upload()
701

702   // ----------------------------------------------------------
703

704   /**
705    * <p>Delete Productocation</p>
706    * If ProductLocation URL point to a file that does not exist then an SQLException will be raised.<br>
707    * Disk files are only deleted for CONTAINER_FILE and CONTAINER_FTP container types. Files pointed by
708    * other protocols (such as CONTAINER_HTTP) will not be deleted with by this method.
709    * @param oConn Database Connection
710    * @throws SQLException
711    */

712   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
713     boolean bRetVal;
714     FileSystem oFS = new FileSystem();
715
716     if (DebugFile.trace) {
717       DebugFile.writeln("Begin ProductLocation.delete(" + getString(DB.gu_location) + ")" );
718       DebugFile.incIdent();
719     }
720
721     try {
722       if (getInt(DB.id_cont_type)==ProductLocation.CONTAINER_FILE || getInt(DB.id_cont_type)==ProductLocation.CONTAINER_FTP) {
723
724         oFS.delete(getURL());
725       } // fi(CONTAINER_*)
726
}
727     catch (IOException JavaDoc ioe) {
728       if (ioe.getMessage().indexOf("No such file or directory")<0)
729         throw new SQLException JavaDoc(ioe.getMessage());
730     }
731
732     oFS = null;
733
734     bRetVal = super.delete(oConn);
735
736     if (DebugFile.trace) {
737       DebugFile.decIdent();
738       DebugFile.writeln("End Product.delete() : " + String.valueOf(bRetVal));
739     }
740
741     return bRetVal;
742   } // delete
743

744   // ----------------------------------------------------------
745

746    public static final short ClassId = 16;
747
748   // ----------------------------------------------------------
749

750   public static final int CONTAINER_FILE = 1;
751   public static final int CONTAINER_HTTP = 2;
752   public static final int CONTAINER_HTTPS = 3;
753   public static final int CONTAINER_FTP = 4;
754   public static final int CONTAINER_ODBC = 5;
755   public static final int CONTAINER_LOTUS = 6;
756   public static final int CONTAINER_JDBC = 7;
757   public static final int CONTAINER_WARE = 100;
758
759 }
760
Popular Tags