001: /*
002: * (C) Copyright 2000 - 2006 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portal.spi.impl.db;
020:
021: import java.rmi.RemoteException;
022: import java.sql.Connection;
023: import java.sql.PreparedStatement;
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: import javax.naming.NamingException;
030: import javax.xml.bind.JAXBException;
031:
032: import com.nabhinc.spi.AccessControlEntry;
033: import com.nabhinc.spi.AccessControlStore;
034: import com.nabhinc.spi.EntityExistsException;
035: import com.nabhinc.spi.NamedCondition;
036: import com.nabhinc.spi.NoSuchEntityException;
037: import com.nabhinc.spi.ResourceType;
038: import com.nabhinc.util.db.DBUtil;
039:
040: /**
041: *
042: *
043: * @author Padmanabh Dabke
044: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
045: */
046: public class AccessControlStoreDBImpl extends BaseDatabaseServiceImpl
047: implements AccessControlStore {
048:
049: private String acsGetNamedConditionWithIDSQL = "SELECT condid, name, descr, spec FROM SB_CONDITIONS WHERE condid = ?";
050:
051: private String acsGetNamedConditionWithNameSQL = "SELECT id, name, descr, spec FROM SB_CONDITIONS WHERE name = ?";
052:
053: private String acsCreateNamedConditionSQL = "INSERT INTO SB_CONDITIONS (name, descr, spec) VALUES (?, ?, ?)";
054:
055: private String acsDeleteNamedConditionSQL = "DELETE FROM SB_CONDITIONS WHERE condid = ?";
056:
057: private String acsUpdateNamedConditionSQL = "UPDATE SB_CONDITIONS SET name = ?, descr = ?, spec = ? WHERE condid = ?";
058:
059: private String acsSelectNamedConditionsSQL = "SELECT condid, name, descr, spec FROM SB_CONDITIONS";
060:
061: private String acsNamedConditionExistsSQL = "SELECT name FROM SB_CONDITIONS WHERE name = ? AND condid != ?";
062:
063: private String acsNamedConditionCountSQL = "SELECT count(*) FROM SB_CONDITIONS";
064:
065: private String acsGetResourceTypeWithNameSQL = "SELECT id, name, descr FROM SB_RESOURCE_TYPES WHERE name = ?";
066:
067: private String acsCreateResourceTypeSQL = "INSERT INTO SB_RESOURCE_TYPES (name, descr) VALUES (?, ?)";
068:
069: private String acsDeleteResourceTypeSQL = "DELETE FROM SB_RESOURCE_TYPES WHERE rtid = ?";
070:
071: private String acsUpdateResourceTypeSQL = "UPDATE SB_RESOURCE_TYPES SET name = ?, descr = ? WHERE rtid = ?";
072:
073: private String acsSelectResourceTypesSQL = "SELECT id, name, descr FROM SB_RESOURCE_TYPES";
074:
075: private String acsResourceTypeExistsSQL = "SELECT name FROM SB_RESOURCE_TYPES WHERE name = ? AND rtid != ?";
076:
077: private String acsGetResourceTypeWithIDSQL = "SELECT id, name, descr FROM SB_RESOURCE_TYPES WHERE rtid = ?";
078:
079: private String acsResourceTypeCountSQL = "SELECT count(*) FROM SB_RESOURCE_TYPES";
080:
081: private String acsGetEntryWithIDSQL = "SELECT SB_AC_ENTRIES.entryid, SB_AC_ENTRIES.resourcename, SB_AC_ENTRIES.resourcetype, SB_AC_ENTRIES.action, SB_CONDITIONS.condid, SB_CONDITIONS.name, SB_CONDITIONS.descr, SB_CONDITIONS.spec FROM SB_AC_ENTRIES, SB_CONDITIONS WHERE SB_AC_ENTRIES.entryid = ? AND SB_AC_ENTRIES.entryid = SB_CONDITIONS.condid";
082:
083: private String acsGetEntriesForResourceTypeSQL = "SELECT SB_AC_ENTRIES.entryid, SB_AC_ENTRIES.resourcename, SB_AC_ENTRIES.resourcetype, SB_AC_ENTRIES.action, SB_CONDITIONS.condid, SB_CONDITIONS.name, SB_CONDITIONS.descr, SB_CONDITIONS.spec FROM SB_AC_ENTRIES, SB_CONDITIONS WHERE SB_AC_ENTRIES.entryid = ? AND SB_AC_ENTRIES.resourcetype = ?";
084:
085: private String acsCreateEntrySQL = "INSERT INTO SB_AC_ENTRIES (resourcename, resourcetype, action, condid) VALUES (?, ?, ?, ?)";
086:
087: private String acsDeleteEntrySQL = "DELETE FROM SB_AC_ENTRIES WHERE entryid = ?";
088:
089: private String acsUpdateEntrySQL = "UPDATE SB_AC_ENTRIES SET resourcename = ?, resourcetype = ?, action = ?, condid = ? WHERE entryid = ?";
090:
091: private String acsSelectEntriesSQL = "SELECT SB_AC_ENTRIES.entryid, SB_AC_ENTRIES.resourcename, SB_AC_ENTRIES.resourcetype, SB_AC_ENTRIES.action, SB_CONDITIONS.condid, SB_CONDITIONS.name, SB_CONDITIONS.descr, SB_CONDITIONS.spec FROM SB_AC_ENTRIES, SB_CONDITIONS WHERE SB_AC_ENTRIES.entryid = SB_CONDITIONS.condid";
092:
093: private String acsEntryExistsSQL = "SELECT id FROM SB_AC_ENTRIES WHERE resourcename = ? AND resourcetype = ? AND action = ? AND entryid != ?";
094:
095: private String acsEntryCountSQL = "SELECT count(*) FROM SB_AC_ENTRIES";
096:
097: public NamedCondition getNamedCondition(long entryID)
098: throws NoSuchEntityException, RemoteException {
099: Connection conn = null;
100: ResultSet results = null;
101: PreparedStatement st = null;
102:
103: try {
104: conn = DBUtil.getConnection(bdsiDataSource);
105: st = conn.prepareStatement(acsGetNamedConditionWithIDSQL);
106: st.setLong(1, entryID);
107: results = st.executeQuery();
108: if (!results.next())
109: throw new NoSuchEntityException();
110: NamedCondition e = new NamedCondition();
111: e.setId(entryID);
112: e.setName(results.getString(1));
113: e.setDescription(results.getString(2));
114: e.setSpecification(results.getString(3));
115: return e;
116: } catch (NamingException ex) {
117: throw new RemoteException("Failed to look up data source: "
118: + bdsiDataSource, ex);
119: } catch (JAXBException e) {
120: throw new RemoteException(
121: "Failed to unmarshal condition spec.", e);
122: } catch (SQLException ex) {
123: throw new RemoteException("System exception.", ex);
124: } finally {
125: DBUtil.close(st);
126: DBUtil.close(results);
127: DBUtil.close(conn);
128: }
129: }
130:
131: public NamedCondition getNamedCondition(String entryName)
132: throws NoSuchEntityException, RemoteException {
133: Connection conn = null;
134: ResultSet results = null;
135: PreparedStatement st = null;
136:
137: try {
138: conn = DBUtil.getConnection(bdsiDataSource);
139: st = conn.prepareStatement(acsGetNamedConditionWithNameSQL);
140: st.setString(1, entryName);
141: results = st.executeQuery();
142: if (!results.next())
143: throw new NoSuchEntityException();
144: NamedCondition e = new NamedCondition();
145: e.setId(results.getLong(1));
146: e.setName(results.getString(2));
147: e.setDescription(results.getString(3));
148: e.setSpecification(results.getString(4));
149: return e;
150: } catch (NamingException ex) {
151: throw new RemoteException("Failed to look up data source: "
152: + bdsiDataSource, ex);
153: } catch (SQLException ex) {
154: throw new RemoteException("System exception.", ex);
155: } catch (JAXBException e) {
156: throw new RemoteException(
157: "Failed to unmarshal condition spec.", e);
158: } finally {
159: DBUtil.close(st);
160: DBUtil.close(results);
161: DBUtil.close(conn);
162: }
163: }
164:
165: public void createNamedCondition(NamedCondition entry)
166: throws EntityExistsException, RemoteException {
167: Connection conn = null;
168: PreparedStatement st = null;
169: ResultSet results = null;
170: try {
171: conn = DBUtil.getConnection(bdsiDataSource);
172:
173: // First check if a role with specified name already exists.
174: st = conn.prepareStatement(acsNamedConditionExistsSQL);
175: st.setString(1, entry.getName());
176: st.setLong(2, -1);
177: results = st.executeQuery();
178: if (results.next())
179: throw new EntityExistsException();
180: results.close();
181: results = null;
182: st.close();
183: st = null;
184:
185: st = conn.prepareStatement(acsCreateNamedConditionSQL);
186: st.setString(1, entry.getName());
187: st.setString(2, entry.getDescription());
188: st.setString(3, entry.getSpecification());
189: st.execute();
190:
191: } catch (NamingException ex) {
192: throw new RemoteException("Failed to look up data source: "
193: + bdsiDataSource, ex);
194: } catch (SQLException ex) {
195: throw new RemoteException("System exception.", ex);
196: } finally {
197: DBUtil.close(results);
198: DBUtil.close(st);
199: DBUtil.close(conn);
200: }
201:
202: }
203:
204: public void updateNamedCondition(NamedCondition entry)
205: throws EntityExistsException, RemoteException {
206: Connection conn = null;
207: PreparedStatement st = null;
208: ResultSet results = null;
209: try {
210: conn = DBUtil.getConnection(bdsiDataSource);
211:
212: // First check if a role with specified name already exists.
213: st = conn.prepareStatement(acsNamedConditionExistsSQL);
214: st.setString(1, entry.getName());
215: st.setLong(2, entry.getId());
216: results = st.executeQuery();
217: if (results.next())
218: throw new EntityExistsException();
219: results.close();
220: results = null;
221: st.close();
222:
223: st = null;
224: st = conn.prepareStatement(acsUpdateNamedConditionSQL);
225: st.setString(1, entry.getName());
226: st.setString(2, entry.getDescription());
227: st.setString(3, entry.getSpecification());
228: st.setLong(4, entry.getId());
229: st.execute();
230:
231: } catch (NamingException ex) {
232: throw new RemoteException("Failed to look up data source: "
233: + bdsiDataSource, ex);
234: } catch (SQLException ex) {
235: throw new RemoteException("System exception.", ex);
236: } finally {
237: DBUtil.close(results);
238: DBUtil.close(st);
239: DBUtil.close(conn);
240: }
241:
242: }
243:
244: public void deleteNamedConditions(long[] id) throws RemoteException {
245: Connection conn = null;
246: PreparedStatement st = null;
247:
248: try {
249: conn = DBUtil.getConnection(bdsiDataSource);
250: conn.setAutoCommit(false);
251: st = conn.prepareStatement(acsDeleteNamedConditionSQL);
252: for (int i = 0; i < id.length; i++) {
253: st.setLong(1, id[i]);
254: st.execute();
255: }
256:
257: conn.commit();
258: } catch (NamingException ex) {
259: try {
260: if (conn != null)
261: conn.rollback();
262: } catch (Exception e) { /* Ignore */
263: }
264: throw new RemoteException("Failed to look up data source: "
265: + bdsiDataSource, ex);
266: } catch (SQLException ex) {
267: try {
268: if (conn != null)
269: conn.rollback();
270: } catch (Exception e) { /* Ignore */
271: }
272: throw new RemoteException("System exception.", ex);
273: } finally {
274: DBUtil.close(st);
275: DBUtil.close(conn);
276: }
277:
278: }
279:
280: @SuppressWarnings("unchecked")
281: public List getNamedConditions(int offset, int maxEntries,
282: String orderby, boolean isDescending)
283: throws RemoteException {
284: Connection conn = null;
285: ResultSet results = null;
286: PreparedStatement st = null;
287:
288: try {
289: String selectSQL = acsSelectNamedConditionsSQL;
290: if (orderby != null) {
291: selectSQL += " ORDER BY " + orderby;
292: if (isDescending)
293: selectSQL += " DESC";
294: }
295:
296: conn = DBUtil.getConnection(bdsiDataSource);
297: st = conn.prepareStatement(selectSQL);
298: results = st.executeQuery();
299: List v = new ArrayList();
300:
301: // Skip upto offset
302: for (int i = 0; i < offset; i++) {
303: if (!results.next())
304: return v;
305: }
306:
307: // Create maxEntries entries and add them to the vector.
308: for (int i = 0; i < maxEntries; i++) {
309: if (!results.next())
310: break;
311: NamedCondition e = new NamedCondition();
312: e.setId(results.getLong(1));
313: e.setName(results.getString(2));
314: e.setDescription(results.getString(3));
315: e.setSpecification(results.getString(4));
316: v.add(e);
317: }
318:
319: return v;
320:
321: } catch (NamingException ex) {
322: throw new RemoteException("Failed to look up data source: "
323: + bdsiDataSource, ex);
324: } catch (SQLException ex) {
325: throw new RemoteException("System exception.", ex);
326: } catch (JAXBException e) {
327: throw new RemoteException(
328: "Failed to unmarshal condition spec.", e);
329: } finally {
330: DBUtil.close(st);
331: DBUtil.close(results);
332: DBUtil.close(conn);
333: }
334: }
335:
336: public long getNamedConditionCount() throws RemoteException {
337: Connection conn = null;
338: ResultSet results = null;
339: PreparedStatement st = null;
340:
341: try {
342: conn = DBUtil.getConnection(bdsiDataSource);
343: st = conn.prepareStatement(acsNamedConditionCountSQL);
344: results = st.executeQuery();
345: results.next();
346: return results.getLong(1);
347:
348: } catch (NamingException ex) {
349: throw new RemoteException("Failed to look up data source: "
350: + bdsiDataSource, ex);
351: } catch (SQLException ex) {
352: throw new RemoteException("System exception.", ex);
353: } finally {
354: DBUtil.close(st);
355: DBUtil.close(results);
356: DBUtil.close(conn);
357: }
358: }
359:
360: public AccessControlEntry getAccessControlEntry(long entryid)
361: throws NoSuchEntityException, RemoteException {
362: Connection conn = null;
363: ResultSet results = null;
364: PreparedStatement st = null;
365:
366: try {
367: conn = DBUtil.getConnection(bdsiDataSource);
368: st = conn.prepareStatement(acsGetEntryWithIDSQL);
369: st.setLong(1, entryid);
370: results = st.executeQuery();
371: if (!results.next())
372: throw new NoSuchEntityException();
373: return constructAccessControlEntry(results);
374: } catch (NamingException ex) {
375: throw new RemoteException("Failed to look up data source: "
376: + bdsiDataSource, ex);
377: } catch (JAXBException e) {
378: throw new RemoteException(
379: "Failed to unmarshal condition spec.", e);
380: } catch (SQLException ex) {
381: throw new RemoteException("System exception.", ex);
382: } finally {
383: DBUtil.close(st);
384: DBUtil.close(results);
385: DBUtil.close(conn);
386: }
387: }
388:
389: public void createAccessControlEntry(AccessControlEntry entry)
390: throws EntityExistsException, RemoteException {
391: Connection conn = null;
392: PreparedStatement st = null;
393: ResultSet results = null;
394: try {
395: conn = DBUtil.getConnection(bdsiDataSource);
396:
397: // First check if an entry with specified resource name/resource type/action
398: // already exists.
399: st = conn.prepareStatement(acsEntryExistsSQL);
400: st.setString(1, entry.getResourceName());
401: st.setLong(2, entry.getResourceType());
402: st.setString(3, entry.getAction());
403: st.setLong(4, -1);
404: results = st.executeQuery();
405: if (results.next())
406: throw new EntityExistsException();
407: results.close();
408: results = null;
409: st.close();
410: st = null;
411:
412: st = conn.prepareStatement(acsCreateEntrySQL);
413: st.setString(1, entry.getResourceName());
414: st.setLong(2, entry.getResourceType());
415: st.setString(3, entry.getAction());
416: st.setLong(4, entry.getNamedCondition().getId());
417: st.execute();
418:
419: } catch (NamingException ex) {
420: throw new RemoteException("Failed to look up data source: "
421: + bdsiDataSource, ex);
422: } catch (SQLException ex) {
423: throw new RemoteException("System exception.", ex);
424: } finally {
425: DBUtil.close(results);
426: DBUtil.close(st);
427: DBUtil.close(conn);
428: }
429:
430: }
431:
432: public void updateAccessControlEntry(AccessControlEntry entry)
433: throws EntityExistsException, RemoteException {
434: Connection conn = null;
435: PreparedStatement st = null;
436: ResultSet results = null;
437: try {
438: conn = DBUtil.getConnection(bdsiDataSource);
439:
440: // First check if an entry with specified resource name/resource type/action
441: // already exists.
442: st = conn.prepareStatement(acsEntryExistsSQL);
443: st.setString(1, entry.getResourceName());
444: st.setLong(2, entry.getResourceType());
445: st.setString(3, entry.getAction());
446: st.setLong(4, entry.getId());
447: results = st.executeQuery();
448: if (results.next())
449: throw new EntityExistsException();
450: results.close();
451: results = null;
452: st.close();
453: st = null;
454:
455: st = conn.prepareStatement(acsUpdateEntrySQL);
456: st.setString(1, entry.getResourceName());
457: st.setLong(2, entry.getResourceType());
458: st.setString(3, entry.getAction());
459: st.setLong(4, entry.getNamedCondition().getId());
460: st.setLong(5, entry.getId());
461: st.execute();
462:
463: } catch (NamingException ex) {
464: throw new RemoteException("Failed to look up data source: "
465: + bdsiDataSource, ex);
466: } catch (SQLException ex) {
467: throw new RemoteException("System exception.", ex);
468: } finally {
469: DBUtil.close(results);
470: DBUtil.close(st);
471: DBUtil.close(conn);
472: }
473:
474: }
475:
476: public void deleteAccessControlEntries(long[] id)
477: throws RemoteException {
478: Connection conn = null;
479: PreparedStatement st = null;
480:
481: try {
482: conn = DBUtil.getConnection(bdsiDataSource);
483: conn.setAutoCommit(false);
484: st = conn.prepareStatement(acsDeleteEntrySQL);
485: for (int i = 0; i < id.length; i++) {
486: st.setLong(1, id[i]);
487: st.execute();
488: }
489:
490: conn.commit();
491: } catch (NamingException ex) {
492: try {
493: if (conn != null)
494: conn.rollback();
495: } catch (Exception e) { /* Ignore */
496: }
497: throw new RemoteException("Failed to look up data source: "
498: + bdsiDataSource, ex);
499: } catch (SQLException ex) {
500: try {
501: if (conn != null)
502: conn.rollback();
503: } catch (Exception e) { /* Ignore */
504: }
505: throw new RemoteException("System exception.", ex);
506: } finally {
507: DBUtil.close(st);
508: DBUtil.close(conn);
509: }
510:
511: }
512:
513: @SuppressWarnings("unchecked")
514: public List getAccessControlEntries(int offset, int maxEntries,
515: String orderby, boolean isDescending)
516: throws RemoteException {
517: Connection conn = null;
518: ResultSet results = null;
519: PreparedStatement st = null;
520:
521: try {
522: String selectSQL = acsSelectEntriesSQL;
523: if (orderby != null) {
524: selectSQL += " ORDER BY " + orderby;
525: if (isDescending)
526: selectSQL += " DESC";
527: }
528:
529: conn = DBUtil.getConnection(bdsiDataSource);
530: st = conn.prepareStatement(selectSQL);
531: results = st.executeQuery();
532: List v = new ArrayList();
533:
534: // Skip upto offset
535: for (int i = 0; i < offset; i++) {
536: if (!results.next())
537: return v;
538: }
539:
540: // Create maxEntries entries and add them to the vector.
541: for (int i = 0; i < maxEntries; i++) {
542: if (!results.next())
543: break;
544: v.add(constructAccessControlEntry(results));
545: }
546:
547: return v;
548:
549: } catch (NamingException ex) {
550: throw new RemoteException("Failed to look up data source: "
551: + bdsiDataSource, ex);
552: } catch (SQLException ex) {
553: throw new RemoteException("System exception.", ex);
554: } catch (JAXBException e) {
555: throw new RemoteException(
556: "Failed to unmarshal condition spec.", e);
557: } finally {
558: DBUtil.close(st);
559: DBUtil.close(results);
560: DBUtil.close(conn);
561: }
562: }
563:
564: @SuppressWarnings("unchecked")
565: public List getAccessControlEntriesForResourceType(long rtid)
566: throws RemoteException {
567: Connection conn = null;
568: ResultSet results = null;
569: PreparedStatement st = null;
570:
571: try {
572:
573: conn = DBUtil.getConnection(bdsiDataSource);
574: st = conn.prepareStatement(acsGetEntriesForResourceTypeSQL);
575: st.setLong(1, rtid);
576: results = st.executeQuery();
577: List v = new ArrayList();
578:
579: while (results.next()) {
580: v.add(constructAccessControlEntry(results));
581: }
582:
583: return v;
584:
585: } catch (NamingException ex) {
586: throw new RemoteException("Failed to look up data source: "
587: + bdsiDataSource, ex);
588: } catch (SQLException ex) {
589: throw new RemoteException("System exception.", ex);
590: } catch (JAXBException e) {
591: throw new RemoteException(
592: "Failed to unmarshal condition spec.", e);
593: } finally {
594: DBUtil.close(st);
595: DBUtil.close(results);
596: DBUtil.close(conn);
597: }
598: }
599:
600: public long getAccessControlEntryCount() throws RemoteException {
601: Connection conn = null;
602: ResultSet results = null;
603: PreparedStatement st = null;
604:
605: try {
606: conn = DBUtil.getConnection(bdsiDataSource);
607: st = conn.prepareStatement(acsEntryCountSQL);
608: results = st.executeQuery();
609: results.next();
610: return results.getLong(1);
611:
612: } catch (NamingException ex) {
613: throw new RemoteException("Failed to look up data source: "
614: + bdsiDataSource, ex);
615: } catch (SQLException ex) {
616: throw new RemoteException("System exception.", ex);
617: } finally {
618: DBUtil.close(st);
619: DBUtil.close(results);
620: DBUtil.close(conn);
621: }
622: }
623:
624: public ResourceType getResourceType(long id)
625: throws NoSuchEntityException, RemoteException {
626: Connection conn = null;
627: ResultSet results = null;
628: PreparedStatement st = null;
629:
630: try {
631: conn = DBUtil.getConnection(bdsiDataSource);
632: st = conn.prepareStatement(acsGetResourceTypeWithIDSQL);
633: st.setLong(1, id);
634: results = st.executeQuery();
635: if (!results.next())
636: throw new NoSuchEntityException();
637: ResourceType e = new ResourceType();
638: e.setId(results.getLong(1));
639: e.setName(results.getString(2));
640: e.setDescription(results.getString(3));
641: return e;
642: } catch (NamingException ex) {
643: throw new RemoteException("Failed to look up data source: "
644: + bdsiDataSource, ex);
645: } catch (SQLException ex) {
646: throw new RemoteException("System exception.", ex);
647: } finally {
648: DBUtil.close(st);
649: DBUtil.close(results);
650: DBUtil.close(conn);
651: }
652: }
653:
654: public ResourceType getResourceType(String rtName)
655: throws NoSuchEntityException, RemoteException {
656: Connection conn = null;
657: ResultSet results = null;
658: PreparedStatement st = null;
659:
660: try {
661: conn = DBUtil.getConnection(bdsiDataSource);
662: st = conn.prepareStatement(acsGetResourceTypeWithNameSQL);
663: st.setString(1, rtName);
664: results = st.executeQuery();
665: if (!results.next())
666: throw new NoSuchEntityException();
667: ResourceType e = new ResourceType();
668: e.setId(results.getLong(1));
669: e.setName(results.getString(2));
670: e.setDescription(results.getString(3));
671: return e;
672: } catch (NamingException ex) {
673: throw new RemoteException("Failed to look up data source: "
674: + bdsiDataSource, ex);
675: } catch (SQLException ex) {
676: throw new RemoteException("System exception.", ex);
677: } finally {
678: DBUtil.close(st);
679: DBUtil.close(results);
680: DBUtil.close(conn);
681: }
682: }
683:
684: public void createResourceType(ResourceType rt)
685: throws EntityExistsException, RemoteException {
686: Connection conn = null;
687: PreparedStatement st = null;
688: ResultSet results = null;
689: try {
690: conn = DBUtil.getConnection(bdsiDataSource);
691:
692: // First check if a role with specified name already exists.
693: st = conn.prepareStatement(acsResourceTypeExistsSQL);
694: st.setString(1, rt.getName());
695: st.setLong(2, -1);
696: results = st.executeQuery();
697: if (results.next())
698: throw new EntityExistsException();
699: results.close();
700: results = null;
701: st.close();
702: st = null;
703:
704: st = conn.prepareStatement(acsCreateResourceTypeSQL);
705: st.setString(1, rt.getName());
706: st.setString(2, rt.getDescription());
707: st.execute();
708:
709: } catch (NamingException ex) {
710: throw new RemoteException("Failed to look up data source: "
711: + bdsiDataSource, ex);
712: } catch (SQLException ex) {
713: throw new RemoteException("System exception.", ex);
714: } finally {
715: DBUtil.close(results);
716: DBUtil.close(st);
717: DBUtil.close(conn);
718: }
719:
720: }
721:
722: public void updateResourceType(ResourceType rt)
723: throws EntityExistsException, RemoteException {
724: Connection conn = null;
725: PreparedStatement st = null;
726: ResultSet results = null;
727: try {
728: conn = DBUtil.getConnection(bdsiDataSource);
729:
730: // First check if a role with specified name already exists.
731: st = conn.prepareStatement(acsResourceTypeExistsSQL);
732: st.setString(1, rt.getName());
733: st.setLong(2, rt.getId());
734: results = st.executeQuery();
735: if (results.next())
736: throw new EntityExistsException();
737: results.close();
738: results = null;
739: st.close();
740:
741: st = null;
742: st = conn.prepareStatement(acsUpdateResourceTypeSQL);
743: st.setString(1, rt.getName());
744: st.setString(2, rt.getDescription());
745: st.setLong(3, rt.getId());
746: st.execute();
747:
748: } catch (NamingException ex) {
749: throw new RemoteException("Failed to look up data source: "
750: + bdsiDataSource, ex);
751: } catch (SQLException ex) {
752: throw new RemoteException("System exception.", ex);
753: } finally {
754: DBUtil.close(results);
755: DBUtil.close(st);
756: DBUtil.close(conn);
757: }
758:
759: }
760:
761: public void deleteResourceTypes(long[] id) throws RemoteException {
762: Connection conn = null;
763: PreparedStatement st = null;
764:
765: try {
766: conn = DBUtil.getConnection(bdsiDataSource);
767: conn.setAutoCommit(false);
768: st = conn.prepareStatement(acsDeleteResourceTypeSQL);
769: for (int i = 0; i < id.length; i++) {
770: st.setLong(1, id[i]);
771: st.execute();
772: }
773:
774: conn.commit();
775: } catch (NamingException ex) {
776: try {
777: if (conn != null)
778: conn.rollback();
779: } catch (Exception e) { /* Ignore */
780: }
781: throw new RemoteException("Failed to look up data source: "
782: + bdsiDataSource, ex);
783: } catch (SQLException ex) {
784: try {
785: if (conn != null)
786: conn.rollback();
787: } catch (Exception e) { /* Ignore */
788: }
789: throw new RemoteException("System exception.", ex);
790: } finally {
791: DBUtil.close(st);
792: DBUtil.close(conn);
793: }
794:
795: }
796:
797: @SuppressWarnings("unchecked")
798: public List getResourceTypes(int offset, int maxTypes,
799: String orderby, boolean isDescending)
800: throws RemoteException {
801: Connection conn = null;
802: ResultSet results = null;
803: PreparedStatement st = null;
804:
805: try {
806: String selectSQL = acsSelectResourceTypesSQL;
807: if (orderby != null) {
808: selectSQL += " ORDER BY " + orderby;
809: if (isDescending)
810: selectSQL += " DESC";
811: }
812:
813: conn = DBUtil.getConnection(bdsiDataSource);
814: st = conn.prepareStatement(selectSQL);
815: results = st.executeQuery();
816: List v = new ArrayList();
817:
818: // Skip upto offset
819: for (int i = 0; i < offset; i++) {
820: if (!results.next())
821: return v;
822: }
823:
824: // Create maxEntries entries and add them to the vector.
825: for (int i = 0; i < maxTypes; i++) {
826: if (!results.next())
827: break;
828: ResourceType e = new ResourceType();
829: e.setId(results.getLong(1));
830: e.setName(results.getString(2));
831: e.setDescription(results.getString(3));
832: v.add(e);
833: }
834:
835: return v;
836:
837: } catch (NamingException ex) {
838: throw new RemoteException("Failed to look up data source: "
839: + bdsiDataSource, ex);
840: } catch (SQLException ex) {
841: throw new RemoteException("System exception.", ex);
842: } finally {
843: DBUtil.close(st);
844: DBUtil.close(results);
845: DBUtil.close(conn);
846: }
847: }
848:
849: public long getResourceTypeCount() throws RemoteException {
850: Connection conn = null;
851: ResultSet results = null;
852: PreparedStatement st = null;
853:
854: try {
855: conn = DBUtil.getConnection(bdsiDataSource);
856: st = conn.prepareStatement(acsResourceTypeCountSQL);
857: results = st.executeQuery();
858: results.next();
859: return results.getLong(1);
860:
861: } catch (NamingException ex) {
862: throw new RemoteException("Failed to look up data source: "
863: + bdsiDataSource, ex);
864: } catch (SQLException ex) {
865: throw new RemoteException("System exception.", ex);
866: } finally {
867: DBUtil.close(st);
868: DBUtil.close(results);
869: DBUtil.close(conn);
870: }
871: }
872:
873: private AccessControlEntry constructAccessControlEntry(
874: ResultSet results) throws SQLException, JAXBException {
875: AccessControlEntry e = new AccessControlEntry();
876: e.setId(results.getLong(1));
877: e.setResourceName(results.getString(2));
878: e.setResourceType(results.getLong(3));
879: e.setAction(results.getString(4));
880:
881: NamedCondition cond = new NamedCondition();
882: cond.setId(results.getLong(5));
883: cond.setName(results.getString(6));
884: cond.setDescription(results.getString(7));
885: cond.setSpecification(results.getString(8));
886: e.setNamedCondition(cond);
887: return e;
888:
889: }
890: }
|