<commit_before>/*
 * Created on Feb 5, 2008
 *
 * Copyright(c) Yale University, Feb 5, 2008.  All rights reserved.
 * (See licensing and redistribution disclosures at end of this file.)
 * 
 */
package edu.yale.its.tp.portlets.calendar.mvc.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Period;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.AbstractController;

import edu.yale.its.tp.portlets.calendar.CalendarConfiguration;
import edu.yale.its.tp.portlets.calendar.adapter.CalendarLinkException;
import edu.yale.its.tp.portlets.calendar.adapter.ICalendarAdapter;
import edu.yale.its.tp.portlets.calendar.dao.CalendarStore;
import edu.yale.its.tp.portlets.calendar.service.IInitializationService;

public class CalendarController extends AbstractController {

	private static Log log = LogFactory.getLog(CalendarController.class);

	public ModelAndView handleRenderRequestInternal(RenderRequest request,
			RenderResponse response) throws Exception {

		Map<String, Object> model = new HashMap<String, Object>();
		PortletSession session = request.getPortletSession(true);
		HashMap<Long, String> hiddenCalendars = null;
		Calendar cal = Calendar.getInstance();
		String subscribeId = null;
		
		/**
		 * If this is a new session, perform any necessary 
		 * portlet initialization.
		 */

		if (session.getAttribute("initialized") == null) {
			
			if(userToken == null || userToken.equalsIgnoreCase("")) {
				subscribeId = request.getRemoteUser();
	    	}
	    	else {
	    		// get the credentials for this portlet from the UserInfo map
	    		Map userinfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
	    		subscribeId = (String) userinfo.get(userToken);    		
	    	}
			
			//default to guest
			if (subscribeId == null) {
				subscribeId = "guest";
			}
			session.setAttribute("subscribeId", subscribeId);

			// get a set of all role names currently configured for
			// default calendars
			List<String> allRoles = calendarStore.getUserRoles();
			log.debug("all roles: " + allRoles);
			
			// determine which of the above roles the user belongs to
			// and store the resulting list in the session
			Set<String> userRoles = new HashSet<String>();
			for (String role : allRoles) {
				if (request.isUserInRole(role))
					userRoles.add(role);
			}
			session.setAttribute("userRoles", userRoles);
			
			// determine if this user belongs to the defined calendar
			// administration group and store the result in the session
			session.setAttribute("isAdmin", 
					request.isUserInRole("calendarAdmin"));

			// update the user's calendar subscriptions to include
			// any calendars that have been associated with his or 
			// her role
			calendarStore.initCalendar(subscribeId, userRoles);

			// create a list of hidden calendars
			hiddenCalendars = new HashMap<Long, String>();
			session.setAttribute("hiddenCalendars", hiddenCalendars);

			// set now as the starting date
			session.setAttribute("startDate", cal.getTime());
			
			// set the default number of days to display
			session.setAttribute("days", defaultDays);

			// perform any other configured initialization tasks
			for (IInitializationService service : initializationServices) {
				service.initialize(request);
			}

			// mark this session as initialized
			session.setAttribute("initialized", "true");
			session.setMaxInactiveInterval(60*60*2);
			
			PortletPreferences prefs = request.getPreferences();<commit_msg>NOJIRA Adding admin flag to application-scoped session<commit_after>/*
 * Created on Feb 5, 2008
 *
 * Copyright(c) Yale University, Feb 5, 2008.  All rights reserved.
 * (See licensing and redistribution disclosures at end of this file.)
 * 
 */
package edu.yale.its.tp.portlets.calendar.mvc.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Period;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.AbstractController;

import edu.yale.its.tp.portlets.calendar.CalendarConfiguration;
import edu.yale.its.tp.portlets.calendar.adapter.CalendarLinkException;
import edu.yale.its.tp.portlets.calendar.adapter.ICalendarAdapter;
import edu.yale.its.tp.portlets.calendar.dao.CalendarStore;
import edu.yale.its.tp.portlets.calendar.service.IInitializationService;

public class CalendarController extends AbstractController {

	private static Log log = LogFactory.getLog(CalendarController.class);

	public ModelAndView handleRenderRequestInternal(RenderRequest request,
			RenderResponse response) throws Exception {

		Map<String, Object> model = new HashMap<String, Object>();
		PortletSession session = request.getPortletSession(true);
		HashMap<Long, String> hiddenCalendars = null;
		Calendar cal = Calendar.getInstance();
		String subscribeId = null;
		
		/**
		 * If this is a new session, perform any necessary 
		 * portlet initialization.
		 */

		if (session.getAttribute("initialized") == null) {
			
			if(userToken == null || userToken.equalsIgnoreCase("")) {
				subscribeId = request.getRemoteUser();
	    	}
	    	else {
	    		// get the credentials for this portlet from the UserInfo map
	    		Map userinfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
	    		subscribeId = (String) userinfo.get(userToken);    		
	    	}
			
			//default to guest
			if (subscribeId == null) {
				subscribeId = "guest";
			}
			session.setAttribute("subscribeId", subscribeId);

			// get a set of all role names currently configured for
			// default calendars
			List<String> allRoles = calendarStore.getUserRoles();
			log.debug("all roles: " + allRoles);
			
			// determine which of the above roles the user belongs to
			// and store the resulting list in the session
			Set<String> userRoles = new HashSet<String>();
			for (String role : allRoles) {
				if (request.isUserInRole(role))
					userRoles.add(role);
			}
			session.setAttribute("userRoles", userRoles);
			
			// determine if this user belongs to the defined calendar
			// administration group and store the result in the session
			session.setAttribute("isAdmin", 
					request.isUserInRole("calendarAdmin"), PortletSession.APPLICATION_SCOPE);

			// update the user's calendar subscriptions to include
			// any calendars that have been associated with his or 
			// her role
			calendarStore.initCalendar(subscribeId, userRoles);

			// create a list of hidden calendars
			hiddenCalendars = new HashMap<Long, String>();
			session.setAttribute("hiddenCalendars", hiddenCalendars);

			// set now as the starting date
			session.setAttribute("startDate", cal.getTime());
			
			// set the default number of days to display
			session.setAttribute("days", defaultDays);

			// perform any other configured initialization tasks
			for (IInitializationService service : initializationServices) {
				service.initialize(request);
			}

			// mark this session as initialized
			session.setAttribute("initialized", "true");
			session.setMaxInactiveInterval(60*60*2);
			
			PortletPreferences prefs = request.getPreferences();
{'fraction_non_alphanumeric': 0.0992847617893078, 'fraction_numerical': 0.004121711722633046, 'mean_word_length': 4.214917825537294, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '5760530', 'n_tokens_mistral': 2620, 'n_tokens_neox': 2312, 'n_words': 610}