package io.cattle.platform.servicediscovery.api.service.impl;

import io.cattle.platform.core.model.Certificate;
import io.cattle.platform.db.jooq.dao.impl.AbstractJooqDao;
import io.cattle.platform.object.ObjectManager;
import io.cattle.platform.servicediscovery.api.resource.ServiceDiscoveryConfigItem;
import io.cattle.platform.servicediscovery.api.service.RancherConfigToComposeFormatter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class RancherCertificatesToComposeFormatter extends AbstractJooqDao
        implements RancherConfigToComposeFormatter {

    @Inject
    ObjectManager objManager;

    @Override
    @SuppressWarnings("unchecked")
    public Object format(ServiceDiscoveryConfigItem item, Object valueToTransform) {
        if (item.getDockerName().equalsIgnoreCase(ServiceDiscoveryConfigItem.CERTIFICATES.getDockerName())) {
            List<Integer> certificateIds = (List<Integer>) valueToTransform;
            List<String> certificateNames = new ArrayList<>();
            for (Integer certificateId : certificateIds) {
                certificateNames.add(getCertName(certificateId));
            }
            return certificateNames;
        } else if (item.getDockerName().equals(ServiceDiscoveryConfigItem.DEFAULT_CERTIFICATE.getDockerName())) {
            Integer defaultCertId = (Integer) valueToTransform;
            return getCertName(defaultCertId);
        } else {
            return null;
        }
    }

    private String getCertName(Integer certId) {
        return objManager.loadResource(Certificate.class, certId.longValue()).getName();
    }
}

@@ -495,16 +495,62 @@
 Named;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A%0A
 @Named%0Ap
@@ -981,21 +981,20 @@
    List%3C
-Integ
+Numb
 er%3E cert
@@ -1012,21 +1012,20 @@
 = (List%3C
-Integ
+Numb
 er%3E) val
@@ -1119,21 +1119,20 @@
    for (
-Integ
+Numb
 er certi
@@ -1180,57 +1180,179 @@
     
-certificateNames.add(getCertName(certificateId));
+String certName = getCertName(certificateId);%0A                if (StringUtils.isNotBlank(certName)) %7B%0A                    certificateNames.add(certName);%0A                %7D
 %0A   
@@ -1718,22 +1718,51 @@
 ame(
-Integer certId
+Number certId) %7B%0A        if (certId == null
 ) %7B%0A
@@ -1769,22 +1769,69 @@
         
+   
+ 
 return
+ null;%0A        %7D%0A        Certificate cert =
  objMana
@@ -1885,16 +1885,59 @@
 Value())
+;%0A        return cert == null ? null : cert
 .getName

Fix export NPE