Project

General

Profile

« Previous | Next » 

Revision 3308

Added by Jing Tao almost 17 years ago

Merge the getting return fields for both elements and attributes.

View differences:

src/edu/ucsb/nceas/metacat/QuerySpecification.java
114 114

  
115 115
    public static final char PREDICATE_END = ']';
116 116

  
117
    private boolean hasAttributeReturnField = false;
117
    //private boolean hasAttributeReturnField = false;
118 118

  
119
    private Hashtable attributeReturnList = new Hashtable();
119
    //private Hashtable attributeReturnList = new Hashtable();
120 120

  
121
    private int countAttributeReturnField = 0;
121
    //private int countAttributeReturnField = 0;
122 122

  
123 123
    private StringBuffer textBuffer = new StringBuffer();
124 124
    
......
393 393
        }
394 394
    }
395 395

  
396
  
396 397
    /**
397
     * A method to get if the query has an attribute return field
398
     */
399
    public boolean containsAttributeReturnField()
400
    {
401
        return hasAttributeReturnField;
402
    }
403

  
404
    /**
405 398
     * Accessor method to return the identifier of this Query
406 399
     */
407 400
    public String getIdentifier()
......
710 703
        }
711 704
    }
712 705
     */
713
    public void handleReturnField(String inputString)
706
    private void handleReturnField(String inputString)
714 707
    {
715 708
        int attributePos = inputString.indexOf(ATTRIBUTESYMBOL);
716 709
        int predicateStart = -1;
......
755 748

  
756 749
        containsExtendedSQL = true;
757 750

  
758
        // make sure if return fields has an attribute or not
759
        if (attributePos == -1)
760
        {
751
     
761 752
            // no attribute value will be returned
762 753
            logMetacat.info("QuerySpecification.handleReturnField(): " );
763 754
            logMetacat.info("  there are no attributes in the XPATH statement" );
764 755
            returnFieldList.add(inputString);
765
            return;
766
        }
756
       
767 757

  
768
        // case where the return field is solely an attribute
769
        logMetacat.info("QuerySpecification.handleReturnField(): " );
770
        logMetacat.info("  Found an attribute in the XPATH statement." );
771
        String returnPath = inputString.substring(0,
772
                attributePos == 0 ? attributePos : attributePos - 1);
773
        String attributeName = inputString.substring(attributePos + 1).trim();
774
        Vector pathInfo = new Vector();
775
        // the vector has the information about return path and
776
        // attributename
777
        pathInfo.addElement(returnPath);
778
        pathInfo.addElement(attributeName);
779
        // put the vector into a hash table. The reseaon why don't put
780
        // return path or attributename as a key is because they are not
781
        // unique
782
        attributeReturnList.put(new Integer(countAttributeReturnField),
783
                pathInfo);
784
        countAttributeReturnField++;
785
        hasAttributeReturnField = true;
758
       
786 759
    }
787 760

  
788 761
    /**
......
1080 1053
            tempVector.add(it.next());
1081 1054
        }
1082 1055

  
1083
        Enumeration attEnum = attributeReturnList.elements();
1056
        /*Enumeration attEnum = attributeReturnList.elements();
1084 1057
        while(attEnum.hasMoreElements()){
1085 1058
            Iterator tempIt = ((Vector)attEnum.nextElement()).iterator();
1086 1059
	    String rfield = "";
......
1097 1070
                }
1098 1071
	    }
1099 1072
            tempVector.add(rfield);
1100
        }
1073
        }*/
1101 1074

  
1102 1075
        // Sort the temporary vector
1103 1076
        java.util.Collections.sort(tempVector);
......
1146 1119
        }
1147 1120
    }*/
1148 1121

  
1149
    /**
1150
     * This method prints sql that finds the values of attributes in the xml
1151
     * documents based upon the whether the returnfield tag in the pathquery
1152
     * document has an attribute symbol (@). This allows for customization of
1153
     * the returned fields.
1154
     *
1155
     * @param doclist the list of document ids to search
1156
     * @param useXMLIndex a boolean flag indicating whether to search using
1157
     *            xml_index
1158
     */
1159
    public String printAttributeQuery(String doclist, boolean useXMLIndex)
1160
    {
1161
        if (useXMLIndex && !containsPredicates) {
1162
            return printAttributeQuery(doclist);
1163
        } else {
1164
            StringBuffer self = new StringBuffer();
1165
            boolean firstfield = true;
1166
            //put the returnfields attributes into the query
1167
            //the for loop allows for multiple fields and attributes
1168
            Enumeration returnAttributes = attributeReturnList.elements();
1169
            while (returnAttributes.hasMoreElements()) {
1170
                Vector currentVector = (Vector) returnAttributes.nextElement();
1171
                String returnPath = (String) currentVector.elementAt(0);
1172
                String attributeName = (String) currentVector.elementAt(1);
1173
                if (firstfield) {
1174
                    firstfield = false;
1175
                } else {
1176
                    self.append(" UNION ");
1177
                }
1178
                self.append("select xml_nodes.docid, '");
1179
                self.append(returnPath.replaceAll("'", "''"));
1180
                self.append("' as path, xml_nodes.nodedata, xml_nodes.nodename ");
1181
                self.append("from xml_nodes, xml_documents ");
1182
                self.append("where parentnodeid IN ");
1183
                self.append(QueryTerm.useNestedStatements(returnPath));
1184
                self.append(" AND xml_nodes.nodename like '");
1185
                self.append(attributeName);
1186
                self.append("' AND xml_nodes.docid in (");
1187
                self.append(doclist);
1188
                self.append(") AND xml_nodes.nodetype = 'ATTRIBUTE'");
1189
                self.append(" AND xml_nodes.rootnodeid = xml_documents.rootnodeid");
1190
            }
1122
   
1191 1123

  
1192
            logMetacat.info("Attribute query: " + self.toString());
1193 1124

  
1194
            return self.toString();
1195
        }
1196
    }
1197 1125

  
1198
    /**
1199
     * This method prints sql that finds the values of attributes in the xml
1200
     * documents based upon the whether the returnfield tag in the pathquery
1201
     * document has an attribute symbol (@). This allows for customization of
1202
     * the returned fields.
1203
     *
1204
     * @param doclist the list of document ids to search
1205
     */
1206
    public String printAttributeQuery(String doclist)
1207
    {
1208
        StringBuffer self = new StringBuffer();
1209
        self.append("select xml_nodes.docid, xml_index.path, ");
1210
        self.append("xml_nodes.nodedata, xml_nodes.nodename ");
1211
        self.append("from xml_index, xml_nodes where xml_index.nodeid=");
1212
        self.append("xml_nodes.parentnodeid and (");
1213
        boolean firstfield = true;
1214
        //put the returnfields attributes into the query
1215
        //the for loop allows for multiple fields and attributes
1216
        Enumeration returnAttributes = attributeReturnList.elements();
1217
        while (returnAttributes.hasMoreElements()) {
1218
            Vector currentVector = (Vector) returnAttributes.nextElement();
1219
            String returnPath = (String) currentVector.elementAt(0);
1220
            String attributeName = (String) currentVector.elementAt(1);
1221
            if (firstfield) {
1222
                firstfield = false;
1223
                self.append("( ");
1224
                if(returnPath != null){
1225
                    self.append("xml_index.path like '");
1226
                    self.append(returnPath);
1227
                    self.append("' AND ");
1228
                }else {
1229
                  logMetacat.info("QuerySpecification.printAttributeQuery: "
1230
                   + "returnPath is: " + returnPath);
1231
                }
1232
                self.append("xml_nodes.nodename like '");
1233
                self.append(attributeName);
1234
                self.append("') ");
1235
            } else {
1236
                self.append(" or (");
1237
                if(returnPath != null){
1238
                    self.append("xml_index.path like '");
1239
                    self.append(returnPath);
1240
                    self.append("' AND ");
1241
                }else {
1242
                  logMetacat.info("QuerySpecification.printAttributeQuery: "
1243
                   + "returnPath is null: " + returnPath);
1244
                }
1245
                self.append("xml_nodes.nodename like '");
1246
                self.append(attributeName);
1247
                self.append("') ");
1248
            }
1249
        }
1250
        self.append(") AND xml_nodes.docid in (");
1251
        self.append(doclist);
1252
        self.append(") AND xml_nodes.nodetype = 'ATTRIBUTE'");
1253
        logMetacat.warn("Attribute query: " + self.toString());
1254

  
1255
        return self.toString();
1256
    }
1257

  
1258 1126
    public static String printRelationSQL(String docid)
1259 1127
    {
1260 1128
        StringBuffer self = new StringBuffer();
src/edu/ucsb/nceas/metacat/DBQuery.java
1143 1143
                   + (docListResultEnd - extendedQueryEnd));
1144 1144
           }
1145 1145

  
1146
           // get attribures return
1147
           double startGetAttribute = System.currentTimeMillis()/1000;
1148
           docListResult = getAttributeValueForReturn(qspec,
1149
                           docListResult, doclist.toString(), useXMLIndex);
1150
           double endGetAttribute = System.currentTimeMillis()/1000;
1151
           logMetacat.warn(
1152
                   "Time to get attribute return value after"
1153
                   + " execute extended query: "
1154
                   + (endGetAttribute - startGetAttribute));
1155
           MetaCatUtil.writeDebugToFile(
1156
                   "Get attribute return field "
1157
                   + (endGetAttribute - startGetAttribute));
1158
           MetaCatUtil.writeDebugToDelimiteredFile(" "+ (endGetAttribute - startGetAttribute), false);
1146
         
1159 1147
           
1160 1148
           
1161 1149
       }//if doclist lenght is great than zero
......
1334 1322
        return enumVector;
1335 1323
    }
1336 1324

  
1337
    /*
1338
     * A method to return search result after running a query which return
1339
     * field have attribue
1340
     */
1341
    private ResultDocumentSet getAttributeValueForReturn(QuerySpecification squery,
1342
            ResultDocumentSet docInformationList, String docList, boolean useXMLIndex)
1343
    {
1344
        StringBuffer XML = null;
1345
        String sql = null;
1346
        DBConnection dbconn = null;
1347
        PreparedStatement pstmt = null;
1348
        ResultSet rs = null;
1349
        int serialNumber = -1;
1350
        boolean tableHasRows = false;
1325
  
1351 1326

  
1352
        //check the parameter
1353
        if (squery == null || docList == null || docList.length() < 0) { return docInformationList; }
1354

  
1355
        // if has attribute as return field
1356
        if (squery.containsAttributeReturnField()) {
1357
            sql = squery.printAttributeQuery(docList, useXMLIndex);
1358
            try {
1359
                dbconn = DBConnectionPool
1360
                        .getDBConnection("DBQuery.getAttributeValue");
1361
                serialNumber = dbconn.getCheckOutSerialNumber();
1362
                pstmt = dbconn.prepareStatement(sql);
1363
                pstmt.execute();
1364
                rs = pstmt.getResultSet();
1365
                tableHasRows = rs.next();
1366
                while (tableHasRows) {
1367
                    String docid = rs.getString(1).trim();
1368
                    String fieldname = rs.getString(2);
1369
                    String fielddata = rs.getString(3);
1370
                    String attirbuteName = rs.getString(4);
1371
                    XML = new StringBuffer();
1372

  
1373
                    XML.append("<param name=\"");
1374
                    XML.append(fieldname);
1375
                    XML.append("/");
1376
                    XML.append(QuerySpecification.ATTRIBUTESYMBOL);
1377
                    XML.append(attirbuteName);
1378
                    XML.append("\">");
1379
                    XML.append(fielddata);
1380
                    XML.append("</param>");
1381
                    tableHasRows = rs.next();
1382

  
1383
                    if (docInformationList.containsDocid(docid)) {
1384
                        String removedelement = (String) docInformationList
1385
                                .remove(docid);
1386
                        docInformationList.put(docid, removedelement
1387
                                + XML.toString());
1388
                    } else {
1389
                        docInformationList.put(docid, XML.toString());
1390
                    }
1391
                }//while
1392
                rs.close();
1393
                pstmt.close();
1394
            } catch (Exception se) {
1395
                logMetacat.error(
1396
                        "Error in DBQuery.getAttributeValue1: "
1397
                                + se.getMessage());
1398
            } finally {
1399
                try {
1400
                    pstmt.close();
1401
                }//try
1402
                catch (SQLException sqlE) {
1403
                    logMetacat.error(
1404
                            "Error in DBQuery.getAttributeValue2: "
1405
                                    + sqlE.getMessage());
1406
                }//catch
1407
                finally {
1408
                    DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1409
                }//finally
1410
            }//finally
1411
        }//if
1412
        return docInformationList;
1413

  
1414
    }
1415

  
1416 1327
    /*
1417 1328
     * A method to create a query to get owner's docid list
1418 1329
     */

Also available in: Unified diff