package com.linkedin.datahub.util;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import com.linkedin.common.Owner;
import com.linkedin.common.OwnershipType;
import com.linkedin.common.urn.Urn;
import com.linkedin.datahub.models.view.DatasetOwner;
import com.linkedin.identity.CorpUser;
import java.net.URISyntaxException;
import javax.annotation.Nonnull;


public class OwnerUtil {

  private OwnerUtil() {

  }

  /**
   * Convert from TMS Owner to WhereHows DatasetOwner
   * @param owner Owner
   * @return DatasetOwner
   */
  @VisibleForTesting
  public static DatasetOwner toWhOwner(@Nonnull Owner owner, @Nonnull CorpUser corpUser) {
    DatasetOwner dsOwner = new DatasetOwner();
    dsOwner.setConfirmedBy("UI");
    dsOwner.setEmail(corpUser.getInfo().getEmail());
    dsOwner.setIdType("USER");
    dsOwner.setIsActive(corpUser.getInfo().isActive());
    dsOwner.setIsGroup(false);
    dsOwner.setName(corpUser.getInfo().getFullName());
    dsOwner.setNamespace("urn:li:corpuser");
    dsOwner.setSource("UI");
    dsOwner.setType(OWNER_CATEGORY_MAP_INV.get(owner.getType()));
    dsOwner.setUserName(corpUser.getUsername());
    dsOwner.setPictureLink(corpUser.getEditableInfo().getPictureLink().toString());

    return dsOwner;
  }

  /**
   * Convert from a WhereHows DatasetOwner to TMS Owner
   * @param dsOwner dsOwner
   * @return Owner
   */
  @Nonnull
  public static Owner toTmsOwner(@Nonnull DatasetOwner dsOwner) throws URISyntaxException {
    return new Owner().setOwner(new Urn(dsOwner.getNamespace() + ":" + dsOwner.getUserName()))
        .setType(OWNER_CATEGORY_MAP.get(dsOwner.getType()));
  }

  /**
   * Mapping between WhereHows owner type values and TMS OwnerCategory
   */
  private static final BiMap<String, OwnershipType> OWNER_CATEGORY_MAP =
          new ImmutableBiMap.Builder<String, OwnershipType>()
                  // format
                  .put("DataOwner", OwnershipType.DATAOWNER)
                  .put("Producer", OwnershipType.PRODUCER)
                  .put("Delegate", OwnershipType.DELEGATE)
                  .put("Stakeholder", OwnershipType.STAKEHOLDER)
                  .put("Consumer", OwnershipType.CONSUMER)
                  .put("Developer", OwnershipType.DEVELOPER)
                  .build();

  private static final BiMap<OwnershipType, String> OWNER_CATEGORY_MAP_INV = OWNER_CATEGORY_MAP.inverse();
}

@@ -834,216 +834,52 @@
 .set
-Email(corpUser.getInfo().getEmail());%0A    dsOwner.setIdType(%22USER%22);%0A    dsOwner.setIsActive(corpUser.getInfo().isActive());%0A    dsOwner.setIsGroup(false);%0A    dsOwner.setName(corpUser.getInfo().getFullName()
+IdType(%22USER%22);%0A    dsOwner.setIsGroup(false
 );%0A 
@@ -943,32 +943,81 @@
 etSource(%22UI%22);%0A
+    dsOwner.setUserName(corpUser.getUsername());%0A
     dsOwner.setT
@@ -1070,57 +1070,359 @@
 ));%0A
-    dsOwner.setUserName(corpUser.getUsername());%0A
+%0A    if (corpUser.hasInfo()) %7B%0A      dsOwner.setEmail(corpUser.getInfo().getEmail());%0A      dsOwner.setIsActive(corpUser.getInfo().isActive());%0A      if (corpUser.getInfo().hasFullName()) %7B%0A        dsOwner.setName(corpUser.getInfo().getFullName());%0A      %7D%0A    %7D%0A%0A    if (corpUser.hasEditableInfo() && corpUser.getEditableInfo().hasPictureLink()) %7B%0A  
     
@@ -1500,16 +1500,22 @@
 ring());
+%0A    %7D
 %0A%0A    re

Handle optional aspects/fields for CorpUser gracefully