Package org.wso2.carbon.governance.gadgetsource.util

Source Code of org.wso2.carbon.governance.gadgetsource.util.Populator

/*
* Copyright (c) 2008, 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.governance.gadgetsource.util;

import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.utils.RegistryUtils;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.RegistryConstants;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.config.StaticConfiguration;
import org.wso2.carbon.registry.core.config.RegistryContext;
import org.wso2.carbon.governance.gadgetsource.beans.LifecycleInfoBean;
import org.wso2.carbon.governance.gadgetsource.beans.LifecycleStageInfoBean;
import org.wso2.carbon.governance.registry.extensions.aspects.ChecklistLifeCycle;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.llom.util.AXIOMUtil;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import java.util.*;

public class Populator {
    public static final String searchServicesQuery = RegistryConstants.CONFIG_REGISTRY_BASE_PATH + RegistryConstants.QUERIES_COLLECTION_PATH +
            "/governance/searchServices";
    public static final String serviceMediaType1 = "application/vnd.wso2-service+xml";
    public static final String serviceMediaType2 = "service+xml";

    public static LifecycleInfoBean[] populateLifecycle(Registry registry) throws RegistryException {
        String sql = "SELECT R.REG_PATH_ID, R.REG_NAME FROM REG_RESOURCE R, REG_PATH P WHERE " +
                "P.REG_PATH_VALUE LIKE ? AND P.REG_PATH_ID=R.REG_PATH_ID AND " +
                "(R.REG_MEDIA_TYPE = ? OR R.REG_MEDIA_TYPE = ?)";
        // This modification is done to stop the queries been stored to the registry.

/*
        if (!registry.resourceExists(searchServicesQuery)) {

            Resource q1 = registry.newResource();
            q1.setContent(sql);
            q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE);
            q1.addProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME,
                    RegistryConstants.RESOURCES_RESULT_TYPE);
            registry.put(searchServicesQuery, q1);
        }
*/
        RegistryContext registryContext = registry.getRegistryContext();
        String serviceStoredPath = registryContext.getServicePath() + "/";

        Map<String, String> parameters = new HashMap<String, String>();
//        if (RegistryUtils.isRegistryReadOnly(registry.getRegistryContext())) {
//            parameters.put("query", sql);
//        }
        parameters.put("query", sql);
        parameters.put("1", RegistryUtils.getAbsolutePath(registryContext, serviceStoredPath) + "%");
        parameters.put("2", serviceMediaType1);
        parameters.put("3", serviceMediaType2);
//        Collection result = registry.executeQuery(searchServicesQuery, parameters);
        Collection result = registry.executeQuery(null, parameters);

        String[] servicePaths = result.getChildren();

        Map<String, LifecycleInfoBean> lifecycleInfoBeanMap = new HashMap<String, LifecycleInfoBean>();
        Map<String, List<LifecycleStageInfoBean>> lifecycleStagesMap = new HashMap<String, List<LifecycleStageInfoBean>>();
        for (String servicePath: servicePaths) {
            Resource serviceResource = registry.get(servicePath);
            String lifecycleName = serviceResource.getProperty("registry.Aspects");
            if (lifecycleName != null) {
                lifecycleName = lifecycleName.replaceAll("\\s", "");
                LifecycleInfoBean lifecycleInfoBean = lifecycleInfoBeanMap.get(lifecycleName);
                List<LifecycleStageInfoBean> lifecycleStages = lifecycleStagesMap.get(lifecycleName);
                if (lifecycleInfoBean == null) {
                    lifecycleInfoBean = new LifecycleInfoBean();
                    lifecycleInfoBean.setName(lifecycleName);
                    lifecycleInfoBeanMap.put(lifecycleName, lifecycleInfoBean);

                    // initializing lifecycle stages
                    lifecycleStages = new ArrayList<LifecycleStageInfoBean>();
                    lifecycleStagesMap.put(lifecycleName, lifecycleStages);
                }
                String lifecycleStageKey = "registry.lifecycle." + lifecycleName + ".state";
                String lifecycleStageValue = serviceResource.getProperty(lifecycleStageKey);

                LifecycleStageInfoBean lifecycleStageInfoBean = null;
               
                // iterate and find the correct stage object
                for (int i = 0; i < lifecycleStages.size(); i ++) {
                    LifecycleStageInfoBean stageInfoBeanIt  = lifecycleStages.get(i);
                    if (stageInfoBeanIt.getName().equals(lifecycleStageValue)) {
                        lifecycleStageInfoBean = stageInfoBeanIt;
                    }
                }

                if (lifecycleStageInfoBean == null) {
                    // create a new life cycle stage
                    lifecycleStageInfoBean =  new LifecycleStageInfoBean();
                    lifecycleStageInfoBean.setName(lifecycleStageValue);
                    lifecycleStageInfoBean.setServiceCount(1);
                    lifecycleStages.add(lifecycleStageInfoBean);
                }
                else {
                    // increment the current service count
                    lifecycleStageInfoBean.setServiceCount(lifecycleStageInfoBean.getServiceCount() + 1);
                }

                LifecycleStageInfoBean[] lifecycleStageInfoBeans = lifecycleStages.toArray(
                            new LifecycleStageInfoBean[lifecycleStages.size()]);
                lifecycleInfoBean.setStages(lifecycleStageInfoBeans);
            }
        }

        // now fill all the beans
        LifecycleInfoBean[] lifecycleInfoBeans = lifecycleInfoBeanMap.values().toArray(
                            new LifecycleInfoBean[lifecycleInfoBeanMap.size()]);
        return lifecycleInfoBeans;
    }


    public static LifecycleInfoBean[] populateLifecycleDummyData() throws RegistryException {
         /* creating dummy data */
        LifecycleInfoBean[] lifecycles = new LifecycleInfoBean[1];
        lifecycles[0] = new LifecycleInfoBean();
        lifecycles[0].setName("No Data Available");
        // application/vnd.wso2-mex+xml
        LifecycleStageInfoBean[] stages = new LifecycleStageInfoBean[1];
        stages[0] = new LifecycleStageInfoBean();
        stages[0].setName("Life Cycle Data Not Available.");
        stages[0].setServiceCount(0);
        /*stages[1] = new LifecycleStageInfoBean();
        stages[1].setName("Lifecycle");
        stages[1].setServiceCount(6);
        stages[2] = new LifecycleStageInfoBean();
        stages[2].setName("Associated");
        stages[2].setServiceCount(2);
        stages[3] = new LifecycleStageInfoBean();
        stages[3].setName("Services");
        stages[3].setServiceCount(3);*/
        lifecycles[0].setStages(stages);

        return lifecycles;
    }
}
TOP

Related Classes of org.wso2.carbon.governance.gadgetsource.util.Populator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.