SearchFacetUpgrade.java 3.39 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.commons.util.impl;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Deprecated
@Component(immediate=1)
public class SearchFacetUpgrade {
    private static final Logger logger = LoggerFactory.getLogger(SearchFacetUpgrade.class);
    @Reference
    private SlingRepository repository;

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Activate
    protected void activate() {
        Session adminSession = null;
        try {
            adminSession = this.repository.loginAdministrative(null);
            this.fixCustomizedSearchFacets(adminSession);
        }
        catch (RepositoryException e) {
            logger.warn("Unable to execute search facet upgrade", (Throwable)e);
        }
        finally {
            if (adminSession != null && adminSession.isLive()) {
                adminSession.logout();
            }
        }
    }

    private void fixCustomizedSearchFacets(Session session) {
        String customizedSearchFacetsPath = "/apps/dam/gui/content/facets/Assets";
        String searchFacetsPath = "/libs/dam/gui/content/facets/assets";
        String predicatesConfig = "predicatesConfig";
        try {
            Node customizedSearchFacetsNode;
            if (session.nodeExists(customizedSearchFacetsPath) && (customizedSearchFacetsNode = session.getNode(customizedSearchFacetsPath)) != null) {
                Node searchFacetsNode;
                this.rename(customizedSearchFacetsNode, "assets");
                if (session.nodeExists(searchFacetsPath) && (searchFacetsNode = session.getNode(searchFacetsPath)).hasProperty(predicatesConfig)) {
                    String predicatesConfigVal = searchFacetsNode.getProperty(predicatesConfig).getString();
                    if (!customizedSearchFacetsNode.hasProperty(predicatesConfig)) {
                        customizedSearchFacetsNode.setProperty(predicatesConfig, predicatesConfigVal);
                    }
                }
                session.save();
                logger.info("Successfully fixed customized search facets");
            }
        }
        catch (RepositoryException e) {
            logger.warn("Unable to fix customized search facets", (Throwable)e);
        }
    }

    private void rename(Node node, String newName) throws RepositoryException {
        node.getSession().move(node.getPath(), node.getParent().getPath() + "/" + newName);
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }
}