001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.api.util;
015
016import org.gbif.api.model.registry.Contact;
017import org.gbif.api.model.registry.Dataset;
018import org.gbif.api.vocabulary.ContactType;
019
020import java.util.ArrayList;
021import java.util.List;
022import java.util.Objects;
023import java.util.stream.Collectors;
024import java.util.stream.Stream;
025
026import org.apache.commons.lang3.StringUtils;
027
028/**
029 * Adapt the {@link Dataset} {@link Contact} list for what metadata documents generally want.
030 *
031 * @author cgendreau
032 */
033@SuppressWarnings("unused")
034public class ContactAdapter {
035
036  private final List<Contact> contactList;
037
038  public ContactAdapter(List<Contact> contacts) {
039    this.contactList = contacts;
040  }
041
042  /**
043   * Get the list of AssociatedParties. This is defined as all non-primary {@link Contact},
044   * excluding contacts with types the following types considered primary types: Originator,
045   * MetadataAuthor and AdministrativePointOfContact.
046   *
047   * @return list of AssociatedParties or empty list if none found
048   */
049  public List<Contact> getAssociatedParties() {
050    List<Contact> contacts = new ArrayList<>();
051    for (Contact c : this.contactList) {
052      if (!c.isPrimary() && !isPreferredContactType(c.getType())) {
053        contacts.add(c);
054      }
055    }
056    return contacts;
057  }
058
059  /** @return true if contact type is considered a preferred type, or false otherwise */
060  private boolean isPreferredContactType(ContactType type) {
061    return type == ContactType.ORIGINATOR
062        || type == ContactType.ADMINISTRATIVE_POINT_OF_CONTACT
063        || type == ContactType.METADATA_AUTHOR;
064  }
065
066  /**
067   * Get the ResourceCreator {@link Contact}. This is defined as the first primary {@link Contact}
068   * of type ContactType.ORIGINATOR.
069   *
070   * @return first preferred ResourceCreator found or null if none were found
071   */
072  public Contact getResourceCreator() {
073    return getFirstPreferredType(ContactType.ORIGINATOR);
074  }
075
076  /**
077   * Format the name of the contact as "FirstName LastName".
078   *
079   * @param contact contact
080   * @return formatted name or "" if the contact is null or empty
081   */
082  public static String formatContactName(Contact contact) {
083    if (contact == null) {
084      return "";
085    }
086    return Stream.of(contact.getFirstName(), contact.getLastName())
087        .filter(Objects::nonNull)
088        .map(String::trim)
089        .collect(Collectors.joining(StringUtils.SPACE));
090  }
091
092  /**
093   * Get the AdministrativeContact {@link Contact}. This is defined as the first primary {@link
094   * Contact} of type ContactType.ADMINISTRATIVE_POINT_OF_CONTACT.
095   *
096   * @return first preferred AdministrativeContact found or null if none were found
097   */
098  public Contact getAdministrativeContact() {
099    return getFirstPreferredType(ContactType.ADMINISTRATIVE_POINT_OF_CONTACT);
100  }
101
102  /**
103   * Filter contacts based on the provided contact types. The order in which the ContactType are
104   * provided will be respected in the response except missing ContactType will be ommited.
105   * Filtering is done by the {@link #getFirstPreferredType} method.
106   *
107   * @param types contact types
108   * @return filtered contacts or an empty list if none matched
109   */
110  public List<Contact> getFilteredContacts(ContactType... types) {
111    List<Contact> contacts = new ArrayList<>();
112    Contact contact;
113    for (ContactType type : types) {
114      contact = getFirstPreferredType(type);
115      if (contact != null) {
116        contacts.add(contact);
117      }
118    }
119    return contacts;
120  }
121
122  /**
123   * Get the first primary {@link Contact} for the provided type.
124   *
125   * @return first preferred type contact found or null if nothing were found
126   */
127  public Contact getFirstPreferredType(ContactType type) {
128    Contact pref = null;
129    for (Contact c : contactList) {
130      if (type == c.getType()) {
131        if (pref == null || c.isPrimary()) {
132          pref = c;
133        }
134      }
135    }
136    return pref;
137  }
138
139  /**
140   * Get the list of {@link Contact} of type ContactType.ORIGINATOR.
141   *
142   * @return all creators found or empty list if none were found
143   */
144  public List<Contact> getCreators() {
145    return getAllType(ContactType.ORIGINATOR);
146  }
147
148  /**
149   * Get the list of {@link Contact} of type ContactType.ADMINISTRATIVE_POINT_OF_CONTACT.
150   *
151   * @return all contacts found or empty list if none were found
152   */
153  public List<Contact> getContacts() {
154    return getAllType(ContactType.ADMINISTRATIVE_POINT_OF_CONTACT);
155  }
156
157  /**
158   * Get the list of {@link Contact} of type ContactType.METADATA_AUTHOR.
159   *
160   * @return all metadataProviders found or empty list if none were found
161   */
162  public List<Contact> getMetadataProviders() {
163    return getAllType(ContactType.METADATA_AUTHOR);
164  }
165
166  /**
167   * Get all {@link Contact} for the provided type.
168   *
169   * @return all {@link Contact} for specified type or empty list if none found
170   */
171  public List<Contact> getAllType(ContactType type) {
172    List<Contact> primary = new ArrayList<>();
173    for (Contact c : contactList) {
174      if (type == c.getType()) {
175        primary.add(c);
176      }
177    }
178    return primary;
179  }
180}