DeviceImpl.java 2.12 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.annotation.CheckForNull
 *  javax.annotation.Nonnull
 *  org.apache.jackrabbit.api.security.user.User
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.cq.screens.device.impl;

import com.adobe.cq.screens.device.Device;
import com.adobe.cq.screens.device.DeviceConfig;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;

public class DeviceImpl
implements Device {
    private Resource resource;
    private String id;

    public DeviceImpl(Resource resource, String id) {
        this.resource = resource;
        this.id = id;
    }

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

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public DeviceConfig getDeviceConfig() {
        Resource profile = this.getProfile();
        String cfgPath = profile == null ? null : (String)profile.getValueMap().get("configPath", String.class);
        Resource r = cfgPath == null ? null : this.resource.getResourceResolver().getResource(cfgPath);
        return r == null ? null : (DeviceConfig)r.adaptTo(DeviceConfig.class);
    }

    @CheckForNull
    private Resource getProfile() {
        return this.resource.getChild("profile_screens");
    }

    public <AdapterType> AdapterType adaptTo(@Nonnull Class<AdapterType> type) {
        if (type == User.class) {
            return (AdapterType)this.resource.adaptTo(type);
        }
        if (type == ValueMap.class) {
            Resource profile = this.getProfile();
            return (AdapterType)(profile == null ? ValueMap.EMPTY : (ValueMap)profile.adaptTo(ValueMap.class));
        }
        if (type == Resource.class) {
            return (AdapterType)this.resource;
        }
        return null;
    }
}