Java tutorial
/*Copyright (c) 2016-2017 testing.com All Rights Reserved. This software is the confidential and proprietary information of testing.com You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the source code license agreement you entered into with testing.com*/ package com.sharedtestproject.sampledb33.controller; /*This is a Studio Managed File. DO NOT EDIT THIS FILE. Your changes may be reverted by Studio.*/ import com.sharedtestproject.sampledb33.service.SampleService; import java.io.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.hibernate.TypeMismatchException; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; 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.RequestParam; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import com.wavemaker.runtime.data.exception.EntityNotFoundException; import com.wavemaker.runtime.data.expression.QueryFilter; import com.wavemaker.runtime.util.WMMultipartUtils; import com.wavemaker.runtime.util.WMRuntimeUtils; import com.wavemaker.runtime.file.model.DownloadResponse; import com.wordnik.swagger.annotations.*; import com.sharedtestproject.sampledb33.*; import com.sharedtestproject.sampledb33.service.*; import com.wavemaker.tools.api.core.annotations.WMAccessVisibility; import com.wavemaker.tools.api.core.models.AccessSpecifier; /** * Controller object for domain model class Sample. * @see com.sharedtestproject.sampledb33.Sample */ @RestController(value = "SampleDB33.SampleController") @RequestMapping("/sampleDB33/Sample") @Api(description = "Exposes APIs to work with Sample resource.", value = "SampleController") public class SampleController { private static final Logger LOGGER = LoggerFactory.getLogger(SampleController.class); @Autowired @Qualifier("sampleDB33.SampleService") private SampleService sampleService; @RequestMapping(value = "/search", method = RequestMethod.POST) @ApiOperation(value = "Returns the list of Sample instances matching the search criteria.") public Page<Sample> findSamples(Pageable pageable, @RequestBody QueryFilter[] queryFilters) { LOGGER.debug("Rendering Samples list"); return sampleService.findAll(queryFilters, pageable); } @RequestMapping(value = "/", method = RequestMethod.GET) @ApiOperation(value = "Returns the list of Sample instances.") public Page<Sample> getSamples(Pageable pageable) { LOGGER.debug("Rendering Samples list"); return sampleService.findAll(pageable); } @RequestMapping(value = "/{id}/content/{fieldName}", method = RequestMethod.GET, produces = "application/octet-stream") @ApiOperation(value = "Retrieves content for the given BLOB field in Sample instance") public DownloadResponse getSampleBLOBContent(@PathVariable("id") Integer id, @PathVariable("fieldName") String fieldName, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @RequestParam(value = "download", defaultValue = "false") boolean download) { LOGGER.debug("Retrieves content for the given BLOB field {} in Sample instance", fieldName); if (!WMRuntimeUtils.isLob(Sample.class, fieldName)) { throw new TypeMismatchException("Given field " + fieldName + " is not a valid BLOB type"); } Sample instance = sampleService.findById(id); return WMMultipartUtils.buildDownloadResponseForBlob(instance, fieldName, httpServletRequest, download); } /** * This setter method should only be used by unit tests * * @param service */ protected void setSampleService(SampleService service) { this.sampleService = service; } @RequestMapping(value = "/", method = RequestMethod.POST) @WMAccessVisibility(value = AccessSpecifier.APP_ONLY) @ApiOperation(value = "Creates a new Sample instance.") public Sample createSample(@RequestBody Sample instance) { LOGGER.debug("Create Sample with information: {}", instance); instance = sampleService.create(instance); LOGGER.debug("Created Sample with information: {}", instance); return instance; } @RequestMapping(value = "/", method = RequestMethod.POST, consumes = { "multipart/form-data" }) @WMAccessVisibility(value = AccessSpecifier.APP_ONLY) @ApiOperation(value = "Creates a new Sample instance.") public Sample createSample(MultipartHttpServletRequest multipartHttpServletRequest) { Sample sample = WMMultipartUtils.toObject(multipartHttpServletRequest, Sample.class, "sampleDB33"); LOGGER.debug("Creating a new sample with information: {}", sample); return sampleService.create(sample); } @RequestMapping(value = "/count", method = RequestMethod.GET) @WMAccessVisibility(value = AccessSpecifier.APP_ONLY) @ApiOperation(value = "Returns the total count of Sample instances.") public Long countAllSamples() { LOGGER.debug("counting Samples"); Long count = sampleService.countAll(); return count; } @RequestMapping(value = "/{id}", method = RequestMethod.GET) @WMAccessVisibility(value = AccessSpecifier.APP_ONLY) @ApiOperation(value = "Returns the Sample instance associated with the given id.") public Sample getSample(@PathVariable(value = "id") Integer id) throws EntityNotFoundException { LOGGER.debug("Getting Sample with id: {}", id); Sample instance = sampleService.findById(id); LOGGER.debug("Sample details with id: {}", instance); return instance; } @RequestMapping(value = "/{id}", method = RequestMethod.PUT) @WMAccessVisibility(value = AccessSpecifier.APP_ONLY) @ApiOperation(value = "Updates the Sample instance associated with the given id.") public Sample editSample(@PathVariable(value = "id") Integer id, @RequestBody Sample instance) throws EntityNotFoundException { LOGGER.debug("Editing Sample with id: {}", instance.getId()); instance.setId(id); instance = sampleService.update(instance); LOGGER.debug("Sample details with id: {}", instance); return instance; } @RequestMapping(value = "/{id}", method = RequestMethod.POST, consumes = { "multipart/form-data" }) @WMAccessVisibility(value = AccessSpecifier.APP_ONLY) @ApiOperation(value = "Updates the Sample instance associated with the given id.This API should be used when Sample instance fields that require multipart data.") public Sample editSample(@PathVariable(value = "id") Integer id, MultipartHttpServletRequest multipartHttpServletRequest) throws EntityNotFoundException { Sample newsample = WMMultipartUtils.toObject(multipartHttpServletRequest, Sample.class, "sampleDB33"); newsample.setId(id); Sample oldsample = sampleService.findById(id); WMMultipartUtils.updateLobsContent(oldsample, newsample); LOGGER.debug("Updating sample with information: {}", newsample); return sampleService.update(newsample); } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) @WMAccessVisibility(value = AccessSpecifier.APP_ONLY) @ApiOperation(value = "Deletes the Sample instance associated with the given id.") public boolean deleteSample(@PathVariable(value = "id") Integer id) throws EntityNotFoundException { LOGGER.debug("Deleting Sample with id: {}", id); Sample deleted = sampleService.delete(id); return deleted != null; } }