Source Code Cross Referenced for DbCitationService.java in  » ERP-CRM-Financial » sakai » org » sakaiproject » citation » impl » 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 » ERP CRM Financial » sakai » org.sakaiproject.citation.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**********************************************************************************
0002:         * $URL: https://source.sakaiproject.org/svn/citations/tags/sakai_2-4-1/citations-impl/impl/src/java/org/sakaiproject/citation/impl/DbCitationService.java $
0003:         * $Id: DbCitationService.java 30509 2007-05-14 20:14:09Z ajpoland@iupui.edu $
0004:         ***********************************************************************************
0005:         *
0006:         * Copyright (c) 2006 The Sakai Foundation.
0007:         *
0008:         * Licensed under the Educational Community License, Version 1.0 (the "License");
0009:         * you may not use this file except in compliance with the License.
0010:         * You may obtain a copy of the License at
0011:         *
0012:         *      http://www.opensource.org/licenses/ecl1.php
0013:         *
0014:         * Unless required by applicable law or agreed to in writing, software
0015:         * distributed under the License is distributed on an "AS IS" BASIS,
0016:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017:         * See the License for the specific language governing permissions and
0018:         * limitations under the License.
0019:         *
0020:         **********************************************************************************/package org.sakaiproject.citation.impl;
0021:
0022:        import java.net.MalformedURLException;
0023:        import java.net.URL;
0024:        import java.sql.Connection;
0025:        import java.sql.ResultSet;
0026:        import java.sql.SQLException;
0027:        import java.util.Collection;
0028:        import java.util.Hashtable;
0029:        import java.util.Iterator;
0030:        import java.util.List;
0031:        import java.util.Map;
0032:        import java.util.Vector;
0033:        import java.util.regex.Matcher;
0034:        import java.util.regex.Pattern;
0035:
0036:        import org.apache.commons.logging.Log;
0037:        import org.apache.commons.logging.LogFactory;
0038:        import org.sakaiproject.citation.api.Citation;
0039:        import org.sakaiproject.citation.api.CitationCollection;
0040:        import org.sakaiproject.citation.api.Schema;
0041:        import org.sakaiproject.citation.api.Schema.Field;
0042:        import org.sakaiproject.citation.impl.BaseCitationService;
0043:        import org.sakaiproject.citation.impl.BaseCitationService.BasicCitation;
0044:        import org.sakaiproject.citation.impl.BaseCitationService.BasicCitationCollection;
0045:        import org.sakaiproject.citation.impl.BaseCitationService.BasicSchema;
0046:        import org.sakaiproject.citation.impl.BaseCitationService.Storage;
0047:        import org.sakaiproject.citation.impl.BaseCitationService.BasicField;
0048:        import org.sakaiproject.citation.impl.BaseCitationService.UrlWrapper;
0049:        import org.sakaiproject.db.api.SqlReader;
0050:        import org.sakaiproject.db.api.SqlService;
0051:        import org.sakaiproject.id.cover.IdManager;
0052:        import org.sakaiproject.thread_local.cover.ThreadLocalManager;
0053:        import org.sakaiproject.time.api.Time;
0054:        import org.sakaiproject.time.cover.TimeService;
0055:
0056:        /**
0057:         *
0058:         */
0059:        public class DbCitationService extends BaseCitationService {
0060:            /**
0061:             * 
0062:             */
0063:            public class DbCitationStorage implements  Storage {
0064:                /* (non-Javadoc)
0065:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#addCitation(java.lang.String)
0066:                 */
0067:                protected Citation addCitation(Connection conn, String mediatype) {
0068:                    // need to create a citation (referred to below as "edit")
0069:                    BasicCitation edit = new BasicCitation(mediatype);
0070:
0071:                    String statement = "insert into "
0072:                            + m_citationTableName
0073:                            + " ("
0074:                            + m_citationTableId
0075:                            + ", PROPERTY_NAME, PROPERTY_VALUE) values ( ?, ?, ? )";
0076:
0077:                    String citationId = edit.getId();
0078:
0079:                    Object[] fields = new Object[3];
0080:                    fields[0] = citationId;
0081:
0082:                    fields[1] = PROP_MEDIATYPE;
0083:                    fields[2] = edit.getSchema().getIdentifier();
0084:                    boolean ok = m_sqlService.dbWrite(conn, statement, fields);
0085:
0086:                    List names = edit.listCitationProperties();
0087:                    boolean first = true;
0088:                    Iterator nameIt = names.iterator();
0089:                    while (nameIt.hasNext()) {
0090:                        String name = (String) nameIt.next();
0091:                        Object value = edit.getCitationProperty(name);
0092:
0093:                        fields[1] = name;
0094:                        fields[2] = value;
0095:
0096:                        // process the insert
0097:                        ok = m_sqlService.dbWrite(conn, statement, fields);
0098:                    }
0099:
0100:                    return edit;
0101:                }
0102:
0103:                public Citation addCitation(String mediatype) {
0104:                    Citation citation = null;
0105:                    Connection conn = null;
0106:                    int wasCommit = AUTO_UNKNOWN;
0107:
0108:                    M_log.debug("addCitation(" + mediatype + ")");
0109:                    try {
0110:                        conn = m_sqlService.borrowConnection();
0111:
0112:                        wasCommit = getAutoCommit(conn);
0113:                        conn.setAutoCommit(false);
0114:
0115:                        citation = this .addCitation(conn, mediatype);
0116:
0117:                        conn.commit();
0118:                        restoreAutoCommit(conn, wasCommit);
0119:                        m_sqlService.returnConnection(conn);
0120:                    } catch (Exception e) {
0121:                        errorCleanup(conn, wasCommit);
0122:                        M_log.warn("addCitation(" + mediatype + ") " + e);
0123:                    }
0124:                    return citation;
0125:                }
0126:
0127:                /* (non-Javadoc)
0128:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#putCollection(java.lang.String, java.util.Map, java.util.List)
0129:                 */
0130:                protected CitationCollection addCollection(Connection conn,
0131:                        Map attributes, List citations) {
0132:                    // need to create a collection (referred to below as "edit")
0133:                    BasicCitationCollection edit = new BasicCitationCollection(
0134:                            attributes, citations);
0135:
0136:                    String statement = "insert into "
0137:                            + m_collectionTableName
0138:                            + " ("
0139:                            + m_collectionTableId
0140:                            + ",PROPERTY_NAME,PROPERTY_VALUE) values ( ?, ?, ? )";
0141:
0142:                    Object[] fields = new Object[3];
0143:                    fields[0] = edit.getId();
0144:                    fields[1] = PROPERTY_HAS_CITATION;
0145:                    boolean ok = true;
0146:
0147:                    List members = edit.getCitations();
0148:                    Iterator citationIt = members.iterator();
0149:                    while (citationIt.hasNext()) {
0150:                        Citation citation = (Citation) citationIt.next();
0151:
0152:                        if (citation instanceof  BasicCitation
0153:                                && ((BasicCitation) citation).isTemporary()) {
0154:                            ((BasicCitation) citation).m_id = IdManager
0155:                                    .createUuid();
0156:                            ((BasicCitation) citation).m_temporary = false;
0157:                            ((BasicCitation) citation).m_serialNumber = null;
0158:                        }
0159:
0160:                        saveCitation(conn, citation);
0161:
0162:                        // process the insert
0163:                        fields[2] = citation.getId();
0164:                        ok = m_sqlService.dbWrite(conn, statement, fields);
0165:                    }
0166:
0167:                    return edit;
0168:                }
0169:
0170:                public CitationCollection addCollection(Map attributes,
0171:                        List citations) {
0172:                    CitationCollection collection = null;
0173:                    Connection conn = null;
0174:                    int wasCommit = AUTO_UNKNOWN;
0175:
0176:                    try {
0177:                        conn = m_sqlService.borrowConnection();
0178:                        wasCommit = getAutoCommit(conn);
0179:                        conn.setAutoCommit(false);
0180:
0181:                        collection = this .addCollection(conn, attributes,
0182:                                citations);
0183:
0184:                        conn.commit();
0185:                        restoreAutoCommit(conn, wasCommit);
0186:                        m_sqlService.returnConnection(conn);
0187:                    } catch (SQLException e) {
0188:                        errorCleanup(conn, wasCommit);
0189:                        M_log.warn("addCollection([" + attributes + "], ["
0190:                                + citations + "]) " + e);
0191:                    }
0192:                    return collection;
0193:                }
0194:
0195:                /* (non-Javadoc)
0196:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#putSchema(org.sakaiproject.citation.api.Schema)
0197:                 */
0198:                protected Schema addSchema(Connection conn, Schema schema) {
0199:                    String statement = "insert into "
0200:                            + m_schemaTableName
0201:                            + " ("
0202:                            + m_schemaTableId
0203:                            + ",PROPERTY_NAME,PROPERTY_VALUE) values ( ?, ?, ? )";
0204:
0205:                    String schemaId = schema.getIdentifier();
0206:
0207:                    boolean ok = true;
0208:                    Object[] fields = new Object[3];
0209:                    fields[0] = schemaId;
0210:                    fields[1] = PROPERTY_HAS_FIELD;
0211:
0212:                    List schemaFields = schema.getFields();
0213:                    for (int i = 0; i < schemaFields.size(); i++) {
0214:                        Field field = (Field) schemaFields.get(i);
0215:                        if (field instanceof  BasicField) {
0216:                            ((BasicField) field).setOrder(i);
0217:                        }
0218:                        putSchemaField(conn, field, schemaId);
0219:                        fields[2] = field.getIdentifier();
0220:                        ok = m_sqlService.dbWrite(conn, statement, fields);
0221:
0222:                    }
0223:
0224:                    Iterator namespaceIt = schema.getNamespaceAbbreviations()
0225:                            .iterator();
0226:                    while (namespaceIt.hasNext()) {
0227:                        String abbrev = (String) namespaceIt.next();
0228:                        String namespaceUri = schema.getNamespaceUri(abbrev);
0229:                        if (abbrev != null && namespaceUri != null) {
0230:                            fields[0] = schemaId;
0231:                            fields[1] = PROPERTY_HAS_NAMESPACE;
0232:                            fields[2] = namespaceUri;
0233:                            ok = m_sqlService.dbWrite(conn, statement, fields);
0234:
0235:                            fields[0] = namespaceUri;
0236:                            fields[1] = PROPERTY_HAS_ABBREVIATION;
0237:                            fields[2] = abbrev;
0238:                            ok = m_sqlService.dbWrite(conn, statement, fields);
0239:                        }
0240:                    }
0241:
0242:                    if (schema.getNamespaceAbbrev() != null
0243:                            && !"".equals(schema.getNamespaceAbbrev().trim())) {
0244:                        fields[0] = schemaId;
0245:                        fields[1] = PROPERTY_NAMESPACE;
0246:                        fields[2] = schema.getNamespaceAbbrev();
0247:                        ok = m_sqlService.dbWrite(conn, statement, fields);
0248:                    }
0249:
0250:                    return schema;
0251:                }
0252:
0253:                public Schema addSchema(Schema schema) {
0254:                    Connection conn = null;
0255:                    int wasCommit = AUTO_UNKNOWN;
0256:
0257:                    try {
0258:                        conn = m_sqlService.borrowConnection();
0259:                        wasCommit = getAutoCommit(conn);
0260:                        conn.setAutoCommit(false);
0261:
0262:                        schema = this .addSchema(conn, schema);
0263:
0264:                        conn.commit();
0265:                        restoreAutoCommit(conn, wasCommit);
0266:                        m_sqlService.returnConnection(conn);
0267:                    } catch (SQLException e) {
0268:                        errorCleanup(conn, wasCommit);
0269:                        M_log.warn("addSchema(" + schema + ") " + e);
0270:                    }
0271:                    return schema;
0272:                }
0273:
0274:                /* (non-Javadoc)
0275:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#checkCitation(java.lang.String)
0276:                 */
0277:                protected boolean checkCitation(Connection conn,
0278:                        String citationId) {
0279:                    String statement = "select " + m_citationTableId + " from "
0280:                            + m_citationTableName + " where ( "
0281:                            + m_citationTableId + " = ? )";
0282:
0283:                    Object fields[] = new Object[1];
0284:                    fields[0] = citationId;
0285:
0286:                    List rows = m_sqlService.dbRead(conn, statement, fields,
0287:                            null);
0288:
0289:                    boolean found = !rows.isEmpty();
0290:
0291:                    return found;
0292:                }
0293:
0294:                public boolean checkCitation(String citationId) {
0295:                    Connection conn = null;
0296:                    int wasCommit = AUTO_UNKNOWN;
0297:                    boolean check = false;
0298:
0299:                    try {
0300:                        conn = m_sqlService.borrowConnection();
0301:                        wasCommit = getAutoCommit(conn);
0302:                        conn.setAutoCommit(false);
0303:
0304:                        check = this .checkCitation(conn, citationId);
0305:
0306:                        conn.commit();
0307:                        restoreAutoCommit(conn, wasCommit);
0308:                        m_sqlService.returnConnection(conn);
0309:                    } catch (SQLException e) {
0310:                        errorCleanup(conn, wasCommit);
0311:                        M_log.warn("checkCitation(" + citationId + ") " + e);
0312:                    }
0313:                    return check;
0314:                }
0315:
0316:                /* (non-Javadoc)
0317:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#checkCollection(java.lang.String)
0318:                 */
0319:                protected boolean checkCollection(Connection conn,
0320:                        String collectionId) {
0321:                    String statement = "select " + m_collectionTableId
0322:                            + " from " + m_collectionTableName + " where ("
0323:                            + m_collectionTableId + " = ?)";
0324:
0325:                    Object fields[] = new Object[1];
0326:                    fields[0] = collectionId;
0327:
0328:                    List rows = m_sqlService.dbRead(conn, statement, fields,
0329:                            null);
0330:
0331:                    boolean found = !rows.isEmpty();
0332:
0333:                    return found;
0334:                }
0335:
0336:                public boolean checkCollection(String collectionId) {
0337:                    Connection conn = null;
0338:                    int wasCommit = AUTO_UNKNOWN;
0339:                    boolean check = false;
0340:                    try {
0341:                        conn = m_sqlService.borrowConnection();
0342:                        wasCommit = getAutoCommit(conn);
0343:                        conn.setAutoCommit(false);
0344:
0345:                        check = this .checkCollection(conn, collectionId);
0346:
0347:                        conn.commit();
0348:                        restoreAutoCommit(conn, wasCommit);
0349:                        m_sqlService.returnConnection(conn);
0350:                    } catch (SQLException e) {
0351:                        errorCleanup(conn, wasCommit);
0352:                        M_log
0353:                                .warn("checkCollection(" + collectionId + ") "
0354:                                        + e);
0355:                    }
0356:                    return check;
0357:                }
0358:
0359:                /* (non-Javadoc)
0360:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#checkSchema(java.lang.String)
0361:                 */
0362:                protected boolean checkSchema(Connection conn, String schemaId) {
0363:                    String statement = "select " + m_schemaTableId + " from "
0364:                            + m_schemaTableName + " where (" + m_schemaTableId
0365:                            + " = ?)";
0366:
0367:                    Object fields[] = new Object[1];
0368:                    fields[0] = schemaId;
0369:
0370:                    List rows = m_sqlService.dbRead(conn, statement, fields,
0371:                            null);
0372:
0373:                    boolean found = !rows.isEmpty();
0374:
0375:                    return found;
0376:                }
0377:
0378:                public boolean checkSchema(String schemaId) {
0379:                    Connection conn = null;
0380:                    int wasCommit = AUTO_UNKNOWN;
0381:                    boolean check = false;
0382:
0383:                    try {
0384:                        conn = m_sqlService.borrowConnection();
0385:                        wasCommit = getAutoCommit(conn);
0386:                        conn.setAutoCommit(false);
0387:
0388:                        check = this .checkSchema(conn, schemaId);
0389:
0390:                        conn.commit();
0391:                        restoreAutoCommit(conn, wasCommit);
0392:                        m_sqlService.returnConnection(conn);
0393:                    } catch (SQLException e) {
0394:                        errorCleanup(conn, wasCommit);
0395:                        M_log.warn("checkSchema(" + schemaId + ") " + e);
0396:                    }
0397:                    return check;
0398:                }
0399:
0400:                /* (non-Javadoc)
0401:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#close()
0402:                 */
0403:                public void close() {
0404:                }
0405:
0406:                protected CitationCollection copyAll(Connection conn,
0407:                        String collectionId) {
0408:                    CitationCollection original = this .getCollection(conn,
0409:                            collectionId);
0410:                    CitationCollection copy = null;
0411:
0412:                    if (original != null) {
0413:                        copy = new BasicCitationCollection();
0414:                        Iterator it = original.iterator();
0415:                        while (it.hasNext()) {
0416:                            BasicCitation citation = (BasicCitation) it.next();
0417:                            BasicCitation newCite = new BasicCitation(citation
0418:                                    .getSchema().getIdentifier());
0419:                            newCite.copy(citation);
0420:                            copy.add(newCite);
0421:                        }
0422:                        this .saveCollection(conn, copy);
0423:                    }
0424:
0425:                    return copy;
0426:                }
0427:
0428:                public CitationCollection copyAll(String collectionId) {
0429:                    CitationCollection copy = null;
0430:                    Connection conn = null;
0431:                    int wasCommit = AUTO_UNKNOWN;
0432:
0433:                    try {
0434:                        conn = m_sqlService.borrowConnection();
0435:                        wasCommit = getAutoCommit(conn);
0436:                        conn.setAutoCommit(false);
0437:
0438:                        copy = this .copyAll(conn, collectionId);
0439:
0440:                        conn.commit();
0441:                        restoreAutoCommit(conn, wasCommit);
0442:                        m_sqlService.returnConnection(conn);
0443:                    } catch (SQLException e) {
0444:                        errorCleanup(conn, wasCommit);
0445:                        M_log.warn("copyAll(" + collectionId + ") " + e);
0446:                    }
0447:                    return copy;
0448:                }
0449:
0450:                /**
0451:                 * @param citationId
0452:                 * @return
0453:                 */
0454:                protected boolean getAdded(Connection conn, String citationId) {
0455:                    String statement = "select PROPERTY_VALUE from "
0456:                            + m_citationTableName
0457:                            + " where (CITATION_ID = ? and PROPERTY_NAME = ?)";
0458:
0459:                    Object fields[] = new Object[2];
0460:                    fields[0] = citationId;
0461:                    fields[1] = PROP_ADDED;
0462:
0463:                    List list = m_sqlService.dbRead(conn, statement, fields,
0464:                            null);
0465:
0466:                    return !list.isEmpty();
0467:                }
0468:
0469:                /* (non-Javadoc)
0470:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#getCitation(java.lang.String)
0471:                 */
0472:                protected Citation getCitation(Connection conn,
0473:                        String citationId) {
0474:                    String schemaId = getMediatype(conn, citationId);
0475:                    boolean added = getAdded(conn, citationId);
0476:
0477:                    Schema schema = getSchema(conn, schemaId);
0478:
0479:                    BasicCitation edit = new BasicCitation(citationId, schema);
0480:                    edit.setAdded(added);
0481:
0482:                    String statement = "select CITATION_ID, PROPERTY_NAME, PROPERTY_VALUE from "
0483:                            + m_citationTableName
0484:                            + " where (CITATION_ID = ?)  order by PROPERTY_NAME";
0485:                    String urlStatement = "select CITATION_ID, PROPERTY_NAME, PROPERTY_VALUE from "
0486:                            + m_citationTableName + " where (CITATION_ID = ?)";
0487:
0488:                    Object fields[] = new Object[1];
0489:                    fields[0] = citationId;
0490:
0491:                    List triples = m_sqlService.dbRead(conn, statement, fields,
0492:                            new TripleReader());
0493:
0494:                    Iterator it = triples.iterator();
0495:                    while (it.hasNext()) {
0496:                        Triple triple = (Triple) it.next();
0497:                        if (triple.isValid()) {
0498:                            String name = triple.getName();
0499:                            String order = "0";
0500:                            Matcher matcher = MULTIVALUED_PATTERN.matcher(name);
0501:                            if (matcher.matches()) {
0502:                                name = matcher.group(1);
0503:                                order = matcher.group(2);
0504:                            }
0505:                            if (PROP_HAS_URL.equals(name)) {
0506:                                String id = (String) triple.getValue();
0507:                                fields[0] = id;
0508:                                List urlfields = m_sqlService.dbRead(conn,
0509:                                        statement, fields, new TripleReader());
0510:                                String label = null;
0511:                                String url = null;
0512:                                Iterator urlFieldIt = urlfields.iterator();
0513:                                while (urlFieldIt.hasNext()) {
0514:                                    Triple urlField = (Triple) urlFieldIt
0515:                                            .next();
0516:                                    if (PROP_URL_LABEL.equals(urlField
0517:                                            .getName())) {
0518:                                        label = (String) urlField.getValue();
0519:                                    } else if (PROP_URL_STRING.equals(urlField
0520:                                            .getName())) {
0521:                                        url = (String) urlField.getValue();
0522:                                    }
0523:                                }
0524:                                edit.m_urls.put(id, new UrlWrapper(label, url));
0525:                            } else if (isMultivalued(schemaId, name)) {
0526:                                edit.addPropertyValue(name, triple.getValue());
0527:                            } else {
0528:                                edit.setCitationProperty(name, triple
0529:                                        .getValue());
0530:                                if (PROP_DISPLAYNAME.equals(name.trim())) {
0531:                                    edit.setDisplayName(triple.getValue()
0532:                                            .toString());
0533:                                }
0534:                            }
0535:                        }
0536:                    }
0537:
0538:                    return edit;
0539:                }
0540:
0541:                public Citation getCitation(String citationId) {
0542:                    Citation citation = null;
0543:                    Connection conn = null;
0544:                    int wasCommit = AUTO_UNKNOWN;
0545:
0546:                    try {
0547:                        conn = m_sqlService.borrowConnection();
0548:                        wasCommit = getAutoCommit(conn);
0549:                        conn.setAutoCommit(false);
0550:
0551:                        citation = this .getCitation(conn, citationId);
0552:
0553:                        conn.commit();
0554:                        restoreAutoCommit(conn, wasCommit);
0555:                        m_sqlService.returnConnection(conn);
0556:                    } catch (SQLException e) {
0557:                        errorCleanup(conn, wasCommit);
0558:                        M_log.warn("getCitation(" + citationId + ") " + e);
0559:                    }
0560:                    return citation;
0561:                }
0562:
0563:                /* (non-Javadoc)
0564:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#getCollection(java.lang.String)
0565:                 */
0566:                protected CitationCollection getCollection(Connection conn,
0567:                        String collectionId) {
0568:                    String statement = "select COLLECTION_ID, PROPERTY_NAME, PROPERTY_VALUE from "
0569:                            + m_collectionTableName
0570:                            + " where (COLLECTION_ID = ?)";
0571:
0572:                    CitationCollection edit = new BasicCitationCollection(
0573:                            collectionId);
0574:
0575:                    Object fields[] = new Object[1];
0576:                    fields[0] = collectionId;
0577:
0578:                    List triples = m_sqlService.dbRead(conn, statement, fields,
0579:                            new TripleReader());
0580:
0581:                    Iterator it = triples.iterator();
0582:                    while (it.hasNext()) {
0583:                        Triple triple = (Triple) it.next();
0584:                        if (triple.isValid()) {
0585:                            if (triple.getName().equals(PROPERTY_HAS_CITATION)) {
0586:                                Citation citation = getCitation(conn,
0587:                                        (String) triple.getValue());
0588:                                edit.add(citation);
0589:                            }
0590:                            /*
0591:                             * TODO: else add property??
0592:                             */
0593:                        }
0594:                    }
0595:
0596:                    return edit;
0597:                }
0598:
0599:                public CitationCollection getCollection(String collectionId) {
0600:                    CitationCollection collection = null;
0601:                    Connection conn = null;
0602:                    int wasCommit = AUTO_UNKNOWN;
0603:
0604:                    try {
0605:                        conn = m_sqlService.borrowConnection();
0606:                        wasCommit = getAutoCommit(conn);
0607:                        conn.setAutoCommit(false);
0608:
0609:                        collection = this .getCollection(conn, collectionId);
0610:
0611:                        conn.commit();
0612:                        restoreAutoCommit(conn, wasCommit);
0613:                        m_sqlService.returnConnection(conn);
0614:                    } catch (SQLException e) {
0615:                        errorCleanup(conn, wasCommit);
0616:                        M_log.warn("getCollection(" + collectionId + ") " + e);
0617:                    }
0618:                    return collection;
0619:                }
0620:
0621:                protected String getMediatype(Connection conn, String citationId) {
0622:                    String statement = "select PROPERTY_VALUE from "
0623:                            + m_citationTableName
0624:                            + " where (CITATION_ID = ? and PROPERTY_NAME = ?)";
0625:
0626:                    Object fields[] = new Object[2];
0627:                    fields[0] = citationId;
0628:                    fields[1] = PROP_MEDIATYPE;
0629:
0630:                    List list = m_sqlService.dbRead(conn, statement, fields,
0631:                            null);
0632:
0633:                    String rv = UNKNOWN_TYPE;
0634:                    if (!list.isEmpty()) {
0635:                        rv = (String) list.get(0);
0636:                    }
0637:
0638:                    return rv;
0639:                }
0640:
0641:                /* (non-Javadoc)
0642:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#getSchema(java.lang.String)
0643:                 */
0644:                protected Schema getSchema(Connection conn, String schemaId) {
0645:                    BasicSchema schema = (BasicSchema) ThreadLocalManager
0646:                            .get(schemaId);
0647:                    if (schema == null) {
0648:                        String statement = "select SCHEMA_ID, PROPERTY_NAME, PROPERTY_VALUE from "
0649:                                + m_schemaTableName + " where (SCHEMA_ID = ?)";
0650:
0651:                        schema = new BasicSchema(schemaId);
0652:
0653:                        Object fields[] = new Object[1];
0654:                        fields[0] = schemaId;
0655:
0656:                        List triples = m_sqlService.dbRead(conn, statement,
0657:                                fields, new TripleReader());
0658:
0659:                        Iterator it = triples.iterator();
0660:                        while (it.hasNext()) {
0661:                            Triple triple = (Triple) it.next();
0662:                            if (triple.isValid()) {
0663:                                if (triple.getName().equals(PROPERTY_HAS_FIELD)) {
0664:                                    String fieldId = (String) triple.getValue();
0665:                                    BasicField field = getSchemaField(conn,
0666:                                            schemaId, fieldId);
0667:                                    schema.addField(field);
0668:                                }
0669:                                /*
0670:                                 * TODO: else add property??
0671:                                 */
0672:                            }
0673:                        }
0674:
0675:                        schema.sortFields();
0676:                        ThreadLocalManager.set(schemaId,
0677:                                new BasicSchema(schema));
0678:                    } else {
0679:                        schema = new BasicSchema(schema);
0680:                    }
0681:
0682:                    return schema;
0683:                }
0684:
0685:                public Schema getSchema(String schemaId) {
0686:                    Connection conn = null;
0687:                    int wasCommit = AUTO_UNKNOWN;
0688:                    BasicSchema schema = (BasicSchema) ThreadLocalManager
0689:                            .get(schemaId);
0690:
0691:                    if (schema == null) {
0692:                        try {
0693:                            conn = m_sqlService.borrowConnection();
0694:                            wasCommit = getAutoCommit(conn);
0695:                            conn.setAutoCommit(false);
0696:
0697:                            schema = (BasicSchema) this .getSchema(conn,
0698:                                    schemaId);
0699:
0700:                            conn.commit();
0701:                            restoreAutoCommit(conn, wasCommit);
0702:                            m_sqlService.returnConnection(conn);
0703:                        } catch (SQLException e) {
0704:                            errorCleanup(conn, wasCommit);
0705:                            M_log.warn("getSchema(" + schemaId + ") " + e);
0706:                        }
0707:                        // ThreadLocalManager.set(schemaId, new BasicSchema(schema));
0708:                    } else {
0709:                        schema = new BasicSchema(schema);
0710:                    }
0711:
0712:                    return schema;
0713:                }
0714:
0715:                /**
0716:                 * @param fieldId
0717:                 * @return
0718:                 */
0719:                protected BasicField getSchemaField(Connection conn,
0720:                        String schemaId, String fieldId) {
0721:                    String statement = "select FIELD_ID, PROPERTY_NAME, PROPERTY_VALUE from "
0722:                            + m_schemaFieldTableName
0723:                            + " where (SCHEMA_ID = ? and FIELD_ID = ?)";
0724:
0725:                    Object[] fields = new Object[2];
0726:                    fields[0] = schemaId;
0727:                    fields[1] = fieldId;
0728:
0729:                    Map values = new Hashtable();
0730:
0731:                    List triples = m_sqlService.dbRead(conn, statement, fields,
0732:                            new TripleReader());
0733:                    Iterator tripleIt = triples.iterator();
0734:                    while (tripleIt.hasNext()) {
0735:                        Triple triple = (Triple) tripleIt.next();
0736:
0737:                        // PROPERTY_NAMESPACE namespace;
0738:                        if (triple.isValid()) {
0739:                            values.put(triple.getName(), triple.getValue());
0740:                        }
0741:                    }
0742:                    String valueType = (String) values
0743:                            .remove(PROPERTY_VALUETYPE);
0744:                    String required = (String) values.remove(PROPERTY_REQUIRED);
0745:                    boolean isRequired = Boolean.TRUE.toString()
0746:                            .equalsIgnoreCase(required);
0747:
0748:                    String maxCardinality = (String) values
0749:                            .remove(PROPERTY_MAXCARDINALITY);
0750:                    int max = 1;
0751:                    if (maxCardinality != null) {
0752:                        max = new Integer(maxCardinality).intValue();
0753:                    }
0754:                    String minCardinality = (String) values
0755:                            .remove(PROPERTY_MINCARDINALITY);
0756:                    int min = 0;
0757:                    if (minCardinality != null) {
0758:                        min = new Integer(minCardinality).intValue();
0759:                    }
0760:
0761:                    BasicField field = new BasicField(fieldId, valueType, true,
0762:                            isRequired, min, max);
0763:
0764:                    // PROPERTY_HAS_ORDER
0765:                    String order = (String) values.remove(PROPERTY_HAS_ORDER);
0766:                    if (order != null) {
0767:                        try {
0768:                            Integer o = new Integer(order);
0769:                            field.setOrder(o.intValue());
0770:                        } catch (Exception e) {
0771:                            // couldn't get integer out of the value
0772:                        }
0773:                    }
0774:
0775:                    // PROPERTY_NAMESPACE
0776:                    String namespace = (String) values
0777:                            .remove(PROPERTY_NAMESPACE);
0778:                    if (namespace != null) {
0779:                        field.setNamespaceAbbreviation(namespace);
0780:                    }
0781:                    // PROPERTY_LABEL label;
0782:                    String label = (String) values.remove(PROPERTY_LABEL);
0783:                    if (label != null) {
0784:                        field.setLabel(label);
0785:                    }
0786:                    // PROPERTY_DESCRIPTION description;
0787:                    String description = (String) values
0788:                            .remove(PROPERTY_DESCRIPTION);
0789:                    if (description != null) {
0790:                        field.setDescription(description);
0791:                    }
0792:                    // PROPERTY_DEFAULTVALUE defaultValue;
0793:                    String defaultValue = (String) values
0794:                            .remove(PROPERTY_DEFAULTVALUE);
0795:                    if (defaultValue == null) {
0796:                        // do nothing
0797:                    } else if (String.class.getName().equalsIgnoreCase(
0798:                            valueType)) {
0799:                        field.setDefaultValue(defaultValue);
0800:                    } else if (Integer.class.getName().equalsIgnoreCase(
0801:                            valueType)) {
0802:                        field.setDefaultValue(new Integer(defaultValue));
0803:                    } else if (Boolean.class.getName().equalsIgnoreCase(
0804:                            valueType)) {
0805:                        field.setDefaultValue(new Boolean(defaultValue));
0806:                    } else if (Time.class.getName().equalsIgnoreCase(valueType)) {
0807:                        field.setDefaultValue(TimeService.newTime(new Integer(
0808:                                defaultValue).longValue()));
0809:                    }
0810:                    // PROP_HAS_RIS_IDENTIFIER RIS identifier
0811:                    String risIdentifier = (String) values
0812:                            .remove(PROP_HAS_RIS_IDENTIFIER);
0813:                    if (risIdentifier != null) {
0814:                        field.setIdentifier(RIS_FORMAT, risIdentifier);
0815:                    }
0816:
0817:                    return field;
0818:                }
0819:
0820:                public List getSchemas() {
0821:                    List schemas = (List) ThreadLocalManager
0822:                            .get("DbCitationStorage.getSchemas");
0823:
0824:                    if (schemas == null) {
0825:                        Connection conn = null;
0826:                        int wasCommit = AUTO_UNKNOWN;
0827:
0828:                        try {
0829:                            conn = m_sqlService.borrowConnection();
0830:                            wasCommit = getAutoCommit(conn);
0831:                            conn.setAutoCommit(false);
0832:
0833:                            schemas = this .getSchemas(conn);
0834:
0835:                            conn.commit();
0836:                            restoreAutoCommit(conn, wasCommit);
0837:                            m_sqlService.returnConnection(conn);
0838:                        } catch (SQLException e) {
0839:                            errorCleanup(conn, wasCommit);
0840:                            M_log.warn("getSchemas() " + e);
0841:                        }
0842:                    } else {
0843:                        List rv = new Vector();
0844:                        Iterator it = schemas.iterator();
0845:                        while (it.hasNext()) {
0846:                            Schema schema = (Schema) it.next();
0847:                            rv.add(new BasicSchema(schema));
0848:                        }
0849:                        schemas = rv;
0850:                    }
0851:                    return schemas;
0852:                }
0853:
0854:                /* (non-Javadoc)
0855:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#getSchemas()
0856:                 */
0857:                protected List getSchemas(Connection conn) {
0858:                    List schemas = (List) ThreadLocalManager
0859:                            .get("DbCitationStorage.getSchemas");
0860:                    if (schemas == null) {
0861:                        String statement = "select SCHEMA_ID, PROPERTY_NAME, PROPERTY_VALUE from "
0862:                                + m_schemaTableName + " order by SCHEMA_ID";
0863:
0864:                        List triples = m_sqlService.dbRead(conn, statement,
0865:                                null, new TripleReader());
0866:
0867:                        schemas = new Vector();
0868:
0869:                        BasicSchema schema = null;
0870:                        String schemaId = "";
0871:                        Iterator it = triples.iterator();
0872:                        while (it.hasNext()) {
0873:                            Triple triple = (Triple) it.next();
0874:                            if (triple.isValid()) {
0875:                                if (!schemaId.equals(triple.getId())) {
0876:                                    schemaId = triple.getId();
0877:                                    schema = new BasicSchema(schemaId);
0878:                                    schemas.add(schema);
0879:                                }
0880:                                if (triple.getName().equals(PROPERTY_HAS_FIELD)) {
0881:                                    String fieldId = (String) triple.getValue();
0882:                                    BasicField field = getSchemaField(conn,
0883:                                            schemaId, fieldId);
0884:                                    schema.addField(field);
0885:                                }
0886:                                /*
0887:                                 * TODO: else add property??
0888:                                 */
0889:                            }
0890:                        }
0891:                        Iterator schemaIt = schemas.iterator();
0892:                        while (schemaIt.hasNext()) {
0893:                            BasicSchema sch = (BasicSchema) schemaIt.next();
0894:                            sch.sortFields();
0895:                        }
0896:                        ThreadLocalManager.set("DbCitationStorage.getSchemas",
0897:                                schemas);
0898:                    }
0899:
0900:                    List rv = new Vector();
0901:                    Iterator it = schemas.iterator();
0902:                    while (it.hasNext()) {
0903:                        Schema schema = (Schema) it.next();
0904:                        rv.add(new BasicSchema(schema));
0905:                    }
0906:                    schemas = rv;
0907:
0908:                    return schemas;
0909:                }
0910:
0911:                public List listSchemas() {
0912:                    Connection conn = null;
0913:                    int wasCommit = AUTO_UNKNOWN;
0914:                    List schemaIds = (List) ThreadLocalManager
0915:                            .get("DbCitationStorage.listSchemas");
0916:
0917:                    if (schemaIds == null) {
0918:                        try {
0919:                            conn = m_sqlService.borrowConnection();
0920:                            wasCommit = getAutoCommit(conn);
0921:                            conn.setAutoCommit(false);
0922:
0923:                            schemaIds = this .listSchemas(conn);
0924:
0925:                            conn.commit();
0926:                            restoreAutoCommit(conn, wasCommit);
0927:                            m_sqlService.returnConnection(conn);
0928:                        } catch (SQLException e) {
0929:                            errorCleanup(conn, wasCommit);
0930:                            M_log.warn("listSchemas() " + e);
0931:                        }
0932:                    } else {
0933:                        schemaIds = new Vector(schemaIds);
0934:                    }
0935:
0936:                    return schemaIds;
0937:                }
0938:
0939:                /* (non-Javadoc)
0940:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#getSchemaNames()
0941:                 */
0942:                protected List listSchemas(Connection conn) {
0943:                    List schemaIds = (List) ThreadLocalManager
0944:                            .get("DbCitationStorage.listSchemas");
0945:                    if (schemaIds == null) {
0946:                        String statement = "select distinct SCHEMA_ID from "
0947:                                + m_schemaTableName + " order by SCHEMA_ID";
0948:
0949:                        schemaIds = m_sqlService.dbRead(conn, statement, null,
0950:                                null);
0951:
0952:                        ThreadLocalManager.set("DbCitationStorage.listSchemas",
0953:                                schemaIds);
0954:                    }
0955:
0956:                    return new Vector(schemaIds);
0957:                }
0958:
0959:                /* (non-Javadoc)
0960:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#open()
0961:                 */
0962:                public void open() {
0963:                }
0964:
0965:                protected void putSchemaField(Connection conn, Field field,
0966:                        String schemaId) {
0967:                    String statement = "insert into "
0968:                            + m_schemaFieldTableName
0969:                            + " ("
0970:                            + m_schemaTableId
0971:                            + ","
0972:                            + m_schemaFieldTableId
0973:                            + ",PROPERTY_NAME,PROPERTY_VALUE) values ( ?, ?, ?, ? )";
0974:
0975:                    boolean ok = true;
0976:
0977:                    Object[] fields = new Object[4];
0978:                    fields[0] = schemaId;
0979:                    fields[1] = field.getIdentifier();
0980:
0981:                    if (field instanceof  BasicField) {
0982:                        int order = ((BasicField) field).getOrder();
0983:                        fields[2] = PROPERTY_HAS_ORDER;
0984:                        fields[3] = new Integer(order).toString();
0985:
0986:                        // process the insert
0987:                        ok = m_sqlService.dbWrite(conn, statement, fields);
0988:                    }
0989:
0990:                    // PROPERTY_NAMESPACE namespace;
0991:                    if (field.getNamespaceAbbreviation() != null
0992:                            && !"".equals(field.getNamespaceAbbreviation()
0993:                                    .trim())) {
0994:                        fields[2] = PROPERTY_NAMESPACE;
0995:                        fields[3] = field.getNamespaceAbbreviation();
0996:
0997:                        // process the insert
0998:                        ok = m_sqlService.dbWrite(conn, statement, fields);
0999:                    }
1000:                    // PROPERTY_LABEL label;
1001:                    if (field.getLabel() != null
1002:                            && !"".equals(field.getLabel().trim())) {
1003:                        fields[2] = PROPERTY_LABEL;
1004:                        fields[3] = field.getLabel();
1005:
1006:                        // process the insert
1007:                        ok = m_sqlService.dbWrite(conn, statement, fields);
1008:                    }
1009:                    // PROPERTY_DESCRIPTION description;
1010:                    if (field.getDescription() != null
1011:                            && !"".equals(field.getDescription().trim())) {
1012:                        fields[2] = PROPERTY_DESCRIPTION;
1013:                        fields[3] = field.getDescription();
1014:
1015:                        // process the insert
1016:                        ok = m_sqlService.dbWrite(conn, statement, fields);
1017:                    }
1018:                    // PROPERTY_REQUIRED required;
1019:                    fields[2] = PROPERTY_REQUIRED;
1020:                    fields[3] = new Boolean(field.isRequired()).toString();
1021:                    ok = m_sqlService.dbWrite(conn, statement, fields);
1022:                    // PROPERTY_MINCARDINALITY minCardinality;
1023:                    fields[2] = PROPERTY_MINCARDINALITY;
1024:                    fields[3] = new Integer(field.getMinCardinality())
1025:                            .toString();
1026:                    ok = m_sqlService.dbWrite(conn, statement, fields);
1027:                    // PROPERTY_MAXCARDINALITY maxCardinality;
1028:                    fields[2] = PROPERTY_MAXCARDINALITY;
1029:                    fields[3] = new Integer(field.getMaxCardinality())
1030:                            .toString();
1031:                    ok = m_sqlService.dbWrite(conn, statement, fields);
1032:                    // PROPERTY_DEFAULTVALUE defaultValue;
1033:                    if (field.getDefaultValue() != null) {
1034:                        fields[2] = PROPERTY_DEFAULTVALUE;
1035:                        fields[3] = field.getDefaultValue().toString();
1036:
1037:                        // process the insert
1038:                        ok = m_sqlService.dbWrite(conn, statement, fields);
1039:                    }
1040:                    // PROPERTY_VALUETYPE valueType;
1041:                    if (field.getValueType() != null
1042:                            && !"".equals(field.getValueType().trim())) {
1043:                        fields[2] = PROPERTY_VALUETYPE;
1044:                        fields[3] = field.getValueType();
1045:
1046:                        // process the insert
1047:                        ok = m_sqlService.dbWrite(conn, statement, fields);
1048:                    }
1049:                    String risIdentifier = field.getIdentifier(RIS_FORMAT);
1050:                    if (risIdentifier != null
1051:                            && !risIdentifier.trim().equals("")) {
1052:                        fields[2] = PROP_HAS_RIS_IDENTIFIER;
1053:                        fields[3] = risIdentifier;
1054:
1055:                        // process the insert
1056:                        ok = m_sqlService.dbWrite(conn, statement, fields);
1057:                    }
1058:                }
1059:
1060:                public void putSchemas(Collection schemas) {
1061:                    Connection conn = null;
1062:                    int wasCommit = AUTO_UNKNOWN;
1063:
1064:                    try {
1065:                        conn = m_sqlService.borrowConnection();
1066:                        wasCommit = getAutoCommit(conn);
1067:                        conn.setAutoCommit(false);
1068:
1069:                        this .putSchemas(conn, schemas);
1070:
1071:                        conn.commit();
1072:                        restoreAutoCommit(conn, wasCommit);
1073:                        m_sqlService.returnConnection(conn);
1074:                    } catch (SQLException e) {
1075:                        errorCleanup(conn, wasCommit);
1076:                        M_log.warn("putSchema(...) " + e);
1077:                    }
1078:                }
1079:
1080:                /* (non-Javadoc)
1081:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#putSchemas(java.util.Collection)
1082:                 */
1083:                protected void putSchemas(Connection conn, Collection schemas) {
1084:                    Iterator it = schemas.iterator();
1085:                    while (it.hasNext()) {
1086:                        Schema schema = (Schema) it.next();
1087:                        addSchema(conn, schema);
1088:                        // ThreadLocalManager.set(schema.getIdentifier(), schema);
1089:                    }
1090:
1091:                    //ThreadLocalManager.set("DbCitationStorage.listSchemas", null);
1092:                    //ThreadLocalManager.set("DbCitationStorage.getSchemas", null);
1093:
1094:                }
1095:
1096:                public void removeCitation(Citation edit) {
1097:                    Connection conn = null;
1098:                    int wasCommit = AUTO_UNKNOWN;
1099:
1100:                    try {
1101:                        conn = m_sqlService.borrowConnection();
1102:                        wasCommit = getAutoCommit(conn);
1103:                        conn.setAutoCommit(false);
1104:
1105:                        this .removeCitation(conn, edit);
1106:
1107:                        conn.commit();
1108:                        restoreAutoCommit(conn, wasCommit);
1109:                        m_sqlService.returnConnection(conn);
1110:                    } catch (SQLException e) {
1111:                        errorCleanup(conn, wasCommit);
1112:                        M_log.warn("removeCitation(" + edit.getId() + ") " + e);
1113:                    }
1114:                }
1115:
1116:                /* (non-Javadoc)
1117:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#removeCitation(org.sakaiproject.citation.api.Citation)
1118:                 */
1119:                protected void removeCitation(Connection conn, Citation edit) {
1120:                    String statement = "delete from " + m_citationTableName
1121:                            + " where (" + m_citationTableId + " = ?)";
1122:
1123:                    Object fields[] = new Object[1];
1124:                    fields[0] = edit.getId();
1125:
1126:                    boolean ok = m_sqlService.dbWrite(conn, statement, fields);
1127:                }
1128:
1129:                public void removeCollection(CitationCollection edit) {
1130:                    Connection conn = null;
1131:                    int wasCommit = AUTO_UNKNOWN;
1132:
1133:                    try {
1134:                        conn = m_sqlService.borrowConnection();
1135:                        wasCommit = getAutoCommit(conn);
1136:                        conn.setAutoCommit(false);
1137:
1138:                        this .removeCollection(conn, edit);
1139:
1140:                        conn.commit();
1141:                        restoreAutoCommit(conn, wasCommit);
1142:                        m_sqlService.returnConnection(conn);
1143:                    } catch (SQLException e) {
1144:                        errorCleanup(conn, wasCommit);
1145:                        M_log.warn("removeCollection(" + edit + ") " + e);
1146:                    }
1147:                }
1148:
1149:                /* (non-Javadoc)
1150:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#removeCollection(org.sakaiproject.citation.api.CitationCollection)
1151:                 */
1152:                protected void removeCollection(Connection conn,
1153:                        CitationCollection edit) {
1154:                    String statement = "delete from " + m_collectionTableName
1155:                            + " where (" + m_collectionTableId + " = ?)";
1156:
1157:                    Object fields[] = new Object[1];
1158:                    fields[0] = edit.getId();
1159:
1160:                    boolean ok = m_sqlService.dbWrite(conn, statement, fields);
1161:                }
1162:
1163:                /* (non-Javadoc)
1164:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#removeSchema(org.sakaiproject.citation.api.Schema)
1165:                 */
1166:                protected void removeSchema(Connection conn, Schema schema) {
1167:                    String statement = "delete from " + m_schemaTableName
1168:                            + " where (" + m_schemaTableId + " = ?)";
1169:
1170:                    Object fields[] = new Object[1];
1171:                    fields[0] = schema.getIdentifier();
1172:
1173:                    boolean ok = m_sqlService.dbWrite(conn, statement, fields);
1174:
1175:                    // need to remove schema fields also
1176:                    if (ok) {
1177:                        String statement2 = "delete from "
1178:                                + m_schemaFieldTableName + " where ("
1179:                                + m_schemaTableId + " = ?)";
1180:
1181:                        ok = m_sqlService.dbWrite(conn, statement2, fields);
1182:                    }
1183:
1184:                }
1185:
1186:                public void removeSchema(Schema schema) {
1187:                    Connection conn = null;
1188:                    int wasCommit = AUTO_UNKNOWN;
1189:
1190:                    try {
1191:                        conn = m_sqlService.borrowConnection();
1192:                        wasCommit = getAutoCommit(conn);
1193:                        conn.setAutoCommit(false);
1194:
1195:                        this .removeSchema(conn, schema);
1196:
1197:                        conn.commit();
1198:                        restoreAutoCommit(conn, wasCommit);
1199:                        m_sqlService.returnConnection(conn);
1200:                    } catch (SQLException e) {
1201:                        errorCleanup(conn, wasCommit);
1202:                        M_log.warn("removeSchema(" + schema + ") " + e);
1203:                    }
1204:                }
1205:
1206:                /**
1207:                 * @param id
1208:                 */
1209:                protected void removeUrl(Connection conn, String id) {
1210:                    String statement = "delete from " + m_citationTableName
1211:                            + " where (" + m_citationTableId + " = ?)";
1212:
1213:                    Object fields[] = new Object[1];
1214:                    fields[0] = id;
1215:
1216:                    boolean ok = m_sqlService.dbWrite(conn, statement, fields);
1217:                }
1218:
1219:                public void saveCitation(Citation edit) {
1220:                    Connection conn = null;
1221:                    int wasCommit = AUTO_UNKNOWN;
1222:
1223:                    try {
1224:                        conn = m_sqlService.borrowConnection();
1225:                        wasCommit = getAutoCommit(conn);
1226:                        conn.setAutoCommit(false);
1227:
1228:                        this .saveCitation(conn, edit);
1229:
1230:                        conn.commit();
1231:                        restoreAutoCommit(conn, wasCommit);
1232:                        m_sqlService.returnConnection(conn);
1233:                    } catch (SQLException e) {
1234:                        errorCleanup(conn, wasCommit);
1235:                        M_log.warn("saveCitation(" + edit + ") " + e);
1236:                    }
1237:                }
1238:
1239:                /* (non-Javadoc)
1240:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#saveCitation(org.sakaiproject.citation.api.Citation)
1241:                 */
1242:                protected void saveCitation(Connection conn, Citation citation) {
1243:                    removeCitation(conn, citation);
1244:
1245:                    String statement = "insert into "
1246:                            + m_citationTableName
1247:                            + " ("
1248:                            + m_citationTableId
1249:                            + ", PROPERTY_NAME, PROPERTY_VALUE) values ( ?, ?, ? )";
1250:
1251:                    String citationId = citation.getId();
1252:
1253:                    boolean ok = true;
1254:                    Object[] fields = new Object[3];
1255:                    fields[0] = citationId;
1256:                    if (citation.getSchema() != null) {
1257:                        fields[1] = PROP_MEDIATYPE;
1258:                        fields[2] = citation.getSchema().getIdentifier();
1259:                        ok = m_sqlService.dbWrite(conn, statement, fields);
1260:                    }
1261:
1262:                    String displayName = citation.getDisplayName();
1263:                    if (displayName != null) {
1264:                        fields[1] = PROP_DISPLAYNAME;
1265:                        fields[2] = displayName.trim();
1266:                        ok = m_sqlService.dbWrite(conn, statement, fields);
1267:
1268:                    }
1269:
1270:                    if (citation.isAdded()) {
1271:                        fields[1] = PROP_ADDED;
1272:                        fields[2] = Boolean.TRUE.toString();
1273:                        ok = m_sqlService.dbWrite(conn, statement, fields);
1274:                    }
1275:
1276:                    List names = citation.listCitationProperties();
1277:                    Iterator nameIt = names.iterator();
1278:                    while (nameIt.hasNext()) {
1279:                        String name = (String) nameIt.next();
1280:                        Object value = citation.getCitationProperty(name);
1281:                        if (value instanceof  List) {
1282:                            List list = (List) value;
1283:                            for (int i = 0; i < list.size(); i++) {
1284:                                Object item = list.get(i);
1285:                                fields[1] = name + PROPERTY_NAME_DELIMITOR + i;
1286:                                fields[2] = item;
1287:
1288:                                ok = m_sqlService.dbWrite(conn, statement,
1289:                                        fields);
1290:
1291:                            }
1292:                        } else if (value instanceof  String) {
1293:                            fields[1] = name;
1294:                            fields[2] = value;
1295:
1296:                            ok = m_sqlService.dbWrite(conn, statement, fields);
1297:                        } else {
1298:                            M_log
1299:                                    .info("DbCitationStorage.saveCitation value not List or String: "
1300:                                            + value.getClass()
1301:                                                    .getCanonicalName()
1302:                                            + " "
1303:                                            + value);
1304:                            fields[1] = name;
1305:                            fields[2] = value;
1306:
1307:                            ok = m_sqlService.dbWrite(conn, statement, fields);
1308:                        }
1309:                    }
1310:
1311:                    int urlCount = 0;
1312:                    Map urls = ((BasicCitation) citation).m_urls;
1313:                    if (urls != null) {
1314:                        Iterator urlIt = urls.keySet().iterator();
1315:                        while (urlIt.hasNext()) {
1316:                            String id = (String) urlIt.next();
1317:                            UrlWrapper wrapper = (UrlWrapper) urls.get(id);
1318:                            fields[1] = PROP_HAS_URL + PROPERTY_NAME_DELIMITOR
1319:                                    + urlCount;
1320:                            fields[2] = id;
1321:                            ok = m_sqlService.dbWrite(conn, statement, fields);
1322:                            saveUrl(conn, id, wrapper.getLabel(), wrapper
1323:                                    .getUrl());
1324:                            urlCount++;
1325:                        }
1326:                    }
1327:                }
1328:
1329:                public void saveCollection(CitationCollection collection) {
1330:                    Connection conn = null;
1331:                    int wasCommit = AUTO_UNKNOWN;
1332:
1333:                    try {
1334:                        conn = m_sqlService.borrowConnection();
1335:                        wasCommit = getAutoCommit(conn);
1336:                        conn.setAutoCommit(false);
1337:
1338:                        this .saveCollection(conn, collection);
1339:
1340:                        conn.commit();
1341:                        restoreAutoCommit(conn, wasCommit);
1342:                        m_sqlService.returnConnection(conn);
1343:                    } catch (SQLException e) {
1344:                        errorCleanup(conn, wasCommit);
1345:                        M_log.warn("saveCollection(" + collection + ") " + e);
1346:                    }
1347:                }
1348:
1349:                /* (non-Javadoc)
1350:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#saveCollection(java.util.Collection)
1351:                 */
1352:                protected void saveCollection(Connection conn,
1353:                        CitationCollection collection) {
1354:                    removeCollection(conn, collection);
1355:
1356:                    String statement = "insert into "
1357:                            + m_collectionTableName
1358:                            + " ("
1359:                            + m_collectionTableId
1360:                            + ",PROPERTY_NAME,PROPERTY_VALUE) values ( ?, ?, ? )";
1361:
1362:                    boolean ok = true;
1363:
1364:                    String collectionId = collection.getId();
1365:                    Object[] fields = new Object[3];
1366:                    fields[0] = collectionId;
1367:
1368:                    List members = collection.getCitations();
1369:                    Iterator citationIt = members.iterator();
1370:                    while (citationIt.hasNext()) {
1371:                        Citation citation = (Citation) citationIt.next();
1372:
1373:                        save(citation);
1374:
1375:                        // process the insert
1376:                        fields[1] = PROPERTY_HAS_CITATION;
1377:                        fields[2] = citation.getId();
1378:                        ok = m_sqlService.dbWrite(conn, statement, fields);
1379:                    }
1380:                }
1381:
1382:                /**
1383:                 * @param id
1384:                 * @param label
1385:                 * @param url
1386:                 */
1387:                protected void saveUrl(Connection conn, String id,
1388:                        String label, String url) {
1389:                    removeUrl(conn, id);
1390:
1391:                    String statement = "insert into "
1392:                            + m_citationTableName
1393:                            + " ("
1394:                            + m_citationTableId
1395:                            + ", PROPERTY_NAME, PROPERTY_VALUE) values ( ?, ?, ? )";
1396:
1397:                    boolean ok = true;
1398:                    Object[] fields = new Object[3];
1399:                    fields[0] = id;
1400:                    fields[1] = PROP_URL_LABEL;
1401:                    fields[2] = label;
1402:                    ok = m_sqlService.dbWrite(conn, statement, fields);
1403:
1404:                    fields[1] = PROP_URL_STRING;
1405:                    fields[2] = url;
1406:                    ok = m_sqlService.dbWrite(conn, statement, fields);
1407:
1408:                }
1409:
1410:                /* (non-Javadoc)
1411:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#updateSchema(org.sakaiproject.citation.api.Schema)
1412:                 */
1413:                protected void updateSchema(Connection conn, Schema schema) {
1414:                    removeSchema(conn, schema);
1415:                    addSchema(conn, schema);
1416:                }
1417:
1418:                public void updateSchema(Schema schema) {
1419:                    Connection conn = null;
1420:                    int wasCommit = AUTO_UNKNOWN;
1421:
1422:                    try {
1423:                        conn = m_sqlService.borrowConnection();
1424:                        wasCommit = getAutoCommit(conn);
1425:                        conn.setAutoCommit(false);
1426:
1427:                        this .updateSchema(conn, schema);
1428:
1429:                        conn.commit();
1430:                        restoreAutoCommit(conn, wasCommit);
1431:                        m_sqlService.returnConnection(conn);
1432:                    } catch (SQLException e) {
1433:                        errorCleanup(conn, wasCommit);
1434:                        M_log.warn("updateSchema(" + schema + ") " + e);
1435:                    }
1436:                }
1437:
1438:                public void updateSchemas(Collection schemas) {
1439:                    Connection conn = null;
1440:                    int wasCommit = AUTO_UNKNOWN;
1441:
1442:                    try {
1443:                        conn = m_sqlService.borrowConnection();
1444:                        wasCommit = getAutoCommit(conn);
1445:                        conn.setAutoCommit(false);
1446:
1447:                        this .updateSchemas(conn, schemas);
1448:
1449:                        conn.commit();
1450:                        restoreAutoCommit(conn, wasCommit);
1451:                        m_sqlService.returnConnection(conn);
1452:                    } catch (SQLException e) {
1453:                        errorCleanup(conn, wasCommit);
1454:                        M_log.warn("updateSchemas(" + schemas + ") " + e);
1455:                    }
1456:                }
1457:
1458:                /* (non-Javadoc)
1459:                 * @see org.sakaiproject.citation.impl.BaseCitationService.Storage#updateSchemas(java.util.Collection)
1460:                 */
1461:                protected void updateSchemas(Connection conn, Collection schemas) {
1462:                    Iterator it = schemas.iterator();
1463:                    while (it.hasNext()) {
1464:                        Schema schema = (Schema) it.next();
1465:                        updateSchema(conn, schema);
1466:                    }
1467:                }
1468:
1469:            }
1470:
1471:            public class Triple {
1472:                protected String m_id;
1473:                protected String m_name;
1474:                protected Object m_value;
1475:
1476:                /**
1477:                 * @param id
1478:                 * @param name
1479:                 * @param value
1480:                 */
1481:                public Triple(String id, String name, Object value) {
1482:                    super ();
1483:                    m_id = id;
1484:                    m_name = name;
1485:                    m_value = value;
1486:                }
1487:
1488:                /**
1489:                 * @return the id
1490:                 */
1491:                public String getId() {
1492:                    return m_id;
1493:                }
1494:
1495:                /**
1496:                 * @return the name
1497:                 */
1498:                public String getName() {
1499:                    return m_name;
1500:                }
1501:
1502:                /**
1503:                 * @return the value
1504:                 */
1505:                public Object getValue() {
1506:                    return m_value;
1507:                }
1508:
1509:                /**
1510:                 * @return
1511:                 */
1512:                public boolean isValid() {
1513:                    return m_id != null && m_name != null && m_value != null;
1514:                }
1515:
1516:                /**
1517:                 * @param id the id to set
1518:                 */
1519:                public void setId(String id) {
1520:                    m_id = id;
1521:                }
1522:
1523:                /**
1524:                 * @param name the name to set
1525:                 */
1526:                public void setName(String name) {
1527:                    m_name = name;
1528:                }
1529:
1530:                /**
1531:                 * @param value the value to set
1532:                 */
1533:                public void setValue(Object value) {
1534:                    m_value = value;
1535:                }
1536:
1537:                public String toString() {
1538:                    return "[triple id = "
1539:                            + ((m_id == null) ? "null" : m_id)
1540:                            + " name = "
1541:                            + ((m_name == null) ? "null" : m_name)
1542:                            + " value = "
1543:                            + ((m_value == null) ? "null" : m_value.toString()
1544:                                    + "]");
1545:                }
1546:
1547:            }
1548:
1549:            public class TripleReader implements  SqlReader {
1550:
1551:                /* (non-Javadoc)
1552:                 * @see org.sakaiproject.db.api.SqlReader#readSqlResultRecord(java.sql.ResultSet)
1553:                 */
1554:                public Object readSqlResultRecord(ResultSet result) {
1555:                    Triple triple = null;
1556:                    try {
1557:                        String citationId = result.getString(1);
1558:                        String name = result.getString(2);
1559:                        Object value = result.getObject(3);
1560:
1561:                        triple = new Triple(citationId, name, value);
1562:                    } catch (SQLException e) {
1563:                        M_log.debug("TripleReader: problem reading triple: "
1564:                                + triple.toString());
1565:                    }
1566:                    return triple;
1567:                }
1568:
1569:            }
1570:
1571:            /** Our logger. */
1572:            private static Log M_log = LogFactory
1573:                    .getLog(DbCitationService.class);
1574:            protected static final Pattern MULTIVALUED_PATTERN = Pattern
1575:                    .compile("^(.*)\\t(\\d+)$");
1576:            protected static final String PROP_ADDED = "sakai:added";
1577:            protected static final String PROP_DISPLAYNAME = "sakai:displayname";
1578:            protected static final String PROP_HAS_RIS_IDENTIFIER = "sakai:ris_identifier";
1579:            protected static final String PROP_HAS_URL = "sakai:has_url";
1580:            protected static final String PROP_MEDIATYPE = "sakai:mediatype";
1581:
1582:            protected static final String PROP_URL_LABEL = "sakai:url_label";
1583:
1584:            protected static final String PROP_URL_STRING = "sakai:url_string";
1585:
1586:            protected static final String PROPERTY_NAME_DELIMITOR = "\t";
1587:
1588:            /** Configuration: to run the ddl on init or not. */
1589:            protected boolean m_autoDdl = false;
1590:
1591:            protected String m_citationTableId = "CITATION_ID";
1592:
1593:            /** Table name for citation. */
1594:            protected String m_citationTableName = "CITATION_CITATION";
1595:
1596:            protected String m_collectionTableId = "COLLECTION_ID";
1597:
1598:            /** Table name for collections. */
1599:            protected String m_collectionTableName = "CITATION_COLLECTION";
1600:
1601:            protected String m_schemaFieldTableId = "FIELD_ID";
1602:
1603:            protected String m_schemaFieldTableName = "CITATION_SCHEMA_FIELD";
1604:
1605:            protected String m_schemaTableId = "SCHEMA_ID";
1606:
1607:            /** Table name for schemas. */
1608:            protected String m_schemaTableName = "CITATION_SCHEMA";
1609:
1610:            /** Dependency: SqlService */
1611:            protected SqlService m_sqlService = null;
1612:
1613:            /* Connection management: Original auto-commit state (unknown, on, off) */
1614:            private static final int AUTO_UNKNOWN = 1;
1615:            private static final int AUTO_TRUE = 2;
1616:            private static final int AUTO_FALSE = 3;
1617:
1618:            public void init() {
1619:                try {
1620:                    // if we are auto-creating our schema, check and create
1621:                    if (m_autoDdl) {
1622:                        m_sqlService.ddl(this .getClass().getClassLoader(),
1623:                                "sakai_citation");
1624:                        M_log.info("init(): tables: " + m_collectionTableName
1625:                                + ", " + m_citationTableName + ", "
1626:                                + m_schemaTableName + ", "
1627:                                + m_schemaFieldTableName);
1628:                    }
1629:
1630:                    super .init();
1631:                } catch (Throwable t) {
1632:                    M_log.warn("init(): ", t);
1633:                }
1634:
1635:            } // init
1636:
1637:            /**
1638:             * Form a string of (field, field, field), for sql insert statements, one for each item in the fields array, plus one before, and one after.
1639:             *
1640:             * @param before
1641:             *        The first field name.
1642:             * @param values
1643:             *        The extra field names, in the middle.
1644:             * @param after
1645:             *        The last field name.
1646:             * @return A sql statement fragment for the insert fields.
1647:             */
1648:            protected String insertFields(String before, String[] fields,
1649:                    String after) {
1650:                StringBuffer buf = new StringBuffer();
1651:                buf.append(" (");
1652:
1653:                buf.append(before);
1654:
1655:                if (fields != null) {
1656:                    for (int i = 0; i < fields.length; i++) {
1657:                        if (i == 0 && before == null) {
1658:                            buf.append(fields[i]);
1659:                        } else {
1660:                            buf.append("," + fields[i]);
1661:                        }
1662:                    }
1663:                }
1664:
1665:                if (after != null) {
1666:                    buf.append("," + after);
1667:                }
1668:
1669:                buf.append(")");
1670:
1671:                return buf.toString();
1672:            }
1673:
1674:            /* (non-Javadoc)
1675:             * @see org.sakaiproject.citation.impl.BaseCitationService#newStorage()
1676:             */
1677:            public Storage newStorage() {
1678:                return new DbCitationStorage();
1679:            }
1680:
1681:            /**
1682:             * Configuration: to run the ddl on init or not.
1683:             *
1684:             * @param value
1685:             *        the auto ddl value.
1686:             */
1687:            public void setAutoDdl(String value) {
1688:                m_autoDdl = new Boolean(value).booleanValue();
1689:            }
1690:
1691:            /**
1692:             * Dependency: SqlService.
1693:             *
1694:             * @param service
1695:             *        The SqlService.
1696:             */
1697:            public void setSqlService(SqlService service) {
1698:                m_sqlService = service;
1699:            }
1700:
1701:            /*
1702:             * Connection management helpers
1703:             */
1704:            /**
1705:             * Determine auto-commit state
1706:             * @param conn Database Connection
1707:             * @return The original connection auto-commit state
1708:             */
1709:            private int getAutoCommit(Connection conn) {
1710:                try {
1711:                    boolean wasCommit = conn.getAutoCommit();
1712:
1713:                    return wasCommit ? AUTO_TRUE : AUTO_FALSE;
1714:                } catch (SQLException exception) {
1715:                    M_log.warn("restoreAutoCommit: " + exception);
1716:                    return AUTO_UNKNOWN;
1717:                }
1718:            }
1719:
1720:            /**
1721:             * Restore auto-commit state
1722:             *
1723:             * @param conn Database Connection
1724:             * @param wasCommit Original connection auto-commit state
1725:             */
1726:            private void restoreAutoCommit(Connection conn, int wasCommit) {
1727:                try {
1728:                    switch (wasCommit) {
1729:                    case AUTO_TRUE:
1730:                        conn.setAutoCommit(true);
1731:                        break;
1732:
1733:                    case AUTO_FALSE:
1734:                        conn.setAutoCommit(false);
1735:                        break;
1736:
1737:                    case AUTO_UNKNOWN:
1738:                        break;
1739:
1740:                    default:
1741:                        M_log.warn("restoreAutoCommit: unknown commit type: "
1742:                                + wasCommit);
1743:                        break;
1744:                    }
1745:                } catch (Throwable throwable) {
1746:                    M_log.warn("restoreAutoCommit: " + throwable);
1747:                }
1748:            }
1749:
1750:            /**
1751:             * Error handling: Rollback the transaction, restore auto-commit, and
1752:             * return the borrowed connection
1753:             *
1754:             * @param conn Database Connection
1755:             * @param wasCommit Original connection auto-commit state
1756:             */
1757:            private void errorCleanup(Connection conn, int wasCommit) {
1758:                if (conn != null) {
1759:                    try {
1760:                        conn.rollback();
1761:                    } catch (Throwable ignore) {
1762:                    }
1763:
1764:                    restoreAutoCommit(conn, wasCommit);
1765:                    m_sqlService.returnConnection(conn);
1766:                }
1767:            }
1768:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.