package de.terrestris.shogun2.rest;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 de.terrestris.shogun2.model.PersistentObject;
import de.terrestris.shogun2.service.AbstractCrudService;

/**
 * @author Kai Volland
 * @author Daniel Koch
 *
 */
@RequestMapping(value="/rest")
public abstract class AbstractRestController<E extends PersistentObject> {


	@Autowired
	private AbstractCrudService<E> service;

	/**
	 * Find all Entities.
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public List<E> findAll() {
		return this.service.findAll();
	}

	/**
	 * Create an entity.
	 * @param json
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	public E create(@RequestBody E json) {
		System.out.println("create() with body "+json+" of type "
				+ json.getClass());
		E created = this.service.saveOrUpdate(json);
		return created;
	}

	/**
	 * Get an entity by id.
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public E get(@PathVariable int id) {
		E instance = this.service.findById(id);
		return instance;
	}

	/**
	 * Updates an entity by id.
	 * @param id
	 * @param json
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/{id}", method=RequestMethod.POST)
	public E update(@PathVariable int id, @RequestBody E json) throws Exception {
		System.out.println("update() of id#"+id+" with body "+json);
		System.out.println("E json is of type "+json.getClass());

		E entity = this.service.findById(id);
		try {
			BeanUtils.copyProperties(entity, json);
		}
		catch (Exception e) {
			System.out.println("while copying properties " + e);
			throw e;
		}

		System.out.println("merged entity: " + entity);

		E updated = this.service.saveOrUpdate(entity);
		System.out.println("updated enitity: " + updated);

		return updated;
	}

	/**
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
	public E delete(@PathVariable int id) {
		E entity = this.service.findById(id);
		this.service.delete(entity);
		return entity;
	}

}

@@ -101,16 +101,48 @@
 nUtils;%0A
+import org.apache.log4j.Logger;%0A
 import o
@@ -719,16 +719,124 @@
 ect%3E %7B%0A%0A
+%09/**%0A%09 * The Logger%0A%09 */%0A%09private static final Logger LOG = Logger.getLogger(AbstractRestController.class);%0A
 %0A%09@Autow
@@ -1183,34 +1183,24 @@
 on) %7B%0A%09%09
-System.out.println
+LOG.info
 (%22create
@@ -1234,13 +1234,9 @@
 pe %22
-%0A%09%09%09%09
+ 
 + js
@@ -1785,34 +1785,24 @@
 ion %7B%0A%09%09
-System.out.println
+LOG.info
 (%22update
@@ -1838,34 +1838,24 @@
 son);%0A%09%09
-System.out.println
+LOG.info
 (%22E json
@@ -2009,34 +2009,24 @@
 e) %7B%0A%09%09%09
-System.out.println
+LOG.info
 (%22while 
@@ -2071,34 +2071,24 @@
 %0A%09%09%7D%0A%0A%09%09
-System.out.println
+LOG.info
 (%22merged
@@ -2165,26 +2165,16 @@
 ;%0A%09%09
-System.out.println
+LOG.info
 (%22up

Use Logger instead of system.out