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

Java tutorial

Introduction

Here is the source code for org.opentestsystem.authoring.testauth.rest.ComputationRuleController.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.ComputationRule;
import org.opentestsystem.authoring.testauth.domain.ComputationRuleMultiplicityType;
import org.opentestsystem.authoring.testauth.domain.ComputationRuleType;
import org.opentestsystem.authoring.testauth.domain.ConversionTableType;
import org.opentestsystem.authoring.testauth.domain.DictionaryIndexType;
import org.opentestsystem.authoring.testauth.service.ComputationRuleService;
import org.opentestsystem.authoring.testauth.validation.ComputationRuleValidator;
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 computationRule info
 */
@Controller
public class ComputationRuleController extends AbstractRestController {

    @Autowired
    private ComputationRuleService computationRuleService;

    @Autowired
    private ComputationRuleValidator computationRuleValidator;

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

    @RequestMapping(value = "/computationRule/{computationRuleId}", 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 scoring functions are non-tenanted
    @ResponseBody
    public ComputationRule findComputationRuleById(@PathVariable final String computationRuleId) {
        return this.computationRuleService.getComputationRule(computationRuleId);
    }

    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = "/computationRule", 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 scoring functions are non-tenanted
    @ResponseBody
    public ComputationRule createComputationRule(@RequestBody @Valid final ComputationRule computationRule,
            final HttpServletResponse response) {
        return this.computationRuleService.createComputationRule(computationRule);
    }

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

    @RequestMapping(value = "/computationRule/{computationRuleId}", method = RequestMethod.DELETE)
    @Secured({ "ROLE_Setup Modify" })
    // NOTE: there is intentionally no @PreAuthorize annotation...ability to modify setup is all you need since scoring functions are non-tenanted
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void removeComputationRuleById(@PathVariable final String computationRuleId) {
        this.computationRuleService.removeComputationRule(computationRuleId);
    }

    @RequestMapping(value = "/computationRule", 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 scoring functions are non-tenanted
    @ResponseBody
    public SearchResponse<ComputationRule> searchComputationRule(final HttpServletRequest request,
            final HttpServletResponse response) {
        return this.computationRuleService.searchComputationRules(request.getParameterMap());
    }

    @RequestMapping(value = "/computationRule/conversionTableTypes", 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 scoring functions are non-tenanted
    @ResponseBody
    public List<ConversionTableType> getConversionTableTypes() {
        return Lists.newArrayList(ConversionTableType.values());
    }

    @RequestMapping(value = "/computationRule/computationRuleTypes", 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 scoring functions are non-tenanted
    @ResponseBody
    public List<ComputationRuleType> getComputationRuleTypes() {
        return Lists.newArrayList(ComputationRuleType.values());
    }

    @RequestMapping(value = "/computationRule/computationRuleMultiplicityTypes", 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 scoring functions are non-tenanted
    @ResponseBody
    public List<ComputationRuleMultiplicityType> getComputationRuleMultiplicityTypes() {
        return Lists.newArrayList(ComputationRuleMultiplicityType.values());
    }

    @RequestMapping(value = "/computationRule/dictionaryIndexTypes", 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 scoring functions are non-tenanted
    @ResponseBody
    public List<DictionaryIndexType> getDictionaryIndexTypes() {
        return Lists.newArrayList(DictionaryIndexType.values());
    }
}