com.adobe.cq.screens.impl.aemio.ScreensChannelModel Maven / Gradle / Ivy
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2016 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
************************************************************************/
package com.adobe.cq.screens.impl.aemio;
import com.adobe.cq.screens.channel.ChannelService;
import com.adobe.cq.screens.device.Device;
import com.adobe.cq.screens.device.DeviceManager;
import com.adobe.granite.haf.annotations.ApiAction;
import com.adobe.granite.haf.annotations.ApiEntities;
import com.adobe.granite.haf.annotations.ApiModel;
import com.adobe.granite.haf.annotations.ApiProperty;
import com.adobe.granite.haf.annotations.HttpFormParam;
import com.day.cq.wcm.api.NameConstants;
import com.day.cq.wcm.api.Page;
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.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import javax.inject.Inject;
import javax.inject.Named;
import javax.jcr.RepositoryException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_ACT_BROADCAST_COMMAND;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_ACT_BROADCAST_COMMAND_WITH_ACK;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_ACT_UNASSIGN_COMMAND;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_DEVICE_COMMAND;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_DEVICE_COMMAND_ACK;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_DEVICE_COMMAND_PAYLOAD;
import static com.adobe.cq.screens.binding.ScreensConstants.REST_PARAM_DISPLAYS;
@Model(adaptables = Resource.class)
@ApiModel(category = ScreensModelLookup.CATEGORY, type={"aem-io/screens/channel"}, modelLookup = ScreensModelLookup.class)
public class ScreensChannelModel {
@Inject
@Self
protected Resource baseResource;
@Inject
@Named(NameConstants.NN_CONTENT)
protected Resource jcrContent;
@ApiProperty
public String getTitle() {
return ResourceUtil.getValueMap(jcrContent).get(NameConstants.PN_TITLE, String.class);
}
@ApiProperty
public String getDescription() {
return ResourceUtil.getValueMap(jcrContent).get(NameConstants.PN_DESCRIPTION, String.class);
}
@ApiProperty
public boolean isSupportsOffline() {
Resource offlineResource = baseResource.getChild("offline");
Resource offlineConfigResource = offlineResource != null ? offlineResource.getChild("config") : null;
return offlineConfigResource != null && offlineConfigResource.isResourceType("contentsync/config");
}
@ApiProperty
public boolean isOffline() {
return isSupportsOffline() && !ResourceUtil.getValueMap(jcrContent).get("forceRemoteContent", true);
}
@ApiEntities
public Iterable children() {
List results = new ArrayList();
Page page = baseResource.adaptTo(Page.class);
if (page != null) {
Iterator children = page.listChildren();
while (children.hasNext()) {
results.add(children.next().adaptTo(Resource.class));
}
}
return results;
}
@ApiAction(method = "POST", name = REST_ACT_BROADCAST_COMMAND)
public void executeCommand(
@HttpFormParam(REST_DEVICE_COMMAND) String msg,
@HttpFormParam(value = REST_DEVICE_COMMAND_PAYLOAD, optional = true) String msgPayload
) throws RepositoryException, JSONException {
executeCommand(msg, msgPayload, false);
}
@ApiAction(method = "POST", name = REST_ACT_BROADCAST_COMMAND_WITH_ACK)
public void executeCommand(
@HttpFormParam(REST_DEVICE_COMMAND) String msg,
@HttpFormParam(value = REST_DEVICE_COMMAND_PAYLOAD, optional = true) String msgPayload,
@HttpFormParam(value = REST_DEVICE_COMMAND_ACK, optional = true) Boolean requiresAck
) throws RepositoryException, JSONException {
DeviceManager dm = baseResource.getResourceResolver().adaptTo(DeviceManager.class);
if (dm == null) {
return;
}
JSONObject payload = null;
if (msgPayload != null) {
payload = new JSONObject(msgPayload);
}
Iterator devices = dm.getRelatedDevices(baseResource);
while (devices.hasNext()) {
dm.executeCommand(devices.next(), msg, payload, requiresAck != null && requiresAck);
}
}
@ApiAction(method = "POST", name = REST_ACT_UNASSIGN_COMMAND)
public void executeCommand(
@HttpFormParam(REST_PARAM_DISPLAYS) String displays
) throws RepositoryException, JSONException {
ResourceResolver resolver = baseResource.getResourceResolver();
ArrayList displayList = new ArrayList();
Resource displayResource;
for (String displayPath: displays.split(",")) {
displayResource = resolver.getResource(displayPath);
if (displayResource != null) {
displayList.add(displayResource);
}
}
if (displayList.size() == 0) {
return;
}
ChannelService channelSvc = resolver.adaptTo(ChannelService.class);
if (channelSvc == null) {
throw new JSONException("Could not load channel service.");
}
try {
channelSvc.unassignFromDisplays(resolver, baseResource, displayList);
}
catch (RepositoryException e) {
throw new JSONException(e.getMessage());
}
catch (PersistenceException e) {
throw new JSONException(e.getMessage());
}
}
}