/**
 * 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.calendar.dao;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;

import org.jasig.portlet.calendar.CalendarSet;
import org.jasig.portlet.calendar.PredefinedCalendarConfiguration;
import org.jasig.portlet.calendar.UserDefinedCalendarConfiguration;
import org.jasig.portlet.calendar.service.SessionSetupInitializationService;
import org.springframework.beans.factory.annotation.Required;

/**
 * 
 * 
 * @author Jen Bourey, jbourey@unicon.net
 * @version $Revision$
 */
public class HibernateCalendarSetDao implements ICalendarSetDao {
    
    private CalendarStore calendarStore;
    
    @Required
    @Resource(name="calendarStore")
    public void setCalendarStore(CalendarStore calendarStore) {
        this.calendarStore = calendarStore;
    }

    /*
     * (non-Javadoc)
     * @see org.jasig.portlet.calendar.dao.ICalendarSetDao#getCalendarSet(javax.portlet.PortletRequest)
     */
    public CalendarSet<?> getCalendarSet(PortletRequest request) {
        
        final String username = getUsername(request);
                
        // retrieve a list of calendar configurations for this user
        final List<UserDefinedCalendarConfiguration> cals = calendarStore
            .getCalendarConfigurations(username);
        
        final Set<UserDefinedCalendarConfiguration> calendars = new HashSet<UserDefinedCalendarConfiguration>();
        calendars.addAll(cals);
        
        final CalendarSet<UserDefinedCalendarConfiguration> set = new CalendarSet<UserDefinedCalendarConfiguration>();
        set.setConfigurations(calendars);
        return set;
    }

    @Override
    public List<PredefinedCalendarConfiguration> getAvailablePredefinedCalendarConfigurations(PortletRequest request) {

        final String username = getUsername(request);

        // For this ICalendarSetDao, the list is the unfiltered collection from CalendarStore...
        return calendarStore.getPredefinedCalendarConfigurations(username, false);
    }
    
    /*
     * Implementation
     */
    
    private String getUsername(PortletRequest request) {
        // Get the username from the portlet session.  This implementation
        // assumes that the session initialization service is run at login time
        final PortletSession session = request.getPortletSession();
        final String rslt = (String) session.getAttribute(SessionSetupInitializationService.USERNAME_KEY);
        return rslt;
    }

}

@@ -806,16 +806,44 @@
 r.dao;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -1270,24 +1270,81 @@
 ionService;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.s
@@ -1400,21 +1400,19 @@
 %0A%0A/**%0A *
- 
 %0A *
- 
 %0A * @aut
@@ -1539,20 +1539,90 @@
 etDao %7B%0A
+%0A
     
+private final Logger log = LoggerFactory.getLogger(this.getClass());%0A
 %0A    pri
@@ -1655,20 +1655,16 @@
 rStore;%0A
-    
 %0A    @Re
@@ -2028,24 +2028,25 @@
 uest) %7B%0A
+%0A
         
 %0A       
@@ -2029,32 +2029,143 @@
 est) %7B%0A%0A        
+final CalendarSet%3CUserDefinedCalendarConfiguration%3E set = new CalendarSet%3CUserDefinedCalendarConfiguration%3E();%0A
 %0A        final S
@@ -2211,25 +2211,45 @@
         
-    
+if (username != null) %7B%0A%0A
     
-%0A
         
@@ -2312,24 +2312,28 @@
 ser%0A        
+   
+ 
 final List%3CU
@@ -2398,16 +2398,20 @@
         
+    
 .getCale
@@ -2440,25 +2440,21 @@
 rname);%0A
-    
+%0A
     
-%0A
         
@@ -2566,16 +2566,20 @@
         
+    
 calendar
@@ -2594,24 +2594,16 @@
 (cals);%0A
-        
 %0A       
@@ -2607,123 +2607,8 @@
     
-final CalendarSet%3CUserDefinedCalendarConfiguration%3E set = new CalendarSet%3CUserDefinedCalendarConfiguration%3E();%0A    
     
@@ -2640,16 +2640,26 @@
 endars);
+%0A        %7D
 %0A       
@@ -2864,24 +2864,60 @@
 (request);%0A%0A
+        if (username != null) %7B%0A    
         // F
@@ -3005,24 +3005,28 @@
 ...%0A        
+    
 return calen
@@ -3088,26 +3088,118 @@
 false);%0A
+        %7D else %7B%0A            return new ArrayList%3CPredefinedCalendarConfiguration%3E();%0A        %7D%0A
     %7D%0A
-    
 %0A    /*%0A
@@ -3228,20 +3228,16 @@
      */%0A
-    
 %0A    pri
@@ -3616,16 +3616,109 @@
 E_KEY);%0A
+        if (rslt == null) %7B%0A            log.warn(%22Username not found in session%22);%0A        %7D%0A
         

Handle case where username is not in session for HibernateCalednarSetDao.