Management.java [src/java/util] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import static java.time.temporal.ChronoUnit.DAYS;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;



/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class Management {

    protected String name;
    protected int rotationYears; // ??
    protected ArrayList<Event> events;
    protected String id;
    protected String path;
    protected double stir;

    public Management() {

    }

    public int getRotationYears() {
        return rotationYears;
    }


//    public void readJSONData(JSONObject data, FORMAT type) throws JSONException, TranslatorException {
//        JSONArray rotations;
//        JSONObject managementTemplate;
//        JSONArray events;
//        JSONObject jdatum;
//        Event datum;
//        int yearoffset = 0;
//        LocalDate latest = null;
//
//        rotationYears = data.getInt(DURATION);
//        rotations = data.getJSONArray(MANAGEMENTS);
//
//        for (int i = 0; i < rotations.length(); i++) {
//            managementTemplate = rotations.getJSONObject(i);
//            events = managementTemplate.getJSONArray(EVENTS);
//
//            for (int j = 0; j < events.length(); j++) {
//                jdatum = events.getJSONObject(j);
//                datum = new Event();
//                datum.readJSONData(jdatum, type);
//                eventData.add(datum);
//                if (eventData.size() == 1) {
//                    yearoffset = eventData.get(0).getDate().getYear() - 1;
//                }
//                datum.setDate(datum.getDate().minusYears(yearoffset));
//                
//                // This event should be before the last event
//                if (eventData.size() > 2 && datum.getDate().isBefore(latest)) {
//                    throw new TranslatorException("Management Error: management " + i + " has an event out of order at position " + (j));
//                }
//                
//                latest = datum.getDate();
//            }
//        }
//    }


     public void rotateEvents(){
        TreeSet<Event> eventSet = new TreeSet();
        TreeSet<Event> newEventSet = new TreeSet();
        int totalCalendarYears = 0;
        int firstYear, lastYear;
        double totalYearDiff;
        int lastOperationDayOfYear;

        for (int i = 0; i < events.size(); i++) {
            eventSet.add(events.get(i));
        }

        Event lastEvent = eventSet.last();
        Event firstEvent = eventSet.first();

        firstYear = firstEvent.getDate().getYear();
        lastYear = lastEvent.getDate().getYear();
        lastOperationDayOfYear = lastEvent.getDate().getDayOfYear();

        totalCalendarYears = lastYear - firstYear + 1;
        long daysBetween = DAYS.between(firstEvent.getDate(), lastEvent.getDate()) + 1;

        totalYearDiff = daysBetween / 365.0;

        //  Can the rotation be squished into fewer years than the years provided?
        if (Math.ceil(totalYearDiff) < totalCalendarYears) {
            if (firstEvent.getDate().getDayOfYear() > lastEvent.getDate().getDayOfYear()) {

                lastYear = (int) Math.ceil(totalYearDiff);

                //  Need to reorder the list and reset the duration...
                totalCalendarYears = lastYear;
                Iterator it = eventSet.iterator();
                //Move last operation up to firstYear.                
                int count = 0;
                int formerYear = -1;

                while (it.hasNext()) {
                    Event tEvent = (Event) it.next();

                    //  Make sure to increment the counter to reset year values.
                    if ((formerYear != tEvent.getDate().getYear()) && (count < totalCalendarYears) && (firstYear < tEvent.getDate().getYear()) ){
                        count++;
                        formerYear = tEvent.getDate().getYear();
                    }

                    if (firstYear == tEvent.getDate().getYear()) {
                        if (tEvent.getDate().getDayOfYear() >= lastOperationDayOfYear) {
                            tEvent.setDate(tEvent.getDate().withYear(lastYear));
                        }
                    } else {
                        tEvent.setDate(tEvent.getDate().withYear(count));
                    }
                    newEventSet.add(tEvent);
                }

                eventSet.clear();
                eventSet.addAll(newEventSet);
            }//  Else...don't need to do anything??
        }
        events.clear();
        events.addAll(eventSet);
        rotationYears = totalCalendarYears;
}
}