Projekt

Obecné

Profil

Stáhnout (1.82 KB) Statistiky
| Větev: | Revize:
1
package vldc.aswi.service;
2

    
3
import lombok.extern.slf4j.Slf4j;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.context.event.ContextRefreshedEvent;
6
import org.springframework.context.event.EventListener;
7
import org.springframework.core.annotation.Order;
8
import org.springframework.dao.InvalidDataAccessResourceUsageException;
9
import org.springframework.stereotype.Service;
10
import vldc.aswi.dao.LocationRepository;
11
import vldc.aswi.domain.Location;
12

    
13
import javax.transaction.Transactional;
14
import java.util.LinkedList;
15
import java.util.List;
16

    
17
/**
18
 * Manager for Location.
19
 */
20
@Service
21
@Slf4j
22
public class LocationManagerImpl implements LocationManager {
23

    
24
    /** Autowired location repository. */
25
    @Autowired
26
    private LocationRepository locationRepository;
27

    
28
    /**
29
     * Initialization setup for location manager.
30
     * Check if table "Umisteni" exists.
31
     */
32
    @EventListener(classes = {
33
            ContextRefreshedEvent.class
34
    })
35
    @Order(1)
36
    @Transactional
37
    public void setup() {
38
        try {
39
            if (this.locationRepository.count() == 0) {
40
                // Just checking if table exists.
41
            }
42
        }
43
        catch (InvalidDataAccessResourceUsageException e) {
44
            log.error("Table \"Umisteni\" did not exists in database!");
45
            System.exit(1);
46
        }
47
    }
48

    
49
    /**
50
     * Get all locations from database.
51
     * @return List of locations.
52
     */
53
    @Override
54
    public List<Location> getLocations() {
55
        List<Location> retVal = new LinkedList<>();
56
        this.locationRepository.findAll().forEach(retVal::add);
57
        return retVal;
58
    }
59

    
60
    /**
61
     * Get location from database by ID.
62
     * @return location by ID.
63
     */
64
    @Override
65
    public Location getLocationById(long id) {
66
        return locationRepository.getById(id);
67
    }
68
}
(8-8/16)