All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencastproject.util.jaxb.ExtendedMetadataAdapter Maven / Gradle / Ivy

There is a newer version: 16.7
Show newest version
/*
 * Licensed to The Apereo Foundation under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 *
 * The Apereo Foundation licenses this file to you under the Educational
 * Community License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License
 * at:
 *
 *   http://opensource.org/licenses/ecl2.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

package org.opencastproject.util.jaxb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * An adapter to transform a map with extended metadata into an object that can be represented in XMl, and back.
 */
public class ExtendedMetadataAdapter extends XmlAdapter>>> {

  @Override
  public ExtendedMetadata marshal(Map>> map) {
    List entries = new ArrayList<>();
    for (String catalogType: map.keySet()) {
      Map> catalog = map.get(catalogType);

      for (String fieldName: catalog.keySet()) {
        List values = catalog.get(fieldName);
        entries.add(new ExtendedMetadataEntry(catalogType, fieldName, values));
      }
    }
    return new ExtendedMetadata(entries);
  }

  @Override
  public Map>> unmarshal(ExtendedMetadata extendedMetadata) {
    Map>> map = new HashMap<>();
    for (ExtendedMetadataEntry entry: extendedMetadata.getEntries()) {
      String catalogType = entry.getCatalogType();
      String fieldName = entry.getFieldName();
      List values = entry.getValues();

      Map> catalog = map.computeIfAbsent(catalogType, c -> new HashMap<>());
      catalog.put(fieldName, values);
    }
    return map;
  }

  /**
   * These classes are used to represent the extended metadata map in XML.
   */

  protected static class ExtendedMetadata {
    @XmlElement(name = "entry")
    private List entries;

    public ExtendedMetadata() {
      // needed by JAXB
    }

    public ExtendedMetadata(List entries) {
      this.entries = entries;
    }

    public List getEntries() {
      return entries;
    }
  }

  protected static class ExtendedMetadataEntry {
    @XmlAttribute
    private String catalogType;
    @XmlAttribute
    private String fieldName;
    @XmlValue
    private List values;

    public ExtendedMetadataEntry() {
      // needed by JAXB
    }

    public ExtendedMetadataEntry(String catalogType, String fieldName, List values) {
      this.catalogType = catalogType;
      this.fieldName = fieldName;
      this.values = values;
    }

    public String getCatalogType() {
      return catalogType;
    }

    public String getFieldName() {
      return fieldName;
    }

    public List getValues() {
      return values;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy