package de.fu_berlin.agdb.crepe.rest.controller;

import de.fu_berlin.agdb.crepe.data.StationMetaData;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;

import java.util.Collections;
import java.util.List;

/**
 * A REST controller to serve metadata of all available weather stations.
 */
@RestController
@RequestMapping("/stations")
public class StationMetaDataController {
    private static final ServiceRegistry serviceRegistry;
    private static final SessionFactory sessionFactory;

    static {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();

            serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties())
                    .build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    private static Session getSession() throws HibernateException {
        return sessionFactory.openSession();
    }


    /**
     * /stations returns a list of the metadata of all available stations.
     */
    @RequestMapping(method = RequestMethod.GET)
    public List<StationMetaData> allStations(@RequestParam(required = false) Long stationId) {
        Query q;
        List<StationMetaData> result;
        final Session session = getSession();
        try {
            if (stationId != null) {
                StationMetaData station = (StationMetaData) session.get(StationMetaData.class, stationId);
                result = station == null ? Collections.emptyList() : Collections.singletonList(station);
            }
            else {
                q = session.createQuery("from StationMetaData");
                result = (List<StationMetaData>) q.list();
            }
        } finally {
            session.close();
        }
        return result;
    }

    /**
     * /stations/{id} returns a single station by its stationId
     * @param stationId A numeric stationId
     * @return The metadata for the corresponding weather station.
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{stationId:[\\d]+}")
    public StationMetaData getStationById(@PathVariable long stationId) {
        return (StationMetaData) getSession().get(StationMetaData.class, stationId);
    }

    /**
     * /stations/{name} returns all stations matching that name.
     */
    @RequestMapping(method = RequestMethod.GET, value = "/find/{stationName}")
    public List<StationMetaData> getStationsByName(@PathVariable String stationName) {
        Query q;
        List<StationMetaData> result;
        final Session session = getSession();
        try {
            q = session.createQuery("from StationMetaData where stationName like :stationName");
            q.setString("stationName", stationName + "%");

            result = (List<StationMetaData>) q.list();
        } finally {
            session.close();
        }

        return result;
    }

    /**
     * Makes this REST controller JSONP capable.
     */
    @ControllerAdvice
    private static class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
        public JsonpAdvice() {
            super("callback");
        }
    }
}

@@ -1760,68 +1760,8 @@
 lt;%0A
-        final Session session = getSession();%0A        try %7B%0A
     
@@ -1839,61 +1839,23 @@
 n = 
-(StationMetaData) session.
 get
-(
 Station
-MetaData.class, 
+ById(
 stat
@@ -1996,24 +1996,78 @@
      else %7B%0A
+                final Session session = getSession();%0A
             
@@ -2186,38 +2186,8 @@
     
-        %7D%0A        %7D finally %7B%0A
     
@@ -2211,33 +2211,38 @@
 lose();%0A        
-%7D
+    %7D%0A
 %0A        return 
@@ -3044,32 +3044,17 @@
 ();%0A
-        try %7B%0A  
+%0A
         
   q 
@@ -3041,34 +3041,32 @@
 ion();%0A%0A        
-  
 q = session.crea
@@ -3142,20 +3142,16 @@
         
-    
 q.setStr
@@ -3190,36 +3190,32 @@
  %22%25%22);%0A%0A        
-  
-  
 result = (List%3CS
@@ -3245,32 +3245,8 @@
 ();%0A
-        %7D finally %7B%0A    
     
@@ -3265,26 +3265,16 @@
 close();
-%0A        %7D
 %0A%0A      

Remove try-finally constructs