RepositoryGalleryController.java

/**
 * Copyright 2016 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.admin;

import java.nio.file.Paths;

import javax.servlet.http.HttpServletRequest;

import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.mvc.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping(RepositoryGalleryController.CONTROLLER_PATH)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class RepositoryGalleryController extends BaseController {
	public static final String CONTROLLER_PATH = "/admin/r/g";
	public static final String JSP_PATH = "/admin/repository/gallery";
	public static final Logger LOG = LoggerFactory.getLogger(RepositoryGalleryController.class);

	@Autowired
	private ImageGalleryService imageGalleryService;

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String listAllFiles(ModelMap model) {
		return "redirect:" + CONTROLLER_PATH + "/1";
	}

	@RequestMapping(value = "/{page:\\d+}", method = RequestMethod.GET)
	public String listAllFiles(ModelMap model, @PathVariable("page") int page) {
		Page<ImageGallery> pagedData = imageGalleryService.listImageGalleries(PageRequest.of(page - 1, 50, Sort.by("path")));
		model.addAttribute("pagedData", pagedData);

		return JSP_PATH + "/index";
	}

	@RequestMapping(value = "/**", method = RequestMethod.GET)
	public String listAllFiles(ModelMap model, HttpServletRequest request) throws InvalidRepositoryPathException {
		String fullpath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
		fullpath = fullpath.substring(CONTROLLER_PATH.length());

		ImageGallery imageGallery = imageGalleryService.loadImageGallery(Paths.get(fullpath));

		if (imageGallery == null) {
			throw new NotFoundElement("No image gallery here!");
		}

		model.addAttribute("thumbnailFormat", "200x200");
		model.addAttribute("imageGallery", imageGallery);

		return JSP_PATH + "/details";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String getEditPage(@RequestParam String galleryPath, ModelMap model) throws NoSuchRepositoryFileException, InvalidRepositoryPathException {
		ImageGallery imageGallery = imageGalleryService.loadImageGallery(Paths.get(galleryPath));
		if (imageGallery == null) {
			imageGallery = new ImageGallery();
			imageGallery.setPath(galleryPath);
		}
		model.addAttribute("galleryPath", galleryPath);
		model.addAttribute("imageGallery", imageGallery);

		return JSP_PATH + "/edit";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateMetadata(@RequestParam String galleryPath, @ModelAttribute ImageGallery imageGallery, RedirectAttributes redirectAttributes) throws NoSuchRepositoryFileException, InvalidRepositoryPathException {

		ImageGallery updatedGallery = imageGalleryService.loadImageGallery(Paths.get(galleryPath));
		if (updatedGallery == null) {
			imageGalleryService.createImageGallery(Paths.get(galleryPath), imageGallery.getTitle(), imageGallery.getDescription());
		} else {
			imageGalleryService.updateImageGalery(updatedGallery, imageGallery.getTitle(), imageGallery.getDescription());
		}

		redirectAttributes.addFlashAttribute("successMessage", "repository.gallery.successfully-updated");
		return "redirect:" + CONTROLLER_PATH;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String deleteFile(@RequestParam String galleryPath, RedirectAttributes redirectAttributes) throws InvalidRepositoryPathException {

		ImageGallery imageGallery = imageGalleryService.loadImageGallery(Paths.get(galleryPath));
		imageGalleryService.removeGallery(imageGallery);

		redirectAttributes.addFlashAttribute("successMessage", "repository.gallery.removed");
		return "redirect:" + CONTROLLER_PATH;
	}
}