NodeStructureUtils.java 18.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
/*
 * 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.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.guide.utils;

import com.adobe.aemds.guide.common.GuideNode;
import com.adobe.aemds.guide.service.GuideException;
import com.adobe.aemds.guide.utils.GuideContainerThreadLocal;
import com.adobe.aemds.guide.utils.GuideFragmentHolder;
import com.adobe.aemds.guide.utils.GuideUtils;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(metatype=0, immediate=1, label="NodeStructureUtils")
@Service(value={NodeStructureUtils.class})
public class NodeStructureUtils {
    private static final Logger log = LoggerFactory.getLogger((String)NodeStructureUtils.class.getName());
    public static final String GUIDECONTAINER_NODENAME = "guideContainer";
    public static final String GUIDE_FRAG_ROOT_PANEL = "rootPanel";
    public static final String LAYOUT_NODENAME = "layout";
    public static final String ROOTPANEL_NODENAME = "rootPanel";
    public static final String ROOTPANEL_NODECLASS = "rootPanelNode";
    public static final String PANEL_NODENAME = "panel";
    public static final String GUIDE_ITEMS = "items";
    public static final String DEFAULT_LAYOUT_GUIDECONTAINER = "defaultGuideLayout";
    public static final String DEFAULT_LAYOUT_ROOTPANEL = "panel/tabbedPanelLayout";
    public static final String DEFAULT_LAYOUT_PANEL = "gridFluidLayout";
    public static String RT_GUIDES_PREFIX = "/libs/fd/af/components/";
    public static String RT_GUIDES_LAYOUT_PREFIX = "/libs/fd/af/layouts/";
    public static final String RT_GUIDES_ROOTPANEL = RT_GUIDES_PREFIX + "rootPanel";
    public static final String RT_GUIDES_LAYOUT_GUIDECONTAINER = RT_GUIDES_LAYOUT_PREFIX + "defaultGuideLayout";
    public static final String RT_GUIDES_LAYOUT_ROOTPANEL = RT_GUIDES_LAYOUT_PREFIX + "panel/tabbedPanelLayout";
    public static final String RT_GUIDES_LAYOUT_PANEL = RT_GUIDES_LAYOUT_PREFIX + "gridFluidLayout";
    public static final String LAYOUT_PATH_PROPERTY = "sling:resourceType";
    public static final String ELEMENT_PROPERTY_NAME = "name";
    private static final String ELEMENT_PROPERTY_DESCRIPTION = "jcr:description";
    private static final String ELEMENT_PROPERTY_QTIP = "qtip";
    private static final String ELEMENT_PROPERTY_TITLE = "jcr:title";
    private static final String ELEMENT_PROPERTY_IMAGE = "imagePath";
    public static final String FRAG_REF = "fragRef";
    public static final String RT_PARSYS = "/libs/foundation/components/parsys";
    private static NodeStructureUtils nodeStructureUtils = null;
    private static Map<String, String> layoutmap;

    public NodeStructureUtils() {
        layoutmap = new HashMap<String, String>();
        layoutmap.put("guideContainer", RT_GUIDES_LAYOUT_GUIDECONTAINER);
        layoutmap.put("rootPanel", RT_GUIDES_LAYOUT_ROOTPANEL);
        layoutmap.put("panel", RT_GUIDES_LAYOUT_PANEL);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    public static NodeStructureUtils getInstance() {
        if (nodeStructureUtils != null) return nodeStructureUtils;
        Class<NodeStructureUtils> class_ = NodeStructureUtils.class;
        synchronized (NodeStructureUtils.class) {
            if (nodeStructureUtils != null) return nodeStructureUtils;
            {
                nodeStructureUtils = new NodeStructureUtils();
            }
            // ** MonitorExit[var0] (shouldn't be in output)
            return nodeStructureUtils;
        }
    }

    private static boolean checkName(Resource resource, String name) {
        return resource.getName().equals(name);
    }

    public static Map<String, Object> getLayoutProperties(SlingHttpServletRequest request, Resource elementResource, String cmpName) throws PersistenceException {
        if (elementResource == null) {
            return null;
        }
        Resource layoutnode = null;
        ResourceResolver resourceResolver = null;
        if (request != null) {
            resourceResolver = request.getResourceResolver();
        }
        Iterator iter = null;
        if (cmpName == null) {
            Resource parentRes = elementResource.getParent();
            iter = elementResource.getParent().listChildren();
        } else {
            iter = elementResource.listChildren();
        }
        while (iter.hasNext() && layoutnode == null) {
            Resource current = (Resource)iter.next();
            if (!NodeStructureUtils.checkName(current, "layout")) continue;
            layoutnode = current;
        }
        if (layoutnode == null) {
            if (resourceResolver == null) {
                return null;
            }
            HashMap<String, Object> map = new HashMap<String, Object>();
            try {
                String resourceType = layoutmap.get(cmpName);
                map.put("sling:resourceType", resourceType);
                String nodeName = "layout";
                resourceResolver.create(elementResource, "layout", map);
            }
            catch (PersistenceException re) {
                log.error("Unable to create missing layout element for guide node " + (Object)elementResource, (Throwable)re);
                throw new GuideException((Exception)re);
            }
            finally {
                if (resourceResolver.hasChanges()) {
                    resourceResolver.refresh();
                }
            }
            return map;
        }
        return ResourceUtil.getValueMap((Resource)layoutnode);
    }

    public static void setGuideName(Resource rsrc, String name) throws GuideException {
        block5 : {
            Session currentSession = (Session)rsrc.getResourceResolver().adaptTo(Session.class);
            try {
                if (currentSession.hasPermission(rsrc.getPath(), "set_property")) {
                    ModifiableValueMap modifiableMap = (ModifiableValueMap)rsrc.adaptTo(ModifiableValueMap.class);
                    modifiableMap.put((Object)"name", (Object)name);
                    try {
                        rsrc.getResourceResolver().commit();
                        break block5;
                    }
                    catch (PersistenceException e) {
                        log.error("unable to set name property on " + rsrc.getPath(), (Throwable)e);
                        throw new GuideException((Exception)e);
                    }
                }
                log.error("user has no write permission on the node " + rsrc.getPath() + ". Adaptive Form will not work properly");
            }
            catch (RepositoryException e) {
                log.error("unable to check the permissions for setting property on " + rsrc.getPath(), (Throwable)e);
                throw new GuideException((Exception)e);
            }
        }
    }

    public static String getGuideName(Resource rsrc) throws GuideException {
        ValueMap properties = ResourceUtil.getValueMap((Resource)rsrc);
        String name = (String)properties.get("name", (Object)"");
        if ("".equals(name)) {
            String resourceName = GuideUtils.removePrefix(rsrc.getName(), "guide") + Calendar.getInstance().getTimeInMillis();
            NodeStructureUtils.setGuideName(rsrc, resourceName);
            name = rsrc.getName();
        }
        return name;
    }

    public static String getLayoutDescription(Resource rsrc) {
        ValueMap properties = ResourceUtil.getValueMap((Resource)rsrc);
        String desc = (String)properties.get("jcr:description", (Object)"");
        if (desc.length() == 0) {
            desc = (String)properties.get("jcr:title", (Object)"");
        }
        return desc;
    }

    public static String getLayoutQtip(Resource rsrc) {
        ValueMap properties = ResourceUtil.getValueMap((Resource)rsrc);
        String qtip = (String)properties.get("qtip", (Object)"");
        return qtip;
    }

    public static String getGuideNavigatorTabProperty(Resource rsrc) {
        ValueMap properties = ResourceUtil.getValueMap((Resource)rsrc);
        String guideNavigatorTab = (String)properties.get("guideNavigatorTab", (Object)"");
        return guideNavigatorTab;
    }

    public static String getFragPrefixString(Resource fragRefNode, String previousPrefixId) {
        String fragPrefix = null;
        try {
            String pathAfterRootPanel = StringUtils.substringAfter((String)fragRefNode.getPath(), (String)"/rootPanel/items/");
            if (StringUtils.isNotEmpty((String)previousPrefixId)) {
                fragPrefix = previousPrefixId + pathAfterRootPanel + "/";
            } else {
                fragPrefix = NodeStructureUtils.generateIdForGuideNodes(fragRefNode.getPath(), fragRefNode);
                fragPrefix = fragPrefix + "/items/";
            }
        }
        catch (Exception e) {
            log.error("Error while getting fragRef prefix string", (Throwable)e);
        }
        return fragPrefix;
    }

    public static String getGuideNodeHtmlId(Resource elementResource) {
        return NodeStructureUtils.getGuideNodeHtmlId(elementResource, null);
    }

    public static String getGuideNodeHtmlId(Resource elementResource, String fragPrefixId) {
        String fragPrefixidString;
        String nodeHtmlId;
        String path;
        path = elementResource.getPath();
        if (!path.contains("/guideContainer")) {
            log.debug("getGuideNodeHtmlId-path does not have guideContainer:" + path);
            return path;
        }
        nodeHtmlId = null;
        fragPrefixidString = null;
        if (fragPrefixId == null) {
            GuideFragmentHolder guideFragmentHolder = null;
            try {
                guideFragmentHolder = GuideContainerThreadLocal.getGuideFragmentHolder();
                if (guideFragmentHolder != null) {
                    fragPrefixidString = guideFragmentHolder.getFragPrefixID();
                }
            }
            catch (Exception e) {
                log.error("Error while getting thread local from NodeStructureUtils", (Throwable)e);
            }
        } else {
            fragPrefixidString = fragPrefixId;
        }
        nodeHtmlId = fragPrefixidString != null && fragPrefixidString.length() > 0 ? NodeStructureUtils.generateIdForFragmentNodes(path, fragPrefixidString) : NodeStructureUtils.generateIdForGuideNodes(path, elementResource);
        return NodeStructureUtils.manipulateNodeHtmlId(nodeHtmlId);
    }

    private static String getContainerNodeName(Resource elementResource) {
        Resource parent;
        String containerName = GuideContainerThreadLocal.getGuideContainerName();
        if (containerName != null) {
            return containerName;
        }
        log.debug("Guide container name not found in ThreadLocal.");
        if (GuideUtils.isGuideContainerResource(elementResource)) {
            log.debug("Container name retrieved from node structure: " + elementResource.getName());
            return elementResource.getName();
        }
        for (parent = elementResource.getParent(); parent != null && !GuideUtils.isGuideContainerResource(parent); parent = parent.getParent()) {
        }
        if (parent != null) {
            String parentName = parent.getName();
            log.debug("Container name retrieved from node structure: " + parentName);
            return parentName;
        }
        return "";
    }

    private static String generateIdForGuideNodes(String path, Resource elementResource) {
        String nodeHtmlId = null;
        String containerName = "guideContainer";
        if (elementResource != null) {
            containerName = NodeStructureUtils.getContainerNodeName(elementResource);
        }
        nodeHtmlId = elementResource != null && GuideUtils.isGuideContainerResource(elementResource) ? containerName : containerName + "-" + StringUtils.substringAfter((String)path, (String)new StringBuilder().append("/").append(containerName).append("/").toString());
        return nodeHtmlId;
    }

    private static String generateIdForFragmentNodes(String path, String fragPrefix) {
        return fragPrefix + StringUtils.substringAfter((String)path, (String)"/rootPanel/items/");
    }

    private static String manipulateNodeHtmlId(String nodeHtmlId) {
        nodeHtmlId = nodeHtmlId.replaceAll("/items/", "-");
        nodeHtmlId = nodeHtmlId.replaceAll("/", "-");
        nodeHtmlId = nodeHtmlId.replaceAll("\\.", "-");
        nodeHtmlId = nodeHtmlId + "__";
        return nodeHtmlId;
    }

    public static Map<String, Object> getLayoutProperties(Resource elementResource, SlingHttpServletRequest request) throws PersistenceException {
        return NodeStructureUtils.getLayoutProperties(request, elementResource, null);
    }

    public static Map<String, Object> getLayoutProperties(Resource elementResource) throws PersistenceException {
        return NodeStructureUtils.getLayoutProperties(null, elementResource, null);
    }

    public static Map<String, Object> getRootPanel(SlingHttpServletRequest request, Resource elementResource) throws RepositoryException {
        if (elementResource == null) {
            return null;
        }
        Iterator iter = elementResource.listChildren();
        Resource rootpanel = null;
        ResourceResolver resourceResolver = request.getResourceResolver();
        while (iter.hasNext() && rootpanel == null) {
            Resource current = (Resource)iter.next();
            if (!NodeStructureUtils.checkName(current, "rootPanel")) continue;
            rootpanel = current;
        }
        if (rootpanel == null) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            String css = "rootPanel";
            try {
                String resourceType = RT_GUIDES_ROOTPANEL;
                map.put("sling:resourceType", resourceType);
                map.put("guideNodeClass", "rootPanelNode");
                map.put("name", "rootPanel");
                String nodeName = "rootPanel";
                resourceResolver.create(elementResource, "rootPanel", map);
            }
            catch (PersistenceException re) {
                log.error("Unable to create missing root panel element for guide node " + (Object)elementResource, (Throwable)re);
                throw new GuideException((Exception)re);
            }
            finally {
                if (resourceResolver.hasChanges()) {
                    resourceResolver.refresh();
                }
            }
            return map;
        }
        return ResourceUtil.getValueMap((Resource)rootpanel);
    }

    public static List getItems(SlingHttpServletRequest request, Resource elementResource) throws RepositoryException {
        Resource current;
        Iterator iter = elementResource.listChildren();
        ResourceResolver resourceResolver = request.getResourceResolver();
        Resource items = elementResource;
        ArrayList<GuideNode> result = new ArrayList<GuideNode>();
        while (iter.hasNext() && items == null) {
            current = (Resource)iter.next();
            if (!NodeStructureUtils.checkName(current, "items")) continue;
            items = current;
        }
        if (items == null) {
            if (elementResource != null) {
                try {
                    String nodeName = "items";
                    resourceResolver.create(elementResource, "items", null);
                }
                catch (PersistenceException re) {
                    log.error("Unable to create missing layout element for guide node " + (Object)elementResource, (Throwable)re);
                    throw new GuideException((Exception)re);
                }
                finally {
                    if (resourceResolver.hasChanges()) {
                        resourceResolver.refresh();
                    }
                }
            }
        } else {
            iter = items.listChildren();
            try {
                while (iter.hasNext()) {
                    current = (Resource)iter.next();
                    GuideNode newNode = new GuideNode();
                    newNode.setResource(current);
                    newNode.setSlingRequest(request);
                    result.add(newNode);
                }
            }
            catch (Exception ex) {
                log.error("unable to iterate items for the resource " + elementResource.getPath(), (Throwable)ex);
                throw new GuideException(ex);
            }
        }
        return result;
    }

    public static void createProperty(Resource elementResource, String propName, String propValue, Boolean override) {
        Node node = (Node)elementResource.adaptTo(Node.class);
        if (node != null) {
            try {
                if (override.booleanValue() || !node.hasProperty(propName)) {
                    node.setProperty(propName, propValue);
                }
                node.getSession().save();
            }
            catch (RepositoryException re) {
                log.error("Unable to set property for the guide node " + (Object)elementResource + " propName :" + propName + " propValue" + propValue, (Throwable)re);
                throw new GuideException((Exception)re);
            }
            finally {
                try {
                    if (node.getSession().hasPendingChanges()) {
                        node.getSession().refresh(false);
                    }
                }
                catch (RepositoryException re) {
                    log.trace("Error in create property: ignoring it." + re.getMessage(), (Throwable)re);
                }
            }
        }
    }
}