MaintenanceWindowImpl.java 4.58 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.granite.maintenance.impl;

import com.adobe.granite.maintenance.MaintenanceTaskInfo;
import com.adobe.granite.maintenance.MaintenanceUtil;
import com.adobe.granite.maintenance.impl.Util;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;

public class MaintenanceWindowImpl
implements Serializable {
    private static final long serialVersionUID = 2;
    private final String title;
    private final String startTimeExpression;
    private final String endTimeExpression;
    private List<MaintenanceTaskInfo> tasks = Collections.emptyList();
    private final MaintenanceTaskInfo.TaskSchedule schedule;

    public MaintenanceWindowImpl(Resource windowResource) throws IllegalArgumentException {
        Integer[] endWeekDays;
        int[] endTimeDef;
        ValueMap vm = ResourceUtil.getValueMap((Resource)windowResource);
        String startTime = (String)vm.get("windowStartTime", String.class);
        String endTime = (String)vm.get("windowEndTime", String.class);
        if (!MaintenanceUtil.isValidTimeInterval(startTime, endTime)) {
            throw new IllegalAccessError("Start and/or end time invalid: " + startTime + ", " + endTime);
        }
        String scheduleInfo = ((String)vm.get("windowSchedule", (Object)"daily")).toUpperCase();
        this.schedule = MaintenanceTaskInfo.TaskSchedule.valueOf(scheduleInfo);
        Integer[] weekDays = (Integer[])vm.get("windowScheduleWeekdays", Integer[].class);
        Integer weekDayFirstLast = (Integer)vm.get("windowFirstLastStartDay", Integer.class);
        this.title = (String)vm.get("jcr:title", (Object)windowResource.getName());
        this.startTimeExpression = Util.getCronExpression(this.schedule, weekDays, startTime, weekDayFirstLast);
        if (this.startTimeExpression == null) {
            throw new IllegalArgumentException("Invalid start time expression");
        }
        boolean adjustEndTime = false;
        int[] startTimeDef = MaintenanceUtil.parseTime(startTime);
        if (startTimeDef[0] > (endTimeDef = MaintenanceUtil.parseTime(endTime))[0] || startTimeDef[0] == endTimeDef[0] && startTimeDef[1] > endTimeDef[1]) {
            adjustEndTime = true;
        }
        if ((endWeekDays = weekDays) != null && endWeekDays.length > 1) {
            endWeekDays[0] = endWeekDays[1];
        }
        if (adjustEndTime) {
            switch (this.schedule) {
                case DAILY: {
                    if (weekDays == null) break;
                    for (int i = 0; i < weekDays.length; ++i) {
                        endWeekDays[i] = weekDays[i] + 1;
                        if (endWeekDays[i] != 8) continue;
                        endWeekDays[i] = 0;
                    }
                    break;
                }
                case WEEKLY: 
                case BIWEEKLY: 
                case MONTHLY: {
                    if (endWeekDays == null) {
                        endWeekDays = new Integer[]{2};
                        break;
                    }
                    if (endWeekDays.length != 1) break;
                    endWeekDays[0] = weekDays[0] + 1;
                    if (endWeekDays[0] != 8) break;
                    endWeekDays[0] = 0;
                }
            }
        }
        this.endTimeExpression = Util.getCronExpression(this.schedule, endWeekDays, endTime, null);
        if (this.endTimeExpression == null) {
            throw new IllegalArgumentException("Invalid end time expression");
        }
    }

    public void setTasks(List<MaintenanceTaskInfo> tasks) {
        this.tasks = tasks;
    }

    public String getStartCronExpression() {
        return this.startTimeExpression;
    }

    public String getEndCronExpression() {
        return this.endTimeExpression;
    }

    public String getStartScheduleName() {
        return MaintenanceWindowImpl.class.getName() + "/" + this.title + "/start";
    }

    public String getEndScheduleName() {
        return MaintenanceWindowImpl.class.getName() + "/" + this.title + "/end";
    }

    public List<MaintenanceTaskInfo> getMaintenanceTaskInfos() {
        return this.tasks;
    }

    public MaintenanceTaskInfo.TaskSchedule getSchedule() {
        return this.schedule;
    }

    public String getTitle() {
        return this.title;
    }

}