com.adobe.cq.screens.impl.aemio.ScreensDevicesFolderModel 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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.collections.Predicate;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import com.adobe.cq.screens.device.Device;
import com.adobe.cq.screens.device.DeviceManager;
import com.adobe.granite.haf.annotations.ApiEntities;
import com.adobe.granite.haf.annotations.ApiModel;
import com.adobe.granite.haf.annotations.ApiProperty;
import com.day.cq.wcm.api.NameConstants;
@Model(adaptables = Resource.class)
@ApiModel(category = ScreensModelLookup.CATEGORY, type={"aem-io/screens/devicesfolder"}, modelLookup = ScreensModelLookup.class)
public class ScreensDevicesFolderModel {
@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);
}
@ApiEntities
public Iterable children() {
List results = new ArrayList();
final String tenant = baseResource.getPath().split("/")[3];
DeviceManager dm = baseResource.getResourceResolver().adaptTo(DeviceManager.class);
if (dm != null) {
Iterator devices = dm.getDevices(new DevicesPredicate(tenant));
while (devices.hasNext()) {
results.add(devices.next().adaptTo(Resource.class));
}
}
return results;
}
private static class DevicesPredicate implements Predicate {
private final String prefix;
private DevicesPredicate(@Nonnull String tenant) {
prefix = "/home/users/screens/" + tenant + "/devices";
}
public boolean evaluate(Object d) {
return ((Device) d).getPath().startsWith(prefix);
}
}
}