home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-19 | 14.1 KB | 397 lines |
- /*
- * @(#QueryNavigator.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
-
- package symantec.itools.db.beans.jdbc;
-
- import java.util.Vector;
- import java.util.Enumeration;
- import java.sql.SQLException;
- import java.util.StringTokenizer;
- import symantec.itools.db.beans.binding.PersistentObject;
- import symantec.itools.db.beans.binding.PositionOutOfRangeException;
- import symantec.itools.db.beans.binding.Synchronizable;
- import symantec.itools.db.beans.binding.ExceptionEvent;
- import symantec.itools.db.beans.binding.TriggerUIEvent;
-
- public class QueryNavigator extends symantec.itools.db.beans.binding.QueryNavigator
- {
- // A join parameter has the format: "col_master" JOIN_SEPARATOR "operator" JOIN_SEPARATOR "col_detail"
- public static final String JOIN_SEPARATOR =" ";
- public static final int JOIN_ELEMENTS=3;
- public static final int DETAIL_LINK_POSITION=0;
- public static final int OPERATOR_LINK_POSITION=DETAIL_LINK_POSITION+1;
- public static final int MASTER_LINK_POSITION=OPERATOR_LINK_POSITION+1;
-
- //protected static final int TABLE_SIZE=10;
- JoinRecordList m_JoinValues=new JoinRecordList();
-
- private String m_WhereClause = null;
- private String[] m_SortOrder = null;
- private String[] m_JoinParameters=null;
-
-
-
- /**
- * Populates a Data Source with the result from the query by example.
- */
- public void executeQueryByExample()
- {
- executeQueryBase(null);
- }
-
- private void executeQueryBase(PersistentObject masterDetail){
- PersistentObject obj = (PersistentObject)getCurrentObject();
- if (obj == null)
- {
- System.out.println("No example provided");
- return;
- }
-
- try {
- m_ObjectVector = null;
-
- if(m_ObjectEnumeration!=null){
- ((ResultSetEnumeration)m_ObjectEnumeration).closeResultSet();
- m_ObjectEnumeration=null;
- }
- if(masterDetail==null)
- {
- m_ObjectEnumeration = obj.querySimilarObjects(getWhereClause(),
- getSortOrder());
- }
- else
- {
- m_JoinValues.setMasterAndDetailData(masterDetail,obj);
- m_ObjectEnumeration = obj.querySimilarObjects(getWhereClause(),m_JoinValues,
- getSortOrder());
- }
-
-
- newSet=true;
- navigate(FIRST,null);
- }
- catch (Exception ex) {
- announceException(new ExceptionEvent(ExceptionEvent.EXCEPTION_SQL,ex.getMessage(),this, ex));
- }
-
- }
-
-
- public void executeQuery(Synchronizable syncro){
- // System.out.println("executeQuery: " + toString());
- // System.out.println("masterReference: " + syncro.toString());
- if(syncro==null){
- executeQueryByExample();
- }
- else
- {
- PersistentObject currentMasterRecord=(PersistentObject)syncro.getCurrentObject();
- if(syncro.isEmpty()){
- try{
- saveAll(CLEAR_ALL_DATA);
- }
- catch(SQLException e){}
- return;
- }
- executeQueryBase(currentMasterRecord);
- /*<vj>*/
- moveDataToUI(0, TriggerUIEvent.RECORDSETCHANGED);
- /*<vj>*/
- }
- }
-
- protected void insertIntern(){
- super.insertIntern();
- PersistentObject po=(PersistentObject)getCurrentObject();
- Enumeration joinEnumeration=m_JoinValues.getJoinRecords();
- while(joinEnumeration.hasMoreElements()){
- JoinRecord joinRecord=(JoinRecord)joinEnumeration.nextElement();
- if(joinRecord.getOperator().equals("=")){
- po.setValueAsObject(joinRecord.getDetailColumnNumber(),joinRecord.getMasterCurrentValue());
- }
- }
- moveDataToUI(getCurrentRowNumber(), TriggerUIEvent.RECORDINSERTED);
- }
-
- public void setWhereClause(String propertyValue)
- {
- m_WhereClause = propertyValue;
- }
- public String getWhereClause()
- {
- return m_WhereClause;
- }
-
- public void setSortOrder(String[] propertyValue)
- {
- m_SortOrder = propertyValue;
- }
- public String[] getSortOrder()
- {
- return m_SortOrder;
- }
-
- public void addSortOrderItem(String propertyValue)
- {
- if (m_SortOrder != null) {
- String[] temp = new String[m_SortOrder.length + 1];
- System.arraycopy(m_SortOrder, 0, temp, 0, m_SortOrder.length);
- temp[m_SortOrder.length] = propertyValue;
- m_SortOrder = temp;
- }
- else {
- m_SortOrder = new String[1];
- m_SortOrder[0] = propertyValue;
- }
- }
-
- public void setJoin(String[] join){
- m_JoinValues=new JoinRecordList(join);
- }
-
- public String[] getJoin(){
- return m_JoinValues.toStringUI();
- }
-
- public void setJoinColumns(String[] join){
- m_JoinValues=new JoinRecordList(join);
- }
-
- // The two methods below were added for backrunner
- // These should make the ones above obsolete??
-
- public String[] getJoinColumns(){
- return m_JoinValues.toStringUI();
- }
-
- public synchronized void addJoin(String join){
- m_JoinValues.addJoinRecord(new JoinRecord(join));
- }
-
- public Synchronizable.JoinRecordList getJoinValues(){
- return m_JoinValues;
- }
-
-
- protected void setDisplaced(int column,Object value, PersistentObject po){
- JoinRecord joinRecord =m_JoinValues.getJoinRecord(column);
- if (joinRecord!=null && joinRecord.isDisplaced(value)){
- po.setMarkedAsDisplaced(true);
- }
- }
-
- public class JoinRecordList extends Synchronizable.JoinRecordList
- {
- //we use a custom class for this list because we expext to have
- //different key access to its records.
-
- //it's a JoinRecord[] structure
- Vector m_JoinRecords=new Vector();
-
- public JoinRecordList(){
- }
-
- public JoinRecordList(String[] strList){
- for(int i=0;i<strList.length;i++){
- addJoinRecord(new JoinRecord(strList[i]));
- }
-
- }
-
- public void setMasterCurrentValue(Object value, int index){
- Enumeration joinEnumeration=m_JoinRecords.elements();
- while(joinEnumeration.hasMoreElements()){
- JoinRecord joinRecord=(JoinRecord)joinEnumeration.nextElement();
- if(joinRecord.getMasterColumnNumber()==index){
- joinRecord.setMasterCurrentValue(value);
- }
- }
-
- }
-
- public JoinRecord getJoinRecord(int colDetailNumber){
- Enumeration joinEnumeration=m_JoinRecords.elements();
- JoinRecord joinReturnRecord=null;
- while(joinEnumeration.hasMoreElements()){
- JoinRecord joinRecord=(JoinRecord)joinEnumeration.nextElement();
- if(joinRecord.getDetailColumnNumber()==colDetailNumber){
- joinReturnRecord=joinRecord;
- break;
- }
- }
- return joinReturnRecord;
- }
-
- public void setMasterCurrentValue(Object value, String colNameMaster){
- Enumeration joinEnumeration=m_JoinRecords.elements();
- while(joinEnumeration.hasMoreElements()){
- JoinRecord joinRecord=(JoinRecord)joinEnumeration.nextElement();
- if(joinRecord.getMasterColumnName().equals(colNameMaster)){
- joinRecord.setMasterCurrentValue(value);
- }
- }
- }
-
- public synchronized String[] toStringUI(){
- int lengthList=m_JoinRecords.size();
- String[] strList=new String[lengthList];
- for(int i=0;i<lengthList;i++){
- JoinRecord joinRecord=(JoinRecord)m_JoinRecords.elementAt(i);
- strList[i]=joinRecord.toStringUI();
- }
- return strList;
- }
-
- public void addJoinRecord(JoinRecord joinRecord){
- m_JoinRecords.addElement(joinRecord);
- }
-
- public Enumeration getJoinRecords(){
- return m_JoinRecords.elements();
- }
-
- public synchronized void setMasterAndDetailData(PersistentObject poMaster, PersistentObject poDetail){
- Enumeration joinEnumeration=m_JoinRecords.elements();
- while(joinEnumeration.hasMoreElements()){
- JoinRecord joinRecord=(JoinRecord)joinEnumeration.nextElement();
- int masterColumnNumber=poMaster.getColumnIndex(joinRecord.getMasterColumnName());
- int detailColumnNumber=poDetail.getColumnIndex(joinRecord.getDetailColumnName());
- joinRecord.setMasterColumnNumber(masterColumnNumber);
- joinRecord.setDetailColumnNumber(detailColumnNumber);
- joinRecord.setMasterCurrentValue(poMaster.getValueAsObject(masterColumnNumber,PersistentObject.ORIGINAL_VALUE));
- }
- }
-
- }
-
-
- public class JoinRecord extends Synchronizable.JoinRecord{
- //we add to the previous values of a join record the number of the
- // master column, the number of the detail column
- public final int NEW_RECORD_SIZE=JOIN_ELEMENTS+2;
- public final int DETAIL_LINK_VALUE_POSITION=MASTER_LINK_POSITION+1;
- public final int MASTER_LINK_VALUE_POSITION=DETAIL_LINK_VALUE_POSITION+1;
-
-
- protected final String EQUAL_OPERATOR="=";
- protected final String NON_EQUAL_OPERATOR="!=";
- protected final String LESS_OPERATOR="<";
- protected final String GREATER_OPERATOR=">";
- protected final String LESS_OR_EQUAL_OPERATOR="<=";
- protected final String GREATER_OR_EQUAL_OPERATOR=">=";
-
- Object[] m_Record =new Object[NEW_RECORD_SIZE];
-
- private Object m_CurrentMasterValue;
-
- public JoinRecord(String joinRec){
- StringTokenizer tokens = new StringTokenizer(joinRec, QueryNavigator.JOIN_SEPARATOR);
-
- for (int indexToken = 0; indexToken < JOIN_ELEMENTS; indexToken++){
- switch(indexToken){
- case QueryNavigator.DETAIL_LINK_POSITION :
- m_Record[DETAIL_LINK_POSITION]=tokens.nextToken();
- break;
- case QueryNavigator.MASTER_LINK_POSITION :
- m_Record[MASTER_LINK_POSITION]=tokens.nextToken();
- break;
- case QueryNavigator.OPERATOR_LINK_POSITION :
- m_Record[OPERATOR_LINK_POSITION]=tokens.nextToken();
- break;
- default:
- tokens.nextToken();
- }
- }
- }
-
- public String getMasterColumnName(){
- return (String)m_Record[MASTER_LINK_POSITION];
- }
-
- public String getDetailColumnName(){
- return (String)m_Record[DETAIL_LINK_POSITION];
- }
-
- public int getDetailColumnNumber(){
- return ((Integer)m_Record[DETAIL_LINK_VALUE_POSITION]).intValue();
- }
- public String getOperator(){
- return (String)m_Record[OPERATOR_LINK_POSITION];
- }
-
- public int getMasterColumnNumber(){
- return ((Integer)m_Record[MASTER_LINK_VALUE_POSITION]).intValue();
- }
-
-
- public synchronized void setMasterCurrentValue(Object value){
- m_CurrentMasterValue=value;
- }
-
- public synchronized void setMasterColumnNumber(int value){
- m_Record[MASTER_LINK_VALUE_POSITION]=new Integer(value);
- }
-
- public synchronized void setDetailColumnNumber(int value){
- m_Record[DETAIL_LINK_VALUE_POSITION]=new Integer(value);
- }
-
- public Object getMasterCurrentValue(){
- return m_CurrentMasterValue;
- }
-
- public String toStringUI(){
- StringBuffer strValue=new StringBuffer();
- for(int i=0;i<JOIN_ELEMENTS;i++){
- if (i > 0) {
- strValue.append(" ");
- }
- strValue.append(m_Record[i]);
- }
- return strValue.toString();
- }
-
- public boolean isDisplaced(Object value){
- String op=getOperator();
- boolean isMisplaced=false;
- try{
- if(op.equals(EQUAL_OPERATOR)){
- isMisplaced=!m_CurrentMasterValue.equals(value);
- }
- else if(op.equals(NON_EQUAL_OPERATOR)){
- isMisplaced=m_CurrentMasterValue.equals(value);
- }
- else if(op.equals(LESS_OPERATOR)){
- //isMisplaced=!(m_CurrentMasterValue.toString().compareTo(value.toString())<0);
- }
- else if(op.equals(GREATER_OPERATOR)){
- //isMisplaced=!(m_CurrentMasterValue.toString().compareTo(value.toString())>0);
- }
- else if(op.equals(LESS_OR_EQUAL_OPERATOR)){
- //isMisplaced=!(m_CurrentMasterValue.toString().compareTo(value.toString())<=0);
- }
- else if(op.equals(GREATER_OR_EQUAL_OPERATOR)){
- //isMisplaced=!(m_CurrentMasterValue.toString().compareTo(value.toString())>=0);
- }
- else {
- }
- }
- catch(Exception e){
- //swallow; return false, we suppose a NullPointerException happened
- }
- return isMisplaced;
- }
- }
-
-
-
-
-
-
- }
-
-