Source Code Cross Referenced for Database.java in  » Workflow-Engines » pegasus-2.1.0 » org » griphyn » common » catalog » transformation » 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 » Workflow Engines » pegasus 2.1.0 » org.griphyn.common.catalog.transformation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * This file or a portion of this file is licensed under the terms of
0003:         * the Globus Toolkit Public License, found at $PEGASUS_HOME/GTPL or
0004:         * http://www.globus.org/toolkit/download/license.html.
0005:         * This notice must appear in redistributions of this file
0006:         * with or without modification.
0007:         *
0008:         * Redistributions of this Software, with or without modification, must reproduce
0009:         * the GTPL in:
0010:         * (1) the Software, or
0011:         * (2) the Documentation or
0012:         * some other similar material which is provided with the Software (if any).
0013:         *
0014:         * Copyright 1999-2004
0015:         * University of Chicago and The University of Southern California.
0016:         * All rights reserved.
0017:         */package org.griphyn.common.catalog.transformation;
0018:
0019:        /**
0020:         * This is the database implementation for the TC.
0021:         *
0022:         * @author Gaurang Mehta gmehta@isi.edu
0023:         * @version $Revision: 199 $
0024:         */
0025:
0026:        import org.griphyn.cPlanner.classes.Profile;
0027:        import org.griphyn.cPlanner.common.LogManager;
0028:        import org.griphyn.cPlanner.common.PegasusProperties;
0029:        import org.griphyn.common.catalog.TransformationCatalog;
0030:        import org.griphyn.common.catalog.TransformationCatalogEntry;
0031:        import org.griphyn.common.classes.SysInfo;
0032:        import org.griphyn.common.classes.TCType;
0033:        import org.griphyn.common.util.Separator;
0034:        import org.griphyn.vdl.dbschema.DatabaseSchema;
0035:
0036:        import java.io.IOException;
0037:        import java.lang.reflect.InvocationTargetException;
0038:        import java.sql.PreparedStatement;
0039:        import java.sql.ResultSet;
0040:        import java.sql.SQLException;
0041:        import java.util.ArrayList;
0042:        import java.util.Iterator;
0043:        import java.util.List;
0044:
0045:        public class Database extends DatabaseSchema implements 
0046:                TransformationCatalog {
0047:
0048:            /**
0049:             * The LogManager object which is
0050:             * used to log all the messages.
0051:             * It's values are set in the CPlanner class.
0052:             */
0053:            protected static LogManager mLogger = LogManager.getInstance();
0054:
0055:            //   private static final String TC_MODE = "Database TC Mode";
0056:
0057:            // private PegasusProperties mProps;
0058:
0059:            private static Database mDatabaseTC = null;
0060:
0061:            private Database() throws ClassNotFoundException,
0062:                    NoSuchMethodException, InstantiationException,
0063:                    IllegalAccessException, InvocationTargetException,
0064:                    SQLException, IOException {
0065:                super ((String) null, "pegasus.catalog.transformation.db.schema");
0066:                //mLogger = LogManager.getInstance();
0067:                mLogger.log("TC Mode being used is " + this .getTCMode(),
0068:                        LogManager.CONFIG_MESSAGE_LEVEL);
0069:
0070:                /**
0071:                 * ADD SECTION
0072:                 */
0073:
0074:                this .m_dbdriver.insertPreparedStatement("stmt.add.lfn",
0075:                        "INSERT INTO tc_logicaltx VALUES (?,?,?,?)");
0076:
0077:                this .m_dbdriver.insertPreparedStatement("stmt.add.pfn",
0078:                        "INSERT INTO tc_physicaltx VALUES (?,?,?,?,?)");
0079:
0080:                this .m_dbdriver.insertPreparedStatement("stmt.add.sysinfo",
0081:                        "INSERT INTO tc_sysinfo VALUES (?,?,?,?,?)");
0082:
0083:                this .m_dbdriver
0084:                        .insertPreparedStatement("stmt.add.pfnprofile",
0085:                                "INSERT INTO tc_pfnprofile(namespace,name,value,pfnid) VALUES (?,?,?,?)");
0086:
0087:                this .m_dbdriver
0088:                        .insertPreparedStatement("stmt.add.lfnprofile",
0089:                                "INSERT INTO tc_lfnprofile(namespace,name,value,lfnid) VALUES (?,?,?,?)");
0090:
0091:                this .m_dbdriver.insertPreparedStatement("stmt.add.lfnpfnmap",
0092:                        "INSERT INTO tc_lfnpfnmap(lfnid,pfnid) VALUES (?,?)");
0093:
0094:                /**
0095:                 * QUERY SECTION
0096:                 */
0097:
0098:                this .m_dbdriver
0099:                        .insertPreparedStatement(
0100:                                "stmt.query.lfns",
0101:                                "SELECT DISTINCT p.resourceid, l.namespace,l.name,l.version, p.type FROM "
0102:                                        + "tc_logicaltx l, tc_physicaltx p, tc_lfnpfnmap m "
0103:                                        + "WHERE l.id=m.lfnid and p.id=m.pfnid and p.resourceid like ? and p.type like ? "
0104:                                        + "ORDER BY resourceid,name,namespace,version");
0105:
0106:                this .m_dbdriver
0107:                        .insertPreparedStatement(
0108:                                "stmt.query.pfns",
0109:                                "SELECT p.resourceid, p.pfn, p.type, s.architecture, s.os, "
0110:                                        + "s.osversion, s.glibc "
0111:                                        + "FROM tc_lfnpfnmap m, tc_logicaltx l, tc_physicaltx p, tc_sysinfo s "
0112:                                        + "WHERE l.id=m.lfnid and p.id=m.pfnid and p.archid=s.id and "
0113:                                        + "l.namespace=? and l.name=? and l.version=? and p.resourceid like ? and p.type like ? "
0114:                                        + "ORDER by p.resourceid");
0115:
0116:                this .m_dbdriver
0117:                        .insertPreparedStatement(
0118:                                "stmt.query.resource",
0119:                                "SELECT DISTINCT p.resourceid FROM tc_logicaltx l, tc_physicaltx p, tc_lfnpfnmap m "
0120:                                        + "WHERE l.id=m.lfnid and p.id=m.pfnid and l.namespace like ? and "
0121:                                        + "l.name like ? and l.version like ? and p.type like ? ORDER by resourceid");
0122:
0123:                this .m_dbdriver
0124:                        .insertPreparedStatement(
0125:                                "stmt.query.lfnprofiles",
0126:                                "SELECT pr.namespace, pr.name, pr.value FROM "
0127:                                        + "tc_logicaltx l, tc_lfnprofile pr WHERE "
0128:                                        + "l.id=pr.lfnid and l.namespace=? and l.name=? and l.version=? ORDER BY namespace");
0129:
0130:                this .m_dbdriver
0131:                        .insertPreparedStatement(
0132:                                "stmt.query.pfnprofiles",
0133:                                "SELECT pr.namespace, pr.name, pr.value FROM tc_physicaltx p, tc_pfnprofile pr "
0134:                                        + "WHERE p.id=pr.pfnid and p.pfn=? and p.resourceid like ? and "
0135:                                        + "p.type like ? ORDER BY namespace");
0136:
0137:                this .m_dbdriver
0138:                        .insertPreparedStatement("stmt.query.lfnid",
0139:                                "SELECT id FROM tc_logicaltx WHERE namespace=? and name=? and version=?");
0140:
0141:                this .m_dbdriver
0142:                        .insertPreparedStatement(
0143:                                "stmt.query.sysid",
0144:                                "SELECT id FROM tc_sysinfo WHERE "
0145:                                        + "architecture=? and os=? and osversion=? and glibc=?");
0146:
0147:                this .m_dbdriver
0148:                        .insertPreparedStatement(
0149:                                "stmt.query.pfnid",
0150:                                "SELECT id FROM tc_physicaltx p WHERE p.pfn = ? and p.type like ? and p.resourceid like ?");
0151:
0152:                this .m_dbdriver
0153:                        .insertPreparedStatement("stmt.query.lfnpfnmap",
0154:                                "SELECT * FROM tc_lfnpfnmap WHERE lfnid like ? and pfnid like ?");
0155:
0156:                this .m_dbdriver
0157:                        .insertPreparedStatement(
0158:                                "stmt.query.tc",
0159:                                "SELECT DISTINCT p.resourceid,l.namespace,l.name, l.version, p.pfn, p.type, "
0160:                                        + "s.architecture, s.os, s.osversion, s.glibc "
0161:                                        + "FROM tc_logicaltx l, tc_physicaltx p, tc_lfnpfnmap m, tc_sysinfo s "
0162:                                        + "WHERE l.id=m.lfnid and p.id=m.pfnid and p.archid=s.id "
0163:                                        + "ORDER by p.resourceid, l.name, l.namespace, l.version");
0164:
0165:                this .m_dbdriver
0166:                        .insertPreparedStatement(
0167:                                "stmt.query.lfnprofileid",
0168:                                "SELECT * FROM tc_lfnprofile WHERE "
0169:                                        + "namespace=? and name = ? and value =? and lfnid=?");
0170:
0171:                this .m_dbdriver
0172:                        .insertPreparedStatement(
0173:                                "stmt.query.pfnprofileid",
0174:                                "SELECT * FROM tc_pfnprofile WHERE "
0175:                                        + "namespace=? and name = ? and value =? and pfnid=?");
0176:
0177:                this .m_dbdriver
0178:                        .insertPreparedStatement(
0179:                                "stmt.query.pfnid2",
0180:                                "SELECT p.id FROM tc_physicaltx p,tc_lfnpfnmap m "
0181:                                        + "WHERE m.pfnid=p.id and p.resourceid like ? and p.type like ? and m.lfnid=?");
0182:
0183:                /**
0184:                 * DELETE SECTION
0185:                 */
0186:
0187:                this .m_dbdriver.insertPreparedStatement("stmt.delete.alllfns",
0188:                        "DELETE FROM tc_logicaltx");
0189:
0190:                this .m_dbdriver.insertPreparedStatement(
0191:                        "stmt.delete.alllfnpfnmap", "DELETE FROM tc_lfnpfnmap");
0192:
0193:                this .m_dbdriver.insertPreparedStatement("stmt.delete.allpfns",
0194:                        "DELETE FROM tc_physicaltx");
0195:
0196:                this .m_dbdriver.insertPreparedStatement(
0197:                        "stmt.delete.allsysinfo", "DELETE FROM tc_sysinfo");
0198:
0199:                this .m_dbdriver.insertPreparedStatement(
0200:                        "stmt.delete.allpfnprofile",
0201:                        "DELETE FROM tc_pfnprofile");
0202:
0203:                this .m_dbdriver.insertPreparedStatement(
0204:                        "stmt.delete.alllfnprofile",
0205:                        "DELETE FROM tc_lfnprofile");
0206:
0207:                this .m_dbdriver.insertPreparedStatement(
0208:                        "stmt.delete.byresourceid",
0209:                        "DELETE FROM tc_physicaltx WHERE resourceid=?");
0210:
0211:                this .m_dbdriver.insertPreparedStatement(
0212:                        "stmt.delete.lfnprofiles",
0213:                        "DELETE FROM tc_lfnprofile WHERE lfnid=?");
0214:
0215:                this .m_dbdriver.insertPreparedStatement(
0216:                        "stmt.delete.pfnprofiles",
0217:                        "DELETE FROM tc_pfnprofile WHERE pfnid=?");
0218:
0219:                this .m_dbdriver
0220:                        .insertPreparedStatement(
0221:                                "stmt.delete.lfnprofile",
0222:                                "DELETE FROM tc_lfnprofile WHERE namespace=? and name=? and value=? and lfnid=?");
0223:
0224:                this .m_dbdriver
0225:                        .insertPreparedStatement(
0226:                                "stmt.delete.pfnprofile",
0227:                                "DELETE FROM tc_pfnprofile WHERE namespace=? and name=? and value=? and pfnid=?");
0228:
0229:                this .m_dbdriver
0230:                        .insertPreparedStatement("stmt.delete.bytype",
0231:                                "DELETE FROM tc_physicaltx WHERE type=? and resourceid like ?");
0232:
0233:                this .m_dbdriver
0234:                        .insertPreparedStatement("stmt.delete.lfnpfnmap",
0235:                                "DELETE FROM tc_lfnpfnmap where lfnid like ? and pfnid like ?");
0236:
0237:                this .m_dbdriver.insertPreparedStatement(
0238:                        "stmt.delete.logicaltx",
0239:                        "DELETE FROM tc_logicaltx WHERE id=?");
0240:
0241:                this .m_dbdriver.insertPreparedStatement(
0242:                        "stmt.delete.physicaltx",
0243:                        "DELETE FROM tc_physicaltx WHERE id=?");
0244:
0245:                this .m_dbdriver
0246:                        .insertPreparedStatement(
0247:                                "stmt.delete.sysinfo",
0248:                                "DELETE FROM tc_sysinfo WHERE architecture=? and os=? and osversion=? and glibc=?");
0249:
0250:            }
0251:
0252:            /**
0253:             * Returns TC entries for a particular logical transformation and/or on a
0254:             * particular resource and/or of a particular type.
0255:             * @param namespace String The namespace of the logical transformation.
0256:             * @param name String the name of the logical transformation.
0257:             * @param version String The version of the logical transformation.
0258:             * @param resourceid String The resourceid where the transformation is
0259:             * located. If <B>NULL</B> it returns all resources.
0260:             * @param type TCType The type of the transformation to search for.
0261:             * If <B>NULL</b> it returns all types.
0262:             * @throws Exception
0263:             * @return List Returns a list of TCEntry objects containing the
0264:             * corresponding entries from the TC. Returns null if no entry found.
0265:             *  The profiles returned are lfn profiles, followed by pfn profiles.
0266:             * The code does not check for similar keys between lfn and pfn profiles
0267:             * and all profiles are kept
0268:             * @see org.griphyn.common.classes.TCType
0269:             * @see org.griphyn.common.catalog.TransformationCatalogEntry
0270:             */
0271:
0272:            public List getTCEntries(String namespace, String name,
0273:                    String version, String resourceid, TCType type)
0274:                    throws Exception {
0275:                List resultEntries = null;
0276:                mLogger.log("Trying to get TCEntries for "
0277:                        + Separator.combine(namespace, name, version)
0278:                        + " on resource "
0279:                        + ((resourceid == null) ? "ALL" : resourceid)
0280:                        + " of type "
0281:                        + ((type == null) ? "ALL" : type.toString()),
0282:                        LogManager.DEBUG_MESSAGE_LEVEL);
0283:
0284:                List pfnentries = this .getTCPhysicalNames(namespace, name,
0285:                        version, resourceid, type);
0286:                if (pfnentries != null) {
0287:                    resultEntries = new ArrayList(pfnentries.size());
0288:                    List lfnprofiles = this .getTCLfnProfiles(namespace, name,
0289:                            version);
0290:                    for (int i = 0; i < pfnentries.size() - 1; i++) {
0291:                        String[] pfnresult = (String[]) pfnentries.get(i);
0292:                        String qresourceid = pfnresult[0];
0293:                        String qpfn = pfnresult[1];
0294:                        String qtype = pfnresult[2];
0295:                        SysInfo qsysinfo = new SysInfo(pfnresult[3]);
0296:                        List pfnprofiles = this .getTCPfnProfiles(qpfn,
0297:                                qresourceid, TCType.fromString(qtype));
0298:                        TransformationCatalogEntry tc = new TransformationCatalogEntry(
0299:                                namespace, name, version, pfnresult[0],
0300:                                pfnresult[1], TCType.fromString(pfnresult[2]),
0301:                                null, qsysinfo);
0302:
0303:                        try {
0304:                            if (lfnprofiles != null) {
0305:                                tc.setProfiles(lfnprofiles);
0306:                            }
0307:                            if (pfnprofiles != null) {
0308:                                tc.setProfiles(pfnprofiles);
0309:                            }
0310:                        } catch (RuntimeException e) {
0311:                            mLogger.log(
0312:                                    "Ignoring errors while parsing profile in Transformation Catalog DB"
0313:                                            + " for "
0314:                                            + Separator.combine(namespace,
0315:                                                    name, version), e,
0316:                                    LogManager.WARNING_MESSAGE_LEVEL);
0317:
0318:                        }
0319:
0320:                        resultEntries.add(tc);
0321:                    }
0322:                }
0323:                return resultEntries;
0324:            }
0325:
0326:            /**
0327:             * Returns TC entries for a particular logical transformation and/or on a
0328:             * number of resources and/or of a particular type.
0329:             * @param namespace String The namespace of the logical transformation.
0330:             * @param name String the name of the logical transformation.
0331:             * @param version String The version of the logical transformation.
0332:             * @param resourceids List The List resourceid where the transformation is
0333:             * located. If <b>NULL</b> it returns all resources.
0334:             * @param type TCType The type of the transformation to search for.
0335:             * If <b>NULL</b> it returns all types.
0336:             * @throws Exception
0337:             * @return List Returns a list of TCEntry objects containing the
0338:             * corresponding entries from the TC. Returns null if no entry found.
0339:             * @see org.griphyn.common.classes.TCType
0340:             * @see org.griphyn.common.catalog.TransformationCatalogEntry
0341:             */
0342:
0343:            public List getTCEntries(String namespace, String name,
0344:                    String version, List resourceids, TCType type)
0345:                    throws Exception {
0346:                List results = null;
0347:                if (resourceids != null) {
0348:                    for (Iterator i = resourceids.iterator(); i.hasNext();) {
0349:                        List tempresults = getTCEntries(namespace, name,
0350:                                version, (String) i.next(), type);
0351:                        if (tempresults != null) {
0352:                            if (results == null) {
0353:                                results = new ArrayList();
0354:                            }
0355:                            results.addAll(tempresults);
0356:                        }
0357:                    }
0358:                } else {
0359:                    results = getTCEntries(namespace, name, version,
0360:                            (String) null, type);
0361:                }
0362:                return results;
0363:            }
0364:
0365:            /**
0366:             * List all the contents of the TC in a column format.
0367:             * @throws Exception
0368:             * @return List Returns a List of String Arrays. Each string array contains
0369:             * the resource, lfn, pfn, type, sysinfo and profiles.
0370:             * The last entry in the list is an array of integers which contain the
0371:             * column lengths for pretty print.
0372:             */
0373:
0374:            public List getTC() throws Exception {
0375:                //get the statement.
0376:                PreparedStatement ps = m_dbdriver
0377:                        .getPreparedStatement("stmt.query.tc");
0378:                //execute the query
0379:                ResultSet rs = ps.executeQuery();
0380:                List result = new ArrayList();
0381:                //       int[] count = {0, 0, 0, 0, 0};
0382:                while (rs.next()) {
0383:                    //            if ( result == null ) {
0384:                    //              result = new ArrayList();
0385:                    //        }
0386:                    //get the entries.
0387:                    String resourceid = rs.getString(1);
0388:                    String namespace = rs.getString(2);
0389:                    String name = rs.getString(3);
0390:                    String version = rs.getString(4);
0391:                    //          String lfn = Separator.combine( namespace, name, version );
0392:                    String pfn = rs.getString(5);
0393:                    String type = rs.getString(6);
0394:                    String sysinfo = new SysInfo(rs.getString(7), rs
0395:                            .getString(8), rs.getString(9), rs.getString(10))
0396:                            .toString();
0397:                    List pfnprofiles = this .getTCPfnProfiles(pfn, resourceid,
0398:                            TCType.fromString(type));
0399:                    List lfnprofiles = this .getTCLfnProfiles(namespace, name,
0400:                            version);
0401:                    //         String profiles = null;
0402:                    List allprofiles = null;
0403:                    if (lfnprofiles != null) {
0404:                        allprofiles = new ArrayList(lfnprofiles);
0405:                    }
0406:                    if (pfnprofiles != null) {
0407:                        if (allprofiles == null) {
0408:                            allprofiles = new ArrayList(pfnprofiles);
0409:                        } else {
0410:                            allprofiles.addAll(pfnprofiles);
0411:                        }
0412:                    }
0413:                    //get the profiles and construct a string out of them.
0414:                    //     if ( allprofiles != null ) {
0415:                    //            profiles = ProfileParser.combine( allprofiles );
0416:                    //      }
0417:
0418:                    //add them to the array.
0419:                    TransformationCatalogEntry tcentry = new TransformationCatalogEntry(
0420:                            namespace, name, version, resourceid, pfn, TCType
0421:                                    .fromValue(type), allprofiles, new SysInfo(
0422:                                    sysinfo));
0423:                    //caculate the max length of each column
0424:                    //            columnLength( s, count );
0425:                    //add the array to the list to be returned.
0426:                    result.add(tcentry);
0427:                }
0428:
0429:                rs.close();
0430:                //     if ( result != null ) {
0431:                //set the column length info as the last element
0432:                //       result.add( count );
0433:                // }
0434:                return result;
0435:            }
0436:
0437:            /**
0438:             * Get the list of Resource ID's where a particular transformation may reside.
0439:             * @param   namespace String The namespace of the transformation to search for.
0440:             * @param   name      String The name of the transformation to search for.
0441:             * @param   version   String The version of the transformation to search for.
0442:             * @param   type      TCType The type of the transformation to search for.<BR>
0443:             * (Enumerated type includes SOURCE, STATIC-BINARY, DYNAMIC-BINARY, PACMAN, INSTALLED, SCRIPT)<BR>
0444:             * If <B>NULL</B> it returns all types.
0445:             * @return  List      Returns a list of Resource Id's as strings.
0446:             * Returns <B>NULL</B> if no results found.
0447:             * @throws  Exception NotImplementedException if not implemented
0448:             * @see org.griphyn.common.classes.TCType
0449:             */
0450:
0451:            public List getTCResourceIds(String namespace, String name,
0452:                    String version, TCType type) throws Exception {
0453:                //get the statement
0454:                PreparedStatement ps = m_dbdriver
0455:                        .getPreparedStatement("stmt.query.resource");
0456:                //set the parameters
0457:
0458:                //if name is null then search for all lfn
0459:                if (name == null) {
0460:                    ps.setString(1, "%");
0461:                    ps.setString(2, "%");
0462:                    ps.setString(3, "%");
0463:                } else {
0464:                    ps.setString(1, makeNotNull(namespace));
0465:                    ps.setString(2, makeNotNull(name));
0466:                    ps.setString(3, makeNotNull(version));
0467:                }
0468:                //if type is null then search for all type
0469:                String temp = (type != null) ? type.toString() : "%";
0470:                ps.setString(4, temp);
0471:                //execute the query.
0472:                ResultSet rs = ps.executeQuery();
0473:
0474:                List result = null;
0475:                while (rs.next()) {
0476:                    if (result == null) {
0477:                        result = new ArrayList();
0478:                    }
0479:                    //add the results to the list.
0480:                    result.add(rs.getString(1));
0481:                }
0482:
0483:                rs.close();
0484:                return result;
0485:            }
0486:
0487:            /**
0488:             * Get the list of PhysicalNames for a particular transformation on a
0489:             * site/sites for a particular type/types;
0490:             * @param  namespace  String The namespace of the transformation to search for.
0491:             * @param  name       String The name of the transformation to search for.
0492:             * @param  version    String The version of the transformation to search for.
0493:             * @param  resourceid String The id of the resource on which you want to
0494:             * search. <BR>
0495:             *                    If <B>NULL</B> then returns entries on all resources
0496:             * @param  type       TCType The type of the transformation to search for. <BR>
0497:             *                        (Enumerated type includes source, binary,
0498:             * dynamic-binary, pacman, installed)<BR>
0499:             *                        If <B>NULL</B> then returns entries of all types.
0500:             * @throws Exception  NotImplementedException if not implemented.
0501:             * @return List       Returns a list of String Arrays.
0502:             *                    Each array contains the resourceid, the physical
0503:             * transformation, the type of the tr and the systeminfo.
0504:             *                    The last entry in the List is a int array containing t
0505:             * he column lengths for pretty print.
0506:             *                    Returns <B>NULL</B> if no results found.
0507:             * @see org.griphyn.common.classes.TCType
0508:             * @see org.griphyn.common.classes.SysInfo
0509:             */
0510:
0511:            public List getTCPhysicalNames(String namespace, String name,
0512:                    String version, String resourceid, TCType type) throws
0513:
0514:            Exception {
0515:                int[] count = { 0, 0, 0 };
0516:                PreparedStatement ps = m_dbdriver
0517:                        .getPreparedStatement("stmt.query.pfns");
0518:
0519:                ps.setString(1, makeNotNull(namespace));
0520:                ps.setString(2, makeNotNull(name));
0521:                ps.setString(3, makeNotNull(version));
0522:
0523:                String temp = (resourceid != null) ? resourceid : "%";
0524:                ps.setString(4, temp);
0525:
0526:                temp = (type != null) ? type.toString() : "%";
0527:                ps.setString(5, temp);
0528:
0529:                ResultSet rs = ps.executeQuery();
0530:
0531:                List result = null;
0532:                while (rs.next()) {
0533:                    if (result == null) {
0534:                        result = new ArrayList();
0535:                    }
0536:                    String[] s = {
0537:                            rs.getString(1),
0538:                            rs.getString(2),
0539:                            rs.getString(3),
0540:                            (new SysInfo(rs.getString(4), rs.getString(5), rs
0541:                                    .getString(6), rs.getString(7))).toString() };
0542:                    result.add(s);
0543:                    columnLength(s, count);
0544:                }
0545:                rs.close();
0546:                if (result != null) {
0547:                    result.add(count);
0548:                }
0549:                return result;
0550:            }
0551:
0552:            /**
0553:             * Get the list of LogicalNames available on a particular resource.
0554:             * @param resourceid String The id of the resource on which you want to search
0555:             * @param type       TCType The type of the transformation to search for. <BR>
0556:             *                  (Enumerated type includes source, binary, dynamic-binary,
0557:             *  pacman, installed)<BR>
0558:             *                   If <B>NULL</B> then return logical name for all types.
0559:             * @throws Exception NotImpementedException if not implemented
0560:             * @return List      Returns a list of String Arrays.
0561:             *                   Each array contains the resourceid, logical transformation
0562:             *                   in the format namespace::name:version and type.
0563:             *                   The last entry in the list is an array of integers
0564:             * specifying the column length for pretty print.
0565:             *                   Returns <B>NULL</B> if no results found.
0566:             */
0567:
0568:            public List getTCLogicalNames(String resourceid, TCType type)
0569:                    throws Exception {
0570:                int[] count = { 0, 0 };
0571:                PreparedStatement ps = null;
0572:                ps = this .m_dbdriver.getPreparedStatement("stmt.query.lfns");
0573:                String temp = (resourceid == null) ? "%" : resourceid;
0574:                ps.setString(1, temp);
0575:                temp = (type == null) ? "%" : type.toString();
0576:                ps.setString(2, temp);
0577:                ResultSet rs = ps.executeQuery();
0578:
0579:                List result = null;
0580:                while (rs.next()) {
0581:                    if (result == null) {
0582:                        result = new ArrayList();
0583:                    }
0584:                    String[] st = {
0585:                            rs.getString(1),
0586:                            Separator.combine(rs.getString(2), rs.getString(3),
0587:                                    rs.getString(4)), rs.getString(5) };
0588:                    columnLength(st, count);
0589:                    result.add(st);
0590:                }
0591:                rs.close();
0592:                if (result != null) {
0593:                    result.add(count);
0594:                }
0595:                return result;
0596:            }
0597:
0598:            /**
0599:             * Get the list of Profiles associated with a particular logical transformation.
0600:             * @param namespace  String The namespace of the transformation to search for.
0601:             * @param name       String The name of the transformation to search for.
0602:             * @param version    String The version of the transformation to search for.
0603:             * @throws Exception NotImplementedException if not implemented.
0604:             * @return List      Returns a list of Profile Objects containing profiles
0605:             * assocaited with the transformation.
0606:             *                   Returns <B>NULL</B> if no profiles found.
0607:             * @see org.griphyn.cPlanner.classes.Profile
0608:             */
0609:
0610:            public List getTCLfnProfiles(String namespace, String name,
0611:                    String version) throws Exception {
0612:                PreparedStatement ps = this .m_dbdriver
0613:                        .getPreparedStatement("stmt.query.lfnprofiles");
0614:                ps.setString(1, makeNotNull(namespace));
0615:                ps.setString(2, makeNotNull(name));
0616:                ps.setString(3, makeNotNull(version));
0617:                ResultSet rs = ps.executeQuery();
0618:
0619:                List result = null;
0620:                while (rs.next()) {
0621:                    if (result == null) {
0622:                        result = new ArrayList();
0623:                    }
0624:
0625:                    Profile p = new Profile(rs.getString(1), rs.getString(2),
0626:                            rs.getString(3));
0627:                    result.add(p);
0628:                }
0629:                rs.close();
0630:
0631:                return result;
0632:            }
0633:
0634:            /**
0635:             * Get the list of Profiles associated with a particular physical transformation.
0636:             * @param pfn        The physical file name to search the transformation by.
0637:             * @param resourceid String The id of the resource on which you want to search.
0638:             * @param type       TCType The type of the transformation to search for. <br>
0639:             *                   (Enumerated type includes source, binary, dynamic-binary,
0640:             *  pacman, installed)<br>
0641:             * @throws Exception NotImplementedException if not implemented.
0642:             * @return List      Returns a list of Profile Objects containing profiles
0643:             * assocaited with the transformation.
0644:             *                   Returns <B>NULL</B> if no profiless found.
0645:             * @see org.griphyn.cPlanner.classes.Profile
0646:             */
0647:
0648:            public List getTCPfnProfiles(String pfn, String resourceid,
0649:                    TCType type) throws Exception {
0650:                PreparedStatement ps = this .m_dbdriver
0651:                        .getPreparedStatement("stmt.query.pfnprofiles");
0652:                ps.setString(1, pfn);
0653:                String temp = (resourceid != null) ? resourceid : "%";
0654:                ps.setString(2, temp);
0655:
0656:                temp = (type != null) ? type.toString() : "%";
0657:                ps.setString(3, temp);
0658:
0659:                ResultSet rs = ps.executeQuery();
0660:
0661:                List result = null;
0662:                while (rs.next()) {
0663:                    if (result == null) {
0664:                        result = new ArrayList();
0665:                    }
0666:                    Profile p = new Profile(rs.getString(1), rs.getString(2),
0667:                            rs.getString(3));
0668:                    result.add(p);
0669:                }
0670:                rs.close();
0671:
0672:                return result;
0673:            }
0674:
0675:            /**
0676:             *  ADDITIONS
0677:             */
0678:
0679:            /**
0680:             * Add multiple TCEntries to the Catalog.
0681:             * @param tcentry List Takes a list of TCEntry objects as input
0682:             * @throws Exception
0683:             * @return boolean Return true if succesful, false if error. Exception is
0684:             * thrown when error occurs.
0685:             * @see org.griphyn.common.catalog.TransformationCatalogEntry
0686:             */
0687:            public boolean addTCEntry(List tcentry) throws Exception {
0688:                for (int i = 0; i < tcentry.size(); i++) {
0689:                    TransformationCatalogEntry entry = ((TransformationCatalogEntry) tcentry
0690:                            .get(i));
0691:                    this .addTCEntry(entry);
0692:                }
0693:                return true;
0694:            }
0695:
0696:            /**
0697:             * Add a single TCEntry to the Catalog. Exception is thrown when error
0698:             * occurs.
0699:             *
0700:             * @param entry a single {@link org.griphyn.common.catalog.TransformationCatalogEntry}
0701:             * object as input.
0702:             *
0703:             * @return boolean Return true if succesful, false if error.
0704:             *
0705:             * @throws Exception
0706:             * @see org.griphyn.common.catalog.TransformationCatalogEntry
0707:             */
0708:            public boolean addTCEntry(TransformationCatalogEntry entry)
0709:                    throws Exception {
0710:                this .addTCEntry(entry.getLogicalNamespace(), entry
0711:                        .getLogicalName(), entry.getLogicalVersion(), entry
0712:                        .getPhysicalTransformation(), entry.getType(), entry
0713:                        .getResourceId(), null, entry.getProfiles(), entry
0714:                        .getSysInfo());
0715:                return true;
0716:            }
0717:
0718:            /**
0719:             * Add a single TCEntry to the Catalog. Exception is thrown when error
0720:             * occurs. This method is a hack and wont commit the additions to the
0721:             * backend catalog
0722:             *
0723:             * @param entry a single {@link org.griphyn.common.catalog.TransformationCatalogEntry}
0724:             * object as input.
0725:             * @param write boolean to commit additions to backend catalog.
0726:             * @return boolean Return true if succesful, false if error.
0727:             *
0728:             * @throws Exception
0729:             * @see org.griphyn.common.catalog.TransformationCatalogEntry
0730:             */
0731:            public boolean addTCEntry(TransformationCatalogEntry entry,
0732:                    boolean write) throws Exception {
0733:                this .addTCEntry(entry.getLogicalNamespace(), entry
0734:                        .getLogicalName(), entry.getLogicalVersion(), entry
0735:                        .getPhysicalTransformation(), entry.getType(), entry
0736:                        .getResourceId(), null, entry.getProfiles(), entry
0737:                        .getSysInfo(), write);
0738:                return true;
0739:            }
0740:
0741:            /**
0742:             * Add an single entry into the transformation catalog.
0743:             *
0744:             * @param namespace    String The namespace of the transformation to be
0745:             * added (Can be null)
0746:             * @param name         String The name of the transformation to be added.
0747:             *
0748:             * @param version      String The version of the transformation to be added.
0749:             * (Can be null)
0750:             * @param physicalname String The physical name/location of the transformation
0751:             *  to be added.
0752:             *  @param type        TCType  The type of the physical transformation.
0753:             * @param resourceid   String The resource location id where the
0754:             * transformation is located.
0755:             * @param lfnprofiles     List   The List of Profile objects associated with
0756:             * a Logical Transformation. (can be null)
0757:             * @param pfnprofiles     List   The List of Profile objects associated with
0758:             * a Physical Transformation. (can be null)
0759:             * @param system    SysInfo  The System information associated with a
0760:             * physical transformation.
0761:             * @throws Exception
0762:             * @return boolean   Returns true if succesfully added, returns false if
0763:             * error and throws exception.
0764:             * @see org.griphyn.common.catalog.TransformationCatalogEntry
0765:             * @see org.griphyn.common.classes.SysInfo
0766:             * @see org.griphyn.cPlanner.classes.Profile
0767:             */
0768:            public boolean addTCEntry(String namespace, String name,
0769:                    String version, String physicalname, TCType type,
0770:                    String resourceid, List lfnprofiles, List pfnprofiles,
0771:                    SysInfo system) throws Exception {
0772:                return this .addTCEntry(namespace, name, version, physicalname,
0773:                        type, resourceid, lfnprofiles, pfnprofiles, system,
0774:                        true);
0775:
0776:            }
0777:
0778:            /**
0779:             * Add an single entry into the transformation catalog.
0780:             *
0781:             * @param namespace    String The namespace of the transformation to be
0782:             * added (Can be null)
0783:             * @param name         String The name of the transformation to be added.
0784:             *
0785:             * @param version      String The version of the transformation to be added.
0786:             * (Can be null)
0787:             * @param physicalname String The physical name/location of the transformation
0788:             *  to be added.
0789:             *  @param type        TCType  The type of the physical transformation.
0790:             * @param resourceid   String The resource location id where the
0791:             * transformation is located.
0792:             * @param lfnprofiles     List   The List of Profile objects associated with
0793:             * a Logical Transformation. (can be null)
0794:             * @param pfnprofiles     List   The List of Profile objects associated with
0795:             * a Physical Transformation. (can be null)
0796:             * @param system    SysInfo  The System information associated with a
0797:             * physical transformation.
0798:             * @param write boolean to commit changes to the backend catalog
0799:             * @throws Exception
0800:             * @return boolean   Returns true if succesfully added, returns false if
0801:             * error and throws exception.
0802:             * @see org.griphyn.common.catalog.TransformationCatalogEntry
0803:             * @see org.griphyn.common.classes.SysInfo
0804:             * @see org.griphyn.cPlanner.classes.Profile
0805:             */
0806:            public boolean addTCEntry(String namespace, String name,
0807:                    String version, String physicalname, TCType type,
0808:                    String resourceid, List lfnprofiles, List pfnprofiles,
0809:                    SysInfo system, boolean write) throws Exception {
0810:                if (!write)
0811:                    return false;
0812:                //ADD LFN
0813:                //try to add the logical name of the transformation
0814:                long lfnid = -1;
0815:                if ((lfnid = this .getLogicalId(namespace, name, version)) == -1) {
0816:                    //the lfn does not exist so we can go ahead
0817:                    if ((lfnid = this .addLogicalTr(namespace, name, version)) == -1) {
0818:                        return false;
0819:                    }
0820:                } else {
0821:                    mLogger.log("Logical tr entry already exists",
0822:                            LogManager.DEBUG_MESSAGE_LEVEL);
0823:                }
0824:
0825:                //ADD SYSINFO
0826:                //now since the lfn is in lets check if the architecture info is there.
0827:                long archid = -1;
0828:                if ((archid = this .getSysInfoId(system)) == -1) {
0829:                    //this means sytem information does not exist and we have to add it.
0830:                    if ((archid = this .addSysInfo(system)) == -1) {
0831:                        return false;
0832:                    }
0833:                    // archid = this.getSysInfoId( system );
0834:                } else {
0835:                    mLogger.log("Sysinfo entry already exists",
0836:                            LogManager.DEBUG_MESSAGE_LEVEL);
0837:                }
0838:
0839:                //ADD PFN
0840:                //now since the sysinfo is in the map lets add the pfn to the table.
0841:                long pfnid = -1;
0842:                if ((pfnid = this .getPhysicalId(physicalname, type, resourceid)) == -1) {
0843:                    //since pfn is not in the database
0844:                    if ((pfnid = this .addPhysicalTr(physicalname, resourceid,
0845:                            type, archid)) == -1) {
0846:                        return false;
0847:                    }
0848:                } else {
0849:                    mLogger.log("PFN entry already exists",
0850:                            LogManager.DEBUG_MESSAGE_LEVEL);
0851:                }
0852:
0853:                //ADD LFN-PFN MAP
0854:                //now since the pfn is in lets check the lfnpfn map if it is correct
0855:                if (this .checkLfnPfnMap(lfnid, pfnid) == -1) {
0856:                    //entry does not exist and we need to add it
0857:                    PreparedStatement ps = this .m_dbdriver
0858:                            .getPreparedStatement("stmt.add.lfnpfnmap");
0859:                    ps.setLong(1, lfnid);
0860:                    ps.setLong(2, pfnid);
0861:                    try {
0862:                        ps.executeUpdate();
0863:                    } catch (SQLException e) {
0864:                        mLogger.log(
0865:                                "Unable to add lfn-pfn mapping entry to the TC :"
0866:                                        + e.getMessage(),
0867:                                LogManager.ERROR_MESSAGE_LEVEL);
0868:                        m_dbdriver
0869:                                .cancelPreparedStatement("stmt.add.lfnpfnmap");
0870:                        m_dbdriver.rollback();
0871:                        return false;
0872:                    }
0873:                } else {
0874:                    mLogger.log("LFN - PFN mapping entry already exists",
0875:                            LogManager.DEBUG_MESSAGE_LEVEL);
0876:                }
0877:
0878:                //lfn pfn mapping is done now lets move to profiles.
0879:
0880:                //ADD PFN PROFILES
0881:                if (pfnprofiles != null) {
0882:                    for (Iterator i = pfnprofiles.iterator(); i.hasNext();) {
0883:                        Profile p = (Profile) i.next();
0884:                        this .addProfile(p, pfnid, true);
0885:                    }
0886:                }
0887:
0888:                //ADD LFN PROFILES
0889:                if (lfnprofiles != null) {
0890:                    for (Iterator i = lfnprofiles.iterator(); i.hasNext();) {
0891:                        Profile p = (Profile) i.next();
0892:                        this .addProfile(p, lfnid, false);
0893:                    }
0894:                }
0895:                //everything seems to have gone ok.
0896:                //so lets commit
0897:
0898:                m_dbdriver.commit();
0899:
0900:                mLogger.log("Added TC entry", LogManager.DEBUG_MESSAGE_LEVEL);
0901:                return true;
0902:            }
0903:
0904:            /**
0905:             *Add additional profile to a logical transformation .
0906:             * @param namespace String The nsamespace of the transformation to be added. (can be null)
0907:             * @param name      String The name of the transformation to be added.
0908:             * @param version   String The version of the transformation to be added. (can be null)
0909:             * @param profiles  List   The List of Profile objects that are to be added to the transformation.
0910:             * @return boolean Returns true if success, false if error.
0911:             * @throws Exception
0912:             * @see org.griphyn.cPlanner.classes.Profile
0913:             */
0914:            public boolean addTCLfnProfile(String namespace, String name,
0915:                    String version, List profiles) throws Exception {
0916:                long lfnid = -1;
0917:                if ((lfnid = getLogicalId(namespace, name, version)) != -1) {
0918:                    if (profiles != null) {
0919:                        for (Iterator i = profiles.iterator(); i.hasNext();) {
0920:                            Profile p = (Profile) i.next();
0921:                            this .addProfile(p, lfnid, false);
0922:                        }
0923:                    }
0924:                } else {
0925:                    mLogger
0926:                            .log(
0927:                                    "The lfn does not exist. Cannot add profiles to non existent lfn",
0928:                                    LogManager.ERROR_MESSAGE_LEVEL);
0929:                }
0930:                m_dbdriver.commit();
0931:                return true;
0932:            }
0933:
0934:            /**
0935:             * Add additional profile to a physical transformation.
0936:             * @param pfn  String       The physical name of the transformation
0937:             * @param type      TCType The type of transformation that the profile is associated with.
0938:             * @param resourceid String The resource on which the physical transformation exists
0939:             * @param profiles  List   The List of Profile objects that are to be added to the transformation.
0940:             * @return boolean Returns true for success, false for error.
0941:             * @throws Exception
0942:             * @see org.griphyn.cPlanner.classes.Profile
0943:             */
0944:            public boolean addTCPfnProfile(String pfn, TCType type,
0945:                    String resourceid, List profiles) throws Exception {
0946:                long pfnid = -1;
0947:                if ((pfnid = getPhysicalId(pfn, type, resourceid)) != -1) {
0948:                    long profileid = -1;
0949:                    if (profiles != null) {
0950:                        for (Iterator i = profiles.iterator(); i.hasNext();) {
0951:                            Profile p = (Profile) i.next();
0952:                            this .addProfile(p, pfnid, true);
0953:                        }
0954:                    }
0955:                    m_dbdriver.commit();
0956:                    return true;
0957:                } else {
0958:                    mLogger
0959:                            .log(
0960:                                    "The pfn does not exist. Cannot add profiles to a non existent pfn",
0961:                                    LogManager.ERROR_MESSAGE_LEVEL);
0962:                    return false;
0963:                }
0964:            }
0965:
0966:            /**
0967:             * DELETIONS
0968:             */
0969:
0970:            /**
0971:             * Delete all entries in the transformation catalog for a give logical tranformation and/or on a resource and/or of a particular type
0972:             * @param namespace   String The namespace of the transformation to be added. (can be null)
0973:             * @param name        String The name of the transformation to be added.
0974:             * @param version     String The version of the transformation to be added. ( can be null)
0975:             * @param resourceid String The resource id for which the transformation is to be deleted.
0976:             *                          If <B>NULL</B> then transformation on all resource are deleted
0977:             * @param type TCType The type of the transformation. If <B>NULL</B> then all types are deleted for the transformation.
0978:             * @throws Exception
0979:             * @return boolean Returns true if success , false if there is any error.
0980:             * @see org.griphyn.common.classes.TCType
0981:             */
0982:
0983:            public boolean deleteTCbyLogicalName(String namespace, String name,
0984:                    String version, String resourceid, TCType type)
0985:                    throws Exception {
0986:
0987:                long lfnid;
0988:                long[] pfnids;
0989:                if (name != null) {
0990:                    //humm this is where we start.
0991:                    //get the logical transformation id first.
0992:                    if ((lfnid = this .getLogicalId(namespace, name, version)) != -1) {
0993:                        //this means lfn is there now we try to get the list of pfnids.
0994:                        PreparedStatement ps = this .m_dbdriver
0995:                                .getPreparedStatement("stmt.query.pfnid2");
0996:                        String temp = (resourceid == null) ? "%" : resourceid;
0997:                        ps.setString(1, temp);
0998:                        temp = (type == null) ? "%" : type.toString();
0999:                        ps.setString(2, temp);
1000:                        ps.setLong(3, lfnid);
1001:                        ResultSet rst = ps.executeQuery();
1002:                        List results = null;
1003:                        while (rst.next()) {
1004:                            if (results == null) {
1005:                                results = new ArrayList();
1006:                            }
1007:                            results.add(new Long(rst.getLong(1)));
1008:                        }
1009:                        if (results != null) {
1010:                            pfnids = new long[results.size()];
1011:                            for (int i = 0; i < results.size(); i++) {
1012:                                pfnids[i] = ((Long) results.get(i)).longValue();
1013:                            }
1014:                        } else {
1015:                            long[] t = { -1 };
1016:                            pfnids = t;
1017:                        }
1018:                        if (pfnids[0] != -1) {
1019:                            //this means there are pfns.
1020:                            ps = this .m_dbdriver
1021:                                    .getPreparedStatement("stmt.delete.lfnpfnmap");
1022:                            ps.setLong(1, lfnid);
1023:                            for (int i = 0; i < pfnids.length; i++) {
1024:                                ps.setLong(2, pfnids[i]);
1025:                                int rs = ps.executeUpdate();
1026:                                if (rs < 1) {
1027:                                    mLogger
1028:                                            .log(
1029:                                                    "No entries found in the lfnpfnmap that could be deleted.",
1030:                                                    LogManager.ERROR_MESSAGE_LEVEL);
1031:                                } else {
1032:                                    mLogger.log("Deleted " + rs
1033:                                            + " mappings  from the lfnpfnmap",
1034:                                            LogManager.DEBUG_MESSAGE_LEVEL);
1035:                                }
1036:                            }
1037:                            //now since the map is deleted we check if the any lfn pfn mappings exist for that lfn
1038:                            ps = this .m_dbdriver
1039:                                    .getPreparedStatement("stmt.query.lfnpfnmap");
1040:                            ps.setLong(1, lfnid);
1041:                            ps.setString(2, "%");
1042:                            rst = ps.executeQuery();
1043:                            int count = 0;
1044:                            while (rst.next()) {
1045:                                count++;
1046:                            }
1047:                            if (count == 0) {
1048:                                //no more pfns are mapped to the same lfn.
1049:                                //It is safe to delete the lfn;
1050:                                PreparedStatement pst = this .m_dbdriver
1051:                                        .getPreparedStatement("stmt.delete.logicaltx");
1052:                                pst.setLong(1, lfnid);
1053:                                int rs = pst.executeUpdate();
1054:                                if (rs < 1) {
1055:                                    //this should not happen cause we checked that it existed before we started the work here.
1056:                                    mLogger.log("No entry for the lfn exists",
1057:                                            LogManager.ERROR_MESSAGE_LEVEL);
1058:                                } else {
1059:                                    mLogger.log(
1060:                                            "Deleted the logical transformation "
1061:                                                    + Separator.combine(
1062:                                                            namespace, name,
1063:                                                            version),
1064:                                            LogManager.DEBUG_MESSAGE_LEVEL);
1065:                                }
1066:                            } else {
1067:                                mLogger.log("Logical transformation "
1068:                                        + Separator.combine(namespace, name,
1069:                                                version) + " is mapped to "
1070:                                        + count + " other pfns.",
1071:                                        LogManager.DEBUG_MESSAGE_LEVEL);
1072:                                mLogger
1073:                                        .log(
1074:                                                "***Wont delete logical transformation***",
1075:                                                LogManager.DEBUG_MESSAGE_LEVEL);
1076:                            }
1077:                            //now that we have handled the lfns lets handle the pfns.
1078:                            for (int i = 0; i < pfnids.length; i++) {
1079:                                ps.clearParameters();
1080:                                ps.setString(1, "%");
1081:                                ps.setLong(2, pfnids[i]);
1082:                                rst = ps.executeQuery();
1083:                                count = 0;
1084:                                while (rst.next()) {
1085:                                    count++;
1086:                                }
1087:                                if (count == 0) {
1088:                                    //this means this pfn is not mapped to any other lfn and is safe to be deleted
1089:                                    PreparedStatement pst = this .m_dbdriver
1090:                                            .getPreparedStatement("stmt.delete.physicaltx");
1091:                                    pst.setLong(1, pfnids[i]);
1092:                                    int rs = pst.executeUpdate();
1093:                                    if (rs < 1) {
1094:                                        //this should not happen cause we checked that pfn existed before we started the work here.
1095:                                        mLogger.log(
1096:                                                "No entry for the pfn exists",
1097:                                                LogManager.ERROR_MESSAGE_LEVEL);
1098:                                    } else {
1099:                                        mLogger.log(
1100:                                                "Deleted the physical transformation with id "
1101:                                                        + pfnids[i],
1102:                                                LogManager.DEBUG_MESSAGE_LEVEL);
1103:                                    }
1104:                                } else {
1105:                                    mLogger.log("Pfn with id " + pfnids[i]
1106:                                            + " is mapped with " + count
1107:                                            + " other lfns.",
1108:                                            LogManager.DEBUG_MESSAGE_LEVEL);
1109:                                    mLogger
1110:                                            .log(
1111:                                                    "***Wont delete physical transformation***",
1112:                                                    LogManager.DEBUG_MESSAGE_LEVEL);
1113:                                }
1114:                            }
1115:                            //hopefully everything went ok so lets commit
1116:                            this .m_dbdriver.commit();
1117:                            return true;
1118:                        } else {
1119:                            mLogger.log(
1120:                                    "No pfns associated with lfn "
1121:                                            + Separator.combine(namespace,
1122:                                                    name, version)
1123:                                            + " of type "
1124:                                            + ((type == null) ? "ALL" : type
1125:                                                    .toString())
1126:                                            + " on resource "
1127:                                            + ((resourceid == null) ? "ALL"
1128:                                                    : resourceid) + " exist",
1129:                                    LogManager.ERROR_MESSAGE_LEVEL);
1130:                            return false;
1131:                        }
1132:                    } else {
1133:                        //logical transformation does not exist
1134:                        mLogger.log("The logical transformation "
1135:                                + Separator.combine(namespace, name, version)
1136:                                + " does not exist.",
1137:                                LogManager.ERROR_MESSAGE_LEVEL);
1138:                        return false;
1139:                    }
1140:                } else {
1141:                    mLogger.log(
1142:                            "The logical transformation name cannot be null.",
1143:                            LogManager.ERROR_MESSAGE_LEVEL);
1144:                    return false;
1145:                }
1146:
1147:            }
1148:
1149:            /**
1150:             * Delete all entries in the transformation catalog for pair of logical and physical transformation.
1151:             * @param physicalname String The physical name of the transformation
1152:             * @param namespace String The namespace assocaited in the logical name of the transformation.
1153:             * @param name String The name of the logical transformation.
1154:             * @param version String The version number of the logical transformation.
1155:             * @param resourceid String The resource on which the transformation is to be deleted.
1156:             *                          If <B>NULL</B> then it searches all the resource id.
1157:             * @param type TCType The type of transformation. If <B>NULL</B> then it search and deletes entries for all types.
1158:             * @throws Exception
1159:             * @return boolean Returns true if sucess, false if any error occurs.
1160:             * @see org.griphyn.common.classes.TCType
1161:             */
1162:
1163:            public boolean deleteTCbyPhysicalName(String physicalname,
1164:                    String namespace, String name, String version,
1165:                    String resourceid, TCType type) throws Exception {
1166:                long lfnid;
1167:                long[] pfnids;
1168:                if (name != null) {
1169:                    if (physicalname != null) {
1170:                        //humm this is where we start.
1171:                        //get the logical transformation id first.
1172:                        if ((lfnid = this 
1173:                                .getLogicalId(namespace, name, version)) != -1) {
1174:                            //this means lfn is there now we try to get the list of pfnids.
1175:                            pfnids = this .getPhysicalIds(physicalname, type,
1176:                                    resourceid);
1177:                            if (pfnids[0] != -1) {
1178:                                //this means there are pfns.
1179:                                PreparedStatement ps = this .m_dbdriver
1180:                                        .getPreparedStatement("stmt.delete.lfnpfnmap");
1181:                                ps.setLong(1, lfnid);
1182:                                for (int i = 0; i < pfnids.length; i++) {
1183:                                    ps.setLong(2, pfnids[i]);
1184:                                    int rs = ps.executeUpdate();
1185:                                    if (rs < 1) {
1186:                                        mLogger
1187:                                                .log(
1188:                                                        "No entries found in the lfnpfnmap that could be deleted.",
1189:                                                        LogManager.ERROR_MESSAGE_LEVEL);
1190:                                    } else {
1191:                                        mLogger
1192:                                                .log(
1193:                                                        "Deleted "
1194:                                                                + rs
1195:                                                                + " mappings  from the lfnpfnmap",
1196:                                                        LogManager.DEBUG_MESSAGE_LEVEL);
1197:                                    }
1198:                                }
1199:                                //now since the map is deleted we check if the any lfn pfn mappings exist for that lfn
1200:                                ps = this .m_dbdriver
1201:                                        .getPreparedStatement("stmt.query.lfnpfnmap");
1202:                                ps.setLong(1, lfnid);
1203:                                ps.setString(2, "%");
1204:                                ResultSet rst = ps.executeQuery();
1205:                                int count = 0;
1206:                                while (rst.next()) {
1207:                                    count++;
1208:                                }
1209:                                if (count == 0) {
1210:                                    //no more pfns are mapped to the same lfn.
1211:                                    //It is safe to delete the lfn;
1212:                                    PreparedStatement pst = this .m_dbdriver
1213:                                            .getPreparedStatement("stmt.delete.logicaltx");
1214:                                    pst.setLong(1, lfnid);
1215:                                    int rs = pst.executeUpdate();
1216:                                    if (rs < 1) {
1217:                                        //this should not happen cause we checked that it existed before we started the work here.
1218:                                        mLogger.log(
1219:                                                "No entry for the lfn exists",
1220:                                                LogManager.DEBUG_MESSAGE_LEVEL);
1221:                                    } else {
1222:                                        mLogger.log(
1223:                                                "Deleted the logical transformation "
1224:                                                        + Separator.combine(
1225:                                                                namespace,
1226:                                                                name, version),
1227:                                                LogManager.DEBUG_MESSAGE_LEVEL);
1228:                                    }
1229:                                } else {
1230:                                    mLogger.log("Logical transformation "
1231:                                            + Separator.combine(namespace,
1232:                                                    name, version)
1233:                                            + " is mapped to " + count
1234:                                            + " other pfns.",
1235:                                            LogManager.DEBUG_MESSAGE_LEVEL);
1236:                                    mLogger
1237:                                            .log(
1238:                                                    "***Wont delete logical transformation***",
1239:                                                    LogManager.DEBUG_MESSAGE_LEVEL);
1240:                                }
1241:                                //now that we have handled the lfns lets handle the pfns.
1242:                                for (int i = 0; i < pfnids.length; i++) {
1243:                                    ps.clearParameters();
1244:                                    ps.setString(1, "%");
1245:                                    ps.setLong(2, pfnids[i]);
1246:                                    rst = ps.executeQuery();
1247:                                    count = 0;
1248:                                    while (rst.next()) {
1249:                                        count++;
1250:                                    }
1251:                                    if (count == 0) {
1252:                                        //this means this pfn is not mapped to any other lfn and is safe to be deleted
1253:                                        PreparedStatement pst = this .m_dbdriver
1254:                                                .getPreparedStatement("stmt.delete.physicaltx");
1255:                                        pst.setLong(1, pfnids[i]);
1256:                                        int rs = pst.executeUpdate();
1257:                                        if (rs < 1) {
1258:                                            //this should not happen cause we checked that pfn existed before we started the work here.
1259:                                            mLogger
1260:                                                    .log(
1261:                                                            "No entry for the pfn exists",
1262:                                                            LogManager.DEBUG_MESSAGE_LEVEL);
1263:                                        } else {
1264:                                            mLogger
1265:                                                    .log(
1266:                                                            "Deleted the physical transformation "
1267:                                                                    + physicalname,
1268:                                                            LogManager.DEBUG_MESSAGE_LEVEL);
1269:                                        }
1270:                                    } else {
1271:                                        mLogger.log("Pfn " + physicalname
1272:                                                + " with id " + pfnids[i]
1273:                                                + " is mapped with " + count
1274:                                                + " other lfns.",
1275:                                                LogManager.DEBUG_MESSAGE_LEVEL);
1276:                                        mLogger
1277:                                                .log(
1278:                                                        "***Wont delete physical transformation***",
1279:                                                        LogManager.DEBUG_MESSAGE_LEVEL);
1280:                                    }
1281:                                }
1282:                                //hopefully everything went ok so lets commit
1283:                                this .m_dbdriver.commit();
1284:                                return true;
1285:                            } else {
1286:                                mLogger.log("The physical transformation "
1287:                                        + physicalname
1288:                                        + " of type "
1289:                                        + ((type == null) ? "ALL" : type
1290:                                                .toString())
1291:                                        + " on resource "
1292:                                        + ((resourceid == null) ? "ALL"
1293:                                                : resourceid),
1294:                                        LogManager.ERROR_MESSAGE_LEVEL);
1295:                                mLogger.log("does not exist",
1296:                                        LogManager.ERROR_MESSAGE_LEVEL);
1297:                                return false;
1298:                            }
1299:                        } else {
1300:                            //logical transformation does not exist
1301:                            mLogger.log("The logical transformation "
1302:                                    + Separator.combine(namespace, name,
1303:                                            version) + " does not exist.",
1304:                                    LogManager.ERROR_MESSAGE_LEVEL);
1305:                            return false;
1306:                        }
1307:                    } else {
1308:                        mLogger.log(
1309:                                "The physical transformation cannot be null.",
1310:                                LogManager.ERROR_MESSAGE_LEVEL);
1311:                        return false;
1312:                    }
1313:                } else {
1314:                    mLogger.log(
1315:                            "The logical transformation name cannot be null.",
1316:                            LogManager.ERROR_MESSAGE_LEVEL);
1317:                    return false;
1318:                }
1319:            }
1320:
1321:            /**
1322:             * Deletes entries from the catalog which have a particular system information.
1323:             * @param sysinfo SysInfo The System Information by which you want to delete
1324:             * @throws Exception
1325:             * @return boolean Returns true for success, false if any error occurs.
1326:             * @see org.griphyn.common.classes.SysInfo
1327:             */
1328:            public boolean deleteTCbySysInfo(SysInfo sysinfo) throws Exception {
1329:                if (sysinfo == null) {
1330:                    mLogger.log("The system information cannot be null",
1331:                            LogManager.ERROR_MESSAGE_LEVEL);
1332:                    return false;
1333:                }
1334:                PreparedStatement ps = this .m_dbdriver
1335:                        .getPreparedStatement("stmt.delete.sysinfo");
1336:                ps.setString(1, this .makeNotNull(sysinfo.getArch().toString()));
1337:                ps.setString(2, this .makeNotNull(sysinfo.getOs().toString()));
1338:                ps.setString(3, this .makeNotNull(sysinfo.getOsversion()));
1339:                ps.setString(4, this .makeNotNull(sysinfo.getGlibc()));
1340:                try {
1341:                    int i = ps.executeUpdate();
1342:                    if (i < 1) {
1343:                        mLogger.log("No entries found that could be deleted",
1344:                                LogManager.ERROR_MESSAGE_LEVEL);
1345:                        return false;
1346:                    } else {
1347:                        mLogger.log("Deleted " + i + "entry with system info "
1348:                                + sysinfo.toString(),
1349:                                LogManager.DEBUG_MESSAGE_LEVEL);
1350:                    }
1351:                } catch (SQLException e) {
1352:                    mLogger.log("Unable to delete entries by SysInfo from TC :"
1353:                            + e.getMessage(), LogManager.ERROR_MESSAGE_LEVEL);
1354:                    this .m_dbdriver.rollback();
1355:                    return false;
1356:                }
1357:                this .m_dbdriver.commit();
1358:                return true;
1359:
1360:            }
1361:
1362:            /**
1363:             * Delete a paricular type of transformation, and/or on a particular resource
1364:             * @param type TCType The type of the transformation
1365:             * @param resourceid String The resource on which the transformation exists.
1366:             *                           If <B>NULL</B> then that type of transformation is deleted from all the resources.
1367:             * @throws Exception
1368:             * @return boolean Returns true if success, false if any error occurs.
1369:             * @see org.griphyn.common.classes.TCType
1370:             */
1371:
1372:            public boolean deleteTCbyType(TCType type, String resourceid)
1373:                    throws Exception {
1374:                if (type == null) {
1375:                    mLogger.log("The type of transformation cannot be null",
1376:                            LogManager.ERROR_MESSAGE_LEVEL);
1377:                    return false;
1378:                }
1379:                PreparedStatement ps = this .m_dbdriver
1380:                        .getPreparedStatement("stmt.delete.bytype");
1381:                ps.setString(1, this .makeNotNull(type.toString()));
1382:                String temp = (resourceid != null) ? resourceid : "%";
1383:                ps.setString(2, temp);
1384:                try {
1385:                    int i = ps.executeUpdate();
1386:                    if (i < 1) {
1387:                        mLogger.log("No entries found that could be deleted",
1388:                                LogManager.ERROR_MESSAGE_LEVEL);
1389:                        return false;
1390:                    } else {
1391:                        mLogger.log("Deleted " + i
1392:                                + " transformations with Type " + type
1393:                                + " and on Resource "
1394:                                + ((resourceid == null) ? "ALL" : resourceid),
1395:                                LogManager.DEBUG_MESSAGE_LEVEL);
1396:                    }
1397:                } catch (SQLException e) {
1398:                    mLogger.log("Unable to delete entries by type from TC :"
1399:                            + e.getMessage(), LogManager.ERROR_MESSAGE_LEVEL);
1400:                    this .m_dbdriver.rollback();
1401:                    return false;
1402:                }
1403:                this .m_dbdriver.commit();
1404:                return true;
1405:            }
1406:
1407:            /**
1408:             * Delete all entries on a particular resource from the transformation catalog.
1409:             * @param resourceid String The resource which you want to remove.
1410:             * @throws Exception
1411:             * @return boolean  Returns true if successm false if any error occurs.
1412:             */
1413:
1414:            public boolean deleteTCbyResourceId(String resourceid)
1415:                    throws Exception {
1416:                PreparedStatement ps = this .m_dbdriver
1417:                        .getPreparedStatement("stmt.delete.byresourceid");
1418:                ps.setString(1, resourceid);
1419:                try {
1420:                    int i = ps.executeUpdate();
1421:                    if (i < 1) {
1422:                        mLogger.log("No entries found that could be deleted",
1423:                                LogManager.ERROR_MESSAGE_LEVEL);
1424:                        return false;
1425:                    } else {
1426:                        mLogger.log("Deleted " + i + " resources",
1427:                                LogManager.INFO_MESSAGE_LEVEL);
1428:                    }
1429:                } catch (SQLException e) {
1430:                    mLogger.log("Unable to delete Resource from TC :"
1431:                            + e.getMessage(), LogManager.ERROR_MESSAGE_LEVEL);
1432:                    this .m_dbdriver.rollback();
1433:                    return false;
1434:                }
1435:                this .m_dbdriver.commit();
1436:                return true;
1437:            }
1438:
1439:            /**
1440:             * Deletes the entire transformation catalog. CLEAN............. USE WITH CAUTION.
1441:             * @throws Exception
1442:             * @return boolean Returns true if delete succeeds, false if any error occurs.
1443:             */
1444:
1445:            public boolean deleteTC() throws Exception {
1446:                PreparedStatement[] ps = {
1447:                        this .m_dbdriver
1448:                                .getPreparedStatement("stmt.delete.alllfnpfnmap"),
1449:                        this .m_dbdriver
1450:                                .getPreparedStatement("stmt.delete.alllfnprofile"),
1451:                        this .m_dbdriver
1452:                                .getPreparedStatement("stmt.delete.allpfnprofile"),
1453:                        this .m_dbdriver
1454:                                .getPreparedStatement("stmt.delete.alllfns"),
1455:                        this .m_dbdriver
1456:                                .getPreparedStatement("stmt.delete.allpfns"),
1457:                        this .m_dbdriver
1458:                                .getPreparedStatement("stmt.delete.allsysinfo") };
1459:                try {
1460:                    for (int i = 0; i < ps.length; i++) {
1461:                        ps[i].executeUpdate();
1462:                    }
1463:                } catch (SQLException e) {
1464:                    mLogger.log("Unable to delete the entire TC ", e,
1465:                            LogManager.ERROR_MESSAGE_LEVEL);
1466:                    this .m_dbdriver.rollback();
1467:                    return false;
1468:                }
1469:                //if we are here all succeeded and we should commit;
1470:                this .m_dbdriver.commit();
1471:                return true;
1472:            }
1473:
1474:            /**
1475:             * Delete a list of profiles or all the profiles associated with a pfn on a
1476:             *  resource and of a type.
1477:             * @param physicalname String The physical name of the transformation.
1478:             * @param type TCType The type of the transformation.
1479:             * @param resourceid String The resource of the transformation.
1480:             * @param profiles List The list of profiles to be deleted. If <B>NULL</B>
1481:             * then all profiles for that pfn+resource+type are deleted.
1482:             * @return boolean Returns true if success, false if any error occurs.
1483:             * @throws Exception
1484:             * @see org.griphyn.cPlanner.classes.Profile
1485:             */
1486:            public boolean deleteTCPfnProfile(String physicalname, TCType type,
1487:                    String resourceid, List profiles) throws Exception {
1488:                long pfnid;
1489:                if ((pfnid = this .getPhysicalId(physicalname, type, resourceid)) != -1) {
1490:                    if (profiles == null) {
1491:                        PreparedStatement ps = this .m_dbdriver
1492:                                .getPreparedStatement("stmt.delete.pfnprofiles");
1493:                        ps.setLong(1, pfnid);
1494:                        try {
1495:                            int i = ps.executeUpdate();
1496:                            if (i < 1) {
1497:                                mLogger
1498:                                        .log(
1499:                                                "No entries found that could be deleted",
1500:                                                LogManager.ERROR_MESSAGE_LEVEL);
1501:                                return false;
1502:                            } else {
1503:                                mLogger.log("Deleted " + i + " pfn profiles",
1504:                                        LogManager.INFO_MESSAGE_LEVEL);
1505:                            }
1506:                        } catch (SQLException e) {
1507:                            mLogger.log("Unable to delete pfnprofiles from TC",
1508:                                    e, LogManager.ERROR_MESSAGE_LEVEL);
1509:                            this .m_dbdriver.rollback();
1510:                            return false;
1511:                        }
1512:                    } else {
1513:                        for (Iterator i = profiles.iterator(); i.hasNext();) {
1514:                            this .deleteProfile((Profile) i.next(), pfnid, true);
1515:                        }
1516:                    }
1517:                    this .m_dbdriver.commit();
1518:                    return true;
1519:                } else {
1520:                    mLogger.log("The pfn " + physicalname + " of type " + type
1521:                            + " on resource " + resourceid + " does not exist",
1522:                            LogManager.ERROR_MESSAGE_LEVEL);
1523:                }
1524:                return false;
1525:            }
1526:
1527:            /**
1528:             * Delete a list of profiles or all the profiles associated with a logical
1529:             *  transformation.
1530:             * @param namespace String  The namespace of the logical transformation.
1531:             * @param name String The name of the logical transformation.
1532:             * @param version String The version of the logical transformation.
1533:             * @param profiles List The List of profiles to be deleted. If <B>NULL</B>
1534:             * then all profiles for the logical transformation are deleted.
1535:             * @return boolean Returns true if success, false if any error occurs.
1536:             * @throws Exception
1537:             * @see org.griphyn.cPlanner.classes.Profile
1538:             */
1539:            public boolean deleteTCLfnProfile(String namespace, String name,
1540:                    String version, List profiles) throws Exception {
1541:                long lfnid;
1542:                if ((lfnid = getLogicalId(namespace, name, version)) != -1) {
1543:                    if (profiles == null) {
1544:                        PreparedStatement ps = this .m_dbdriver
1545:                                .getPreparedStatement("stmt.delete.lfnprofiles");
1546:                        ps.setLong(1, lfnid);
1547:                        try {
1548:                            int i = ps.executeUpdate();
1549:                            if (i < 1) {
1550:                                mLogger
1551:                                        .log(
1552:                                                "No entries found that could be deleted",
1553:                                                LogManager.ERROR_MESSAGE_LEVEL);
1554:                                return false;
1555:                            } else {
1556:                                mLogger.log("Deleted " + i + " lfn profiles",
1557:                                        LogManager.INFO_MESSAGE_LEVEL);
1558:                            }
1559:                        } catch (SQLException e) {
1560:                            mLogger.log("Unable to delete lfnprofiles from TC",
1561:                                    e, LogManager.ERROR_MESSAGE_LEVEL);
1562:                            this .m_dbdriver.rollback();
1563:                            return false;
1564:                        }
1565:                    } else {
1566:                        for (Iterator i = profiles.iterator(); i.hasNext();) {
1567:                            this 
1568:                                    .deleteProfile((Profile) i.next(), lfnid,
1569:                                            false);
1570:                        }
1571:                    }
1572:                    this .m_dbdriver.commit();
1573:                    return true;
1574:                } else {
1575:                    mLogger
1576:                            .log("Logical transformation "
1577:                                    + Separator.combine(namespace, name,
1578:                                            version) + " does not exist",
1579:                                    LogManager.ERROR_MESSAGE_LEVEL);
1580:                }
1581:                return false;
1582:            }
1583:
1584:            public static TransformationCatalog getInstance() {
1585:                try {
1586:                    mDatabaseTC = (mDatabaseTC == null) ? new Database()
1587:                            : mDatabaseTC;
1588:                    return mDatabaseTC;
1589:                } catch (Exception e) {
1590:                    mLogger.log("Unable to create Database TC Instance", e,
1591:                            LogManager.ERROR_MESSAGE_LEVEL);
1592:                    return null;
1593:                }
1594:            }
1595:
1596:            /**
1597:             * Returns the TC implementation being used
1598:             * @return String
1599:             */
1600:            public String getTCMode() {
1601:                return new String(PegasusProperties.nonSingletonInstance()
1602:                        .getTCMode());
1603:            }
1604:
1605:            public void close() {
1606:                try {
1607:                    super .close();
1608:                } catch (SQLException e) {
1609:                    mLogger.log("Unable to call Close on the Database", e,
1610:                            LogManager.ERROR_MESSAGE_LEVEL);
1611:                }
1612:            }
1613:
1614:            public boolean connect(java.util.Properties props) {
1615:                //not implemented
1616:                return true;
1617:            }
1618:
1619:            public boolean isClosed() {
1620:                //not impelemented
1621:                return true;
1622:            }
1623:
1624:            /**
1625:             * Returns the id  associated with the logical transformation.
1626:             * @param namespace String
1627:             * @param name String
1628:             * @param version String
1629:             * @throws Exception
1630:             * @return long Returns -1 if entry does not exist
1631:             */
1632:            private long getLogicalId(String namespace, String name,
1633:                    String version) throws Exception {
1634:                PreparedStatement ps = this .m_dbdriver
1635:                        .getPreparedStatement("stmt.query.lfnid");
1636:                ps.setString(1, this .makeNotNull(namespace));
1637:                ps.setString(2, this .makeNotNull(name));
1638:                ps.setString(3, this .makeNotNull(version));
1639:                ResultSet rs = ps.executeQuery();
1640:                if (rs.next()) {
1641:                    long l = rs.getLong(1);
1642:                    rs.close();
1643:
1644:                    return l;
1645:                } else {
1646:                    return -1;
1647:                }
1648:            }
1649:
1650:            /**
1651:             * Checks if a lfn,pfn exist in the map.
1652:             * @param lfnid long
1653:             * @param pfnid long
1654:             * @throws Exception
1655:             * @return long Returns 1 if exists, -1 if not exists
1656:             */
1657:            private long checkLfnPfnMap(long lfnid, long pfnid)
1658:                    throws Exception {
1659:                PreparedStatement ps = this .m_dbdriver
1660:                        .getPreparedStatement("stmt.query.lfnpfnmap");
1661:                ps.setLong(1, lfnid);
1662:                ps.setLong(2, pfnid);
1663:                ResultSet rs = ps.executeQuery();
1664:                if (rs.next()) {
1665:
1666:                    rs.close();
1667:
1668:                    return 1;
1669:                } else {
1670:                    return -1;
1671:                }
1672:            }
1673:
1674:            /**
1675:             * Checks if a given profile exists
1676:             * @param namespace String
1677:             * @param name String
1678:             * @param value String
1679:             * @param id long
1680:             * @param pfn boolean
1681:             * @throws Exception
1682:             * @return long Returns 1 if exists , -1 if does not exist.
1683:             */
1684:            private long checkProfile(String namespace, String name,
1685:                    String value, long id, boolean pfn) throws Exception {
1686:                PreparedStatement ps = null;
1687:
1688:                if (pfn) {
1689:                    ps = this .m_dbdriver
1690:                            .getPreparedStatement("stmt.query.pfnprofileid");
1691:                } else {
1692:                    ps = this .m_dbdriver
1693:                            .getPreparedStatement("stmt.query.lfnprofileid");
1694:                }
1695:                ps.setString(1, makeNotNull(namespace));
1696:                ps.setString(2, makeNotNull(name));
1697:                ps.setString(3, makeNotNull(value));
1698:                ps.setLong(4, id);
1699:                ResultSet rs = ps.executeQuery();
1700:                if (rs.next()) {
1701:                    rs.close();
1702:
1703:                    return 1;
1704:                } else {
1705:                    return -1;
1706:                }
1707:            }
1708:
1709:            /**
1710:             * Gets the id for the system information entry.
1711:             * @param system SysInfo
1712:             * @throws Exception
1713:             * @return long Returns -1 if it does not exist
1714:             * @see org.griphyn.common.classes.SysInfo
1715:             */
1716:            private long getSysInfoId(SysInfo system) throws Exception {
1717:                PreparedStatement ps = this .m_dbdriver
1718:                        .getPreparedStatement("stmt.query.sysid");
1719:                ps.setString(1, system.getArch().getValue());
1720:                ps.setString(2, system.getOs().getValue());
1721:                ps.setString(3, this .makeNotNull(system.getOsversion()));
1722:                ps.setString(4, this .makeNotNull(system.getGlibc()));
1723:                ResultSet rs = ps.executeQuery();
1724:
1725:                if (rs.next()) {
1726:                    long l = rs.getLong(1);
1727:                    rs.close();
1728:                    return l;
1729:                } else {
1730:                    return -1;
1731:                }
1732:            }
1733:
1734:            /**
1735:             * Adds a system information entry into the TC.
1736:             * @param system SysInfo
1737:             * @throws Exception
1738:             * @return boolean Returns true if success, false if error occurs.
1739:             */
1740:            private long addSysInfo(SysInfo system) throws Exception {
1741:                PreparedStatement ps = this .m_dbdriver
1742:                        .getPreparedStatement("stmt.add.sysinfo");
1743:                long id = -1;
1744:                try {
1745:                    id = m_dbdriver.sequence1("tc_sysinfo_id_seq");
1746:                } catch (SQLException e) {
1747:                    mLogger.log("Error while getting sysinfoid : ", e,
1748:                            LogManager.ERROR_MESSAGE_LEVEL);
1749:                    mLogger.log("Starting Rollback",
1750:                            LogManager.ERROR_MESSAGE_LEVEL);
1751:                    m_dbdriver.cancelPreparedStatement("stmt.add.sysinfo");
1752:                    m_dbdriver.rollback();
1753:                    mLogger.log("Finished Rollback",
1754:                            LogManager.ERROR_MESSAGE_LEVEL);
1755:                    return -1;
1756:                }
1757:                longOrNull(ps, 1, id);
1758:                ps.setString(2, this .makeNotNull(system.getArch().getValue()));
1759:                ps.setString(3, this .makeNotNull(system.getOs().getValue()));
1760:                ps.setString(4, this .makeNotNull(system.getOsversion()));
1761:                ps.setString(5, this .makeNotNull(system.getGlibc()));
1762:                try {
1763:                    ps.executeUpdate();
1764:                    mLogger.log("Added the system info " + system.toString(),
1765:                            LogManager.DEBUG_MESSAGE_LEVEL);
1766:                    if (id == -1) {
1767:                        id = m_dbdriver.sequence2(ps, "tc_sysinfo_id_seq", 1);
1768:                    }
1769:                } catch (SQLException e) {
1770:                    mLogger.log("Unable to add system info to the TC", e,
1771:                            LogManager.ERROR_MESSAGE_LEVEL);
1772:                    m_dbdriver.cancelPreparedStatement("stmt.add.sysinfo");
1773:                    m_dbdriver.rollback();
1774:                    return -1;
1775:                }
1776:                return id;
1777:            }
1778:
1779:            /**
1780:             * Adds a logical entry to the logicaltx table
1781:             * @param namespace String The namespace of the transformation
1782:             * @param name String The name of the transformation
1783:             * @param version String The version of the transformation
1784:             * @return long   The position in table at which the entry is added.
1785:             * If there is an error -1 is returned.
1786:             * @throws Exception
1787:             */
1788:            private long addLogicalTr(String namespace, String name,
1789:                    String version) throws Exception {
1790:                PreparedStatement ps = this .m_dbdriver
1791:                        .getPreparedStatement("stmt.add.lfn");
1792:                long id = -1;
1793:                try {
1794:                    id = m_dbdriver.sequence1("tc_logicaltx_id_seq");
1795:                } catch (SQLException e) {
1796:                    mLogger.log("Unable to get lfnid : ", e,
1797:                            LogManager.ERROR_MESSAGE_LEVEL);
1798:                    mLogger.log("Starting Rollback",
1799:                            LogManager.ERROR_MESSAGE_LEVEL);
1800:                    m_dbdriver.cancelPreparedStatement("stmt.add.lfn");
1801:                    m_dbdriver.rollback();
1802:                    mLogger.log("Finished Rollback",
1803:                            LogManager.ERROR_MESSAGE_LEVEL);
1804:                    return -1;
1805:                }
1806:                longOrNull(ps, 1, id);
1807:                //fill the data
1808:                ps.setString(2, this .makeNotNull(namespace));
1809:                ps.setString(3, this .makeNotNull(name));
1810:                ps.setString(4, this .makeNotNull(version));
1811:                try {
1812:                    //run the command
1813:                    ps.executeUpdate();
1814:                    mLogger.log("Added the lfn "
1815:                            + Separator.combine(namespace, name, version),
1816:                            LogManager.DEBUG_MESSAGE_LEVEL);
1817:                    if (id == -1) {
1818:                        id = m_dbdriver.sequence2(ps, "tc_logicaltx_id_seq", 1);
1819:                    }
1820:                    // lfnid = this.getLogicalId( namespace, name, version );
1821:                } catch (SQLException e) {
1822:                    mLogger.log(
1823:                            "Unable to add logical transformation into the TC",
1824:                            e, LogManager.ERROR_MESSAGE_LEVEL);
1825:                    m_dbdriver.cancelPreparedStatement("stmt.add.lfn");
1826:                    m_dbdriver.rollback();
1827:                    return -1;
1828:                }
1829:                return id;
1830:            }
1831:
1832:            /**
1833:             * Adds a physical entry to the physicaltxtable
1834:             * @param physicalname String The physical name of the transformation
1835:             * @param resourceid String The resource on which the transformation exists
1836:             * @param type TCType  The type of the transformation
1837:             * @param archid long  The architecture id from the sysinfo table for the tr.
1838:             * @return long  The position in the physicaltx table at which the entry is
1839:             * stored. If there is an error -1 is returned.
1840:             * @throws Exception
1841:             */
1842:            private long addPhysicalTr(String physicalname, String resourceid,
1843:                    TCType type, long archid) throws Exception {
1844:                PreparedStatement ps = this .m_dbdriver
1845:                        .getPreparedStatement("stmt.add.pfn");
1846:                long id = -1;
1847:                try {
1848:                    id = m_dbdriver.sequence1("tc_physicaltx_id_seq");
1849:                } catch (SQLException e) {
1850:                    mLogger.log("Error while getting pfnid : ", e,
1851:                            LogManager.ERROR_MESSAGE_LEVEL);
1852:                    mLogger.log("Starting Rollback",
1853:                            LogManager.ERROR_MESSAGE_LEVEL);
1854:                    m_dbdriver.cancelPreparedStatement("stmt.add.pfn");
1855:                    m_dbdriver.rollback();
1856:                    mLogger.log("Finished Rollback",
1857:                            LogManager.ERROR_MESSAGE_LEVEL);
1858:                    return -1;
1859:                }
1860:                longOrNull(ps, 1, id);
1861:
1862:                //this means the pfn entry does not exist and we have to add it.
1863:                ps.setString(2, makeNotNull(resourceid));
1864:                ps.setString(3, makeNotNull(physicalname));
1865:                ps.setString(4, makeNotNull(type.toString()));
1866:                ps.setLong(5, archid);
1867:                try {
1868:                    ps.executeUpdate();
1869:                    mLogger.log("Added the pfn " + physicalname,
1870:                            LogManager.DEBUG_MESSAGE_LEVEL);
1871:                    mLogger.log("Added the type " + type.toString(),
1872:                            LogManager.DEBUG_MESSAGE_LEVEL);
1873:                    mLogger.log("Added the resource " + resourceid,
1874:                            LogManager.DEBUG_MESSAGE_LEVEL);
1875:                    mLogger.log("Added the archid " + archid,
1876:                            LogManager.DEBUG_MESSAGE_LEVEL);
1877:                    if (id == -1) {
1878:                        id = m_dbdriver
1879:                                .sequence2(ps, "tc_physicaltx_id_seq", 1);
1880:                    }
1881:                    // pfnid = this.getPhysicalId( physicalname, type, resourceid );
1882:                } catch (SQLException e) {
1883:                    mLogger.log("Unable to add pfn entry to the TC", e,
1884:                            LogManager.ERROR_MESSAGE_LEVEL);
1885:                    m_dbdriver.cancelPreparedStatement("stmt.add.pfn");
1886:                    m_dbdriver.rollback();
1887:                    return -1;
1888:                }
1889:                return id;
1890:            }
1891:
1892:            /**
1893:             * Add a lfn or pfn profile to the TC
1894:             * @param p Profile The profile to be added
1895:             * @param id long The lfn or pfn id to which the profile is associated.
1896:             * @param pfn boolean if true entry is added to the pfn, false to the lfn.
1897:             * @throws Exception
1898:             * @return boolean Returns true if success, false if error occurs.
1899:             */
1900:            private boolean addProfile(Profile p, long id, boolean pfn)
1901:                    throws Exception {
1902:                String namespace = p.getProfileNamespace();
1903:                String key = p.getProfileKey();
1904:                String value = p.getProfileValue();
1905:                long profileid = -1;
1906:                PreparedStatement ps = null;
1907:                if ((profileid = this .checkProfile(namespace, key, value, id,
1908:                        pfn)) == -1) {
1909:                    if (pfn) {
1910:                        //add pfn profile
1911:                        //the profile doesnt exist so lets add it in there.
1912:                        ps = m_dbdriver
1913:                                .getPreparedStatement("stmt.add.pfnprofile");
1914:                    } else {
1915:                        //add lfn profile
1916:                        ps = m_dbdriver
1917:                                .getPreparedStatement("stmt.add.lfnprofile");
1918:                    }
1919:                    ps.setString(1, makeNotNull(namespace));
1920:                    ps.setString(2, makeNotNull(key));
1921:                    ps.setString(3, makeNotNull(value));
1922:                    ps.setLong(4, id);
1923:                    try {
1924:                        ps.executeUpdate();
1925:
1926:                    } catch (SQLException e) {
1927:                        mLogger.log("Unable to add Profile to the TC", e,
1928:                                LogManager.ERROR_MESSAGE_LEVEL);
1929:                        m_dbdriver.rollback();
1930:                        return false;
1931:                    }
1932:                    return true;
1933:                } else {
1934:                    String temp = (pfn) ? "PFN" : "LFN";
1935:                    mLogger.log("The " + temp + "profile exists.",
1936:                            LogManager.DEBUG_MESSAGE_LEVEL);
1937:                    return false;
1938:                }
1939:
1940:            }
1941:
1942:            /**
1943:             * Delete a given lfn or pfn profile
1944:             * @param p Profile The profile to be deleted
1945:             * @param id long The lfn or pfnid with which the profile is associated
1946:             * @param pfn boolean If true the pfn profile is deleted, if false lfn profile is deleted
1947:             * @throws Exception
1948:             * @return boolean Returns true if success, false if any error occurs.
1949:             */
1950:            private boolean deleteProfile(Profile p, long id, boolean pfn)
1951:                    throws Exception {
1952:                String namespace = p.getProfileNamespace();
1953:                String key = p.getProfileKey();
1954:                String value = p.getProfileValue();
1955:                long profileid = -1;
1956:                PreparedStatement ps = null;
1957:                if ((profileid = this .checkProfile(namespace, key, value, id,
1958:                        pfn)) != -1) {
1959:                    if (pfn) {
1960:                        //delete pfn profile
1961:                        //the profile exists so lets delete it.
1962:                        ps = m_dbdriver
1963:                                .getPreparedStatement("stmt.delete.pfnprofile");
1964:                    } else {
1965:                        //delete lfn profile
1966:                        ps = m_dbdriver
1967:                                .getPreparedStatement("stmt.delete.lfnprofile");
1968:                    }
1969:                    ps.setString(1, makeNotNull(namespace));
1970:                    ps.setString(2, makeNotNull(key));
1971:                    ps.setString(3, makeNotNull(value));
1972:                    ps.setLong(4, id);
1973:                    try {
1974:                        ps.executeUpdate();
1975:
1976:                    } catch (SQLException e) {
1977:                        mLogger.log("Unable to delete Profile to the TC", e,
1978:                                LogManager.ERROR_MESSAGE_LEVEL);
1979:                        m_dbdriver.rollback();
1980:                        return false;
1981:                    }
1982:                    return true;
1983:                } else {
1984:                    String temp = (pfn) ? "PFN" : "LFN";
1985:                    mLogger.log("The " + pfn + " profile does not exist.",
1986:                            LogManager.DEBUG_MESSAGE_LEVEL);
1987:                    return false;
1988:                }
1989:
1990:            }
1991:
1992:            /**
1993:             * Returns the id of the physical transformation
1994:             * @param pfn String the physical transformation
1995:             * @param type TCType The type of the transformation
1996:             * @param resourceid String The resource on which the transformation exists.
1997:             * @throws Exception
1998:             * @return long Returns -1 if entry does not exist.
1999:             */
2000:            private long getPhysicalId(String pfn, TCType type,
2001:                    String resourceid) throws Exception {
2002:                PreparedStatement ps = this .m_dbdriver
2003:                        .getPreparedStatement("stmt.query.pfnid");
2004:                ps.setString(1, this .makeNotNull(pfn));
2005:                ps.setString(2, this .makeNotNull(type.toString()));
2006:                ps.setString(3, this .makeNotNull(resourceid));
2007:                ResultSet rs = ps.executeQuery();
2008:
2009:                if (rs.next()) {
2010:                    long l = rs.getLong(1);
2011:
2012:                    rs.close();
2013:
2014:                    return l;
2015:                } else {
2016:                    return -1;
2017:                }
2018:            }
2019:
2020:            /**
2021:             * Returns a list of pfnid for a given pfn on any resource, and of any type.
2022:             * @param pfn String The physical transformation to search for.
2023:             * @param type TCType The type to search for. If <B>NULL</B> then all types are searched.
2024:             * @param resourceid String The resource to search for. If <B>NULL</B> then all resources are searched.
2025:             * @throws Exception
2026:             * @return long[] Returns -1 is no entry exist.
2027:             */
2028:            private long[] getPhysicalIds(String pfn, TCType type,
2029:                    String resourceid) throws Exception {
2030:                PreparedStatement ps = this .m_dbdriver
2031:                        .getPreparedStatement("stmt.query.pfnid");
2032:                String temp = (type == null) ? "%" : type.toString();
2033:                ps.setString(1, pfn);
2034:                ps.setString(2, temp);
2035:                temp = (resourceid == null) ? "%" : resourceid;
2036:                ps.setString(3, temp);
2037:                ResultSet rs = ps.executeQuery();
2038:                List resultlist = null;
2039:                while (rs.next()) {
2040:                    if (resultlist == null) {
2041:                        resultlist = new ArrayList();
2042:                    }
2043:                    resultlist.add(new Long(rs.getLong(1)));
2044:                }
2045:                if (resultlist != null) {
2046:                    long[] result = new long[resultlist.size()];
2047:                    int count = 0;
2048:                    for (Iterator i = resultlist.iterator(); i.hasNext();) {
2049:                        result[count] = ((Long) i.next()).longValue();
2050:                        count++;
2051:                    }
2052:                    return result;
2053:                } else {
2054:                    long[] result = { -1 };
2055:                    return result;
2056:                }
2057:            }
2058:
2059:            /**
2060:             * Computes the maximum column lenght for pretty printing.
2061:             * @param s String[]
2062:             * @param count int[]
2063:             */
2064:            private static void columnLength(String[] s, int[] count) {
2065:                for (int i = 0; i < count.length; i++) {
2066:                    if (s[i].length() > count[i]) {
2067:                        count[i] = s[i].length();
2068:                    }
2069:                }
2070:
2071:            }
2072:
2073:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.