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

Java tutorial

Introduction

Here is the source code for com.artivisi.belajar.restful.ui.controller.SaleController.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 sales and limitations under
 * the License.
 */
package com.artivisi.belajar.restful.ui.controller;

import com.artivisi.belajar.restful.domain.trans.Sale;
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 SaleController {

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

    @RequestMapping("/sale/{idjual}")
    @ResponseBody
    public Sale findById(@PathVariable String id) {
        logger.debug("Mencari ID Penjualan.....");
        Sale x = belajarRestfulService.findSaleById(id);
        if (x == null) {
            throw new IllegalStateException();
        }
        return x;
    }

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

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

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

    @RequestMapping(method = RequestMethod.PUT, value = "/sale/{idjual}")
    @ResponseStatus(HttpStatus.OK)
    public void update(@PathVariable String id, @RequestBody @Valid Sale x) {
        Sale a = belajarRestfulService.findSaleById(id);
        if (a == null) {
            logger.warn("Produk dengan id-jual [{}] tidak ditemukan", id);
            throw new IllegalStateException();
        }
        x.setIdJual(a.getIdJual());
        belajarRestfulService.save(x);
    }

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

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

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

}