<commit_before>package edu.northwestern.bioinformatics.studycalendar.web.schedule;

import edu.northwestern.bioinformatics.studycalendar.domain.ScheduledActivityMode;
import edu.northwestern.bioinformatics.studycalendar.domain.ScheduledActivity;
import edu.northwestern.bioinformatics.studycalendar.domain.ScheduledCalendar;
import edu.northwestern.bioinformatics.studycalendar.domain.scheduledactivitystate.ScheduledActivityState;
import edu.northwestern.bioinformatics.studycalendar.domain.scheduledactivitystate.DatedScheduledActivityState;
import edu.northwestern.bioinformatics.studycalendar.dao.ScheduledCalendarDao;

import java.util.Date;
import java.util.Calendar;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author <NAME>
 */
public class BatchRescheduleCommand {
    private ScheduledActivityMode<?> newMode;
    private Integer dateOffset;
    private Integer moveDateOffset;
    private String newReason;
    private Set<ScheduledActivity> events;
    private ScheduledCalendar scheduledCalendar;

    private ScheduledCalendarDao scheduledCalendarDao;

    private static final Logger log = LoggerFactory.getLogger(BatchRescheduleCommand.class.getName());

    public BatchRescheduleCommand(ScheduledCalendarDao scheduledCalendarDao) {
        this.scheduledCalendarDao = scheduledCalendarDao;
    }

    public void apply() {
        for (ScheduledActivity event : events) {
            if (getNewMode() == null || (event.isValidNewState(newMode.getClazz()))) {
                changeState(event);
            }
        }
        scheduledCalendarDao.save(getScheduledCalendar());
    }

    private void changeState(ScheduledActivity event) {
        ScheduledActivityState newState;
        if (getNewMode() == null) {
            newState = event.getCurrentState();
        } else {
            newState = getNewMode().createStateInstance();
        }
        newState.setReason(createReason());

        if (newState instanceof DatedScheduledActivityState) {
            ((DatedScheduledActivityState) newState).setDate(createDate(event.getActualDate()));
        }
        event.changeState(newState);
    }

    private Date createDate(Date baseDate) {
        Calendar c = Calendar.getInstance();<commit_msg>Fixing bug # 401: History of ScheduledActivity for "move the date" option is now fixed. History is now kept, instead of being overwriten.<commit_after>package edu.northwestern.bioinformatics.studycalendar.web.schedule;

import edu.northwestern.bioinformatics.studycalendar.domain.ScheduledActivityMode;
import edu.northwestern.bioinformatics.studycalendar.domain.ScheduledActivity;
import edu.northwestern.bioinformatics.studycalendar.domain.ScheduledCalendar;
import edu.northwestern.bioinformatics.studycalendar.domain.scheduledactivitystate.ScheduledActivityState;
import edu.northwestern.bioinformatics.studycalendar.domain.scheduledactivitystate.DatedScheduledActivityState;
import edu.northwestern.bioinformatics.studycalendar.dao.ScheduledCalendarDao;

import java.util.Date;
import java.util.Calendar;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author <NAME>
 */
public class BatchRescheduleCommand {
    private ScheduledActivityMode<?> newMode;
    private Integer dateOffset;
    private Integer moveDateOffset;
    private String newReason;
    private Set<ScheduledActivity> events;
    private ScheduledCalendar scheduledCalendar;

    private ScheduledCalendarDao scheduledCalendarDao;

    private static final Logger log = LoggerFactory.getLogger(BatchRescheduleCommand.class.getName());

    public BatchRescheduleCommand(ScheduledCalendarDao scheduledCalendarDao) {
        this.scheduledCalendarDao = scheduledCalendarDao;
    }

    public void apply() {
        for (ScheduledActivity event : events) {
            if (getNewMode() == null || (event.isValidNewState(newMode.getClazz()))) {
                changeState(event);
            }
        }
        scheduledCalendarDao.save(getScheduledCalendar());
    }

    private void changeState(ScheduledActivity event) {
        ScheduledActivityState newState;
        if (getNewMode() == null) {
            newState = event.getCurrentState().getMode().createStateInstance();
        } else {
            newState = getNewMode().createStateInstance();
        }
        newState.setReason(createReason());

        if (newState instanceof DatedScheduledActivityState) {
            ((DatedScheduledActivityState) newState).setDate(createDate(event.getActualDate()));
        }
        event.changeState(newState);
    }

    private Date createDate(Date baseDate) {
        Calendar c = Calendar.getInstance();
{'fraction_non_alphanumeric': 0.08691032724181895, 'fraction_numerical': 0.0014874628134296643, 'mean_word_length': 4.09967497291441, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5457142', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1235, 'n_words': 226}