com.pedra.core.setup.CoreSystemSetup.java Source code

Java tutorial

Introduction

Here is the source code for com.pedra.core.setup.CoreSystemSetup.java

Source

/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2012 hybris AG
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of hybris
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with hybris.
 * 
 *  
 */
package com.pedra.core.setup;

import de.hybris.platform.acceleratorservices.setup.AbstractSystemSetup;
import de.hybris.platform.acceleratorservices.setup.data.ImportData;
import de.hybris.platform.acceleratorservices.setup.events.CoreDataImportedEvent;
import de.hybris.platform.core.Registry;
import de.hybris.platform.core.initialization.SystemSetup;
import de.hybris.platform.core.initialization.SystemSetup.Process;
import de.hybris.platform.core.initialization.SystemSetup.Type;
import de.hybris.platform.core.initialization.SystemSetupContext;
import de.hybris.platform.core.initialization.SystemSetupParameter;
import de.hybris.platform.core.initialization.SystemSetupParameterMethod;
import de.hybris.platform.servicelayer.impex.ImportService;
import de.hybris.platform.util.Config;
import de.hybris.platform.validation.services.ValidationService;

import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.jfree.util.Log;

import com.pedra.core.constants.PedraCoreConstants;

/**
 * This class provides hooks into the system's initialization and update processes.
 * 
 * @see "https://wiki.hybris.com/display/release4/Hooks+for+Initialization+and+Update+Process"
 */
@SystemSetup(extension = PedraCoreConstants.EXTENSIONNAME)
public class CoreSystemSetup extends AbstractSystemSetup {
    public static final String IMPORT_SITES = "importSites";
    public static final String IMPORT_SYNC_CATALOGS = "syncProducts&ContentCatalogs";
    public static final String IMPORT_COCKPIT_COMPONENTS = "cockpitComponents";
    public static final String IMPORT_ACCESS_RIGHTS = "accessRights";
    public static final String ACTIVATE_SOLR_CRON_JOBS = "activateSolrCronJobs";
    public static final String UPDATE_DIRECTORY = "updateDirectory";

    public static final String PEDRA = "pedra";

    private ImportService importService;

    /**
     * This method will be called by system creator during initialization and system update. Be sure that this method can
     * be called repeatedly.
     * 
     * @param context
     *           the context provides the selected parameters and values
     */
    @SystemSetup(type = Type.ESSENTIAL, process = Process.ALL)
    public void createEssentialData(final SystemSetupContext context) {
        importImpexFile(context, "/pedracore/import/common/essential-data.impex");
        importImpexFile(context, "/pedracore/import/common/countries.impex");
        importImpexFile(context, "/pedracore/import/common/delivery-modes.impex");
        importImpexFile(context, "/pedracore/import/common/themes.impex");
        importImpexFile(context, "/pedracore/import/common/user-groups.impex");
    }

    /**
     * Generates the Dropdown and Multi-select boxes for the project data import
     */
    @Override
    @SystemSetupParameterMethod
    public List<SystemSetupParameter> getInitializationOptions() {
        final List<SystemSetupParameter> params = new ArrayList<SystemSetupParameter>();

        params.add(createBooleanSystemSetupParameter(IMPORT_SITES, "Import Sites", true));
        params.add(
                createBooleanSystemSetupParameter(IMPORT_SYNC_CATALOGS, "Sync Products & Content Catalogs", false));
        params.add(createBooleanSystemSetupParameter(IMPORT_ACCESS_RIGHTS, "Import Users & Groups", true));
        params.add(createBooleanSystemSetupParameter(ACTIVATE_SOLR_CRON_JOBS, "Activate Solr Cron Jobs", false));
        params.add(createUpdateDirectoriesSystemSetupParameter(UPDATE_DIRECTORY));

        return params;
    }

    /**
     * This method will be called during the system initialization.
     * 
     * @param context
     *           the context provides the selected parameters and values
     */
    @SystemSetup(type = Type.PROJECT, process = Process.ALL)
    public void createProjectData(final SystemSetupContext context) {
        final boolean importSites = getBooleanSystemSetupParameter(context, IMPORT_SITES);
        final boolean importAccessRights = getBooleanSystemSetupParameter(context, IMPORT_ACCESS_RIGHTS);
        final String projectdataRelativeUpdateDirectoryValue = getStringSystemSetupParameter(context,
                UPDATE_DIRECTORY);

        final ImportData pedraImportData = new ImportData();
        pedraImportData.setProductCatalogName(PEDRA);
        pedraImportData.setContentCatalogNames(Arrays.asList(PEDRA));
        pedraImportData.setStoreNames(Arrays.asList(PEDRA));

        if (importSites) {
            new ImportCatalogAwareDataStrategy(context, pedraImportData).importAllData();

            final ValidationService validation = getBeanForName("validationService");
            validation.reloadValidationEngine();
        }

        final List<String> extensionNames = getExtensionNames();

        if (importAccessRights && extensionNames.contains("cmscockpit")) {
            importImpexFile(context, "/pedracore/import/cockpits/cmscockpit/cmscockpit-users.impex");
            importImpexFile(context, "/pedracore/import/cockpits/cmscockpit/cmscockpit-access-rights.impex");
        }

        if (importAccessRights && extensionNames.contains("btgcockpit")) {
            importImpexFile(context, "/pedracore/import/cockpits/cmscockpit/btgcockpit-users.impex");
            importImpexFile(context, "/pedracore/import/cockpits/cmscockpit/btgcockpit-access-rights.impex");
        }

        if (importAccessRights && extensionNames.contains("productcockpit")) {
            importImpexFile(context, "/pedracore/import/cockpits/productcockpit/productcockpit-users.impex");
            importImpexFile(context,
                    "/pedracore/import/cockpits/productcockpit/productcockpit-access-rights.impex");
            importImpexFile(context, "/pedracore/import/cockpits/productcockpit/productcockpit-constraints.impex");
        }

        if (importAccessRights && extensionNames.contains("cscockpit")) {
            importImpexFile(context, "/pedracore/import/cockpits/cscockpit/cscockpit-users.impex");
            importImpexFile(context, "/pedracore/import/cockpits/cscockpit/cscockpit-access-rights.impex");
        }

        if (importAccessRights && extensionNames.contains("reportcockpit")) {
            importImpexFile(context, "/pedracore/import/cockpits/reportcockpit/reportcockpit-users.impex");
            importImpexFile(context, "/pedracore/import/cockpits/reportcockpit/reportcockpit-access-rights.impex");
        }

        if (extensionNames.contains("mcc")) {
            importImpexFile(context, "/pedracore/import/common/mcc-sites-links.impex");
        }

        if (StringUtils.isNotEmpty(projectdataRelativeUpdateDirectoryValue)
                && !projectdataRelativeUpdateDirectoryValue.equalsIgnoreCase("-- none --")) {
            final File projectdataUpdateDirectory = getProjectdataUpdateDirectory(
                    "/" + projectdataRelativeUpdateDirectoryValue);
            if (projectdataUpdateDirectory != null && projectdataUpdateDirectory.listFiles() != null) {
                final String relativePath = Config.getString("projectdata.update.folder",
                        "/pedracore/import/versions") + "/" + projectdataRelativeUpdateDirectoryValue;
                for (final File file : projectdataUpdateDirectory.listFiles()) {
                    if (file.isFile()) {
                        importImpexFile(context, relativePath + "/" + file.getName());
                    }
                }
            }
        }

        // Send an event to notify any AddOns that the core data import is complete
        getEventService().publishEvent(new CoreDataImportedEvent(context, Arrays.asList(pedraImportData)));
    }

    protected List<String> getExtensionNames() {
        return Registry.getCurrentTenant().getTenantSpecificExtensionNames();
    }

    protected <T> T getBeanForName(final String name) {
        return (T) Registry.getApplicationContext().getBean(name);
    }

    /**
     * Helper method for creating a setup parameter containing all subfolders within the folder configured to be the
     * project's projectdata update folder.
     * 
     * @param key
     */
    protected SystemSetupParameter createUpdateDirectoriesSystemSetupParameter(final String key) {

        final SystemSetupParameter setupParam = new SystemSetupParameter(key);

        setupParam.setLabel("Update entire folder");
        setupParam.addValue("-- none --", true);
        final File projectdataUpdateFolder = getProjectdataUpdateDirectory(null);

        for (final File fileEntry : projectdataUpdateFolder.listFiles()) {
            if (fileEntry.isDirectory()) {
                setupParam.addValue(fileEntry.getName());
            }
        }

        return setupParam;
    }

    /**
     * Helper method for checking setting of a String setup parameter.
     * 
     * @param context
     * @param key
     * @return true if parameter is set to Yes
     */
    protected String getStringSystemSetupParameter(final SystemSetupContext context, final String key) {
        final String parameterValue = context.getParameter(context.getExtensionName() + "_" + key);
        if (parameterValue != null) {
            return parameterValue;
        }
        // Have not been able to determine value from context, fallback to default value
        Log.warn("Missing setup parameter for key [" + key + "], falling back to null");
        return null;
    }

    private File getProjectdataUpdateDirectory(String relativeUpdateDirectory) {
        if (relativeUpdateDirectory == null) {
            relativeUpdateDirectory = "";
        }
        String projectdataUpdateFolderProperty = Config.getString("projectdata.update.folder",
                "/pedracore/import/versions");
        projectdataUpdateFolderProperty = projectdataUpdateFolderProperty + relativeUpdateDirectory;
        File projectdataUpdateFolder = null;
        try {
            projectdataUpdateFolder = new File(getClass().getResource(projectdataUpdateFolderProperty).toURI());
        } catch (final URISyntaxException e) {
            Log.error("error finding project data update directory[" + projectdataUpdateFolderProperty + "]", e);
            return null;
        }
        if (!projectdataUpdateFolder.exists()) {
            Log.warn("project data update directory [" + projectdataUpdateFolderProperty + "] does not exist");
            return null;
        } else if (ArrayUtils.isEmpty(projectdataUpdateFolder.listFiles())) {
            Log.info("Project datad update directory[" + projectdataUpdateFolderProperty + "] is empty");
        }
        return projectdataUpdateFolder;
    }

    /**
     * Abstraction for a import process which should be adopted to use a dependent CatalogVersionSyncJob
     */
    private class ImportCatalogAwareDataStrategy {
        private final SystemSetupContext context;
        private final ImportData importData;

        public ImportCatalogAwareDataStrategy(final SystemSetupContext context, final ImportData importData) {
            this.context = context;
            this.importData = importData;
        }

        public String getProductCatalogName() {
            return importData.getProductCatalogName();
        }

        public List<String> getContentCatalogNames() {
            return importData.getContentCatalogNames();
        }

        public List<String> getStoreNames() {
            return importData.getStoreNames();
        }

        private boolean isSyncCatalog() {
            return getBooleanSystemSetupParameter(context, IMPORT_SYNC_CATALOGS);
        }

        public void importAllData() {
            createProductCatalog(getProductCatalogName());//

            for (final String contentCatalogName : getContentCatalogNames()) {

                createContentCatalog(contentCatalogName);
            }

            assignDependent(getProductCatalogName(), getContentCatalogNames());

            for (final String contentCatalogName : getContentCatalogNames()) {

                if (isSyncCatalog()) {
                    syncContentCatalog(contentCatalogName);
                }
            }

            if (isSyncCatalog()) {
                syncProductCatalog(getProductCatalogName());
            }

            for (final String storeName : getStoreNames()) {
                createStore(storeName);
                createAndActivateSolrIndex(storeName);
            }
        }

        private void createAndActivateSolrIndex(final String storeName) {
            logInfo(context, "Begin SOLR index setup [" + storeName + "]");

            importImpexFile(context, "/pedracore/import/stores/" + storeName + "/solr.impex");

            createSolrIndexerCronJobs(storeName + "Index");

            importImpexFile(context, "/pedracore/import/stores/" + storeName + "/solrtrigger.impex");

            if (getBooleanSystemSetupParameter(context, ACTIVATE_SOLR_CRON_JOBS)) {
                executeSolrIndexerCronJob(storeName + "Index", true);
                activateSolrIndexerCronJobs(storeName + "Index");
            }

            logInfo(context, "Done SOLR index setup [" + storeName + "]");
        }

        private void createStore(final String storeName) {
            logInfo(context, "Begin importing store [" + storeName + "]");

            importImpexFile(context, "/pedracore/import/stores/" + storeName + "/store.impex");
            importImpexFile(context, "/pedracore/import/stores/" + storeName + "/site.impex");

            logInfo(context, "Done importing store [" + storeName + "]");
        }

        /**
         * fires product catalog with synchronization with respect for a dependent CatalogVersionSyncJob
         */
        private void syncProductCatalog(final String productCatalogName) {
            executeCatalogSyncJob(context, productCatalogName + "ProductCatalog");
        }

        /**
         * fires content catalog with synchronization with respect for a depends on CatalogVersionSyncJob
         * 
         */
        private void syncContentCatalog(final String contentCatalogName) {
            executeCatalogSyncJob(context, contentCatalogName + "ContentCatalog");
        }

        private void assignDependent(final String dependsOnProduct, final List<String> dependentContents) {
            if (CollectionUtils.isNotEmpty(dependentContents) && StringUtils.isNotBlank(dependsOnProduct)) {
                final Set<String> dependentSyncJobsNames = new HashSet<String>();
                for (final String content : dependentContents) {
                    dependentSyncJobsNames.add(content + "ContentCatalog");
                }

                getSetupSyncJobService().assignDependentSyncJobs(dependsOnProduct + "ProductCatalog",
                        dependentSyncJobsNames);
            }
        }

        private void createContentCatalog(final String contentCatalogName) {
            logInfo(context, "Begin importing catalog [" + contentCatalogName + "]");

            importImpexFile(context,
                    "/pedracore/import/contentCatalogs/" + contentCatalogName + "ContentCatalog/catalog.impex",
                    true);
            importImpexFile(context,
                    "/pedracore/import/contentCatalogs/" + contentCatalogName + "ContentCatalog/cms-content.impex",
                    false);
            importImpexFile(context, "/pedracore/import/contentCatalogs/" + contentCatalogName
                    + "ContentCatalog/cms-mobile-content.impex", false);
            importImpexFile(context, "/pedracore/import/contentCatalogs/" + contentCatalogName
                    + "ContentCatalog/email-content.impex", false);

            createContentCatalogSyncJob(context, contentCatalogName + "ContentCatalog");

        }

        private void createProductCatalog(final String productCatalogName) {
            logInfo(context, "Begin importing catalog [" + productCatalogName + "]");

            importImpexFile(context,
                    "/pedracore/import/productCatalogs/" + productCatalogName + "ProductCatalog/catalog.impex",
                    true);

            createProductCatalogSyncJob(context, productCatalogName + "ProductCatalog");
        }

    }
}