KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > datastore > jdbc > PublisherAssertionTable


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.datastore.jdbc;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.juddi.datatype.KeyedReference;
26 import org.apache.juddi.datatype.assertion.PublisherAssertion;
27 import org.apache.juddi.datatype.response.AssertionStatusItem;
28 import org.apache.juddi.datatype.response.CompletionStatus;
29 import org.apache.juddi.datatype.response.KeysOwned;
30
31 /**
32  * @author Steve Viens (steve@users.sourceforge.net)
33  */

34 class PublisherAssertionTable
35 {
36   // private reference to the jUDDI logger
37
private static Log log = LogFactory.getLog(PublisherAssertionTable.class);
38
39   static String JavaDoc insertSQL = null;
40   static String JavaDoc selectSQL = null;
41   static String JavaDoc deleteDeadAssertionsSQL = null;
42   static String JavaDoc updateFromCheckSQL = null;
43   static String JavaDoc updateToCheckSQL = null;
44   static String JavaDoc updateFromCheckByFromKeySQL = null;
45   static String JavaDoc updateToCheckByToKeySQL = null;
46   static String JavaDoc selectAssertionsSQL = null;
47   static String JavaDoc selectRelationships = null;
48
49   static {
50     // buffer used to build SQL statements
51
StringBuffer JavaDoc sql = null;
52
53     // build insertSQL
54
sql = new StringBuffer JavaDoc(150);
55     sql.append("INSERT INTO PUBLISHER_ASSERTION (");
56     sql.append("FROM_KEY,");
57     sql.append("TO_KEY,");
58     sql.append("TMODEL_KEY,");
59     sql.append("KEY_NAME,");
60     sql.append("KEY_VALUE,");
61     sql.append("FROM_CHECK,");
62     sql.append("TO_CHECK) ");
63     sql.append("VALUES (?,?,?,?,?,?,?)");
64     insertSQL = sql.toString();
65
66     // build selectSQL
67
sql = new StringBuffer JavaDoc(200);
68     sql.append("SELECT ");
69     sql.append("FROM_KEY,");
70     sql.append("TO_KEY,");
71     sql.append("TMODEL_KEY,");
72     sql.append("KEY_NAME,");
73     sql.append("KEY_VALUE ");
74     sql.append("FROM PUBLISHER_ASSERTION ");
75     sql.append("WHERE FROM_KEY=? ");
76     sql.append("AND TO_KEY=? ");
77     sql.append("AND TMODEL_KEY=? ");
78     sql.append("AND KEY_NAME=? ");
79     sql.append("AND KEY_VALUE=?");
80     selectSQL = sql.toString();
81
82     // build deleteDeadAssertionsSQL
83
sql = new StringBuffer JavaDoc(200);
84     sql.append("DELETE FROM PUBLISHER_ASSERTION ");
85     sql.append("WHERE FROM_CHECK='false' ");
86     sql.append("AND TO_CHECK='false'");
87     deleteDeadAssertionsSQL = sql.toString();
88
89     // build updateFromCheckSQL
90
sql = new StringBuffer JavaDoc(200);
91     sql.append("UPDATE PUBLISHER_ASSERTION ");
92     sql.append("SET FROM_CHECK=? ");
93     sql.append("WHERE FROM_KEY=? ");
94     sql.append("AND TO_KEY=? ");
95     sql.append("AND TMODEL_KEY=? ");
96     sql.append("AND KEY_NAME=? ");
97     sql.append("AND KEY_VALUE=?");
98     updateFromCheckSQL = sql.toString();
99
100     // build updateToCheckSQL
101
sql = new StringBuffer JavaDoc(200);
102     sql.append("UPDATE PUBLISHER_ASSERTION ");
103     sql.append("SET TO_CHECK=? ");
104     sql.append("WHERE FROM_KEY=? ");
105     sql.append("AND TO_KEY=? ");
106     sql.append("AND TMODEL_KEY=? ");
107     sql.append("AND KEY_NAME=? ");
108     sql.append("AND KEY_VALUE=?");
109     updateToCheckSQL = sql.toString();
110
111     // build updateFromCheckByFromKeySQL
112
sql = new StringBuffer JavaDoc(200);
113     sql.append("UPDATE PUBLISHER_ASSERTION ");
114     sql.append("SET FROM_CHECK=? ");
115     sql.append("WHERE FROM_KEY IN ");
116     updateFromCheckByFromKeySQL = sql.toString();
117
118     // build updateFromCheckByFromKeySQL
119
sql = new StringBuffer JavaDoc(200);
120     sql.append("UPDATE PUBLISHER_ASSERTION ");
121     sql.append("SET TO_CHECK=? ");
122     sql.append("WHERE TO_KEY IN ");
123     updateFromCheckByFromKeySQL = sql.toString();
124
125     // build selectAssertionsSQL
126
sql = new StringBuffer JavaDoc(200);
127     sql.append("SELECT ");
128     sql.append("FROM_KEY,");
129     sql.append("TO_KEY,");
130     sql.append("TMODEL_KEY,");
131     sql.append("KEY_NAME,");
132     sql.append("KEY_VALUE,");
133     sql.append("FROM_CHECK,");
134     sql.append("TO_CHECK ");
135     sql.append("FROM PUBLISHER_ASSERTION ");
136     selectAssertionsSQL = sql.toString();
137
138     // build selectRelationships
139
sql = new StringBuffer JavaDoc(200);
140     sql.append("SELECT TMODEL_KEY,KEY_NAME,KEY_VALUE ");
141     sql.append("FROM PUBLISHER_ASSERTION ");
142     sql.append(
143       "WHERE ((FROM_KEY = ? AND TO_KEY = ?) OR (FROM_KEY = ? AND TO_KEY = ?)) ");
144     sql.append("AND FROM_CHECK = 'true' ");
145     sql.append("AND TO_CHECK = 'true' ");
146     selectRelationships = sql.toString();
147   }
148
149   /**
150    * Insert new row into the PUBLISHER_ASSERTION table.
151    *
152    * @param assertion Publisher Assertion object holding values to be inserted
153    * @param fromCheck boolean true if the FROM_KEY is owned by the individual 'adding' this assertion (otherwise false).
154    * @param toCheck boolean true if the TO_KEY is owned by the individual 'adding' this assertion (otherwise false).
155    * @param connection JDBC connection
156    * @throws java.sql.SQLException
157    */

158   public static void insert(
159     PublisherAssertion assertion,
160     boolean fromCheck,
161     boolean toCheck,
162     Connection JavaDoc connection)
163     throws java.sql.SQLException JavaDoc
164   {
165     PreparedStatement JavaDoc statement = null;
166
167     try
168     {
169       // prep insert values
170
String JavaDoc tModelKey = null;
171       String JavaDoc keyedRefName = null;
172       String JavaDoc keyedRefValue = null;
173
174       if (assertion.getKeyedReference() != null)
175       {
176         tModelKey = assertion.getKeyedReference().getTModelKey();
177         keyedRefName = assertion.getKeyedReference().getKeyName();
178         keyedRefValue = assertion.getKeyedReference().getKeyValue();
179       }
180
181       statement = connection.prepareStatement(insertSQL);
182       statement.setString(1, assertion.getFromKey());
183       statement.setString(2, assertion.getToKey());
184       statement.setString(3, tModelKey);
185       statement.setString(4, keyedRefName);
186       statement.setString(5, keyedRefValue);
187       statement.setString(6, String.valueOf(fromCheck));
188       statement.setString(7, String.valueOf(toCheck));
189
190       log.debug(
191         "insert into PUBLISHER_ASSERTION table:\n\n\t"
192           + insertSQL
193           + "\n\t FROM_KEY="
194           + assertion.getFromKey()
195           + "\n\t TO_KEY="
196           + assertion.getToKey()
197           + "\n\t TMODEL_KEY="
198           + tModelKey
199           + "\n\t KEY_NAME="
200           + keyedRefName
201           + "\n\t KEY_VALUE="
202           + keyedRefValue
203           + "\n\t FROM_CHECK="
204           + fromCheck
205           + "\n\t TO_CHECK="
206           + toCheck
207           + "\n");
208
209       // insert
210
statement.executeUpdate();
211     }
212     catch (java.sql.SQLException JavaDoc sqlex)
213     {
214       log.error(sqlex.getMessage());
215       throw sqlex;
216     }
217     finally
218     {
219       try
220       {
221         statement.close();
222       }
223       catch (Exception JavaDoc e)
224       { /* ignored */
225       }
226     }
227   }
228
229   /**
230    * Select one row from the PUBLISHER_ASSERTION table.
231    *
232    * @param assertionIn
233    * @param connection JDBC connection
234    * @throws java.sql.SQLException
235    */

236   public static PublisherAssertion select(
237     PublisherAssertion assertionIn,
238     Connection JavaDoc connection)
239     throws java.sql.SQLException JavaDoc
240   {
241     PublisherAssertion assertionOut = null;
242     PreparedStatement JavaDoc statement = null;
243     ResultSet JavaDoc resultSet = null;
244
245     try
246     {
247       KeyedReference keyedRefIn = assertionIn.getKeyedReference();
248
249       statement = connection.prepareStatement(selectSQL);
250       statement.setString(1, assertionIn.getFromKey());
251       statement.setString(2, assertionIn.getToKey());
252       statement.setString(3, keyedRefIn.getTModelKey());
253       statement.setString(4, keyedRefIn.getKeyName());
254       statement.setString(5, keyedRefIn.getKeyValue());
255
256       log.debug(
257         "select from PUBLISHER_ASSERTION table:\n\n\t"
258           + selectSQL
259           + "\n\t FROM_KEY="
260           + assertionIn.getFromKey()
261           + "\n\t TO_KEY="
262           + assertionIn.getToKey()
263           + "\n\t TMODEL_KEY="
264           + keyedRefIn.getTModelKey()
265           + "\n\t KEY_NAME="
266           + keyedRefIn.getKeyName()
267           + "\n\t KEY_VALUE="
268           + keyedRefIn.getKeyValue()
269           + "\n");
270
271       resultSet = statement.executeQuery();
272       if (resultSet.next())
273       {
274         KeyedReference keyedRefOut = new KeyedReference();
275         keyedRefOut.setKeyName(resultSet.getString(4)); //("KEY_NAME"));
276
keyedRefOut.setKeyValue(resultSet.getString(5)); //("KEY_VALUE"));
277
keyedRefOut.setTModelKey(resultSet.getString(3)); //("TMODEL_KEY"));
278

279         assertionOut = new PublisherAssertion();
280         assertionOut.setFromKey(resultSet.getString(1)); //("FROM_KEY"));
281
assertionOut.setToKey(resultSet.getString(2)); //("TO_KEY"));
282
assertionOut.setKeyedReference(keyedRefOut);
283       }
284
285       return assertionOut;
286     }
287     catch (java.sql.SQLException JavaDoc sqlex)
288     {
289       log.error(sqlex.getMessage());
290       throw sqlex;
291     }
292     finally
293     {
294       try
295       {
296         resultSet.close();
297       }
298       catch (Exception JavaDoc e)
299       { /* ignored */
300       }
301       try
302       {
303         statement.close();
304       }
305       catch (Exception JavaDoc e)
306       { /* ignored */
307       }
308     }
309   }
310
311   /**
312    * Delete row from the PUBLISHER_ASSERTION table.
313    *
314    * @throws java.sql.SQLException
315    */

316   public static void deleteDeadAssertions(Connection JavaDoc connection)
317     throws java.sql.SQLException JavaDoc
318   {
319     PreparedStatement JavaDoc statement = null;
320
321     try
322     {
323       // prepare the delete
324
statement = connection.prepareStatement(deleteDeadAssertionsSQL);
325
326       log.debug(
327         "delete from PUBLISHER_ASSERTION table:\n\n\t"
328           + deleteDeadAssertionsSQL
329           + "\n");
330
331       // execute
332
statement.executeUpdate();
333     }
334     catch (java.sql.SQLException JavaDoc sqlex)
335     {
336       log.error(sqlex.getMessage());
337       throw sqlex;
338     }
339     finally
340     {
341       try
342       {
343         statement.close();
344       }
345       catch (Exception JavaDoc e)
346       { /* ignored */
347       }
348     }
349   }
350
351   /**
352    * Update the FROM_CHECK column in the PUBLISHER_ASSERTION table for a
353    * particular PublisherAssertion.
354    *
355    * @param assertion The PublisherAssertion to update BusinessKey
356    * @param fromCheck The value to set the FROM_CHECK column to.
357    * @param connection JDBC connection
358    * @throws java.sql.SQLException
359    */

360   public static void updateFromCheck(
361     PublisherAssertion assertion,
362     boolean fromCheck,
363     Connection JavaDoc connection)
364     throws java.sql.SQLException JavaDoc
365   {
366     KeyedReference keyedRef = assertion.getKeyedReference();
367     PreparedStatement JavaDoc statement = null;
368     ResultSet JavaDoc resultSet = null;
369
370     try
371     {
372       log.debug(
373         "update PUBLISHER_ASSERTION table:\n\n\t"
374           + updateFromCheckSQL
375           + "\n\t FROM_CHECK="
376           + String.valueOf(fromCheck)
377           + "\n\t FROM_KEY="
378           + assertion.getFromKey()
379           + "\n\t TO_KEY="
380           + assertion.getToKey()
381           + "\n\t TMODEL_KEY="
382           + keyedRef.getTModelKey()
383           + "\n\t KEY_NAME="
384           + keyedRef.getKeyName()
385           + "\n\t KEY_VALUE="
386           + keyedRef.getKeyValue()
387           + "\n");
388
389       // create a statement to query with
390
statement = connection.prepareStatement(updateFromCheckSQL);
391       statement.setString(1, String.valueOf(fromCheck));
392       statement.setString(2, assertion.getFromKey());
393       statement.setString(3, assertion.getToKey());
394       statement.setString(4, keyedRef.getTModelKey());
395       statement.setString(5, keyedRef.getKeyName());
396       statement.setString(6, keyedRef.getKeyValue());
397
398       // execute
399
statement.executeUpdate();
400     }
401     catch (java.sql.SQLException JavaDoc sqlex)
402     {
403       log.error(sqlex.getMessage());
404       throw sqlex;
405     }
406     finally
407     {
408       try
409       {
410         resultSet.close();
411       }
412       catch (Exception JavaDoc e)
413       { /* ignored */
414       }
415       try
416       {
417         statement.close();
418       }
419       catch (Exception JavaDoc e)
420       { /* ignored */
421       }
422     }
423   }
424
425   /**
426    * Update the TO_CHECK column in the PUBLISHER_ASSERTION table
427    * for a particular PublisherAssertion.
428    *
429    * @param assertion The PublisherAssertion to update BusinessKey
430    * @param toCheck The value to set the TO_CHECK column to.
431    * @param connection JDBC connection
432    * @throws java.sql.SQLException
433    */

434   public static void updateToCheck(
435     PublisherAssertion assertion,
436     boolean toCheck,
437     Connection JavaDoc connection)
438     throws java.sql.SQLException JavaDoc
439   {
440     KeyedReference keyedRef = assertion.getKeyedReference();
441     PreparedStatement JavaDoc statement = null;
442     ResultSet JavaDoc resultSet = null;
443
444     try
445     {
446       log.debug(
447         "update PUBLISHER_ASSERTION table:\n\n\t"
448           + updateToCheckSQL
449           + "\n\t TO_CHECK="
450           + String.valueOf(toCheck)
451           + "\n\t FROM_KEY="
452           + assertion.getFromKey()
453           + "\n\t TO_KEY="
454           + assertion.getToKey()
455           + "\n\t TMODEL_KEY="
456           + keyedRef.getTModelKey()
457           + "\n\t KEY_NAME="
458           + keyedRef.getKeyName()
459           + "\n\t KEY_VALUE="
460           + keyedRef.getKeyValue()
461           + "\n");
462
463       // create a statement to query with
464
statement = connection.prepareStatement(updateToCheckSQL);
465       statement.setString(1, String.valueOf(toCheck));
466       statement.setString(2, assertion.getFromKey());
467       statement.setString(3, assertion.getToKey());
468       statement.setString(4, keyedRef.getTModelKey());
469       statement.setString(5, keyedRef.getKeyName());
470       statement.setString(6, keyedRef.getKeyValue());
471
472       // execute
473
statement.executeUpdate();
474     }
475     catch (java.sql.SQLException JavaDoc sqlex)
476     {
477       log.error(sqlex.getMessage());
478       throw sqlex;
479     }
480     finally
481     {
482       try
483       {
484         resultSet.close();
485       }
486       catch (Exception JavaDoc e)
487       { /* ignored */
488       }
489       try
490       {
491         statement.close();
492       }
493       catch (Exception JavaDoc e)
494       { /* ignored */
495       }
496     }
497   }
498
499   /**
500    * Update the FROM_CHECK column for all rows from in the PUBLISHER_ASSERTION
501    * table whose FROM_KEY is in the Vector of BusinessKeys passed in.
502    *
503    * @param fromKeysIn A Vector of BusinessKeys to update
504    * @param fromCheck The value to set the FROM_CHECK column to
505    * @param connection JDBC connection
506    * @throws java.sql.SQLException
507    */

508   public static void updateFromCheckByFromKey(
509     Vector JavaDoc fromKeysIn,
510     boolean fromCheck,
511     Connection JavaDoc connection)
512     throws java.sql.SQLException JavaDoc
513   {
514     StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
515     sql.append(updateFromCheckByFromKeySQL);
516     sql.append("WHERE FROM_KEY IN ");
517     appendIn(sql, fromKeysIn);
518
519     PreparedStatement JavaDoc statement = null;
520     ResultSet JavaDoc resultSet = null;
521
522     try
523     {
524       // prepare
525
statement = connection.prepareStatement(sql.toString());
526       statement.setString(1, String.valueOf(fromCheck));
527
528       log.debug(
529         "update PUBLISHER_ASSERTION table:\n\n\t" + sql.toString() + "\n");
530
531       // execute
532
statement.executeUpdate();
533     }
534     catch (java.sql.SQLException JavaDoc sqlex)
535     {
536       log.error(sqlex.getMessage());
537       throw sqlex;
538     }
539     finally
540     {
541       try
542       {
543         resultSet.close();
544       }
545       catch (Exception JavaDoc e)
546       { /* ignored */
547       }
548       try
549       {
550         statement.close();
551       }
552       catch (Exception JavaDoc e)
553       { /* ignored */
554       }
555     }
556   }
557
558   /**
559    * Update the TO_CHECK column for all rows from in the PUBLISHER_ASSERTION
560    * table whose TO_KEY is in the Vector of BusinessKeys passed in.
561    *
562    * @param toKeysIn A Vector of BusinessKeys to update
563    * @param toCheck The value to set the TO_KEY column to
564    * @param connection JDBC connection
565    * @throws java.sql.SQLException
566    */

567   public static void updateToCheckByToKey(
568     Vector JavaDoc toKeysIn,
569     boolean toCheck,
570     Connection JavaDoc connection)
571     throws java.sql.SQLException JavaDoc
572   {
573     StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
574     sql.append(updateFromCheckByFromKeySQL);
575     sql.append("WHERE TO_KEY IN ");
576     appendIn(sql, toKeysIn);
577
578     PreparedStatement JavaDoc statement = null;
579     ResultSet JavaDoc resultSet = null;
580
581     try
582     {
583       // create a statement to query with
584
statement = connection.prepareStatement(sql.toString());
585       statement.setString(1, String.valueOf(toCheck));
586
587       log.debug(
588         "update PUBLISHER_ASSERTION table:\n\n\t" + sql.toString() + "\n");
589
590       // execute
591
statement.executeUpdate();
592     }
593     catch (java.sql.SQLException JavaDoc sqlex)
594     {
595       log.error(sqlex.getMessage());
596       throw sqlex;
597     }
598     finally
599     {
600       try
601       {
602         resultSet.close();
603       }
604       catch (Exception JavaDoc e)
605       { /* ignored */
606       }
607       try
608       {
609         statement.close();
610       }
611       catch (Exception JavaDoc e)
612       { /* ignored */
613       }
614     }
615   }
616
617   /**
618    * Select any rows from the PUBLISHER_ASSERTION table where the FROM_KEY
619    * or TO_KEY column value is found in the Vector of BusinessKeys passed in
620    * and return the results as a Vector of assertionStatusItem instances.
621    *
622    * The assertionStatusItems returned represent PublisherAssertions in
623    * which the fromKey and toKey are both are under the control of a
624    * particular Publisher.
625    *
626    * NOTE: Each AssertionStatusItem returned from this method will have a
627    * completion stauts of 'status:complete' because only assertions
628    * in which both business entities are managed (was published) by
629    * same publisher.
630    *
631    * @param keysIn Vector business keys to look for in the FROM_KEY and TO_KEY column.
632    * @param connection JDBC connection
633    * @throws java.sql.SQLException
634    */

635   public static Vector JavaDoc selectBothKeysOwnedAssertion(
636     Vector JavaDoc keysIn,
637     Connection JavaDoc connection)
638     throws java.sql.SQLException JavaDoc
639   {
640     if ((keysIn == null) || (keysIn.size() == 0))
641       return null;
642
643     StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
644     sql.append(selectAssertionsSQL);
645     sql.append("WHERE FROM_KEY IN ");
646     appendIn(sql, keysIn);
647     sql.append("AND TO_KEY IN ");
648     appendIn(sql, keysIn);
649
650     Vector JavaDoc itemList = new Vector JavaDoc();
651     PreparedStatement JavaDoc statement = null;
652     ResultSet JavaDoc resultSet = null;
653
654     try
655     {
656       statement = connection.prepareStatement(sql.toString());
657
658       log.debug(
659         "select from PUBLISHER_ASSERTION table:\n\n\t" + selectSQL + "\n");
660
661       resultSet = statement.executeQuery();
662       while (resultSet.next())
663       {
664         AssertionStatusItem item = new AssertionStatusItem();
665         item.setFromKey(resultSet.getString(1)); //("FROM_KEY"));
666
item.setToKey(resultSet.getString(2)); //("TO_KEY"));
667

668         // construct and set the KeyedReference instance
669
KeyedReference keyedRef = new KeyedReference();
670         keyedRef.setTModelKey(resultSet.getString(3)); //("TMODEL_KEY"));
671
keyedRef.setKeyName(resultSet.getString(4)); //("KEY_NAME"));
672
keyedRef.setKeyValue(resultSet.getString(5)); //("KEY_VALUE"));
673
item.setKeyedReference(keyedRef);
674
675         // construct and set the KeysOwned instance
676
KeysOwned keysOwned = new KeysOwned();
677         keysOwned.setFromKey(item.getFromKey());
678         keysOwned.setToKey(item.getToKey());
679         item.setKeysOwned(keysOwned);
680
681         // determine & set the 'completionStatus' (always 'status:complete' here)
682
item.setCompletionStatus(
683           new CompletionStatus(CompletionStatus.COMPLETE));
684
685         // add the assertionStatusItem
686
itemList.addElement(item);
687       }
688
689       return itemList;
690     }
691     catch (java.sql.SQLException JavaDoc sqlex)
692     {
693       log.error(sqlex.getMessage());
694       throw sqlex;
695     }
696     finally
697     {
698       try
699       {
700         resultSet.close();
701       }
702       catch (Exception JavaDoc e)
703       { /* ignored */
704       }
705       try
706       {
707         statement.close();
708       }
709       catch (Exception JavaDoc e)
710       { /* ignored */
711       }
712     }
713   }
714
715   /**
716    * Select any rows from the PUBLISHER_ASSERTION table where the FROM_KEY column
717    * DOES CONTAIN one of the business keys found in the Vector of keys passed in
718    * and the TO_KEY column DOES NOT CONTAIN one of the business keys from the same
719    * Vector of keys. Return the results as a Vector of assertionStatusItem instances.
720    *
721    * The assertionStatusItems returned represent PublisherAssertions in
722    * which ONLY the "fromKey" is under the control of a particular Publisher.
723
724    * @param keysIn Vector business keys to look for in the FROM_KEY and TO_KEY column.
725    * @param connection JDBC connection
726    * @throws java.sql.SQLException
727    */

728   public static Vector JavaDoc selectFromKeyOwnedAssertion(
729     Vector JavaDoc keysIn,
730     Connection JavaDoc connection)
731     throws java.sql.SQLException JavaDoc
732   {
733     if ((keysIn == null) || (keysIn.size() == 0))
734       return null;
735
736     StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
737     sql.append(selectAssertionsSQL);
738     sql.append("WHERE FROM_KEY IN ");
739     appendIn(sql, keysIn);
740     sql.append("AND TO_KEY NOT IN ");
741     appendIn(sql, keysIn);
742
743     Vector JavaDoc itemList = new Vector JavaDoc();
744     PreparedStatement JavaDoc statement = null;
745     ResultSet JavaDoc resultSet = null;
746
747     try
748     {
749       statement = connection.prepareStatement(sql.toString());
750
751       log.debug(
752         "select from PUBLISHER_ASSERTION table:\n\n\t" + selectSQL + "\n");
753
754       resultSet = statement.executeQuery();
755       while (resultSet.next())
756       {
757         AssertionStatusItem item = new AssertionStatusItem();
758         item.setFromKey(resultSet.getString(1)); //("FROM_KEY"));
759
item.setToKey(resultSet.getString(2)); //("TO_KEY"));
760

761         // construct and set the KeyedReference instance
762
KeyedReference keyedRef = new KeyedReference();
763         keyedRef.setTModelKey(resultSet.getString(3)); //("TMODEL_KEY"));
764
keyedRef.setKeyName(resultSet.getString(4)); //("KEY_NAME"));
765
keyedRef.setKeyValue(resultSet.getString(5)); //("KEY_VALUE"));
766
item.setKeyedReference(keyedRef);
767
768         // construct and set the KeysOwned instance
769
KeysOwned keysOwned = new KeysOwned();
770         keysOwned.setFromKey(item.getFromKey());
771         keysOwned.setToKey(null);
772         item.setKeysOwned(keysOwned);
773
774         // determine and set the assertions 'completionStatus'
775
CompletionStatus status = null;
776         boolean fromCheck =
777           new Boolean JavaDoc(resultSet.getString(6)).booleanValue();//("FROM_CHECK")
778
boolean toCheck =
779           new Boolean JavaDoc(resultSet.getString(7)).booleanValue();//("TO_CHECK")
780
if ((fromCheck) && (toCheck))
781           status = new CompletionStatus(CompletionStatus.COMPLETE);
782         else if ((fromCheck) && (!toCheck))
783           status = new CompletionStatus(CompletionStatus.TOKEY_INCOMPLETE);
784         else if ((!fromCheck) && (toCheck))
785           status = new CompletionStatus(CompletionStatus.FROMKEY_INCOMPLETE);
786         item.setCompletionStatus(status);
787
788         // add the assertionStatusItem
789
itemList.addElement(item);
790       }
791
792       return itemList;
793     }
794     catch (java.sql.SQLException JavaDoc sqlex)
795     {
796       log.error(sqlex.getMessage());
797       throw sqlex;
798     }
799     finally
800     {
801       try
802       {
803         resultSet.close();
804       }
805       catch (Exception JavaDoc e)
806       { /* ignored */
807       }
808       try
809       {
810         statement.close();
811       }
812       catch (Exception JavaDoc e)
813       { /* ignored */
814       }
815     }
816   }
817
818   /**
819    * Select any rows from the PUBLISHER_ASSERTION table where the FROM_KEY column
820    * DOES NOT CONTAIN one of the business keys found in the Vector of keys passed
821    * in and the TO_KEY column DOES CONTAIN one of the business keys from the same
822    * Vector of keys. Return the results as a Vector of assertionStatusItem instances.
823    *
824    * The assertionStatusItems returned represent PublisherAssertions in
825    * which ONLY the "toKey" is under the control of a particular Publisher.
826    *
827    * @param keysIn Vector business keys to look for in the FROM_KEY and TO_KEY column.
828    * @param connection JDBC connection
829    * @throws java.sql.SQLException
830    */

831   public static Vector JavaDoc selectToKeyOwnedAssertion(
832     Vector JavaDoc keysIn,
833     Connection JavaDoc connection)
834     throws java.sql.SQLException JavaDoc
835   {
836     if ((keysIn == null) || (keysIn.size() == 0))
837       return null;
838
839     StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
840     sql.append(selectAssertionsSQL);
841     sql.append("WHERE FROM_KEY NOT IN ");
842     appendIn(sql, keysIn);
843     sql.append("AND TO_KEY IN ");
844     appendIn(sql, keysIn);
845
846     Vector JavaDoc itemList = new Vector JavaDoc();
847     PreparedStatement JavaDoc statement = null;
848     ResultSet JavaDoc resultSet = null;
849
850     try
851     {
852       statement = connection.prepareStatement(sql.toString());
853
854       log.debug(
855         "select from PUBLISHER_ASSERTION table:\n\n\t" + selectSQL + "\n");
856
857       resultSet = statement.executeQuery();
858       while (resultSet.next())
859       {
860         AssertionStatusItem item = new AssertionStatusItem();
861         item.setFromKey(resultSet.getString(1));//("FROM_KEY"));
862
item.setToKey(resultSet.getString(2));//("TO_KEY"));
863

864         // construct and set the KeyedReference instance
865
KeyedReference keyedRef = new KeyedReference();
866         keyedRef.setKeyName(resultSet.getString(4));//("KEY_NAME"));
867
keyedRef.setKeyValue(resultSet.getString(5));//("KEY_VALUE"));
868
keyedRef.setTModelKey(resultSet.getString(3));//("TMODEL_KEY"));
869
item.setKeyedReference(keyedRef);
870
871         // construct and set the KeysOwned instance
872
KeysOwned keysOwned = new KeysOwned();
873         keysOwned.setFromKey(null);
874         keysOwned.setToKey(item.getToKey());
875         item.setKeysOwned(keysOwned);
876
877         // determine and set the assertions 'completionStatus'
878
CompletionStatus status = null;
879         boolean fromCheck =
880           new Boolean JavaDoc(resultSet.getString(6)).booleanValue();//("FROM_CHECK"));
881
boolean toCheck =
882           new Boolean JavaDoc(resultSet.getString(7)).booleanValue();//("TO_CHECK"));
883
if ((fromCheck) && (toCheck))
884           status = new CompletionStatus(CompletionStatus.COMPLETE);
885         else if ((fromCheck) && (!toCheck))
886           status = new CompletionStatus(CompletionStatus.TOKEY_INCOMPLETE);
887         else if ((!fromCheck) && (toCheck))
888           status = new CompletionStatus(CompletionStatus.FROMKEY_INCOMPLETE);
889         item.setCompletionStatus(status);
890
891         // add the assertionStatusItem
892
itemList.addElement(item);
893       }
894
895       return itemList;
896     }
897     catch (java.sql.SQLException JavaDoc sqlex)
898     {
899       log.error(sqlex.getMessage());
900       throw sqlex;
901     }
902     finally
903     {
904       try
905       {
906         resultSet.close();
907       }
908       catch (Exception JavaDoc e)
909       { /* ignored */
910       }
911       try
912       {
913         statement.close();
914       }
915       catch (Exception JavaDoc e)
916       { /* ignored */
917       }
918     }
919   }
920
921   /**
922    * Select any rows from the PUBLISHER_ASSERTION table where the FROM_KEY column
923    * CONTAINS one of the business keys found in the Vector of keys passed in OR
924    * the TO_KEY column CONTAINS one of the business keys from the same Vector
925    * f keys. Return the results as a Vector of PublisherAssertion instances.
926    *
927    * @param keysIn Vector business keys to look for in the FROM_KEY and TO_KEY column.
928    * @param connection JDBC connection
929    * @throws java.sql.SQLException
930    */

931   public static Vector JavaDoc selectAssertions(Vector JavaDoc keysIn, Connection JavaDoc connection)
932     throws java.sql.SQLException JavaDoc
933   {
934     if ((keysIn == null) || (keysIn.size() == 0))
935       return null;
936
937     StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
938     sql.append(selectAssertionsSQL);
939     sql.append("WHERE (FROM_KEY IN ");
940     appendIn(sql, keysIn);
941     sql.append("AND FROM_CHECK = 'true') ");
942     sql.append("OR (TO_KEY IN ");
943     appendIn(sql, keysIn);
944     sql.append("AND TO_CHECK = 'true')");
945
946     Vector JavaDoc assertionList = new Vector JavaDoc();
947     PreparedStatement JavaDoc statement = null;
948     ResultSet JavaDoc resultSet = null;
949
950     try
951     {
952       statement = connection.prepareStatement(sql.toString());
953
954       log.debug(
955         "select from PUBLISHER_ASSERTION table:\n\n\t" + selectSQL + "\n");
956
957       resultSet = statement.executeQuery();
958       while (resultSet.next())
959       {
960         PublisherAssertion assertion = new PublisherAssertion();
961         assertion.setFromKey(resultSet.getString(1));//("FROM_KEY"));
962
assertion.setToKey(resultSet.getString(2));//("TO_KEY"));
963

964         // construct and set the KeyedReference instance
965
KeyedReference keyedRef = new KeyedReference();
966         keyedRef.setKeyName(resultSet.getString(4));//("KEY_NAME"));
967
keyedRef.setKeyValue(resultSet.getString(5));//("KEY_VALUE"));
968
keyedRef.setTModelKey(resultSet.getString(3));//("TMODEL_KEY"));
969
assertion.setKeyedReference(keyedRef);
970
971         // add the assertionStatusItem
972
assertionList.addElement(assertion);
973       }
974
975       return assertionList;
976     }
977     catch (java.sql.SQLException JavaDoc sqlex)
978     {
979       log.error(sqlex.getMessage());
980       throw sqlex;
981     }
982     finally
983     {
984       try
985       {
986         resultSet.close();
987       }
988       catch (Exception JavaDoc e)
989       { /* ignored */
990       }
991       try
992       {
993         statement.close();
994       }
995       catch (Exception JavaDoc e)
996       { /* ignored */
997       }
998     }
999   }
1000
1001  /**
1002   * Retrieve the TMODEL_KEY, KEY_NAME and KEY_VALUE from all assertions
1003   * where the FROM_KEY = businessKey and the TO_KEY = relatedBusinessKey
1004   * parameters or the FROM_KEY = relatedBusinessKey and the TO_KEY =
1005   * businessKey.
1006   *
1007   * @param businessKey The BusinessKey we're searching for relationships to.
1008   * @param relatedKey The BusinessKey of the related BusinessEntity.
1009   * @param connection JDBC connection
1010   * @throws java.sql.SQLException
1011   */

1012  public static Vector JavaDoc selectRelatedBusinesses(
1013    String JavaDoc businessKey,
1014    String JavaDoc relatedKey,
1015    Connection JavaDoc connection)
1016    throws java.sql.SQLException JavaDoc
1017  {
1018    Vector JavaDoc refList = new Vector JavaDoc();
1019    PreparedStatement JavaDoc statement = null;
1020    ResultSet JavaDoc resultSet = null;
1021
1022    try
1023    {
1024      statement = connection.prepareStatement(selectRelationships);
1025      statement.setString(1, businessKey);
1026      statement.setString(2, relatedKey);
1027      statement.setString(3, relatedKey);
1028      statement.setString(4, businessKey);
1029
1030      log.debug(
1031        "select from PUBLISHER_ASSERTION table:\n\n\t"
1032          + selectRelationships
1033          + "\n\t BUSINESS_KEY="
1034          + businessKey.toString()
1035          + "\n\t RELATED_BUSINESS_KEY="
1036          + relatedKey.toString()
1037          + "\n");
1038
1039      resultSet = statement.executeQuery();
1040      if (resultSet.next())
1041      {
1042        KeyedReference keyedRef = new KeyedReference();
1043        keyedRef.setKeyName(resultSet.getString(2));//("KEY_NAME"));
1044
keyedRef.setKeyValue(resultSet.getString(3));//("KEY_VALUE"));
1045
keyedRef.setTModelKey(resultSet.getString(1));//("TMODEL_KEY"));
1046

1047        // add the KeyedRef to the Vector
1048
refList.addElement(keyedRef);
1049      }
1050
1051      return refList;
1052    }
1053    catch (java.sql.SQLException JavaDoc sqlex)
1054    {
1055      log.error(sqlex.getMessage());
1056      throw sqlex;
1057    }
1058    finally
1059    {
1060      try
1061      {
1062        resultSet.close();
1063      }
1064      catch (Exception JavaDoc e)
1065      { /* ignored */
1066      }
1067      try
1068      {
1069        statement.close();
1070      }
1071      catch (Exception JavaDoc e)
1072      { /* ignored */
1073      }
1074    }
1075  }
1076
1077  /**
1078   * Utility method used to construct SQL "IN" statements such as
1079   * the following SQL example:
1080   *
1081   * SELECT * FROM TABLE WHERE MONTH IN ('jan','feb','mar')
1082   *
1083   * @param sql StringBuffer to append the final results to
1084   * @param keysIn Vector of Strings used to construct the "IN" clause
1085   */

1086  private static void appendIn(StringBuffer JavaDoc sql, Vector JavaDoc keysIn)
1087  {
1088    if (keysIn == null)
1089      return;
1090
1091    sql.append("(");
1092
1093    int keyCount = keysIn.size();
1094    for (int i = 0; i < keyCount; i++)
1095    {
1096      String JavaDoc key = (String JavaDoc) keysIn.elementAt(i);
1097      sql.append("'").append(key).append("'");
1098
1099      if ((i + 1) < keyCount)
1100        sql.append(",");
1101    }
1102
1103    sql.append(") ");
1104  }
1105}
1106
Popular Tags