Revision 1354
Added by Jing Tao almost 22 years ago
src/edu/ucsb/nceas/metacat/QuerySpecification.java | ||
---|---|---|
33 | 33 |
import edu.ucsb.nceas.dbadapter.*; |
34 | 34 |
|
35 | 35 |
import java.io.*; |
36 |
import java.util.Hashtable; |
|
36 | 37 |
import java.util.Stack; |
37 | 38 |
import java.util.Vector; |
38 | 39 |
import java.util.Enumeration; |
... | ... | |
88 | 89 |
private String [] group = null; |
89 | 90 |
|
90 | 91 |
public static final String ATTRIBUTESYMBOL = "@"; |
92 |
private boolean hasAttributeReturnField = false; |
|
93 |
private Hashtable attributeReturnList = new Hashtable(); |
|
94 |
private int countAttributeReturnField = 0; |
|
91 | 95 |
/** |
92 | 96 |
* construct an instance of the QuerySpecification class |
93 | 97 |
* |
... | ... | |
342 | 346 |
} |
343 | 347 |
|
344 | 348 |
/** |
349 |
* A method to get if the query has an attribute return field |
|
350 |
*/ |
|
351 |
public boolean containAttributeReturnField() |
|
352 |
{ |
|
353 |
return hasAttributeReturnField; |
|
354 |
} |
|
355 |
|
|
356 |
/** |
|
345 | 357 |
* Accessor method to return the identifier of this Query |
346 | 358 |
*/ |
347 | 359 |
public String getIdentifier() |
... | ... | |
574 | 586 |
} else if (currentTag.equals("filterdoctype")) { |
575 | 587 |
filterDocList.add(inputString); |
576 | 588 |
} else if (currentTag.equals("returnfield")) { |
577 |
returnFieldList.add(inputString); |
|
578 |
containsExtendedSQL = true; |
|
589 |
// make sure if return fields has an attribute or not |
|
590 |
if (inputString.indexOf(ATTRIBUTESYMBOL) ==-1) |
|
591 |
{ |
|
592 |
// no attribute value will be returned |
|
593 |
returnFieldList.add(inputString); |
|
594 |
containsExtendedSQL = true; |
|
595 |
} |
|
596 |
else |
|
597 |
{ |
|
598 |
// has a attribute return field |
|
599 |
// divied the return filed into two parts, one is path and the |
|
600 |
// other is attribue name |
|
601 |
String returnPath = newPathExpressionWithOutAttribute(inputString); |
|
602 |
String attributeName = getAttributeName(inputString); |
|
603 |
Vector pathInfo = new Vector(); |
|
604 |
// the vector has the information about return path and attributename |
|
605 |
pathInfo.addElement(returnPath); |
|
606 |
pathInfo.addElement(attributeName); |
|
607 |
// put the vector into a hash table. The reseaon why don't put |
|
608 |
// return path or attributename as a key is because they are not unique |
|
609 |
attributeReturnList.put |
|
610 |
(new Integer(countAttributeReturnField), pathInfo); |
|
611 |
countAttributeReturnField++; |
|
612 |
hasAttributeReturnField = true; |
|
613 |
containsExtendedSQL = true; |
|
614 |
|
|
615 |
} |
|
579 | 616 |
} else if (currentTag.equals("filterdoctype")) { |
580 | 617 |
filterDocList.add(inputString); |
581 | 618 |
} else if (currentTag.equals("owner")) { |
... | ... | |
710 | 747 |
return self.toString(); |
711 | 748 |
} |
712 | 749 |
|
750 |
/** |
|
751 |
* This method prints sql based upon the returnfield tag in the |
|
752 |
* pathquery document has an attribute. This allows for customization of the |
|
753 |
* returned fields |
|
754 |
* @param doclist the list of document ids to search by |
|
755 |
*/ |
|
756 |
public String printAttributeQuery(String doclist) |
|
757 |
{ |
|
758 |
StringBuffer self = new StringBuffer(); |
|
759 |
self.append("select xml_nodes.docid, xml_index.path, "); |
|
760 |
self.append("xml_nodes.nodedata, xml_nodes.nodename "); |
|
761 |
self.append("from xml_index, xml_nodes where xml_index.nodeid="); |
|
762 |
self.append("xml_nodes.parentnodeid and ("); |
|
763 |
boolean firstfield = true; |
|
764 |
//put the returnfields attributes into the query |
|
765 |
//the for loop allows for multiple fields and attributes |
|
766 |
Enumeration returnAttributes = attributeReturnList.elements(); |
|
767 |
while (returnAttributes.hasMoreElements()) |
|
768 |
{ |
|
769 |
Vector currentVector = (Vector)returnAttributes.nextElement(); |
|
770 |
String returnPath = (String)currentVector.elementAt(0); |
|
771 |
String attributeName = (String)currentVector.elementAt(1); |
|
772 |
if(firstfield) |
|
773 |
{ |
|
774 |
firstfield = false; |
|
775 |
self.append("( xml_index.path like '"); |
|
776 |
self.append(returnPath); |
|
777 |
self.append("' AND xml_nodes.nodename like '"); |
|
778 |
self.append(attributeName); |
|
779 |
self.append("') "); |
|
780 |
} |
|
781 |
else |
|
782 |
{ |
|
783 |
self.append(" or ( xml_index.path like '"); |
|
784 |
self.append(returnPath); |
|
785 |
self.append("' AND xml_nodes.nodename like '"); |
|
786 |
self.append(attributeName); |
|
787 |
self.append("') "); |
|
788 |
} |
|
789 |
} |
|
790 |
self.append(") AND xml_nodes.docid in ("); |
|
791 |
//self.append(query.printSQL()); |
|
792 |
self.append(doclist); |
|
793 |
self.append(")"); |
|
794 |
self.append(" AND xml_nodes.nodetype = 'ATTRIBUTE'"); |
|
795 |
MetaCatUtil.debugMessage("Attribute query: "+self.toString(), 30); |
|
796 |
|
|
797 |
return self.toString(); |
|
798 |
} |
|
799 |
|
|
713 | 800 |
public static String printRelationSQL(String docid) |
714 | 801 |
{ |
715 | 802 |
StringBuffer self = new StringBuffer(); |
... | ... | |
821 | 908 |
// return "meta_file_id=" + meta_file_id + "\n" + |
822 | 909 |
// "querytitle=" + querytitle + "\n" + query; |
823 | 910 |
} |
911 |
|
|
912 |
/* A method to get rid of attribute part in path expression*/ |
|
913 |
public static String newPathExpressionWithOutAttribute(String pathExpression) |
|
914 |
{ |
|
915 |
if (pathExpression == null) |
|
916 |
{ |
|
917 |
return null; |
|
918 |
} |
|
919 |
int index = pathExpression.lastIndexOf(ATTRIBUTESYMBOL); |
|
920 |
String newExpression = null; |
|
921 |
if (index != 1) |
|
922 |
{ |
|
923 |
newExpression=pathExpression.substring(0, index-1); |
|
924 |
} |
|
925 |
MetaCatUtil.debugMessage("The path expression without attributes: )" + |
|
926 |
newExpression, 30); |
|
927 |
return newExpression; |
|
928 |
} |
|
929 |
|
|
930 |
/* A method to get attribute name from path */ |
|
931 |
public static String getAttributeName(String path) |
|
932 |
{ |
|
933 |
if (path == null) |
|
934 |
{ |
|
935 |
return null; |
|
936 |
} |
|
937 |
int index = path.lastIndexOf(ATTRIBUTESYMBOL); |
|
938 |
int size = path.length(); |
|
939 |
String attributeName = null; |
|
940 |
if (index != 1) |
|
941 |
{ |
|
942 |
attributeName = path.substring(index+1, size); |
|
943 |
} |
|
944 |
MetaCatUtil.debugMessage("The attirbute name from path: )" + |
|
945 |
attributeName, 30); |
|
946 |
return attributeName; |
|
947 |
} |
|
824 | 948 |
|
825 | 949 |
/** a utility class that represents a group of terms in a query */ |
826 | 950 |
private class QueryGroup { |
... | ... | |
1107 | 1231 |
} |
1108 | 1232 |
} |
1109 | 1233 |
|
1110 |
/* A method to get rid of attribute part in path expression*/ |
|
1111 |
private String newPathExpressionWithOutAttribute(String pathExpression) |
|
1112 |
{ |
|
1113 |
if (pathExpression == null) |
|
1114 |
{ |
|
1115 |
return null; |
|
1116 |
} |
|
1117 |
int index = pathExpression.lastIndexOf(ATTRIBUTESYMBOL); |
|
1118 |
String newExpression = null; |
|
1119 |
if (index != 1) |
|
1120 |
{ |
|
1121 |
newExpression=pathExpression.substring(0, index-1); |
|
1122 |
} |
|
1123 |
MetaCatUtil.debugMessage("The path expression without attributes: )" + |
|
1124 |
newExpression, 30); |
|
1125 |
return newExpression; |
|
1126 |
} |
|
1234 |
|
|
1127 | 1235 |
|
1128 |
/* A method to get attribute name from path */ |
|
1129 |
private String getAttributeName(String path) |
|
1130 |
{ |
|
1131 |
if (path == null) |
|
1132 |
{ |
|
1133 |
return null; |
|
1134 |
} |
|
1135 |
int index = path.lastIndexOf(ATTRIBUTESYMBOL); |
|
1136 |
int size = path.length(); |
|
1137 |
String attributeName = null; |
|
1138 |
if (index != 1) |
|
1139 |
{ |
|
1140 |
attributeName = path.substring(index+1, size); |
|
1141 |
} |
|
1142 |
MetaCatUtil.debugMessage("The attirbute name from path: )" + |
|
1143 |
attributeName, 30); |
|
1144 |
return attributeName; |
|
1145 |
} |
|
1146 |
|
|
1147 | 1236 |
/* |
1148 | 1237 |
* Constraint the query with @pathexp without using the XML Index, |
1149 | 1238 |
* but nested SQL statements instead. The query migth be slower. |
Also available in: Unified diff
Support returnfields has attributes.