DeviceConfigImpl.java 1.97 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.text.Text
 *  javax.annotation.Nonnull
 *  javax.jcr.Node
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.cq.screens.device.impl;

import com.adobe.cq.screens.device.DeviceConfig;
import com.day.cq.wcm.api.Page;
import com.day.text.Text;
import javax.annotation.Nonnull;
import javax.jcr.Node;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

public class DeviceConfigImpl
implements DeviceConfig {
    private final Resource pageResource;
    private final Resource contentResource;

    public DeviceConfigImpl(Resource resource) {
        if ("jcr:content".equals(resource.getName())) {
            this.contentResource = resource;
            this.pageResource = resource.getParent();
        } else {
            this.contentResource = resource.getChild("jcr:content");
            this.pageResource = resource;
        }
    }

    @Override
    public String getPath() {
        return this.pageResource.getPath();
    }

    @Nonnull
    @Override
    public String getDisplayPath() {
        return Text.getRelativeParent((String)this.getPath(), (int)1);
    }

    @Override
    public String getAssignedDeviceId() {
        return (String)((ValueMap)this.contentResource.adaptTo(ValueMap.class)).get("deviceId", String.class);
    }

    public <AdapterType> AdapterType adaptTo(@Nonnull Class<AdapterType> type) {
        if (type == Page.class) {
            return (AdapterType)this.pageResource.adaptTo(type);
        }
        if (type == ValueMap.class) {
            return (AdapterType)this.contentResource.adaptTo(type);
        }
        if (type == Resource.class) {
            return (AdapterType)this.contentResource;
        }
        if (type == Node.class) {
            return (AdapterType)this.pageResource.adaptTo(type);
        }
        return null;
    }
}