/**
 * Copyright 2011 The Board of Regents of the University of Wisconsin System.
 */
package org.jasig.schedassist.web.profiles;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.jasig.schedassist.impl.owner.PublicProfileDao;
import org.jasig.schedassist.model.PublicProfile;
import org.jasig.schedassist.model.PublicProfileId;
import org.jasig.schedassist.model.PublicProfileTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * {@link Controller} for browsing lists of {@Link PublicProfile}s.
 * 
 * @author Nicholas Blair
 * @version $Id: PublicProfilesBrowseController.java $
 */
@Controller
public class PublicProfilesBrowseController {

	private PublicProfileDao publicProfileDao;
	/**
	 * @param publicProfileDao the publicProfileDao to set
	 */
	@Autowired
	public void setPublicProfileDao(PublicProfileDao publicProfileDao) {
		this.publicProfileDao = publicProfileDao;
	}
	
	/**
	 * Retrieve profiles between startIndex and (startIndex + resultsPerPage).
	 * @param startIndex
	 * @param resultsPerPage
	 * @return the view name to display links to each {@link PublicProfile}
	 */
	@RequestMapping(value="/public/browse.html", method=RequestMethod.GET)
	public String retrieveAllProfiles(final ModelMap model, @RequestParam(value="startIndex",required=false,defaultValue="0") int startIndex) {
		List<PublicProfileId> profileIds = publicProfileDao.getPublicProfileIds();
		if(profileIds.isEmpty()) {
			// short circuit
			model.addAttribute("titleSuffix", "");
			return "profiles/public-listing";
		}
		
		populateModel(model, profileIds, startIndex);
		return "profiles/public-listing";
	}
	
	/**
	 * 
	 * @param model
	 * @return the view name for the listing of advisors with {@link PublicProfile}s
	 */
	@RequestMapping(value="/public/advisors.html", method=RequestMethod.GET)
	public String retrieveAdvisorProfiles(final ModelMap model, @RequestParam(value="startIndex",required=false,defaultValue="0") int startIndex) {
		List<PublicProfileId> profileIds = publicProfileDao.getAdvisorPublicProfileIds();
		if(profileIds.isEmpty()) {
			// short circuit for empty list
			model.addAttribute("titleSuffix", "");
			return "profiles/public-advisor-listing";
		}
		
		populateModel(model, profileIds, startIndex);
		return "profiles/public-advisor-listing";
	}
	
	/**
	 * 
	 * @param model
	 * @return the view name for the listing of instructors with {@link PublicProfile}s
	 */
	@RequestMapping(value="/public/instructors.html", method=RequestMethod.GET)
	public String retrieveInstructorProfiles(final ModelMap model, @RequestParam(value="startIndex",required=false,defaultValue="0") int startIndex) {
		List<PublicProfileId> profileIds = publicProfileDao.getInstructorPublicProfileIds();
		if(profileIds.isEmpty()) {
			// short circuit for empty list
			model.addAttribute("titleSuffix", "");
			return "profiles/public-instructor-listing";
		}
		
		populateModel(model, profileIds, startIndex);
		return "profiles/public-instructor-listing";
	}
	
	/**
	 * Populate the {@link ModelMap} with the appropriate information.
	 * 
	 * @param model
	 * @param profileIds
	 * @param startIndex
	 */
	protected void populateModel(ModelMap model, List<PublicProfileId> profileIds, int startIndex) {
		Collections.sort(profileIds);
		ProfilePageInformation pageInfo = new ProfilePageInformation(profileIds, startIndex);
		List<PublicProfileId> sublist = pageInfo.getSublist();
		Map<PublicProfileId, List<PublicProfileTag>> profileMap = publicProfileDao.getProfileTagsBatch(sublist);
		model.addAttribute("titleSuffix", buildPageTitleSuffix(pageInfo.getStartIndex(), pageInfo.getEndIndex()));
		model.addAttribute("profileIds", sublist);
		model.addAttribute("profileMap", profileMap);
		model.addAttribute("showPrev", pageInfo.isShowPrev());
		model.addAttribute("showPrevIndex", pageInfo.getShowPrevIndex());
		model.addAttribute("showNext", pageInfo.isShowNext());
		model.addAttribute("showNextIndex", pageInfo.getShowNextIndex());
	}
	
	/**
	 * Create a string appended to the document title, e.g.:
	 * 
	 * "Public Profiles 1 - 10".
	 * 
	 * @param startIndex
	 * @param endIndex
	 * @return
	 */
	protected String buildPageTitleSuffix(int startIndex, int endIndex) {
		StringBuilder title = new StringBuilder();
		title.append(" ");
		title.append(startIndex == 0 ? 1 : startIndex + 1);
		title.append(" - ");
		title.append(endIndex);
		return title.toString();
	}
}

@@ -4,87 +4,773 @@
 %0A * 
-Copyright 2011 The Board of Regents of the University of Wisconsin System
+Licensed to Jasig under one or more contributor license%0A * agreements. See the NOTICE file distributed with this work%0A * for additional information regarding copyright ownership.%0A * Jasig licenses this file to you under the Apache License,%0A * Version 2.0 (the %22License%22); you may not use this file%0A * except in compliance with the License. You may obtain a%0A * copy of the License at:%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing,%0A * software distributed under the License is distributed on%0A * an %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY%0A * KIND, either express or implied. See the License for the%0A * specific language governing permissions and limitations%0A * under the License
 .%0A */%0A
+%0A
 pack

update license header