package com.linkedin.datahub.util;

import com.linkedin.common.urn.CorpuserUrn;
import com.linkedin.datahub.models.table.CompanyUser;
import com.linkedin.datahub.models.table.User;
import com.linkedin.datahub.models.table.UserEntity;
import com.linkedin.identity.CorpUser;

import javax.annotation.Nonnull;
import java.net.URISyntaxException;


public class CorpUserUtil {
    private CorpUserUtil() {
    }

    /**
     * Deserialize {@link CorpuserUrn} string
     *
     * @param urnStr urn string
     * @return {@link CorpuserUrn}
     */
    @Nonnull
    public static CorpuserUrn toCorpUserUrn(@Nonnull String urnStr) throws URISyntaxException {
        return CorpuserUrn.createFromString(urnStr);
    }

    /**
     * Convert {@link CorpUser} to {@link User}
     * @param corpUser {@link CorpUser}
     * @return {@link User}
     */
    @Nonnull
    public static User toCorpUserView(CorpUser corpUser) {
        User user = new User();
        user.setEmail(corpUser.getInfo().getEmail());
        user.setName(corpUser.getInfo().getFullName());
        user.setUserName(corpUser.getUsername());
        user.setPictureLink(corpUser.getEditableInfo().getPictureLink().toString());
        return user;
    }

    /**
     * Convert {@link CorpUser} to {@link CompanyUser}
     * @param corpUser {@link CorpUser}
     * @return {@link CompanyUser}
     */
    @Nonnull
    public static CompanyUser toCompanyUserView(CorpUser corpUser) {
        CompanyUser user = new CompanyUser();
        user.setUserName(corpUser.getUsername());
        user.setDisplayName(corpUser.getInfo().getDisplayName());
        user.setPictureLink(corpUser.getEditableInfo().getPictureLink().toString());
        return user;
    }

    /**
     * Convert {@link CorpUser} to {@link UserEntity}
     * @param corpUser {@link CorpUser}
     * @return {@link UserEntity}
     */
    @Nonnull
    public static UserEntity toUserEntityView(CorpUser corpUser) {
        UserEntity user = new UserEntity();
        user.setCategory("person");
        user.setLabel(corpUser.getUsername());
        user.setDisplayName(corpUser.getInfo().getDisplayName());
        user.setPictureLink(corpUser.getEditableInfo().getPictureLink().toString());
        return user;
    }
}
@@ -2062,32 +2062,70 @@
 getUsername());%0A
+        if (corpUser.hasInfo()) %7B%0A    
         user.set
@@ -2166,32 +2166,135 @@
 DisplayName());%0A
+        %7D%0A        if (corpUser.hasEditableInfo() && corpUser.getEditableInfo().hasPictureLink()) %7B%0A    
         user.set
@@ -2354,32 +2354,42 @@
 ().toString());%0A
+        %7D%0A
         return u

load default picture link if not present (#1709)