package fi.otavanopisto.pyramus.views.resources;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import fi.internetix.smvc.controllers.PageRequestContext;
import fi.otavanopisto.pyramus.I18N.Messages;
import fi.otavanopisto.pyramus.breadcrumbs.Breadcrumbable;
import fi.otavanopisto.pyramus.dao.DAOFactory;
import fi.otavanopisto.pyramus.dao.resources.ResourceCategoryDAO;
import fi.otavanopisto.pyramus.domainmodel.resources.ResourceCategory;
import fi.otavanopisto.pyramus.domainmodel.users.Role;
import fi.otavanopisto.pyramus.framework.PyramusViewController;
import fi.otavanopisto.pyramus.framework.UserRole;
import fi.otavanopisto.pyramus.util.JSONArrayExtractor;
import fi.otavanopisto.pyramus.util.StringAttributeComparator;
/**
* The controller responsible of the Manage Resource Categories view of the application.
*
* @see fi.otavanopisto.pyramus.json.settings.SaveResourceCategoriesJSONRequestController
*/
public class ResourceCategoriesViewController extends PyramusViewController implements Breadcrumbable {
/**
* Processes the page request by including the corresponding JSP page to the response.
*
* @param pageRequestContext Page request context
*/
public void process(PageRequestContext pageRequestContext) {
ResourceCategoryDAO resourceCategoryDAO = DAOFactory.getInstance().getResourceCategoryDAO();
List<ResourceCategory> resourceCategories = resourceCategoryDAO.listUnarchived();
Collections.sort(resourceCategories, new StringAttributeComparator("getName"));
String jsonResourceCategories = new JSONArrayExtractor("name", "id").extractString(resourceCategories);
this.setJsDataVariable(pageRequestContext, "resourceCategories", jsonResourceCategories);
pageRequestContext.setIncludeJSP("/templates/resources/resourcecategories.jsp");
}
/**
* Returns the roles allowed to access this page. Managing fields of education requires
* {@link Role#ADMINISTRATOR} privileges.
*
* @return The roles allowed to access this page
*/
public UserRole[] getAllowedRoles() {
return new UserRole[] { UserRole.MANAGER, UserRole.STUDY_PROGRAMME_LEADER, UserRole.ADMINISTRATOR };
}
/**
* Returns the localized name of this page. Used e.g. for breadcrumb navigation.
*
* @param locale The locale to be used for the name
*
* @return The localized name of this page
*/
public String getName(Locale locale) {
return Messages.getInstance().getText(locale, "resources.resourceCategories.pageTitle");
}
}