LiveRelationshipInfoProvider.java 5.79 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.ListInfoProvider
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageInfoProvider
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.Blueprint
 *  com.day.cq.wcm.msm.api.BlueprintManager
 *  com.day.cq.wcm.msm.api.LiveCopy
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveRelationshipManager
 *  com.day.cq.wcm.msm.api.LiveStatus
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 */
package com.day.cq.wcm.msm.impl;

import com.day.cq.commons.ListInfoProvider;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageInfoProvider;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.Blueprint;
import com.day.cq.wcm.msm.api.BlueprintManager;
import com.day.cq.wcm.msm.api.LiveCopy;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.LiveStatus;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

@Component(metatype=0, immediate=1)
@Service
public class LiveRelationshipInfoProvider
implements ListInfoProvider,
PageInfoProvider {
    @Reference
    protected LiveRelationshipManager relationshipManager = null;

    public void updatePageInfo(SlingHttpServletRequest request, JSONObject info, Resource resource) throws JSONException {
        Page page = (Page)resource.adaptTo(Page.class);
        if (page != null) {
            try {
                resource = (Resource)page.adaptTo(Resource.class);
                BlueprintManager bpm = (BlueprintManager)resource.getResourceResolver().adaptTo(BlueprintManager.class);
                JSONObject msm = new JSONObject();
                info.put("msm", (Object)msm);
                boolean isInBlueprint = bpm != null && bpm.getContainingBlueprint(resource.getPath()) != null;
                msm.put("msm:isLiveCopy", this.relationshipManager.hasLiveRelationship(resource));
                msm.put("msm:isInBlueprint", isInBlueprint);
                msm.put("msm:isSource", isInBlueprint && this.relationshipManager.isSource(resource));
            }
            catch (WCMException e) {
                throw new JSONException((Throwable)e);
            }
        }
    }

    public void updateListGlobalInfo(SlingHttpServletRequest request, JSONObject info, Resource resource) throws JSONException {
    }

    public void updateListItemInfo(SlingHttpServletRequest request, JSONObject info, Resource resource) throws JSONException {
        try {
            boolean isInBlueprint;
            LiveRelationship liveRelationship = this.relationshipManager.getLiveRelationship(resource, true);
            BlueprintManager bpm = (BlueprintManager)resource.getResourceResolver().adaptTo(BlueprintManager.class);
            if (liveRelationship == null) {
                info.put("msm:isLiveCopy", false);
            } else {
                info.put("msm:liveRelationship", (Object)LiveRelationshipInfoProvider.buildRelation(liveRelationship));
                info.put("msm:isLiveCopy", this.relationshipManager.hasLiveRelationship(resource));
            }
            boolean bl = isInBlueprint = bpm != null && bpm.getContainingBlueprint(resource.getPath()) != null;
            if (isInBlueprint) {
                info.put("msm:isInBlueprint", true);
                info.put("msm:isSource", this.relationshipManager.isSource(resource));
            } else {
                info.put("msm:isInBlueprint", false);
                info.put("msm:isSource", false);
            }
        }
        catch (WCMException e) {
            throw new JSONException((Throwable)e);
        }
    }

    private static JSONObject buildRelation(LiveRelationship relation) throws JSONException {
        JSONObject ret = new JSONObject();
        ret.put("msm:syncPath", (Object)relation.getSyncPath());
        ret.put("msm:sourcePath", (Object)relation.getSourcePath());
        ret.put("msm:targetPath", (Object)relation.getTargetPath());
        ret.put("msm:isRootConfig", relation.getLiveCopy().isRoot());
        ret.put("msm:status", (Object)LiveRelationshipInfoProvider.buildStatus(relation.getStatus()));
        return ret;
    }

    private static JSONObject buildStatus(LiveStatus status) throws JSONException {
        JSONObject ret = new JSONObject();
        ret.put("msm:isCancelled", status.isCancelled());
        ret.put("msm:isCancelledForChildren", status.isCancelledForChildren());
        ret.put("msm:isTargetExisting", status.isTargetExisting());
        ret.put("msm:isSourceExisting", status.isSourceExisting());
        for (String key : status.getAdvancedStatus().keySet()) {
            ret.put(key, (Object)status.getAdvancedStatus(key));
        }
        return ret;
    }

    protected void bindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        this.relationshipManager = liveRelationshipManager;
    }

    protected void unbindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        if (this.relationshipManager == liveRelationshipManager) {
            this.relationshipManager = null;
        }
    }
}