"FROM dcvalue " + "WHERE dc_type_id = (" + " SELECT dc_type_id " + " FROM dctyperegistry " +
"FROM metadatavalue " + "WHERE metadata_field_id = (" + " SELECT metadata_field_id " + " FROM metadatafieldregistry " +
    public static Integer getNumItems(Context context, String type)        throws SQLException    {        // FIXME: this method is clearly not optimised                // FIXME: we don't yet collect total statistics, such as number of items        // withdrawn, number in process of submission etc.  We should probably do        // that                // start the type constraint        String typeQuery = null;                if (type != null)        {            typeQuery = "SELECT item_id " +                        "FROM dcvalue " +                        "WHERE text_value LIKE '%" + type + "%' " +                        "AND dc_type_id = (" +                        " SELECT dc_type_id " +                        " FROM dctyperegistry " +                        " WHERE element = 'type' " +                        " AND qualifier IS NULL) ";        }                // start the date constraint query buffer        StringBuffer dateQuery = new StringBuffer();        dateQuery.append("SELECT item_id " +                          "FROM dcvalue " +                          "WHERE dc_type_id = (" +                          " SELECT dc_type_id " +                          " FROM dctyperegistry " +                          " WHERE element = 'date' " +                          " AND qualifier = 'accessioned') ");              if (startDate != null)        {           dateQuery.append(" AND text_value::timestamp > '" +                          unParseDate(startDate) + "'::timestamp ");        }                if (endDate != null)        {            dateQuery.append(" AND text_value::timestamp < ' " +                          unParseDate(endDate) + "'::timestamp ");        }                // build the final query        StringBuffer query = new StringBuffer();                query.append("SELECT COUNT(*) AS number " +                  "FROM item " +                  "WHERE in_archive = true " +                   "AND withdrawn = false ");                if (startDate != null || endDate != null)        {            query.append(" AND item_id IN ( " +                         dateQuery.toString() + ") ");        }        if (type != null)        {            query.append(" AND item_id IN ( " +                         typeQuery + ") ");        }                TableRow row = DatabaseManager.querySingle(context, query.toString());        // for some reason the number column is of "long" data type!        Long count = new Long(row.getLongColumn("number"));        return new Integer(count.intValue());    }