com.adobe.cq.screens.impl.aemio.ScreensAssignmentModel 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.Calendar;
import java.util.Date;
import javax.inject.Inject;
import javax.jcr.RepositoryException;
import javax.servlet.http.HttpServletResponse;
import com.adobe.cq.screens.offlinecontent.OfflineContentService;
import org.apache.commons.lang.time.DateUtils;
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.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.cq.screens.assignment.AssignmentService;
import com.adobe.cq.screens.binding.ScreensConstants;
import com.adobe.cq.screens.impl.util.DeviceUtils;
import com.adobe.granite.haf.annotations.ApiAction;
import com.adobe.granite.haf.annotations.ApiLink;
import com.adobe.granite.haf.annotations.ApiModel;
import com.adobe.granite.haf.annotations.ApiProperty;
import com.adobe.granite.haf.api.ApiResponse;
import com.adobe.granite.haf.api.ApiResponseBuilder;
import com.adobe.granite.haf.api.ApiResponseBuilderFactory;
@Model(adaptables = Resource.class)
@ApiModel(category = ScreensModelLookup.CATEGORY, type={"aem-io/screens/assignment"}, modelLookup = ScreensModelLookup.class)
public class ScreensAssignmentModel {
private Logger log = LoggerFactory.getLogger(ScreensAssignmentModel.class);
@OSGiService
private ApiResponseBuilderFactory arbFactory;
@Inject
@Self
protected Resource baseResource;
@ApiLink(rel = "about")
public String entity() {
String url = ResourceUtil.getValueMap(baseResource).get("path", String.class);
return url == null ? null : url + ".json";
}
@ApiProperty
public String getRole() {
return ResourceUtil.getValueMap(baseResource).get("role", String.class);
}
@ApiProperty
public String getSchedule() {
return ResourceUtil.getValueMap(baseResource).get("schedule", String.class);
}
@ApiProperty
public long getStartDate() {
Date date = ResourceUtil.getValueMap(baseResource).get("startDate", Date.class);
return date != null ? DateUtils.truncate(date, Calendar.DATE).getTime() : 0;
}
@ApiProperty
public long getEndDate() {
Date date = ResourceUtil.getValueMap(baseResource).get("endDate", Date.class);
return date != null ? DateUtils.addMilliseconds(DateUtils.ceiling(date, Calendar.DATE), -1).getTime() : 0;
}
@ApiProperty
public long getPriority() {
return ResourceUtil.getValueMap(baseResource).get("priority", 1L);
}
@ApiAction(method = "POST", name = ScreensConstants.REST_ACT_UPDATE_CACHE_COMMAND)
public void executeCommand() throws JSONException, PersistenceException, RepositoryException {
OfflineContentService offlineContentSvc = DeviceUtils.safeAdaptTo(baseResource.getResourceResolver(), OfflineContentService.class);
offlineContentSvc.prepareOfflineContentForAssignment(baseResource);
}
/**
* {@inheritDoc}
*/
@ApiAction(method = "DELETE", name = ScreensConstants.REST_ACT_DELETE)
public ApiResponse delete() {
ResourceResolver resolver = baseResource.getResourceResolver();
ApiResponseBuilder builder = arbFactory.createBuilder();
try {
AssignmentService assignmentService = resolver.adaptTo(AssignmentService.class);
if (assignmentService != null) {
assignmentService.delete(baseResource);
resolver.commit();
}
} catch (PersistenceException e) {
log.warn("Cannot delete the assignment.");
}
return builder.setStatus(HttpServletResponse.SC_NO_CONTENT, "Deleted").build();
}
}