package org.wso2.carbon.appmgt.impl;

import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.appmgt.api.AppManagementException;
import org.wso2.carbon.appmgt.api.model.App;
import org.wso2.carbon.appmgt.api.model.EntitlementPolicyGroup;
import org.wso2.carbon.appmgt.impl.dao.AppMDAO;
import org.wso2.carbon.appmgt.impl.service.ServiceReferenceHolder;
import org.wso2.carbon.appmgt.impl.utils.AppManagerUtil;
import org.wso2.carbon.registry.api.RegistryService;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;

/**
 * The default implementation of DefaultAppRepository which uses RDBMS and Carbon registry for persistence.
 */
public class DefaultAppRepository implements AppRepository{


    private RegistryService registryService;

    public DefaultAppRepository(){
        this.registryService = ServiceReferenceHolder.getInstance().getRegistryService();
    }

    @Override
    public String saveApp(App app) {

        savePolicyGroups(app);
        saveRegistryArtifact(app);
        saveAppToRDMS(app);
        saveServiceProvider(app);

        return null;
    }

    private void savePolicyGroups(App app) {

    }

    private long savePolicyGroup(EntitlementPolicyGroup policyGroup){
        return -1;
    }

    private String saveRegistryArtifact(App app){
        return null;
    }

    private long saveAppToRDMS(App app){
        return -1;
    }

    private void saveServiceProvider(App app){

    }

}

@@ -31,16 +31,101 @@
 .impl;%0A%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import o
@@ -495,16 +495,62 @@
 erUtil;%0A
+import org.wso2.carbon.context.CarbonContext;%0A
 import o
@@ -759,32 +759,84 @@
 n.UserRegistry;%0A
+import org.wso2.carbon.user.api.UserStoreException;%0A
 import org.wso2.
@@ -1054,16 +1054,17 @@
 pository
+ 
 %7B%0A%0A%0A    
@@ -1108,133 +1108,1148 @@
 ce;%0A
-%0A    public DefaultAppRepository()%7B%0A        this.registryService = ServiceReferenceHolder.getInstance().getRegistryService();
+    private Registry registry;%0A    private int tenantId;%0A    private String tenantDomain;%0A    private String username;%0A    private Log log = LogFactory.getLog(getClass());%0A%0A    public DefaultAppRepository() throws AppManagementException %7B%0A        try %7B%0A            String loggedInUsername = CarbonContext.getThreadLocalCarbonContext().getUsername();%0A%0A            this.tenantDomain = MultitenantUtils.getTenantDomain(username);%0A            this.username = MultitenantUtils.getTenantAwareUsername(loggedInUsername);%0A            ;%0A            this.tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(username);%0A            AppManagerUtil.loadTenantRegistry(tenantId);%0A            this.registry = ServiceReferenceHolder.getInstance().%0A                    getRegistryService().getGovernanceUserRegistry(username, tenantId);%0A%0A        %7D catch (RegistryException e) %7B%0A            handleException(%22Error while instantiating Registry for user : %22 + username, e);%0A        %7D catch (UserStoreException e) %7B%0A            handleException(%22Error while obtaining user registry for user:%22 + username, e);%0A        %7D
 %0A   
@@ -2809,11 +2809,344 @@
 %0A%0A    %7D%0A
+    protected void handleException(String msg, Exception e) throws AppManagementException %7B%0A        log.error(msg, e);%0A        throw new AppManagementException(msg, e);%0A    %7D%0A%0A    protected void handleException(String msg) throws AppManagementException %7B%0A        log.error(msg);%0A        throw new AppManagementException(msg);%0A    %7D%0A%0A
 %0A%7D%0A

Add DefaultAppRepository constructor