ProjectLinkImpl.java 5.25 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.projects.api.Project
 *  com.adobe.cq.projects.api.ProjectException
 *  com.adobe.cq.projects.api.ProjectLink
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.adapter.annotations.Adaptable
 *  org.apache.sling.adapter.annotations.Adapter
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ResourceWrapper
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.cq.projects.impl;

import com.adobe.cq.projects.api.Project;
import com.adobe.cq.projects.api.ProjectException;
import com.adobe.cq.projects.api.ProjectLink;
import com.adobe.cq.projects.impl.util.ProjectUtils;
import java.io.InputStream;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.adapter.annotations.Adaptable;
import org.apache.sling.adapter.annotations.Adapter;
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.ResourceWrapper;
import org.apache.sling.api.resource.ValueMap;

@Adaptable(adaptableClass=ProjectLink.class, adapters={@Adapter(value={Resource.class})})
public class ProjectLinkImpl
extends ResourceWrapper
implements ProjectLink {
    private final Project parentProject;

    public ProjectLinkImpl(Project parentProject, Resource resource) {
        super(resource);
        this.parentProject = parentProject;
    }

    public Project getProject() {
        return this.parentProject;
    }

    public String getResolvedTitle() {
        Resource target;
        String specifiedTitle = this.getTitle();
        if (!StringUtils.isBlank((CharSequence)specifiedTitle)) {
            return specifiedTitle;
        }
        String suffix = ProjectUtils.getStringProperty((Resource)this, "suffix");
        if (!StringUtils.isBlank((CharSequence)suffix) && (target = this.getResourceResolver().getResource(suffix)) != null && !ResourceUtil.isNonExistingResource((Resource)target)) {
            ValueMap targetContentProperties;
            ValueMap targetProperties = (ValueMap)target.adaptTo(ValueMap.class);
            if (targetProperties.containsKey((Object)"jcr:title")) {
                return (String)targetProperties.get("jcr:title", String.class);
            }
            Resource targetContent = target.getChild("jcr:content");
            if (targetContent != null && !ResourceUtil.isNonExistingResource((Resource)targetContent) && (targetContentProperties = (ValueMap)targetContent.adaptTo(ValueMap.class)).containsKey((Object)"jcr:title")) {
                return (String)targetContentProperties.get("jcr:title", String.class);
            }
        }
        return "";
    }

    public String getTarget() {
        return ProjectUtils.getStringProperty((Resource)this, "suffix");
    }

    public void setTarget(String target) {
        try {
            ProjectUtils.setStringProperty((Resource)this, "suffix", target);
        }
        catch (RepositoryException e) {
            throw new ProjectException("Failed to update link target", (Throwable)e);
        }
    }

    public String getTitle() {
        return ProjectUtils.getStringProperty((Resource)this, "jcr:title");
    }

    public void setTitle(String title) {
        try {
            ProjectUtils.setStringProperty((Resource)this, "jcr:title", title);
        }
        catch (RepositoryException e) {
            throw new ProjectException("Failed to update link title", (Throwable)e);
        }
    }

    public String getDescription() {
        return ProjectUtils.getStringProperty((Resource)this, "jcr:description");
    }

    public void setDescription(String description) {
        try {
            ProjectUtils.setStringProperty((Resource)this, "jcr:description", description);
        }
        catch (RepositoryException e) {
            throw new ProjectException("Failed to update link title", (Throwable)e);
        }
    }

    public void setCoverImage(String mimeType, InputStream stream) {
        ProjectUtils.setCoverImage((Resource)this, this.getProject(), mimeType, stream, this.getName());
        this.save();
    }

    public Resource getCoverImage() {
        Resource imageResource;
        String coverPath = ProjectUtils.getStringProperty((Resource)this, "coverUrl");
        if (!StringUtils.isBlank((CharSequence)coverPath) && (imageResource = this.getResourceResolver().getResource(coverPath)) != null && !ResourceUtil.isNonExistingResource((Resource)imageResource)) {
            return imageResource;
        }
        return null;
    }

    private void save() {
        try {
            ((Session)this.getResourceResolver().adaptTo(Session.class)).save();
        }
        catch (RepositoryException e) {
            throw new ProjectException("Unable to save the link.", (Throwable)e);
        }
    }

    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
        if (Resource.class == type) {
            return (AdapterType)this.getResource();
        }
        return (AdapterType)super.adaptTo(type);
    }
}