com.artivisi.belajar.restful.ui.controller.ProdukController.java Source code

Java tutorial

Introduction

Here is the source code for com.artivisi.belajar.restful.ui.controller.ProdukController.java

Source

/**
 * Copyright (C) 2011 ArtiVisi Intermedia <info@artivisi.com>
 *
 * 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 produks and limitations under
 * the License.
 */
package com.artivisi.belajar.restful.ui.controller;

import com.artivisi.belajar.restful.domain.Produk;
import com.artivisi.belajar.restful.service.BelajarRestfulService;
import java.net.URI;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
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 org.springframework.web.util.UriTemplate;

@Controller
public class ProdukController {

    @Autowired
    private BelajarRestfulService belajarRestfulService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("/produk/{id}")
    @ResponseBody
    public Produk findById(@PathVariable String id) {
        logger.debug("Mencari ID Produk.....");
        Produk x = belajarRestfulService.findProdukById(id);
        if (x == null) {
            throw new IllegalStateException();
        }
        return x;
    }

    @RequestMapping("/produk/{kdbrg}/findkdbrg")
    @ResponseBody
    public Produk findByKdBrg(@PathVariable String kdbrg) {
        logger.debug("Mencari Kode Produk.....    " + kdbrg);
        Produk x = belajarRestfulService.findProdukByKdBrg(kdbrg);
        logger.debug("ISI Kode Produk.....    " + x.getKdbrg());
        if (x == null) {
            throw new IllegalStateException();
        }
        return x;
    }

    /*    
        @RequestMapping("/produk/findkdbrg")
        @ResponseBody
        public Produk findByKdBrg(@PathVariable string kdbrg @RequestBody @Valid Produk x, HttpServletRequest request, HttpServletResponse response) {
    return belajarRestfulService.findProdukByKdBrg(x);
        }  */

    @RequestMapping(value = "/produk", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public void create(@RequestBody @Valid Produk x, HttpServletRequest request, HttpServletResponse response) {
        belajarRestfulService.save(x);
        String requestUrl = request.getRequestURL().toString();
        URI uri = new UriTemplate("{requestUrl}/{id}").expand(requestUrl, x.getId());
        response.setHeader("Location", uri.toASCIIString());
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/produk/{id}")
    @ResponseStatus(HttpStatus.OK)
    public void update(@PathVariable String id, @RequestBody @Valid Produk x) {
        Produk a = belajarRestfulService.findProdukById(id);
        if (a == null) {
            logger.warn("Produk dengan id [{}] tidak ditemukan", id);
            throw new IllegalStateException();
        }
        x.setId(a.getId());
        belajarRestfulService.save(x);
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/produk/{id}")
    @ResponseStatus(HttpStatus.OK)
    public void delete(@PathVariable String id) {
        Produk a = belajarRestfulService.findProdukById(id);
        if (a == null) {
            logger.warn("Produk dengan id [{}] tidak ditemukan", id);
            throw new IllegalStateException();
        }
        belajarRestfulService.delete(a);
    }

    @RequestMapping(value = "/produk", method = RequestMethod.GET)
    @ResponseBody
    public List<Produk> findAll(Pageable pageable, HttpServletResponse response) {
        return belajarRestfulService.findAllProduk(pageable).getContent();
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler({ IllegalStateException.class })
    public void handle() {
        logger.debug("Resource dengan URI tersebut tidak ditemukan");
    }

}