/**
   * Create a MEF2 file in ZIP format.
   *
   * @param context
   * @param uuids List of records to export.
   * @param format {@link Format} to export.
   * @param skipUUID
   * @param stylePath
   * @return MEF2 File
   * @throws Exception
   */
  public static Path doExport(
      ServiceContext context,
      Set<String> uuids,
      Format format,
      boolean skipUUID,
      Path stylePath,
      boolean resolveXlink,
      boolean removeXlinkAttribute)
      throws Exception {

    Path file = Files.createTempFile("mef-", ".mef");
    SearchManager searchManager = context.getBean(SearchManager.class);
    String contextLang =
        context.getLanguage() == null ? Geonet.DEFAULT_LANGUAGE : context.getLanguage();
    try (FileSystem zipFs = ZipUtil.createZipFs(file);
        IndexAndTaxonomy indexReaderAndTaxonomy = searchManager.getNewIndexReader(contextLang); ) {
      StringBuilder csvBuilder =
          new StringBuilder(
              "\"schema\";\"uuid\";\"id\";\"type\";\"isHarvested\";\"title\";\"abstract\"\n");
      Element html =
          new Element("html")
              .addContent(
                  new Element("head")
                      .addContent(
                          Arrays.asList(
                              new Element("title").setText("Export Index"),
                              new Element("link")
                                  .setAttribute("rel", "stylesheet")
                                  .setAttribute(
                                      "href",
                                      "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"),
                              new Element("style")
                                  .setText(
                                      "body {\n"
                                          + "  padding-left: 10px;\n"
                                          + "}\n"
                                          + "p.abstract {\n"
                                          + "  font-style: italic;\n"
                                          + "}\n"
                                          + ".entry {\n"
                                          + "  padding: 20px;\n"
                                          + "  margin: 20px 0;\n"
                                          + "  border: 1px solid #eee;\n"
                                          + "  border-left-width: 5px;\n"
                                          + "  border-radius: 3px;\n"
                                          + "  border-left-color: #1b809e;\n"
                                          + "}\n"
                                          + ".entry:hover {\n"
                                          + "  background-color: #f5f5f5;\n"
                                          + "}\n"))));
      Element body = new Element("body");
      html.addContent(body);
      for (Object uuid1 : uuids) {
        String uuid = (String) uuid1;
        IndexSearcher searcher = new IndexSearcher(indexReaderAndTaxonomy.indexReader);
        BooleanQuery query = new BooleanQuery();
        query.add(new BooleanClause(new TermQuery(new Term(UUID, uuid)), BooleanClause.Occur.MUST));
        query.add(
            new BooleanClause(
                new TermQuery(new Term(LOCALE, contextLang)), BooleanClause.Occur.SHOULD));
        TopDocs topDocs = searcher.search(query, NoFilterFilter.instance(), 5);
        String mdSchema = null, mdTitle = null, mdAbstract = null, id = null, isHarvested = null;
        MetadataType mdType = null;

        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
          Document doc = searcher.doc(scoreDoc.doc);
          String locale = doc.get(Geonet.IndexFieldNames.LOCALE);
          if (mdSchema == null) {
            mdSchema = doc.get(Geonet.IndexFieldNames.SCHEMA);
          }
          if (mdTitle == null || contextLang.equals(locale)) {
            mdTitle = doc.get(LuceneIndexField.TITLE);
          }
          if (mdAbstract == null || contextLang.equals(locale)) {
            mdAbstract = doc.get(LuceneIndexField.ABSTRACT);
          }
          if (id == null) {
            id = doc.get(LuceneIndexField.ID);
          }
          if (isHarvested == null) {
            isHarvested = doc.get(Geonet.IndexFieldNames.IS_HARVESTED);
          }
          if (mdType == null) {
            String tmp = doc.get(LuceneIndexField.IS_TEMPLATE);
            mdType = MetadataType.lookup(tmp.charAt(0));
          }
        }

        if (mdType == null) {
          mdType = MetadataType.METADATA;
        }
        csvBuilder
            .append('"')
            .append(cleanForCsv(mdSchema))
            .append("\";\"")
            .append(cleanForCsv(uuid))
            .append("\";\"")
            .append(cleanForCsv(id))
            .append("\";\"")
            .append(mdType.toString())
            .append("\";\"")
            .append(cleanForCsv(isHarvested))
            .append("\";\"")
            .append(cleanForCsv(mdTitle))
            .append("\";\"")
            .append(cleanForCsv(mdAbstract))
            .append("\"\n");

        body.addContent(
            new Element("div")
                .setAttribute("class", "entry")
                .addContent(
                    Arrays.asList(
                        new Element("h4")
                            .setAttribute("class", "title")
                            .addContent(
                                new Element("a")
                                    .setAttribute("href", uuid)
                                    .setText(cleanXml(mdTitle))),
                        new Element("p")
                            .setAttribute("class", "abstract")
                            .setText(cleanXml(mdAbstract)),
                        new Element("table")
                            .setAttribute("class", "table")
                            .addContent(
                                Arrays.asList(
                                    new Element("thead")
                                        .addContent(
                                            new Element("tr")
                                                .addContent(
                                                    Arrays.asList(
                                                        new Element("th").setText("ID"),
                                                        new Element("th").setText("UUID"),
                                                        new Element("th").setText("Type"),
                                                        new Element("th").setText("isHarvested")))),
                                    new Element("tbody")
                                        .addContent(
                                            new Element("tr")
                                                .addContent(
                                                    Arrays.asList(
                                                        new Element("td")
                                                            .setAttribute("class", "id")
                                                            .setText(id),
                                                        new Element("td")
                                                            .setAttribute("class", "uuid")
                                                            .setText(
                                                                xmlContentEscaper().escape(uuid)),
                                                        new Element("td")
                                                            .setAttribute("class", "type")
                                                            .setText(mdType.toString()),
                                                        new Element("td")
                                                            .setAttribute("class", "isHarvested")
                                                            .setText(isHarvested)))))))));
        createMetadataFolder(
            context, uuid, zipFs, skipUUID, stylePath, format, resolveXlink, removeXlinkAttribute);
      }
      Files.write(zipFs.getPath("/index.csv"), csvBuilder.toString().getBytes(Constants.CHARSET));
      Files.write(zipFs.getPath("/index.html"), Xml.getString(html).getBytes(Constants.CHARSET));
    }
    return file;
  }
Example #2
0
  public static List<Element> handlePropertyName(
      String[] propertyNames,
      ServiceContext context,
      boolean freq,
      int maxRecords,
      String cswServiceSpecificConstraint,
      LuceneConfig luceneConfig)
      throws Exception {

    List<Element> domainValuesList = null;

    if (Log.isDebugEnabled(Geonet.CSW))
      Log.debug(
          Geonet.CSW,
          "Handling property names '"
              + Arrays.toString(propertyNames)
              + "' with max records of "
              + maxRecords);

    for (int i = 0; i < propertyNames.length; i++) {

      if (i == 0) domainValuesList = new ArrayList<Element>();

      // Initialize list of values element.
      Element listOfValues = null;

      // Generate DomainValues element
      Element domainValues = new Element("DomainValues", Csw.NAMESPACE_CSW);

      // FIXME what should be the type ???
      domainValues.setAttribute("type", "csw:Record");

      String property = propertyNames[i].trim();

      // Set propertyName in any case.
      Element pn = new Element("PropertyName", Csw.NAMESPACE_CSW);
      domainValues.addContent(pn.setText(property));

      GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
      SearchManager sm = gc.getSearchmanager();

      IndexAndTaxonomy indexAndTaxonomy = sm.getNewIndexReader(null);
      try {
        GeonetworkMultiReader reader = indexAndTaxonomy.indexReader;
        BooleanQuery groupsQuery = (BooleanQuery) CatalogSearcher.getGroupsQuery(context);
        BooleanQuery query = null;

        // Apply CSW service specific constraint
        if (StringUtils.isNotEmpty(cswServiceSpecificConstraint)) {
          Query constraintQuery =
              CatalogSearcher.getCswServiceSpecificConstraintQuery(
                  cswServiceSpecificConstraint, luceneConfig);

          query = new BooleanQuery();

          BooleanClause.Occur occur = LuceneUtils.convertRequiredAndProhibitedToOccur(true, false);

          query.add(groupsQuery, occur);
          query.add(constraintQuery, occur);

        } else {
          query = groupsQuery;
        }

        List<Pair<String, Boolean>> sortFields =
            Collections.singletonList(Pair.read(Geonet.SearchResult.SortBy.RELEVANCE, true));
        Sort sort = LuceneSearcher.makeSort(sortFields, context.getLanguage(), false);
        CachingWrapperFilter filter = null;

        Pair<TopDocs, Element> searchResults =
            LuceneSearcher.doSearchAndMakeSummary(
                maxRecords,
                0,
                maxRecords,
                context.getLanguage(),
                null,
                reader,
                query,
                filter,
                sort,
                null,
                false,
                false,
                false,
                false // Scoring is useless for GetDomain operation
                );
        TopDocs hits = searchResults.one();

        try {
          // Get mapped lucene field in CSW configuration
          String indexField = CatalogConfiguration.getFieldMapping().get(property.toLowerCase());
          if (indexField != null) property = indexField;

          // check if params asked is in the index using getFieldNames ?
          FieldInfos fi = new SlowCompositeReaderWrapper(reader).getFieldInfos();
          if (fi.fieldInfo(property) == null) continue;

          boolean isRange = false;
          if (CatalogConfiguration.getGetRecordsRangeFields().contains(property)) isRange = true;

          if (isRange) listOfValues = new Element("RangeOfValues", Csw.NAMESPACE_CSW);
          else listOfValues = new Element("ListOfValues", Csw.NAMESPACE_CSW);

          Set<String> fields = new HashSet<String>();
          fields.add(property);
          fields.add("_isTemplate");

          // parse each document in the index
          String[] fieldValues;
          SortedSet<String> sortedValues = new TreeSet<String>();
          HashMap<String, Integer> duplicateValues = new HashMap<String, Integer>();
          for (int j = 0; j < hits.scoreDocs.length; j++) {
            DocumentStoredFieldVisitor selector = new DocumentStoredFieldVisitor(fields);
            reader.document(hits.scoreDocs[j].doc, selector);
            Document doc = selector.getDocument();

            // Skip templates and subTemplates
            String[] isTemplate = doc.getValues("_isTemplate");
            if (isTemplate[0] != null && !isTemplate[0].equals("n")) continue;

            // Get doc values for specified property
            fieldValues = doc.getValues(property);
            if (fieldValues == null) continue;

            addtoSortedSet(sortedValues, fieldValues, duplicateValues);
          }

          SummaryComparator valuesComparator =
              new SummaryComparator(SortOption.FREQUENCY, Type.STRING, context.getLanguage(), null);
          TreeSet<Map.Entry<String, Integer>> sortedValuesFrequency =
              new TreeSet<Map.Entry<String, Integer>>(valuesComparator);
          sortedValuesFrequency.addAll(duplicateValues.entrySet());

          if (freq) return createValuesByFrequency(sortedValuesFrequency);
          else listOfValues.addContent(createValuesElement(sortedValues, isRange));

        } finally {
          // any children means that the catalog was unable to determine
          // anything about the specified parameter
          if (listOfValues != null && listOfValues.getChildren().size() != 0)
            domainValues.addContent(listOfValues);

          // Add current DomainValues to the list
          domainValuesList.add(domainValues);
        }
      } finally {
        sm.releaseIndexReader(indexAndTaxonomy);
      }
    }
    return domainValuesList;
  }