org.opentestsystem.authoring.testauth.rest.ItemSelectionAlgorithmController.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.authoring.testauth.rest.ItemSelectionAlgorithmController.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/
package org.opentestsystem.authoring.testauth.rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.opentestsystem.authoring.testauth.domain.ItemSelectionAlgorithm;
import org.opentestsystem.authoring.testauth.domain.ItemSelectionAlgorithmType;
import org.opentestsystem.authoring.testauth.domain.ItemSelectionPurpose;
import org.opentestsystem.authoring.testauth.domain.ItemSelectionType;
import org.opentestsystem.authoring.testauth.service.ItemSelectionAlgorithmService;
import org.opentestsystem.authoring.testauth.validation.ItemSelectionAlgorithmValidator;
import org.opentestsystem.shared.exception.LocalizedException;
import org.opentestsystem.shared.search.domain.SearchResponse;
import org.opentestsystem.shared.web.AbstractRestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.google.common.collect.Lists;

/**
 * Used to retrieve itemSelectionAlgorithm info
 */
@Controller
public class ItemSelectionAlgorithmController extends AbstractRestController {

    @Autowired
    private ItemSelectionAlgorithmService itemSelectionAlgorithmService;

    @Autowired
    private ItemSelectionAlgorithmValidator itemSelectionAlgorithmValidator;

    @InitBinder("itemSelectionAlgorithm")
    public void initBinder(final WebDataBinder binder) {
        binder.setValidator(this.itemSelectionAlgorithmValidator);
    }

    @RequestMapping(value = "/itemSelectionAlgorithm/{itemSelectionAlgorithmId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @Secured({ "ROLE_Setup Read" })
    // NOTE: there is intentionally no @PreAuthorize annotation...ability to modify setup is all you need since Item selection algorithms are non-tenanted
    @ResponseBody
    public ItemSelectionAlgorithm findItemSelectionAlgorithmById(
            @PathVariable final String itemSelectionAlgorithmId) {
        return this.itemSelectionAlgorithmService.getItemSelectionAlgorithm(itemSelectionAlgorithmId);
    }

    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = "/itemSelectionAlgorithm", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @Secured({ "ROLE_Setup Modify" })
    // NOTE: there is intentionally no @PreAuthorize annotation...ability to modify setup is all you need since Item selection algorithms are non-tenanted
    @ResponseBody
    public ItemSelectionAlgorithm createItemSelectionAlgorithm(
            @RequestBody @Valid final ItemSelectionAlgorithm itemSelectionAlgorithm,
            final HttpServletResponse response) {
        return this.itemSelectionAlgorithmService.createItemSelectionAlgorithm(itemSelectionAlgorithm);
    }

    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/itemSelectionAlgorithm/{itemSelectionAlgorithmId}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @Secured({ "ROLE_Setup Modify" })
    // NOTE: there is intentionally no @PreAuthorize annotation...ability to modify setup is all you need since Item selection algorithms are non-tenanted
    @ResponseBody
    public ItemSelectionAlgorithm updateItemSelectionAlgorithm(@PathVariable final String itemSelectionAlgorithmId,
            @RequestBody @Valid final ItemSelectionAlgorithm itemSelectionAlgorithm,
            final HttpServletResponse response) {
        if (!StringUtils.equals(itemSelectionAlgorithmId, itemSelectionAlgorithm.getId())) {
            throw new LocalizedException("itemSelectionAlgorithm.invalid.id");
        }
        return this.itemSelectionAlgorithmService.updateItemSelectionAlgorithm(itemSelectionAlgorithm);
    }

    @RequestMapping(value = "/itemSelectionAlgorithm/{itemSelectionAlgorithmId}", method = RequestMethod.DELETE)
    @Secured({ "ROLE_Setup Modify" })
    // NOTE: there is intentionally no @PreAuthorize annotation...ability to modify setup is all you need since Item selection algorithms are non-tenanted
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void removeItemSelectionAlgorithmById(@PathVariable final String itemSelectionAlgorithmId) {
        this.itemSelectionAlgorithmService.removeItemSelectionAlgorithm(itemSelectionAlgorithmId);
    }

    @RequestMapping(value = "/itemSelectionAlgorithm", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @Secured({ "ROLE_Setup Read" })
    // NOTE: there is intentionally no @PreAuthorize annotation...ability to read setup is all you need since Item selection algorithms are non-tenanted
    @ResponseBody
    public SearchResponse<ItemSelectionAlgorithm> searchItemSelectionAlgorithm(final HttpServletRequest request,
            final HttpServletResponse response) {
        final SearchResponse<ItemSelectionAlgorithm> searchResponse = this.itemSelectionAlgorithmService
                .searchItemSelectionAlgorithms(request.getParameterMap());
        this.itemSelectionAlgorithmService.checkItemSelectionAlgorithmInUse(searchResponse.getSearchResults());
        return searchResponse;
    }

    @RequestMapping(value = "/itemSelectionAlgorithm/itemSelectionAlgorithmTypes", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @Secured({ "ROLE_Setup Read" })
    // NOTE: there is intentionally no @PreAuthorize annotation...ability to read setup is all you need since Item selection algorithms are non-tenanted
    @ResponseBody
    public List<ItemSelectionAlgorithmType> getItemSelectionAlgorithmTypes() {
        return Lists.newArrayList(ItemSelectionAlgorithmType.values());
    }

    @RequestMapping(value = "/itemSelectionAlgorithm/itemSelectionPurposes", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @Secured({ "ROLE_Setup Read" })
    // NOTE: there is intentionally no @PreAuthorize annotation...ability to read setup is all you need since Item selection algorithms are non-tenanted
    @ResponseBody
    public List<ItemSelectionPurpose> getItemSelectionPurposes() {
        return Lists.newArrayList(ItemSelectionPurpose.values());
    }

    @RequestMapping(value = "/itemSelectionAlgorithm/itemSelectionTypes", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @Secured({ "ROLE_Setup Read" })
    // NOTE: there is intentionally no @PreAuthorize annotation...ability to read setup is all you need since Item selection algorithms are non-tenanted
    @ResponseBody
    public List<ItemSelectionType> getItemSelectionTypes() {
        return Lists.newArrayList(ItemSelectionType.values());
    }
}