// Path: scheduled-actions-core/src/main/java/com/netflix/scheduledactions/triggers/CronExpressionFuzzer.java
// public class CronExpressionFuzzer {
// 
//   private static final String TOKEN = "H";
// 
//   public static String fuzz(String id, String expression) {
//     if (!hasFuzzyExpression(expression)) {
//       return expression;
//     }
//     return Tokens.tokenize(expression).toFuzzedExpression(id);
//   }
// 
//   public static void validate(String expression) {
//     TriggerUtils.validateCronExpression(fuzz("temp", expression));
//   }
// 
//   public static boolean hasFuzzyExpression(String expression) {
//     return expression.contains(TOKEN);
//   }
// 
//   private static class Tokens {
//     private String seconds;
//     private String minutes;
//     private String hours;
//     private String dayOfMonth;
//     private String month;
//     private String dayOfWeek;
//     private String year;
// 
//     private Tokens(String seconds,
//                    String minutes,
//                    String hours,
//                    String dayOfMonth,
//                    String month,
//                    String dayOfWeek,
//                    String year) {
//       this.seconds = seconds;
//       this.minutes = minutes;
//       this.hours = hours;
//       this.dayOfMonth = dayOfMonth;
//       this.month = month;
//       this.dayOfWeek = dayOfWeek;
//       this.year = year;
//     }
// 
//     static Tokens tokenize(String expression) {
//       String[] t = expression.trim().split(" ");
//       return new Tokens(t[0], t[1], t[2], t[3], t[4], t[5], (t.length == 7) ? t[6] : null);
//     }
// 
//     String toFuzzedExpression(String id) {
//       if (seconds.contains(TOKEN)) {
//         seconds = seconds.replace(TOKEN, hash(id, 59));
//       }
//       if (minutes.contains(TOKEN)) {
//         minutes = minutes.replace(TOKEN, hash(id, 59));
//       }
//       if (hours.contains(TOKEN)) {
//         hours = hours.replace(TOKEN, hash(id, 23));
//       }
// 
//       return (year == null)
//         ? format("%s %s %s %s %s %s", seconds, minutes, hours, dayOfMonth, month, dayOfWeek)
//         : format("%s %s %s %s %s %s %s", seconds, minutes, hours, dayOfMonth, month, dayOfWeek, year);
//     }
// 
//     private String hash(String id, Integer maxRange) {
//       return Integer.toString(Math.abs(id.hashCode() % maxRange));
//     }
//   }
// }


import com.google.common.collect.ImmutableMap;
import com.netflix.fenzo.triggers.TriggerUtils;
import com.netflix.scheduledactions.triggers.CronExpressionFuzzer;
import net.redhogs.cronparser.CronExpressionDescriptor;
import net.redhogs.cronparser.Options;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;
/*
 * Copyright 2015 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.scheduledactions.web.controllers;



@RestController
public class ValidationController {

    @RequestMapping(value = "/validateCronExpression", method = RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.OK)
    public Map<String,Object> validateCronExpression(@RequestParam String cronExpression) {
        ImmutableMap.Builder<String,Object> mapBuilder = ImmutableMap.builder();
        try {
            CronExpressionFuzzer.validate(cronExpression);
            mapBuilder.put("response", "Cron expression is valid");
            if (CronExpressionFuzzer.hasFuzzyExpression(cronExpression)) {
                mapBuilder.put("description", "No description available for fuzzy cron expressions");
            } else {
                try {
                    Options options = new Options();
                    options.setZeroBasedDayOfWeek(false);
// Path: scheduled-actions-core/src/main/java/com/netflix/scheduledactions/triggers/CronExpressionFuzzer.java
// public class CronExpressionFuzzer {
// 
//   private static final String TOKEN = "H";
// 
//   public static String fuzz(String id, String expression) {
//     if (!hasFuzzyExpression(expression)) {
//       return expression;
//     }
//     return Tokens.tokenize(expression).toFuzzedExpression(id);
//   }
// 
//   public static void validate(String expression) {
//     TriggerUtils.validateCronExpression(fuzz("temp", expression));
//   }
// 
//   public static boolean hasFuzzyExpression(String expression) {
//     return expression.contains(TOKEN);
//   }
// 
//   private static class Tokens {
//     private String seconds;
//     private String minutes;
//     private String hours;
//     private String dayOfMonth;
//     private String month;
//     private String dayOfWeek;
//     private String year;
// 
//     private Tokens(String seconds,
//                    String minutes,
//                    String hours,
//                    String dayOfMonth,
//                    String month,
//                    String dayOfWeek,
//                    String year) {
//       this.seconds = seconds;
//       this.minutes = minutes;
//       this.hours = hours;
//       this.dayOfMonth = dayOfMonth;
//       this.month = month;
//       this.dayOfWeek = dayOfWeek;
//       this.year = year;
//     }
// 
//     static Tokens tokenize(String expression) {
//       String[] t = expression.trim().split(" ");
//       return new Tokens(t[0], t[1], t[2], t[3], t[4], t[5], (t.length == 7) ? t[6] : null);
//     }
// 
//     String toFuzzedExpression(String id) {
//       if (seconds.contains(TOKEN)) {
//         seconds = seconds.replace(TOKEN, hash(id, 59));
//       }
//       if (minutes.contains(TOKEN)) {
//         minutes = minutes.replace(TOKEN, hash(id, 59));
//       }
//       if (hours.contains(TOKEN)) {
//         hours = hours.replace(TOKEN, hash(id, 23));
//       }
// 
//       return (year == null)
//         ? format("%s %s %s %s %s %s", seconds, minutes, hours, dayOfMonth, month, dayOfWeek)
//         : format("%s %s %s %s %s %s %s", seconds, minutes, hours, dayOfMonth, month, dayOfWeek, year);
//     }
// 
//     private String hash(String id, Integer maxRange) {
//       return Integer.toString(Math.abs(id.hashCode() % maxRange));
//     }
//   }
// }


// Path: scheduled-actions-web/src/main/java/com/netflix/scheduledactions/web/controllers/ValidationController.java
import com.google.common.collect.ImmutableMap;
import com.netflix.fenzo.triggers.TriggerUtils;
import com.netflix.scheduledactions.triggers.CronExpressionFuzzer;
import net.redhogs.cronparser.CronExpressionDescriptor;
import net.redhogs.cronparser.Options;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

/*
 * Copyright 2015 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.scheduledactions.web.controllers;



@RestController
public class ValidationController {

    @RequestMapping(value = "/validateCronExpression", method = RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.OK)
    public Map<String,Object> validateCronExpression(@RequestParam String cronExpression) {
        ImmutableMap.Builder<String,Object> mapBuilder = ImmutableMap.builder();
        try {
            CronExpressionFuzzer.validate(cronExpression);
            mapBuilder.put("response", "Cron expression is valid");
            if (CronExpressionFuzzer.hasFuzzyExpression(cronExpression)) {
                mapBuilder.put("description", "No description available for fuzzy cron expressions");
            } else {
                try {
                    Options options = new Options();
                    options.setZeroBasedDayOfWeek(false);
                    mapBuilder.put("description", CronExpressionDescriptor.getDescription(cronExpression, options));