ArticleController.java

/**
 * Copyright 2014 Global Crop Diversity Trust
 *
 * Licensed 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.genesys.server.mvc;

import javax.naming.NoPermissionException;

import org.apache.commons.lang3.StringUtils;
import org.genesys.server.api.v1.model.Article;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/content")
public class ArticleController extends BaseController {

	@Autowired
	private ContentService contentService;
	
	@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
	@RequestMapping("/{slug:.+}/{classPkShortName}/{targetId:\\d+}/{language}")
	public String viewScoped(ModelMap model, @PathVariable(value = "slug") String slug, @PathVariable("classPkShortName") String classPkShortName,
			@PathVariable("targetId") Long targetId, @PathVariable("language") String language) {
		LOG.debug("Viewing article {}", slug);

		final Article article = contentService.getArticleBySlugLangTargetIdClassPk(slug, language, targetId, classPkShortName);

		if (article == null) {
			throw new NotFoundElement();
		}
		
		model.addAttribute("title", article.getTitle());
		model.addAttribute("article", article);

		return "/content/article";
	}

	@RequestMapping("{slug:.+}")
	public String view(ModelMap model, @PathVariable(value = "slug") String slug) throws NoPermissionException {
		LOG.debug("Viewing article {}", slug);

		final Article article = contentService.getGlobalArticle(slug, getLocale());
		if (article == null) {
			throw new NotFoundElement();
		}else{
			if (article.isTemplate() && !hasRole("ADMINISTRATOR") && !hasRole("CONTENTMANAGER")){
				throw new AccessDeniedException("You do not have permission to access the resource");
			}
		}

		model.addAttribute("title", article.getTitle());
		model.addAttribute("article", article);

		return "/content/article";
	}

	@RequestMapping("{menu}/{url:.+}")
	public String viewWithMenu(ModelMap model, @PathVariable(value = "menu") String menuKey, @PathVariable(value = "url") String slug) throws NoPermissionException {
		String result = view(model, slug);
		if (StringUtils.isNotBlank(menuKey)) {
			LOG.debug("Loading menu {}", menuKey);
			model.addAttribute("menu", contentService.getMenu(menuKey));
		}
		return result;
	}
}