home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / QueryNavigator.java < prev    next >
Encoding:
Java Source  |  1998-03-19  |  14.1 KB  |  397 lines

  1. /*
  2.  * @(#QueryNavigator.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. package symantec.itools.db.beans.jdbc;
  9.  
  10. import java.util.Vector;
  11. import java.util.Enumeration;
  12. import java.sql.SQLException;
  13. import java.util.StringTokenizer;
  14. import symantec.itools.db.beans.binding.PersistentObject;
  15. import symantec.itools.db.beans.binding.PositionOutOfRangeException;
  16. import symantec.itools.db.beans.binding.Synchronizable;
  17. import symantec.itools.db.beans.binding.ExceptionEvent;
  18. import symantec.itools.db.beans.binding.TriggerUIEvent;
  19.  
  20. public class QueryNavigator extends symantec.itools.db.beans.binding.QueryNavigator
  21. {
  22.     // A join parameter has the format: "col_master" JOIN_SEPARATOR "operator" JOIN_SEPARATOR "col_detail"
  23.     public static final String JOIN_SEPARATOR =" ";
  24.     public static final int JOIN_ELEMENTS=3;
  25.     public static final int DETAIL_LINK_POSITION=0;
  26.     public static final int OPERATOR_LINK_POSITION=DETAIL_LINK_POSITION+1;
  27.     public static final int MASTER_LINK_POSITION=OPERATOR_LINK_POSITION+1;
  28.  
  29.     //protected static final int TABLE_SIZE=10;
  30.     JoinRecordList m_JoinValues=new JoinRecordList();
  31.  
  32.     private String m_WhereClause = null;
  33.     private String[] m_SortOrder = null;
  34.     private String[] m_JoinParameters=null;
  35.  
  36.  
  37.  
  38.      /**
  39.      *  Populates a Data Source with the result from the query by example.
  40.      */
  41.     public void executeQueryByExample()
  42.     {
  43.         executeQueryBase(null);
  44.     }
  45.  
  46.     private void executeQueryBase(PersistentObject masterDetail){
  47.         PersistentObject obj = (PersistentObject)getCurrentObject();
  48.         if (obj == null)
  49.         {
  50.             System.out.println("No example provided");
  51.             return;
  52.         }
  53.  
  54.         try {
  55.             m_ObjectVector = null;
  56.  
  57.             if(m_ObjectEnumeration!=null){
  58.                 ((ResultSetEnumeration)m_ObjectEnumeration).closeResultSet();
  59.                 m_ObjectEnumeration=null;
  60.             }
  61.             if(masterDetail==null)
  62.             {
  63.                 m_ObjectEnumeration = obj.querySimilarObjects(getWhereClause(),
  64.                     getSortOrder());
  65.             }
  66.             else
  67.             {
  68.                 m_JoinValues.setMasterAndDetailData(masterDetail,obj);
  69.                 m_ObjectEnumeration = obj.querySimilarObjects(getWhereClause(),m_JoinValues,
  70.                     getSortOrder());
  71.             }
  72.  
  73.  
  74.             newSet=true;
  75.             navigate(FIRST,null);
  76.         }
  77.         catch (Exception ex) {
  78.             announceException(new ExceptionEvent(ExceptionEvent.EXCEPTION_SQL,ex.getMessage(),this, ex));
  79.         }
  80.  
  81.     }
  82.  
  83.  
  84.     public void executeQuery(Synchronizable syncro){
  85.         // System.out.println("executeQuery: " + toString());
  86.         // System.out.println("masterReference: " + syncro.toString());
  87.         if(syncro==null){
  88.             executeQueryByExample();
  89.         }
  90.         else
  91.         {
  92.             PersistentObject currentMasterRecord=(PersistentObject)syncro.getCurrentObject();
  93.             if(syncro.isEmpty()){
  94.                 try{
  95.                     saveAll(CLEAR_ALL_DATA);
  96.                 }
  97.                 catch(SQLException e){}
  98.                 return;
  99.             }
  100.             executeQueryBase(currentMasterRecord);
  101.             /*<vj>*/
  102.             moveDataToUI(0, TriggerUIEvent.RECORDSETCHANGED);
  103.             /*<vj>*/
  104.         }
  105.     }
  106.  
  107.     protected void insertIntern(){
  108.         super.insertIntern();
  109.         PersistentObject po=(PersistentObject)getCurrentObject();
  110.         Enumeration joinEnumeration=m_JoinValues.getJoinRecords();
  111.         while(joinEnumeration.hasMoreElements()){
  112.             JoinRecord joinRecord=(JoinRecord)joinEnumeration.nextElement();
  113.             if(joinRecord.getOperator().equals("=")){
  114.                 po.setValueAsObject(joinRecord.getDetailColumnNumber(),joinRecord.getMasterCurrentValue());
  115.             }
  116.         }
  117.         moveDataToUI(getCurrentRowNumber(), TriggerUIEvent.RECORDINSERTED);
  118.     }
  119.  
  120.     public void setWhereClause(String propertyValue)
  121.     {
  122.         m_WhereClause = propertyValue;
  123.     }
  124.     public String getWhereClause()
  125.     {
  126.         return m_WhereClause;
  127.     }
  128.  
  129.     public void setSortOrder(String[] propertyValue)
  130.     {
  131.         m_SortOrder = propertyValue;
  132.     }
  133.     public String[] getSortOrder()
  134.     {
  135.         return m_SortOrder;
  136.     }
  137.  
  138.     public void addSortOrderItem(String propertyValue)
  139.     {
  140.         if (m_SortOrder != null) {
  141.             String[] temp = new String[m_SortOrder.length + 1];
  142.             System.arraycopy(m_SortOrder, 0, temp, 0, m_SortOrder.length);
  143.             temp[m_SortOrder.length] = propertyValue;
  144.             m_SortOrder = temp;
  145.         }
  146.         else {
  147.             m_SortOrder = new String[1];
  148.             m_SortOrder[0] = propertyValue;
  149.         }
  150.     }
  151.  
  152.     public void setJoin(String[] join){
  153.         m_JoinValues=new JoinRecordList(join);
  154.     }
  155.  
  156.     public String[] getJoin(){
  157.         return m_JoinValues.toStringUI();
  158.     }
  159.  
  160.     public void setJoinColumns(String[] join){
  161.         m_JoinValues=new JoinRecordList(join);
  162.     }
  163.  
  164.     // The two methods below were added for backrunner
  165.     // These should make the ones above obsolete??
  166.  
  167.     public String[] getJoinColumns(){
  168.         return m_JoinValues.toStringUI();
  169.     }
  170.  
  171.     public synchronized void addJoin(String join){
  172.          m_JoinValues.addJoinRecord(new JoinRecord(join));
  173.     }
  174.  
  175.     public Synchronizable.JoinRecordList getJoinValues(){
  176.         return m_JoinValues;
  177.      }
  178.  
  179.  
  180.     protected void  setDisplaced(int column,Object value, PersistentObject po){
  181.         JoinRecord joinRecord =m_JoinValues.getJoinRecord(column);
  182.         if (joinRecord!=null && joinRecord.isDisplaced(value)){
  183.             po.setMarkedAsDisplaced(true);
  184.         }
  185.     }
  186.  
  187.    public class JoinRecordList extends Synchronizable.JoinRecordList
  188.     {
  189.         //we use a custom class for this list because we expext to have
  190.         //different key access to its records.
  191.  
  192.         //it's a JoinRecord[] structure
  193.         Vector m_JoinRecords=new Vector();
  194.  
  195.         public JoinRecordList(){
  196.         }
  197.  
  198.         public JoinRecordList(String[] strList){
  199.             for(int i=0;i<strList.length;i++){
  200.                 addJoinRecord(new JoinRecord(strList[i]));
  201.             }
  202.  
  203.         }
  204.  
  205.         public void setMasterCurrentValue(Object value, int index){
  206.             Enumeration joinEnumeration=m_JoinRecords.elements();
  207.             while(joinEnumeration.hasMoreElements()){
  208.                 JoinRecord joinRecord=(JoinRecord)joinEnumeration.nextElement();
  209.                 if(joinRecord.getMasterColumnNumber()==index){
  210.                     joinRecord.setMasterCurrentValue(value);
  211.                 }
  212.             }
  213.  
  214.         }
  215.  
  216.         public JoinRecord getJoinRecord(int colDetailNumber){
  217.             Enumeration joinEnumeration=m_JoinRecords.elements();
  218.             JoinRecord joinReturnRecord=null;
  219.             while(joinEnumeration.hasMoreElements()){
  220.                 JoinRecord joinRecord=(JoinRecord)joinEnumeration.nextElement();
  221.                 if(joinRecord.getDetailColumnNumber()==colDetailNumber){
  222.                     joinReturnRecord=joinRecord;
  223.                     break;
  224.                 }
  225.             }
  226.             return joinReturnRecord;
  227.         }
  228.  
  229.         public void setMasterCurrentValue(Object value, String colNameMaster){
  230.             Enumeration joinEnumeration=m_JoinRecords.elements();
  231.             while(joinEnumeration.hasMoreElements()){
  232.                 JoinRecord joinRecord=(JoinRecord)joinEnumeration.nextElement();
  233.                 if(joinRecord.getMasterColumnName().equals(colNameMaster)){
  234.                     joinRecord.setMasterCurrentValue(value);
  235.                 }
  236.             }
  237.         }
  238.  
  239.         public synchronized String[] toStringUI(){
  240.             int lengthList=m_JoinRecords.size();
  241.             String[] strList=new String[lengthList];
  242.             for(int i=0;i<lengthList;i++){
  243.                 JoinRecord joinRecord=(JoinRecord)m_JoinRecords.elementAt(i);
  244.                 strList[i]=joinRecord.toStringUI();
  245.             }
  246.             return strList;
  247.         }
  248.  
  249.         public void addJoinRecord(JoinRecord joinRecord){
  250.             m_JoinRecords.addElement(joinRecord);
  251.         }
  252.  
  253.          public Enumeration getJoinRecords(){
  254.             return m_JoinRecords.elements();
  255.          }
  256.  
  257.         public synchronized void setMasterAndDetailData(PersistentObject poMaster, PersistentObject poDetail){
  258.             Enumeration joinEnumeration=m_JoinRecords.elements();
  259.             while(joinEnumeration.hasMoreElements()){
  260.                 JoinRecord joinRecord=(JoinRecord)joinEnumeration.nextElement();
  261.                 int masterColumnNumber=poMaster.getColumnIndex(joinRecord.getMasterColumnName());
  262.                 int detailColumnNumber=poDetail.getColumnIndex(joinRecord.getDetailColumnName());
  263.                 joinRecord.setMasterColumnNumber(masterColumnNumber);
  264.                 joinRecord.setDetailColumnNumber(detailColumnNumber);
  265.                 joinRecord.setMasterCurrentValue(poMaster.getValueAsObject(masterColumnNumber,PersistentObject.ORIGINAL_VALUE));
  266.             }
  267.         }
  268.  
  269.     }
  270.  
  271.  
  272.     public class JoinRecord extends Synchronizable.JoinRecord{
  273.             //we add to the previous values of a join record the number of the
  274.             // master column, the number of the detail column
  275.             public  final int NEW_RECORD_SIZE=JOIN_ELEMENTS+2;
  276.             public  final int DETAIL_LINK_VALUE_POSITION=MASTER_LINK_POSITION+1;
  277.             public  final int MASTER_LINK_VALUE_POSITION=DETAIL_LINK_VALUE_POSITION+1;
  278.  
  279.  
  280.             protected  final String EQUAL_OPERATOR="=";
  281.             protected  final String NON_EQUAL_OPERATOR="!=";
  282.             protected  final String LESS_OPERATOR="<";
  283.             protected  final String GREATER_OPERATOR=">";
  284.             protected  final String LESS_OR_EQUAL_OPERATOR="<=";
  285.             protected  final String GREATER_OR_EQUAL_OPERATOR=">=";
  286.  
  287.             Object[] m_Record =new Object[NEW_RECORD_SIZE];
  288.  
  289.             private Object m_CurrentMasterValue;
  290.  
  291.             public JoinRecord(String joinRec){
  292.                 StringTokenizer tokens = new StringTokenizer(joinRec, QueryNavigator.JOIN_SEPARATOR);
  293.  
  294.                 for (int indexToken = 0; indexToken < JOIN_ELEMENTS; indexToken++){
  295.                     switch(indexToken){
  296.                         case QueryNavigator.DETAIL_LINK_POSITION :
  297.                             m_Record[DETAIL_LINK_POSITION]=tokens.nextToken();
  298.                             break;
  299.                         case QueryNavigator.MASTER_LINK_POSITION :
  300.                              m_Record[MASTER_LINK_POSITION]=tokens.nextToken();
  301.                             break;
  302.                         case QueryNavigator.OPERATOR_LINK_POSITION :
  303.                             m_Record[OPERATOR_LINK_POSITION]=tokens.nextToken();
  304.                             break;
  305.                         default:
  306.                             tokens.nextToken();
  307.                     }
  308.                 }
  309.             }
  310.  
  311.             public String getMasterColumnName(){
  312.                 return (String)m_Record[MASTER_LINK_POSITION];
  313.             }
  314.  
  315.             public String getDetailColumnName(){
  316.                 return (String)m_Record[DETAIL_LINK_POSITION];
  317.             }
  318.  
  319.             public int getDetailColumnNumber(){
  320.                 return ((Integer)m_Record[DETAIL_LINK_VALUE_POSITION]).intValue();
  321.             }
  322.             public String getOperator(){
  323.                 return (String)m_Record[OPERATOR_LINK_POSITION];
  324.             }
  325.  
  326.             public int getMasterColumnNumber(){
  327.                 return ((Integer)m_Record[MASTER_LINK_VALUE_POSITION]).intValue();
  328.             }
  329.  
  330.  
  331.             public synchronized void setMasterCurrentValue(Object value){
  332.                 m_CurrentMasterValue=value;
  333.             }
  334.  
  335.             public synchronized void setMasterColumnNumber(int value){
  336.                 m_Record[MASTER_LINK_VALUE_POSITION]=new Integer(value);
  337.             }
  338.  
  339.             public synchronized void setDetailColumnNumber(int value){
  340.                 m_Record[DETAIL_LINK_VALUE_POSITION]=new Integer(value);
  341.             }
  342.  
  343.             public Object getMasterCurrentValue(){
  344.                 return m_CurrentMasterValue;
  345.             }
  346.  
  347.             public String toStringUI(){
  348.                 StringBuffer strValue=new StringBuffer();
  349.                 for(int i=0;i<JOIN_ELEMENTS;i++){
  350.                     if (i > 0) {
  351.                         strValue.append(" ");
  352.                     }
  353.                     strValue.append(m_Record[i]);
  354.                 }
  355.                 return strValue.toString();
  356.             }
  357.  
  358.             public boolean isDisplaced(Object value){
  359.                 String op=getOperator();
  360.                 boolean isMisplaced=false;
  361.                 try{
  362.                     if(op.equals(EQUAL_OPERATOR)){
  363.                         isMisplaced=!m_CurrentMasterValue.equals(value);
  364.                     }
  365.                     else if(op.equals(NON_EQUAL_OPERATOR)){
  366.                         isMisplaced=m_CurrentMasterValue.equals(value);
  367.                     }
  368.                     else if(op.equals(LESS_OPERATOR)){
  369.                         //isMisplaced=!(m_CurrentMasterValue.toString().compareTo(value.toString())<0);
  370.                     }
  371.                     else if(op.equals(GREATER_OPERATOR)){
  372.                         //isMisplaced=!(m_CurrentMasterValue.toString().compareTo(value.toString())>0);
  373.                     }
  374.                     else if(op.equals(LESS_OR_EQUAL_OPERATOR)){
  375.                         //isMisplaced=!(m_CurrentMasterValue.toString().compareTo(value.toString())<=0);
  376.                     }
  377.                     else if(op.equals(GREATER_OR_EQUAL_OPERATOR)){
  378.                         //isMisplaced=!(m_CurrentMasterValue.toString().compareTo(value.toString())>=0);
  379.                     }
  380.                     else {
  381.                     }
  382.                 }
  383.                 catch(Exception e){
  384.                     //swallow; return false, we suppose a NullPointerException happened
  385.                 }
  386.                 return isMisplaced;
  387.             }
  388.         }
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395. }
  396.  
  397.