All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.aemds.guide.themes.SaveThemePostOperation Maven / Gradle / Ivy

/*
 * **********************************************************************
 *  ADOBE CONFIDENTIAL
 *  __________________
 *
 *  Copyright 2016 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 *  NOTICE:  All information contained herein is, and remains
 *  the property of Adobe Systems Incorporated and its suppliers,
 *  if any.  The intellectual and technical concepts contained
 *  herein are proprietary to Adobe Systems Incorporated and its
 *  suppliers and may be covered by U.S. and Foreign Patents,
 *  patents in process, and are protected by trade secret or copyright law.
 *  Dissemination of this information or reproduction of this material
 *  is strictly forbidden unless prior written permission is obtained
 *  from Adobe Systems Incorporated.
 *  **********************************************************************
 */
package com.adobe.aemds.guide.themes;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.PostOperation;
import org.apache.sling.servlets.post.PostResponse;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.apache.sling.api.resource.PersistenceException;
import com.adobe.aemds.guide.service.GuideException;

import java.lang.Boolean;
import java.util.Iterator;
import java.util.Set;

@Component(metatype = false)
@Service
@Property(name = PostOperation.PROP_OPERATION_NAME, value = "af:saveStyleOperation")
public class SaveThemePostOperation implements PostOperation {

    @Reference(target = "(sling.post.operation=import)")
    private PostOperation importOperation;

    @Reference(target = "(sling.post.operation=delete)")
    private PostOperation deleteOperation;

    public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) {
        //Clear style properties from node if ":content" property is null.
        Boolean isTheme = isTheme(request);
        if(request.getParameter(":content") == null) {
            if(!isTheme) {
                Resource resource = request.getResource();
                if (resource != null) {
                    ResourceResolver rr = request.getResourceResolver();
                    ModifiableValueMap map = resource.adaptTo(ModifiableValueMap.class);
                    if (map != null) {
                        if (map.containsKey(GuideThemeConstants.CSS_STYLE_PROPERTY)) {
                            map.remove(GuideThemeConstants.CSS_STYLE_PROPERTY);
                        }
                        if (map.containsKey(GuideThemeConstants.CSS_CLASS_PROPERTY)) {
                            map.remove(GuideThemeConstants.CSS_CLASS_PROPERTY);
                        }
                    }
                    Resource styleResource = resource.getChild(GuideThemeConstants.STYLE_NODE);
                    try {
                        if (styleResource != null) {
                            rr.delete(styleResource);
                        }
                        rr.commit();
                    } catch (PersistenceException e) {
                        throw new GuideException(e);
                    }
                }
            }   else {
                deleteOperation.run(request, response, processors);
            }
        } else {
            //remove existing properties first
            if(isTheme) {
                ModifiableValueMap map = request.getResource().adaptTo(ModifiableValueMap.class);
                if (map != null) {
                    Set keySet = request.getResource().adaptTo(ValueMap.class).keySet();
                    Iterator keys = request.getResource().adaptTo(ValueMap.class).keySet().iterator();
                    while (keys.hasNext()) {
                        String key = keys.next();
                        if (key.contains("#") && !key.startsWith("jcr:")) {
                            map.remove(key);
                        }
                    }
                }
            }
            //perform the default import operation
            importOperation.run(request, response, processors);
        }
    }
    public Boolean isTheme(SlingHttpServletRequest request) {
        if(request.getParameter("themePath") == null) {
            return false;
        }
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy