/*
 * 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.identity.governance;

import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.core.AbstractAdmin;
import org.wso2.carbon.identity.application.common.model.Property;
import org.wso2.carbon.identity.governance.bean.ConnectorConfig;
import org.wso2.carbon.identity.governance.common.IdentityConnectorConfig;
import org.wso2.carbon.identity.governance.internal.IdentityMgtServiceDataHolder;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class IdentityGovernanceAdminService extends AbstractAdmin {

    public ConnectorConfig[] getConnectorList() throws IdentityGovernanceException {

        String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
        IdentityGovernanceService identityGovernanceService = IdentityMgtServiceDataHolder.getInstance()
                .getIdentityGovernanceService();
        List<IdentityConnectorConfig> list = IdentityMgtServiceDataHolder.getInstance()
                .getIdentityGovernanceConnectorList();
        Property[] properties = identityGovernanceService.getConfiguration(tenantDomain);
        ConnectorConfig[] configs = new ConnectorConfig[list.size()];
        String[] connectorProperties;
        for (int i = 0; i < list.size(); i++) {
            ConnectorConfig config = new ConnectorConfig();
            Map<String, String> propertyFriendlyNames = list.get(i).getPropertyNameMapping();
            Map<String, String> propertyDescriptions = list.get(i).getPropertyDescriptionMapping();
            config.setFriendlyName(list.get(i).getFriendlyName());
            config.setCategory(list.get(i).getCategory());
            config.setSubCategory(list.get(i).getSubCategory());
            config.setOrder(list.get(i).getOrder());
            connectorProperties = list.get(i).getPropertyNames();
            Property[] configProperties = new Property[connectorProperties.length];
            for (int j = 0; j < connectorProperties.length; j++) {
                String connectorProperty = connectorProperties[j];
                boolean propertyExists = false;
                for (Property property : properties) {
                    if (connectorProperty.equals(property.getName())) {
                        configProperties[j] = property;
                        configProperties[j].setDisplayName(propertyFriendlyNames.get(configProperties[j].getName()));
                        configProperties[j].setDescription(propertyDescriptions.get(configProperties[j].getName()));
                        propertyExists = true;
                        break;
                    }
                }
                if (!propertyExists) {
                    Property newProperty = new Property();
                    newProperty.setName(connectorProperty);
                    newProperty.setDescription(list.get(i).getPropertyDescriptionMapping().get(connectorProperty));
                    newProperty.setDisplayName(list.get(i).getPropertyNameMapping().get(connectorProperty));
                    Properties defaultPropertyValues = list.get(i).getDefaultPropertyValues(tenantDomain);
                    newProperty.setValue(String.valueOf(defaultPropertyValues.get(connectorProperty)));
                    identityGovernanceService.updateConfiguration(tenantDomain,
                            Collections.singletonMap(newProperty.getName(), newProperty.getValue()));
                    configProperties[j] = newProperty;
                }
            }
            config.setProperties(configProperties);
            configs[i] = config;
        }
        return configs;
    }

    public void updateConfigurations(Property[] configurations) throws IdentityGovernanceException {

        String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
        IdentityGovernanceService identityGovernanceService = IdentityMgtServiceDataHolder.getInstance()
                .getIdentityGovernanceService();
        Map<String, String> confMap = new HashMap<>();
        for (Property configuration : configurations) {
            confMap.put(configuration.getName(), configuration.getValue());
        }
        identityGovernanceService.updateConfiguration(tenantDomain, confMap);

    }

}

@@ -1074,38 +1074,8 @@
 r;%0A%0A
-import java.util.Collections;%0A
 impo
@@ -1144,37 +1144,8 @@
 Map;
-%0Aimport java.util.Properties;
 %0A%0Apu
@@ -2617,18 +2617,14 @@
 nt j
- = 
+=
 0; j
- %3C 
+%3C
 conn
@@ -2646,17 +2646,16 @@
 .length;
- 
 j++) %7B%0A 
@@ -2673,158 +2673,50 @@
     
-String connectorProperty = connectorProperties%5Bj%5D;%0A                boolean propertyExists = false;%0A                for (Property property : properties
+for (int k = 0; k %3C properties.length; k++
 ) %7B%0A
@@ -2755,17 +2755,22 @@
 rPropert
-y
+ies%5Bj%5D
 .equals(
@@ -2772,25 +2772,30 @@
 uals(propert
-y
+ies%5Bk%5D
 .getName()))
@@ -2846,25 +2846,30 @@
 j%5D = propert
-y
+ies%5Bk%5D
 ;%0A          
@@ -3121,932 +3121,36 @@
     
-propertyExists = true;%0A                        break;%0A                    %7D%0A                %7D%0A                if (!propertyExists) %7B%0A                    Property newProperty = new Property();%0A                    newProperty.setName(connectorProperty);%0A                    newProperty.setDescription(list.get(i).getPropertyDescriptionMapping().get(connectorProperty));%0A                    newProperty.setDisplayName(list.get(i).getPropertyNameMapping().get(connectorProperty));%0A                    Properties defaultPropertyValues = list.get(i).getDefaultPropertyValues(tenantDomain);%0A                    newProperty.setValue(String.valueOf(defaultPropertyValues.get(connectorProperty)));%0A                    identityGovernanceService.updateConfiguration(tenantDomain,%0A                            Collections.singletonMap(newProperty.getName(), newProperty.getValue()));%0A                    configProperties%5Bj%5D = newProperty;
+break;%0A                    %7D
 %0A   

Remove unwanted update login from identity gov admin service