/** * Loads page of information. * * @param pageNumber page number * @throws DiscoveryException if loading page fails */ private void loadPage(int pageNumber) throws DiscoveryException { // clear buffers feedRecords.clear(); query.getResult().getRecords().clear(); // prepare and execute query int startRecord = getStartRecordOfPage(pageNumber); query.getFilter().setStartRecord(startRecord); LuceneQueryAdapterImpl lqa = new LuceneQueryAdapterImpl(); lqa.execute(context, query); ResourceIdentifier resourceIdentifier = ResourceIdentifier.newIdentifier(context); // traverse through discovered records and build IFeedRecord for each of them (as // DiscoveredRecordAdapter) DiscoveredRecords records = query.getResult().getRecords(); for (DiscoveredRecord dr : records) { DiscoveredRecordAdapter record = new DiscoveredRecordAdapter(resourceIdentifier, dr); Map<String, IFeedAttribute> attrs = new HashMap<String, IFeedAttribute>(); Map<String, List<String>> data = lqa.getMapping().get(dr); if (data != null) { for (Map.Entry<String, List<String>> e : data.entrySet()) { List<IFeedAttribute> l = new ArrayList<IFeedAttribute>(); for (String s : e.getValue()) { l.add(IFeedAttribute.Factory.create(s, 256)); } attrs.put(e.getKey(), IFeedAttribute.Factory.create(l)); } record.getData(IFeedRecord.STD_COLLECTION_INDEX).putAll(attrs); } linkBuilder.build(record); feedRecords.add(record); } }
/** * Performs search operation. * * @param request HTTP servlet request * @param response HTTP servlet response * @param context request context * @param query query * @return records * @throws Exception if searching fails */ @Override public IFeedRecords doSearch( HttpServletRequest request, HttpServletResponse response, RequestContext context, RestQuery query) throws Exception { MessageBroker msgBroker = new FacesContextBroker(request, response).extractMessageBroker(); final Map<DiscoveredRecord, Map<String, List<String>>> mapping = new HashMap<DiscoveredRecord, Map<String, List<String>>>(); List<IFeedRecords.FieldMeta> fields = new ArrayList<IFeedRecords.FieldMeta>(); loadStdFieldMeta(fields); int startRecord = query.getFilter().getStartRecord(); boolean returnIdsOnly = Val.chkBool(Val.chkStr(request.getParameter("returnIdsOnly")), false); if (returnIdsOnly) { startRecord = 1; query.getFilter().setMaxRecords(1); LuceneQueryAdapter tmp = new LuceneQueryAdapter(); tmp.execute(context, query); query.getFilter().setMaxRecords(query.getResult().getNumberOfHits()); } query.getFilter().setStartRecord(startRecord); LuceneQueryAdapter lqa = new LuceneQueryAdapter() { @Override protected void onRecord(DiscoveredRecord record, Document document) { Map<String, List<String>> fieldMap = new HashMap<String, List<String>>(); for (Fieldable field : document.getFields()) { String name = field.name(); List<String> fieldValues = fieldMap.get(name); if (fieldValues == null) { fieldValues = new ArrayList<String>(); fieldMap.put(name, fieldValues); } fieldValues.add(field.stringValue()); } mapping.put(record, fieldMap); } }; lqa.execute(context, query); startRecord += query.getFilter().getMaxRecords(); loadLuceneMeta(context, fields); OpenSearchProperties osProps = new OpenSearchProperties(); osProps.setShortName(msgBroker.retrieveMessage("catalog.openSearch.shortName")); osProps.setNumberOfHits(query.getResult().getNumberOfHits()); osProps.setStartRecord(query.getFilter().getStartRecord()); osProps.setRecordsPerPage(query.getFilter().getMaxRecords()); ResourceIdentifier resourceIdentifier = ResourceIdentifier.newIdentifier(context); DiscoveredRecordsAdapter discoveredRecordsAdapter = new DiscoveredRecordsAdapter( resourceIdentifier, osProps, fields, query.getResult().getRecords(), mapping); FeedLinkBuilder linkBuilder = new FeedLinkBuilder(context, RequestContext.resolveBaseContextPath(request), msgBroker); for (IFeedRecord record : discoveredRecordsAdapter) { linkBuilder.build(record); } return discoveredRecordsAdapter; /* MessageBroker msgBroker = new FacesContextBroker(request, response).extractMessageBroker(); FeedLinkBuilder linkBuilder = new FeedLinkBuilder(RequestContext.resolveBaseContextPath(request), msgBroker); DcatRecordsAdapter discoveredRecordsAdapter = new DcatRecordsAdapter(msgBroker, linkBuilder, context, query); return discoveredRecordsAdapter; */ }