Java tutorial
/* * Copyright 2014 nateriver. * * 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 permissions and * limitations under the License. */ package be.bittich.quote.controller.impl; import be.bittich.quote.controller.GenericRestController; import static be.bittich.quote.core.Constant.APPLICATION_JSON; import be.bittich.quote.exception.EntityExistException; import be.bittich.quote.exception.EntityNotFoundException; import be.bittich.quote.model.ID; import be.bittich.quote.service.GenericService; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import java.io.Serializable; import java.util.List; import javax.validation.Valid; import org.springframework.http.HttpStatus; 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.ResponseStatus; /** * * @author nateriver * @param <T> * @param <PK> */ public abstract class AbstractController<T extends ID, PK extends Serializable> implements GenericRestController<T, PK> { private static final long serialVersionUID = 7077629412775767207L; public abstract GenericService<T, PK> getService(); @Override @RequestMapping(value = "/create", method = { RequestMethod.POST }, consumes = APPLICATION_JSON) @ResponseStatus(HttpStatus.OK) public T create(@RequestBody @Valid T t) { if (t.getId() != null && getService().findOneById((PK) t.getId()) != null) { getLogger().info(String.format("%s: %s", "Attempt to create an existing entity with id", t.getId())); throw new EntityExistException("Ajout impossible : l'entit existe dj!"); } checkArgument(this.canCreate(t), "Cration impossible: permission non accorde."); T insert = this.getService().insert(t); getLogger().info(String.format("%s: %s", "Entity added successfully with id", t.getId())); return insert; } @Override @RequestMapping(value = "/get/{id}", method = RequestMethod.GET) public T find(@PathVariable("id") PK id) { return this.getService().findOneById(id); } @Override @RequestMapping(value = "/list") public List<T> listAll() { return this.getService().findAll(); } @Override @RequestMapping(value = "/edit", method = RequestMethod.PUT) public T update(@RequestBody @Valid T t) { if (t.getId() == null || getService().findOneById((PK) t.getId()) == null) { getLogger().info(String.format("%s: %s", "Attempt to update an unknown entity with id", t.getId())); throw new EntityNotFoundException("Mise jour impossible : Entit introuvable"); } checkArgument(this.canUpdate(t), "Modification impossible: permission non accorde."); T saveOrUpdate = this.getService().saveOrUpdate(t); getLogger().info(String.format("%s: %s", "Entity edited successfully with id", t.getId())); return saveOrUpdate; } @Override @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.OK) public T delete(@PathVariable("id") PK id) { checkNotNull(id, "Suppression impossible : l'id ne peut tre vide!"); T t = getService().findOneById(id); if (getService().findOneById((PK) t.getId()) == null) { getLogger().info(String.format("%s: %s", "Attempt to delete an unknown entity with id", t.getId())); throw new EntityNotFoundException("Suppression impossible : Entit introuvable"); } checkArgument(this.canDelete(id), "Suppression impossible: permission non accorde."); this.getService().delete(t); getLogger().info(String.format("%s: %s", "Entity deleted successfully with id", t.getId())); return t; } @Override @RequestMapping(value = "/count", method = RequestMethod.GET) public Long count() { return this.getService().count(); } }