/*
 * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.wso2.carbon.idp.mgt.ui.client;

import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.governance.stub.IdentityGovernanceAdminServiceIdentityGovernanceExceptionException;
import org.wso2.carbon.identity.governance.stub.IdentityGovernanceAdminServiceStub;
import org.wso2.carbon.identity.governance.stub.bean.ConnectorConfig;
import org.wso2.carbon.identity.governance.stub.bean.Property;

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

public class IdentityGovernanceAdminClient {

    protected IdentityGovernanceAdminServiceStub stub = null;
    public static final String IDENTITY_MGT_ADMIN_SERVICE_URL = "IdentityGovernanceAdminService";
    public static final String DEFAULT = "DEFAULT";

    protected static Log log = LogFactory.getLog(IdentityGovernanceAdminClient.class);

    public IdentityGovernanceAdminClient(String cookie, String backendServerURL,
                                         ConfigurationContext configContext)
            throws Exception {
        try {
            stub = new IdentityGovernanceAdminServiceStub(configContext, backendServerURL + IDENTITY_MGT_ADMIN_SERVICE_URL);
            ServiceClient client = stub._getServiceClient();
            Options option = client.getOptions();
            option.setManageSession(true);
            option.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, cookie);
        } catch (Exception e) {
            throw new Exception("Error occurred while creating TenantIdentityMgtClient Object", e);
        }
    }

    public Map<String, Map<String, List<ConnectorConfig>>> getConnectorList() throws RemoteException, IdentityGovernanceAdminServiceIdentityGovernanceExceptionException {

        ConnectorConfig[] configs = stub.getConnectorList();
        Map<String, Map<String, List<ConnectorConfig>>> catMap = new HashMap<String, Map<String, List<ConnectorConfig>>>();

        for (ConnectorConfig conf : configs) {
            String categoryName = StringUtils.isBlank(conf.getCategory()) ? DEFAULT : conf.getCategory();
            String subCategoryName = StringUtils.isBlank(conf.getSubCategory()) ? DEFAULT : conf.getSubCategory();
            if (catMap.containsKey(categoryName)) {
                Map<String, List<ConnectorConfig>> subCatMap = catMap.get(categoryName);
                if (subCatMap.containsKey(subCategoryName)) {
                    List<ConnectorConfig> confList = subCatMap.get(subCategoryName);
                    confList.add(conf);
                } else {
                    List<ConnectorConfig> confList = new ArrayList<ConnectorConfig>();
                    confList.add(conf);
                    subCatMap.put(subCategoryName, confList);
                }
            } else {
                Map<String, List<ConnectorConfig>> subCatMap = new HashMap<String, List<ConnectorConfig>>();
                catMap.put(categoryName, subCatMap);
                List<ConnectorConfig> confList = new ArrayList<ConnectorConfig>();
                confList.add(conf);
                subCatMap.put(subCategoryName, confList);
            }
        }
        return catMap;
    }

    public void updateConfigurations(Property[] properties) throws RemoteException, IdentityGovernanceAdminServiceIdentityGovernanceExceptionException {
        stub.updateConfigurations(properties);
    }

}

@@ -2851,25 +2851,56 @@
 g%3E%3E%3E();%0A
-%0A
+        if (configs != null) %7B%0A 
         
 for (Con
@@ -2891,16 +2891,19 @@
         
+   
 for (Con
@@ -2929,24 +2929,28 @@
  configs) %7B%0A
+    
             
@@ -3051,24 +3051,28 @@
             
+ 
+   
 String subCa
@@ -3170,24 +3170,28 @@
             
+    
 if (catMap.c
@@ -3226,32 +3226,36 @@
                 
+ 
+   
 Map%3CString, List
@@ -3323,24 +3323,28 @@
             
+    
 if (subCatMa
@@ -3389,32 +3389,36 @@
                 
+ 
+   
 List%3CConnectorCo
@@ -3458,32 +3458,36 @@
 bCategoryName);%0A
+    
                 
@@ -3518,32 +3518,36 @@
                 
+    
 %7D else %7B%0A       
@@ -3531,32 +3531,36 @@
        %7D else %7B%0A
+    
                 
@@ -3642,32 +3642,36 @@
                 
+    
 confList.add(con
@@ -3666,32 +3666,36 @@
 List.add(conf);%0A
+    
                 
@@ -3748,34 +3748,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D el
@@ -3779,32 +3779,36 @@
        %7D else %7B%0A
+    
                 
@@ -3916,16 +3916,20 @@
         
+   
+ 
 catMap.p
@@ -3957,16 +3957,20 @@
 atMap);%0A
+    
         
@@ -4052,32 +4052,36 @@
                 
+   
+ 
 confList.add(con
@@ -4076,32 +4076,36 @@
 List.add(conf);%0A
+    
                 
@@ -4138,32 +4138,50 @@
 ame, confList);%0A
+                %7D%0A
             %7D%0A  

Fix NPE while rendering resident IDP UI when conectors are not available