/**
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you 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 org.jasig.portlet.notice.service;

import java.io.File;
import java.net.URL;

import javax.portlet.PortletRequest;

import org.apache.commons.io.FileUtils;
import org.jasig.portlet.notice.NotificationResponse;
import org.springframework.beans.factory.annotation.Required;

/**
 * This is a simple demo service provider. It reads data from
 * a file and returns it.
 */
public class DemoNotificationService extends AbstractNotificationService {

    private String demoFilename;

	/**
	 * Set the filename of the demo data.
	 * @param filename Location of the demo data file
	 */
    @Required
	public void setFilename(String filename) {
		demoFilename = filename;
	}

    @Override
    public NotificationResponse getNotifications(PortletRequest req, boolean refresh) {
        return readFromFile(demoFilename);
    }
    
    /*
     * Implementation
     */

    /**
     * Deserialize the given JSON formatted file back into a object.
     *
     * @param filename The path and name of the file to be read.
     * @return NotificationRequest, null if the de-serialization fails.
     */
    private NotificationResponse readFromFile(String filename) {
        
        URL location = getClass().getClassLoader().getResource(filename);
        
        if (location == null) {
            String msg = "Demo file not found:  " + filename;
            throw new RuntimeException(msg);
        }

        try {
            File f = new File(location.toURI());
            String json = FileUtils.readFileToString(f, "UTF-8");
            return NotificationResponse.fromJson(json);
        } catch(Exception e) {
            String msg = "Failed to read the demo data file:  " + location;
            throw new RuntimeException(msg);
        }

    }

}

@@ -877,88 +877,284 @@
 L;%0D%0A
-%0D%0Aimport javax.portlet.PortletRequest;%0D%0A%0D%0Aimport org.apache.commons.io.FileUtils
+import java.util.Calendar;%0D%0Aimport java.util.Date;%0D%0Aimport java.util.GregorianCalendar;%0D%0A%0D%0Aimport javax.portlet.PortletRequest;%0D%0A%0D%0Aimport org.apache.commons.io.FileUtils;%0D%0Aimport org.jasig.portlet.notice.NotificationCategory;%0D%0Aimport org.jasig.portlet.notice.NotificationEntry
 ;%0D%0Ai
@@ -1447,16 +1447,168 @@
 rvice %7B%0D
+%0A    %0D%0A    private static final int MIN_DAY_DELTA = 1;%0D%0A    private static final int MAX_DAY_DELTA = 14;%0D%0A    private static final int BLUE_SHIFT = -7;%0D
 %0A%0D%0A    p
@@ -2583,24 +2583,77 @@
 %0D%0A        %7D%0D
+%0A        %0D%0A        NotificationResponse rslt = null;%0D
 %0A%0D%0A        t
@@ -2788,21 +2788,22 @@
        r
-eturn
+slt = 
  Notific
@@ -2999,16 +2999,1040 @@
      %7D%0D%0A
+        %0D%0A        // A dash of post-processing:  let's make all the due dates at or near today%0D%0A        for (NotificationCategory nc : rslt.getCategories()) %7B%0D%0A            for (NotificationEntry y : nc.getEntries()) %7B%0D%0A                if (y.getDueDate() != null) %7B%0D%0A                    // Just manipulate the ones that actually have %0D%0A                    // a due date;  leave the others blank%0D%0A                    y.setDueDate(generateRandomDueDate());%0D%0A                %7D%0D%0A            %7D%0D%0A        %7D%0D%0A        %0D%0A        return rslt;%0D%0A%0D%0A    %7D%0D%0A    %0D%0A    /*%0D%0A     * Implementation%0D%0A     */%0D%0A%0D%0A    private Date generateRandomDueDate() %7B%0D%0A        int randomDelta = MIN_DAY_DELTA %0D%0A                + (int)(Math.random() * ((MAX_DAY_DELTA - MIN_DAY_DELTA) + 1))%0D%0A                + BLUE_SHIFT;  // Puts some dates in the past, some in the future%0D%0A        Calendar rslt = new GregorianCalendar();%0D%0A        rslt.setTimeInMillis(System.currentTimeMillis());%0D%0A        rslt.add(Calendar.DATE, randomDelta);%0D%0A        return rslt.getTime();
 %0D%0A    %7D%0D

Enhance the DemoNotificationService to generate due dates at or around the present (better demo)