Source Code Cross Referenced for ProductLocation.java in  » Groupware » hipergate » com » knowgate » hipergate » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Groupware » hipergate » com.knowgate.hipergate 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:          Copyright (C) 2003  Know Gate S.L. All rights reserved.
003:                              C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005:          Redistribution and use in source and binary forms, with or without
006:          modification, are permitted provided that the following conditions
007:          are met:
008:
009:          1. Redistributions of source code must retain the above copyright
010:             notice, this list of conditions and the following disclaimer.
011:
012:          2. The end-user documentation included with the redistribution,
013:             if any, must include the following acknowledgment:
014:             "This product includes software parts from hipergate
015:             (http://www.hipergate.org/)."
016:             Alternately, this acknowledgment may appear in the software itself,
017:             if and wherever such third-party acknowledgments normally appear.
018:
019:          3. The name hipergate must not be used to endorse or promote products
020:             derived from this software without prior written permission.
021:             Products derived from this software may not be called hipergate,
022:             nor may hipergate appear in their name, without prior written
023:             permission.
024:
025:          This library is distributed in the hope that it will be useful,
026:          but WITHOUT ANY WARRANTY; without even the implied warranty of
027:          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029:          You should have received a copy of hipergate License with this code;
030:          if not, visit http://www.hipergate.org or mail to info@hipergate.org
031:         */
032:
033:        package com.knowgate.hipergate;
034:
035:        import java.lang.System;
036:        import java.io.IOException;
037:        import java.io.File;
038:
039:        import java.sql.PreparedStatement;
040:        import java.sql.ResultSet;
041:        import java.sql.SQLException;
042:
043:        import java.net.URL;
044:        import java.net.MalformedURLException;
045:
046:        import com.knowgate.debug.DebugFile;
047:        import com.knowgate.jdc.JDCConnection;
048:        import com.knowgate.dataobjs.DB;
049:        import com.knowgate.dataobjs.DBPersist;
050:        import com.knowgate.dfs.FileSystem;
051:        import com.knowgate.misc.Gadgets;
052:
053:        /**
054:         * <p>Product Location</p>
055:         * Location semantics depend upon what Product is used for.<br>
056:         * <ul>
057:         * <li>For Downloadable Products, ProductLocations represent mirror download URLs.
058:         * <li>For Versioned Products, ProductLocations represent different versions of the same File.
059:         * <li>For Compound Products, ProductLocations represent parts of the Product each one being a File.
060:         * <li>For Physical Products, ProductLocations represent stock of Product at different warehouses.
061:         * </ul>
062:         * @author Sergio Montoro Ten
063:         * @version 2.2
064:         */
065:        public class ProductLocation extends DBPersist {
066:
067:            public ProductLocation() {
068:                super (DB.k_prod_locats, "ProductLocation");
069:            }
070:
071:            /**
072:             * Load ProductLocation from database
073:             * @param oConn Database Conenction
074:             * @param iIdLocation GUID of ProductLocation to be loaded
075:             * @throws SQLException
076:             */
077:            public ProductLocation(JDCConnection oConn, String iIdLocation)
078:                    throws SQLException {
079:                super (DB.k_prod_locats, "ProductLocation");
080:
081:                Object aProd[] = { iIdLocation };
082:
083:                load(oConn, aProd);
084:            }
085:
086:            // ----------------------------------------------------------
087:
088:            /**
089:             * <p>Get Path to URL or file</p>
090:             * @return xprotocol [xhost] [:xport] / xpath<br>
091:             * Example 1: file:///opt/knowgate/storage/userdata/<br>
092:             * Example 2: http://www.hipergate.org/es/<br>
093:             * Example 3: http://localhost:8080/
094:             */
095:            public String getPath() {
096:                String sPath;
097:                String sXProtocol = getString(DB.xprotocol);
098:                String sXPath = getStringNull(DB.xpath, "");
099:                String sFileSep = sXProtocol.startsWith("file") ? System
100:                        .getProperty("file.separator") : "/";
101:
102:                if (!sXProtocol.endsWith("://"))
103:                    sXProtocol += "://";
104:
105:                sPath = sXProtocol;
106:
107:                if (!sXProtocol.startsWith("file"))
108:                    sPath += getString("xhost");
109:
110:                if (!isNull(DB.xport))
111:                    sPath += ":" + getShort(DB.xport);
112:
113:                if (!sPath.endsWith(sFileSep) && !sXPath.startsWith(sFileSep))
114:                    sPath += sFileSep;
115:
116:                sPath += sXPath;
117:
118:                return sPath;
119:            } // getPath()
120:
121:            // ----------------------------------------------------------
122:
123:            /**
124:             * Get URL for ProductLocation
125:             * @return getPath() [xfile] [#xanchor]
126:             */
127:
128:            public String getURL() {
129:                String sURL;
130:                String sXProtocol = getString(DB.xprotocol);
131:                String sFileSep = sXProtocol.startsWith("file") ? System
132:                        .getProperty("file.separator") : "/";
133:
134:                sURL = getPath();
135:
136:                if (null != sURL) {
137:                    sURL = sURL.trim();
138:
139:                    if (!sURL.endsWith(sFileSep) && !isNull(DB.xfile))
140:                        sURL += sFileSep;
141:
142:                    if (!isNull(DB.xfile))
143:                        sURL += getString(DB.xfile);
144:                    if (!isNull(DB.xanchor))
145:                        sURL += "#" + getString(DB.xanchor);
146:                }
147:
148:                return sURL;
149:            } // getURL()
150:
151:            // ----------------------------------------------------------
152:
153:            /**
154:             * <p>Get numeric identifier for container type.</p>
155:             * Usually, container type is computed from protocol.<br>
156:             * <table border=1 cellpadding=4>
157:             * <tr><td><b>Protocol</b></td><td><b>Container Type</b></td></tr>
158:             * <tr><td align=middle>file://</td><td align=middle>CONTAINER_FILE</td></tr>
159:             * <tr><td align=middle>http://</td><td align=middle>CONTAINER_HTTP</td></tr>
160:             * <tr><td align=middle>https://</td><td align=middle>CONTAINER_HTTPS</td></tr>
161:             * <tr><td align=middle>ftp://</td><td align=middle>CONTAINER_FTP</td></tr>
162:             * <tr><td align=middle>odbc://</td><td align=middle>CONTAINER_ODBC</td></tr>
163:             * <tr><td align=middle>lotus://</td><td align=middle>CONTAINER_LOTUS</td></tr>
164:             * <tr><td align=middle>jdbc://</td><td align=middle>CONTAINER_JDBC</td></tr>
165:             * <tr><td align=middle>ware://</td><td align=middle>CONTAINER_WARE</td></tr>
166:             * </table>
167:             * @return Container Type. One of { CONTAINER_FILE, CONTAINER_HTTP, CONTAINER_HTTPS, CONTAINER_FTP, CONTAINER_ODBC, CONTAINER_JDBC, CONTAINER_LOTUS, CONTAINER_WARE }
168:             */
169:            public int getContainerType() {
170:
171:                if (DebugFile.trace) {
172:                    DebugFile
173:                            .writeln("Begin ProductLocation.getContainerType()");
174:                    DebugFile.incIdent();
175:                    DebugFile
176:                            .writeln("protocol="
177:                                    + getStringNull(DB.xprotocol, "null")
178:                                            .toLowerCase());
179:                }
180:
181:                String sProtocol = getString(DB.xprotocol).toLowerCase();
182:                int iProdType = 0;
183:
184:                if (sProtocol.startsWith("file:"))
185:                    iProdType = 1;
186:                else if (sProtocol.startsWith("http:"))
187:                    iProdType = 2;
188:                else if (sProtocol.startsWith("https:"))
189:                    iProdType = 3;
190:                else if (sProtocol.startsWith("ftp:"))
191:                    iProdType = 4;
192:                else if (sProtocol.startsWith("odbc:"))
193:                    iProdType = 5;
194:                else if (sProtocol.startsWith("lotus:"))
195:                    iProdType = 6;
196:                else if (sProtocol.startsWith("jdbc:"))
197:                    iProdType = 7;
198:                else if (sProtocol.startsWith("ware:"))
199:                    iProdType = 100;
200:
201:                if (DebugFile.trace) {
202:                    DebugFile.decIdent();
203:                    DebugFile
204:                            .writeln("End ProductLocation.getContainerType() : "
205:                                    + String.valueOf(iProdType));
206:                }
207:
208:                return iProdType;
209:            } // getContainerType()
210:
211:            // ----------------------------------------------------------
212:
213:            /**
214:             * <p>Remove querystring from a file name</p>
215:             * Used for separating a file extension from the query string in a URL.<br>
216:             * The tactic used is finding the first character of a the set {'?', '#', '/', '&' }
217:             * and cut file extension at that point.
218:             * @param sType File Name and query string
219:             * @return File Name without query string
220:             */
221:            private String removeQueryString(String sType) {
222:                int iLastQuery, iNextQuery;
223:
224:                // If 4 characters or less, then return as is.
225:                if (sType.length() <= 4)
226:                    return sType;
227:
228:                iLastQuery = iNextQuery = sType.indexOf("?");
229:
230:                iNextQuery = sType.indexOf("#");
231:                if (iNextQuery > 0
232:                        && (iNextQuery < iLastQuery || iLastQuery == -1))
233:                    iLastQuery = iNextQuery;
234:
235:                iNextQuery = sType.indexOf(",");
236:                if (iNextQuery > 0
237:                        && (iNextQuery < iLastQuery || iLastQuery == -1))
238:                    iLastQuery = iNextQuery;
239:
240:                iNextQuery = sType.indexOf("/");
241:                if (iNextQuery > 0
242:                        && (iNextQuery < iLastQuery || iLastQuery == -1))
243:                    iLastQuery = iNextQuery;
244:
245:                iNextQuery = sType.indexOf("&");
246:                if (iNextQuery > 0
247:                        && (iNextQuery < iLastQuery || iLastQuery == -1))
248:                    iLastQuery = iNextQuery;
249:
250:                if (iLastQuery < 0)
251:                    iLastQuery = 4;
252:
253:                return sType.substring(0, iLastQuery);
254:            } // removeQueryString()
255:
256:            // ----------------------------------------------------------
257:
258:            /**
259:             * <p>Get product type from file path.</p>
260:             * @return Product Type as listed in k_lu_prod_types table computed following these rules:<br>
261:             * 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>
262:             * 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>
263:             * If xfile is not <b>null</b> and file name has at least one dot ('.') then last dot is considered the extension delimiter.<br>
264:             * <b>Example 1</b>:<br>
265:             * ProductLocation oLoca = new ProductLocation();<br>
266:             * oLoca.setURL("http://www.hipergate.org/index.jsp?lang=en");<br>
267:             * String sProdType = oLoca.getProductType();<br>
268:             * // sProdType == "JSP"<br><br>
269:             * <b>Example 2</b>:<br>
270:             * ProductLocation oLoca = new ProductLocation();<br>
271:             * oLoca.put(DB.id_cont_type, ProductLocation.CONTAINER_HTTP);<br>
272:             * oLoca.setURL("http://www.hipergate.org/");<br>
273:             * // sProdType == "HTML"
274:             */
275:
276:            public String getProductType() {
277:                String sFile;
278:                String sType;
279:                int iLast;
280:
281:                sFile = isNull(DB.xfile) ? getStringNull(DB.xpath, "")
282:                        : getStringNull(DB.xfile, "");
283:
284:                if (sFile.length() == 0) {
285:                    if (isNull(DB.id_cont_type))
286:                        return "?";
287:                    else
288:                        switch (getInt(DB.id_cont_type)) {
289:                        case CONTAINER_HTTP:
290:                        case CONTAINER_HTTPS:
291:                            return "HTML";
292:                        default:
293:                            return "?";
294:                        }
295:                } else {
296:                    iLast = sFile.lastIndexOf(".");
297:                    if (iLast > 0) {
298:                        sType = removeQueryString(sFile.substring(++iLast)
299:                                .toUpperCase());
300:
301:                        return sType;
302:                    } else {
303:                        if (isNull(DB.id_cont_type))
304:                            return "?";
305:                        else
306:                            switch (getInt(DB.id_cont_type)) {
307:                            case CONTAINER_HTTP:
308:                            case CONTAINER_HTTPS:
309:                                return "HTML";
310:                            default:
311:                                return "?";
312:                            }
313:                    }
314:                }
315:            } // getProductType()
316:
317:            // ----------------------------------------------------------
318:
319:            /**
320:             * <p>Set URL for ProductLocation</p>
321:             * @throws MalformedURLException
322:             */
323:
324:            public void setURL(String sURL) throws MalformedURLException {
325:                String sURI = sURL.toLowerCase();
326:                URL oURL;
327:
328:                if (DebugFile.trace) {
329:                    DebugFile.writeln("Begin ProductLocation.setURL(" + sURL
330:                            + ")");
331:                    DebugFile.incIdent();
332:                }
333:
334:                if (!sURI.startsWith("http://") && !sURI.startsWith("https://")
335:                        && !sURI.startsWith("ftp://")
336:                        && !sURI.startsWith("file://")
337:                        && !sURI.startsWith("odbc:")
338:                        && !sURI.startsWith("jdbc:")
339:                        && !sURI.startsWith("lotus://")
340:                        && !sURI.startsWith("ware://"))
341:                    sURI = "http://" + sURI;
342:
343:                if (sURI.startsWith("https://"))
344:                    oURL = new URL("http" + sURI.substring(5));
345:                else
346:                    oURL = new URL(sURI);
347:
348:                replace(DB.xprotocol, sURI
349:                        .substring(0, sURI.indexOf("://") + 3));
350:
351:                replace(DB.xhost, oURL.getHost());
352:
353:                if (-1 != oURL.getPort())
354:                    replace(DB.xport, oURL.getPort());
355:                else
356:                    remove(DB.xport);
357:
358:                String sPath = oURL.getPath();
359:                if (null != sPath) {
360:                    if (sPath.equals("/"))
361:                        remove(DB.xpath);
362:                    else
363:                        replace(DB.xpath, sPath);
364:                } else
365:                    remove(DB.xpath);
366:
367:                String sFile = oURL.getFile();
368:                if (null != sFile) {
369:                    if (sPath.equals("/")) {
370:                        remove(DB.xfile);
371:                        remove(DB.xoriginalfile);
372:                    } else {
373:                        replace(DB.xfile, sFile);
374:                        replace(DB.xoriginalfile, sFile);
375:                    }
376:                } else {
377:                    remove(DB.xfile);
378:                    remove(DB.xoriginalfile);
379:                }
380:
381:                if (null != oURL.getRef())
382:                    replace(DB.xanchor, oURL.getRef());
383:                else
384:                    remove(DB.xanchor);
385:
386:                if (DebugFile.trace) {
387:                    DebugFile.decIdent();
388:                    DebugFile.writeln("End ProductLocation.setURL() : "
389:                            + oURL.toExternalForm());
390:                }
391:
392:            } // setURL()
393:
394:            // ----------------------------------------------------------
395:
396:            /**
397:             * <p>Set file length in bytes</p>
398:             * Max file length is 2147483647 bytes
399:             * @param lLen Length of file pointed by ProductLocation URL
400:             * @throws IllegalArgumentException If lLen > 2147483647
401:             */
402:            public void setLength(long lLen) throws IllegalArgumentException {
403:
404:                if (lLen > (long) 2147483647)
405:                    throw new IllegalArgumentException(
406:                            "Maximum allowed file length is 2Gb");
407:
408:                replace(DB.len_file, new Long(lLen).intValue());
409:            }
410:
411:            // ----------------------------------------------------------
412:
413:            /**
414:             * <p>Set file length in bytes</p>
415:             * @param lLen Length of file pointed by ProductLocation URL
416:             */
417:            public void setLength(int iLen) {
418:                replace(DB.len_file, iLen);
419:            }
420:
421:            // ----------------------------------------------------------
422:
423:            /**
424:             * <p>Set File Path</p>
425:             * @param sProtocol Protocol. One of { "file://", "http://", "https://", "ftp://", "odbc://", "jdbc://", "ware://" }
426:             * @param sHost Host Name for electronic Products or WareHouse Name for physical Products. Example: "www.hipergate.org"
427:             * @param sPath Absolute Access Path. Example: "/opt/knowgate/users/"
428:             * @param sFile Uploaded File Name
429:             * @param sOriginalFile Original File Name before uploading.
430:             * In some cases, files may be renamed upon upload and then renamed again to their original names when downloaded.
431:             */
432:            public void setPath(String sProtocol, String sHost, String sPath,
433:                    String sFile, String sOriginalFile) {
434:                if (DebugFile.trace) {
435:                    DebugFile.writeln("Begin ProductLocation.setPath("
436:                            + sProtocol + "," + sHost + "," + sPath + ","
437:                            + sFile + "," + sOriginalFile + ")");
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:
447:                if (DebugFile.trace) {
448:                    DebugFile.decIdent();
449:                    DebugFile.writeln("End ProductLocation.setPath()");
450:                }
451:            } // setPath
452:
453:            // ----------------------------------------------------------
454:
455:            /**
456:             * <p>Set File Path</p>
457:             * @param sProtocol Protocol. One of { "file://", "http://", "https://", "ftp://", "odbc://", "jdbc://", "ware://" }
458:             * @param sHost Host Name for electronic Products or WareHouse Name for physical Products. Example: "www.hipergate.org"
459:             * @param sPath Absolute Access Path. Example: "/opt/knowgate/users/"
460:             * @param sFile Uploaded File Name
461:             * @param sOriginalFile Original File Name before uploading.
462:             * In some cases, files may be renamed upon upload and then renamed again to their original names when downloaded.
463:             * @param sAnchor Anchor
464:             */
465:            public void setPath(String sProtocol, String sHost, String sPath,
466:                    String sFile, String sOriginalFile, String sAnchor) {
467:                if (DebugFile.trace) {
468:                    DebugFile
469:                            .writeln("Begin ProductLocation.setPath("
470:                                    + sProtocol + "," + sHost + "," + sPath
471:                                    + "," + sFile + "," + sOriginalFile + ","
472:                                    + sAnchor + ")");
473:                    DebugFile.incIdent();
474:                }
475:
476:                replace(DB.xprotocol, sProtocol);
477:                replace(DB.xhost, sHost);
478:                replace(DB.xpath, sPath);
479:                replace(DB.xfile, sFile);
480:                replace(DB.xoriginalfile, sOriginalFile);
481:                replace(DB.xanchor, sAnchor);
482:
483:                if (DebugFile.trace) {
484:                    DebugFile.decIdent();
485:                    DebugFile.writeln("End ProductLocation.setPath()");
486:                }
487:            } // setPath
488:
489:            // ----------------------------------------------------------
490:
491:            /**
492:             * <p>Set Local Path for ProductLocation</p>
493:             * File Separator is taken from System.getProperty("file.separator") method.
494:             * @param sHost Host Name
495:             * @param sLocalPath Full Local Path and File Name.<br>
496:             * Example 1: "file:///opt/knowgate/userfiles/logo.gif"<br>
497:             * Example 2: "file://C:\\TEMP\\UserGuide.doc"<br>
498:             */
499:
500:            public void setPath(String sHost, String sLocalPath) {
501:                final String sFileSep = System.getProperty("file.separator");
502:                String sPath = sLocalPath;
503:                int iLast;
504:
505:                if (DebugFile.trace) {
506:                    DebugFile.writeln("Begin ProductLocation.setPath(" + sHost
507:                            + "," + sLocalPath + ")");
508:                    DebugFile.incIdent();
509:                }
510:
511:                if (!sPath.startsWith("file://"))
512:                    sPath = "file://" + sPath;
513:
514:                iLast = sPath.lastIndexOf(sFileSep);
515:
516:                replace(DB.xprotocol, "file");
517:                replace(DB.xhost, sHost);
518:
519:                if (DebugFile.trace)
520:                    DebugFile.writeln(DB.xpath + "="
521:                            + sPath.substring(7, iLast));
522:
523:                replace(DB.xpath, sPath.substring(7, iLast));
524:
525:                if (iLast < sPath.length() - 1) {
526:                    if (DebugFile.trace)
527:                        DebugFile.writeln(DB.xfile + "="
528:                                + sPath.substring(iLast + 1));
529:
530:                    replace(DB.xfile, sPath.substring(iLast + 1));
531:
532:                    if (DebugFile.trace)
533:                        DebugFile.writeln(DB.xoriginalfile + "="
534:                                + sPath.substring(iLast + 1));
535:
536:                    replace(DB.xoriginalfile, sPath.substring(iLast + 1));
537:                } else {
538:                    remove(DB.xfile);
539:                    remove(DB.xoriginalfile);
540:                }
541:
542:                remove(DB.xport);
543:                remove(DB.xanchor);
544:
545:                if (DebugFile.trace) {
546:                    DebugFile.decIdent();
547:                    DebugFile.writeln("End ProductLocation.setPath()");
548:                }
549:            } // setPath()
550:
551:            // ----------------------------------------------------------
552:
553:            /**
554:             * <p>Store productLocation</p>
555:             * Properties gu_location, dt_uploaded, dt_modified, id_cont_type,
556:             * id_prod_type, pg_prod_locat and status are automatically set if not present
557:             * at internal properties colelction when calling store().
558:             * @param oConn Database Connection
559:             * @throws SQLException
560:             */
561:            public boolean store(JDCConnection oConn) throws SQLException {
562:                boolean iRetVal;
563:                String sProtocol;
564:                String sProdType;
565:                PreparedStatement oStmt;
566:                ResultSet oRSet;
567:                Object oNextLoca;
568:                final String EmptyStr = "";
569:                java.util.Date dtNow = new java.util.Date();
570:                java.sql.Timestamp dtSQL = new java.sql.Timestamp(dtNow
571:                        .getTime());
572:
573:                if (DebugFile.trace) {
574:                    DebugFile
575:                            .writeln("Begin ProductLocation.store([Connection])");
576:                    DebugFile.incIdent();
577:                }
578:
579:                if (!AllVals.containsKey(DB.gu_location))
580:                    put(DB.gu_location, Gadgets.generateUUID());
581:
582:                if (!AllVals.containsKey(DB.dt_uploaded))
583:                    put(DB.dt_uploaded, dtSQL);
584:
585:                if (!AllVals.containsKey(DB.dt_modified))
586:                    put(DB.dt_modified, dtSQL);
587:
588:                if (!AllVals.containsKey(DB.id_cont_type)) {
589:                    if (DebugFile.trace)
590:                        DebugFile.writeln(DB.id_cont_type + "="
591:                                + String.valueOf(getContainerType()));
592:
593:                    put(DB.id_cont_type, new Integer(getContainerType()));
594:                }
595:
596:                if (!AllVals.containsKey(DB.id_prod_type)) {
597:                    sProdType = getProductType();
598:
599:                    if (DebugFile.trace)
600:                        DebugFile.writeln(DB.id_prod_type + "=" + sProdType);
601:
602:                    put(DB.id_prod_type, sProdType.length() > 0 ? sProdType
603:                            : "?");
604:                } else {
605:                    sProdType = (String) AllVals.get(DB.id_prod_type);
606:                }
607:
608:                if (!AllVals.containsKey(DB.xhost))
609:                    put(DB.xhost, "localhost");
610:                else if (EmptyStr.equals(AllVals.get(DB.xhost)))
611:                    put(DB.xhost, "localhost");
612:
613:                PreparedStatement oStTp = oConn
614:                        .prepareStatement("SELECT NULL FROM "
615:                                + DB.k_lu_prod_types + " WHERE "
616:                                + DB.id_prod_type + "=?",
617:                                ResultSet.TYPE_FORWARD_ONLY,
618:                                ResultSet.CONCUR_READ_ONLY);
619:                oStTp.setString(1, sProdType.length() > 0 ? sProdType : "?");
620:                ResultSet oRsTp = oStTp.executeQuery();
621:                boolean bRegistredType = oRsTp.next();
622:                oRsTp.close();
623:                oStTp.close();
624:                if (!bRegistredType) {
625:                    oStTp = oConn.prepareStatement("INSERT INTO "
626:                            + DB.k_lu_prod_types + " (" + DB.id_prod_type + ","
627:                            + DB.de_prod_type + "," + DB.nm_icon
628:                            + ") VALUES (?,'" + sProdType + " file',NULL)");
629:                    oStTp.setString(1, sProdType);
630:                    oStTp.executeUpdate();
631:                    oStTp.close();
632:                }
633:
634:                if (AllVals.containsKey(DB.xprotocol))
635:                    replace(DB.xprotocol, getString(DB.xprotocol).toLowerCase());
636:
637:                if (!AllVals.containsKey(DB.pg_prod_locat)) {
638:                    if (DebugFile.trace)
639:                        DebugFile
640:                                .writeln("Connection.prepareStatement(SELECT MAX("
641:                                        + DB.pg_prod_locat
642:                                        + ")+1 FROM "
643:                                        + DB.k_prod_locats
644:                                        + " WHERE "
645:                                        + DB.gu_product
646:                                        + "='"
647:                                        + getStringNull(DB.gu_product, "")
648:                                        + "')");
649:
650:                    oStmt = oConn.prepareStatement("SELECT MAX("
651:                            + DB.pg_prod_locat + ")+1 FROM " + DB.k_prod_locats
652:                            + " WHERE " + DB.gu_product + "=?");
653:                    oStmt.setString(1, getString(DB.gu_product));
654:
655:                    if (DebugFile.trace)
656:                        DebugFile.writeln("PreparedStatement.executeQuery()");
657:
658:                    oRSet = oStmt.executeQuery();
659:                    if (oRSet.next()) {
660:                        oNextLoca = oRSet.getObject(1);
661:                        if (oRSet.wasNull())
662:                            put(DB.pg_prod_locat, new Integer(1));
663:                        else
664:                            put(DB.pg_prod_locat, new Integer(oNextLoca
665:                                    .toString()));
666:                    } else
667:                        put(DB.pg_prod_locat, new Integer(1));
668:                    oRSet.close();
669:                    oStmt.close();
670:                    oStmt = null;
671:
672:                    if (DebugFile.trace)
673:                        DebugFile.writeln(DB.pg_prod_locat + "="
674:                                + String.valueOf(getInt(DB.pg_prod_locat)));
675:                }
676:
677:                if (AllVals.containsKey(DB.status))
678:                    replace(DB.status, 1);
679:
680:                iRetVal = super .store(oConn);
681:
682:                if (DebugFile.trace) {
683:                    DebugFile.decIdent();
684:                    DebugFile.writeln("End ProductLocation.store() : "
685:                            + String.valueOf(iRetVal));
686:                }
687:
688:                return iRetVal;
689:            } // store()
690:
691:            // ----------------------------------------------------------
692:
693:            /**
694:             * <p>Rename a Product Location</p>
695:             * This method updates the xfile value from table k_prod_locats and changes the physical file name accordingly
696:             * @param oConn Database connection
697:             * @param oFileSys FileSystem Object for accessing physical file
698:             * @param sNewFile New file name
699:             * @return <b>true</b> if file was successfully renamed
700:             * @since 2.1
701:             * @throws Exception
702:             * @throws IOException
703:             * @throws SQLException
704:             * @throws SecurityException
705:             */
706:            public boolean rename(JDCConnection oConn, FileSystem oFileSys,
707:                    String sNewFile) throws Exception, IOException,
708:                    SQLException, SecurityException {
709:                String sPath;
710:                String sOldFile;
711:                java.util.Date dtNow = new java.util.Date();
712:                java.sql.Timestamp dtSQL = new java.sql.Timestamp(dtNow
713:                        .getTime());
714:
715:                sOldFile = getString(DB.xfile);
716:
717:                if (getString(DB.xprotocol).equalsIgnoreCase("file://")) {
718:                    sPath = Gadgets.chomp(getString(DB.xprotocol)
719:                            + getString(DB.xpath), File.separator);
720:                } else {
721:                    if (isNull(DB.xport))
722:                        sPath = Gadgets.chomp(getString(DB.xprotocol)
723:                                + getString(DB.xhost) + "/"
724:                                + getString(DB.xpath), '/');
725:                    else
726:                        sPath = Gadgets.chomp(getString(DB.xprotocol)
727:                                + getString(DB.xhost) + ":"
728:                                + String.valueOf(getInt(DB.xport)) + "/"
729:                                + getString(DB.xpath), '/');
730:                }
731:
732:                replace(DB.xfile, sNewFile);
733:                replace(DB.id_prod_type, getProductType());
734:                replace(DB.dt_modified, dtSQL);
735:
736:                store(oConn);
737:
738:                return oFileSys.rename(sPath + sOldFile, sPath + sNewFile);
739:            } // rename
740:
741:            // ----------------------------------------------------------
742:
743:            /**
744:             * Move a File from a temporary directory up to the final location referenced
745:             * by ProductLocation URL.
746:             * @param oConn Database Connection
747:             * @param oFileSys FileSystem object used for moving the file.<br>
748:             * If FileSystem requieres additional parameters (such as user authentification for FTP)
749:             * it will be taken from hipergate.cnf file by using Environment singleton.
750:             * @param sSourcePath Source Directory. For example: "file:///tmp/archivos/"
751:             * @param sSourceFile Original File Name. For example: "notes.pdf"
752:             * @param sTargetPath Target Directory. For example: "ftp://saturno/opt/storage/"
753:             * @param sTargetFile Final File Name. For Example: "notes-13-05-03.pdf"
754:             * @throws Exception
755:             * @throws IOException
756:             * @throws SQLException
757:             * @see com.knowgate.misc.Environment
758:             */
759:            public void upload(JDCConnection oConn, FileSystem oFileSys,
760:                    String sSourcePath, String sSourceFile, String sTargetPath,
761:                    String sTargetFile) throws Exception, IOException,
762:                    SQLException {
763:                String sFileSep = System.getProperty("file.separator");
764:
765:                if (DebugFile.trace) {
766:                    DebugFile
767:                            .writeln("Begin ProductLocation.upload([Connection], [FileSystem], "
768:                                    + sSourcePath
769:                                    + ","
770:                                    + sSourceFile
771:                                    + sTargetPath + "," + sTargetFile + ")");
772:                    DebugFile.incIdent();
773:                }
774:
775:                if (!sSourcePath.endsWith(sFileSep))
776:                    sSourcePath += sFileSep;
777:                if (!sTargetPath.endsWith(sFileSep))
778:                    sTargetPath += sFileSep;
779:
780:                oFileSys.move(sSourcePath + sSourceFile, sTargetPath
781:                        + sTargetFile);
782:
783:                if (DebugFile.trace) {
784:                    DebugFile.decIdent();
785:                    DebugFile.writeln("End ProductLocation.upload()");
786:                }
787:            } // upload()
788:
789:            // ----------------------------------------------------------
790:
791:            /**
792:             * <p>Delete Productocation</p>
793:             * If ProductLocation URL point to a file that does not exist then an SQLException will be raised.<br>
794:             * Disk files are only deleted for CONTAINER_FILE and CONTAINER_FTP container types. Files pointed by
795:             * other protocols (such as CONTAINER_HTTP) will not be deleted with by this method.
796:             * @param oConn Database Connection
797:             * @throws SQLException
798:             */
799:            public boolean delete(JDCConnection oConn) throws SQLException {
800:                boolean bRetVal;
801:                FileSystem oFS = new FileSystem();
802:
803:                if (DebugFile.trace) {
804:                    DebugFile.writeln("Begin ProductLocation.delete("
805:                            + getString(DB.gu_location) + ")");
806:                    DebugFile.incIdent();
807:                }
808:
809:                try {
810:                    if (getInt(DB.id_cont_type) == ProductLocation.CONTAINER_FILE
811:                            || getInt(DB.id_cont_type) == ProductLocation.CONTAINER_FTP) {
812:
813:                        oFS.delete(getURL());
814:                    } // fi(CONTAINER_*)
815:                } catch (IOException ioe) {
816:                    if (ioe.getMessage().indexOf("No such file or directory") < 0)
817:                        throw new SQLException(ioe.getMessage());
818:                }
819:
820:                oFS = null;
821:
822:                bRetVal = super .delete(oConn);
823:
824:                if (DebugFile.trace) {
825:                    DebugFile.decIdent();
826:                    DebugFile.writeln("End Product.delete() : "
827:                            + String.valueOf(bRetVal));
828:                }
829:
830:                return bRetVal;
831:            } // delete
832:
833:            // ----------------------------------------------------------
834:
835:            public static final short ClassId = 16;
836:
837:            // ----------------------------------------------------------
838:
839:            public static final int CONTAINER_FILE = 1;
840:            public static final int CONTAINER_HTTP = 2;
841:            public static final int CONTAINER_HTTPS = 3;
842:            public static final int CONTAINER_FTP = 4;
843:            public static final int CONTAINER_ODBC = 5;
844:            public static final int CONTAINER_LOTUS = 6;
845:            public static final int CONTAINER_JDBC = 7;
846:            public static final int CONTAINER_WARE = 100;
847:
848:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.