001// license-header java merge-point
002//
003// Attention: Generated code! Do not modify by hand!
004// Generated by: SpringHibernateDaoImpl.vsl in andromda-spring-cartridge.
005//
006package fr.ifremer.adagio.core.dao.referential.location;
007
008/*
009 * #%L
010 * SIH-Adagio :: Core
011 * $Id: LocationDaoImpl.java 12005 2014-03-29 00:14:41Z bl05b3e $
012 * $HeadURL: https://forge.ifremer.fr/svn/sih-adagio/tags/adagio-3.5.6/core/src/main/java/fr/ifremer/adagio/core/dao/referential/location/LocationDaoImpl.java $
013 * %%
014 * Copyright (C) 2012 - 2013 Ifremer
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Affero General Public License as published by
018 * the Free Software Foundation, either version 3 of the License, or
019 * (at your option) any later version.
020 * 
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Affero General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * #L%
029 */
030
031import java.util.ArrayList;
032import java.util.Iterator;
033import java.util.List;
034
035import org.hibernate.Query;
036import org.springframework.dao.DataRetrievalFailureException;
037
038import fr.ifremer.adagio.core.service.referential.location.LocationUtils;
039
040/**
041 * @see fr.ifremer.adagio.core.dao.referential.location.Location
042 */
043@org.springframework.stereotype.Repository("locationDao")
044public class LocationDaoImpl
045        extends fr.ifremer.adagio.core.dao.referential.location.LocationDaoBase
046        implements LocationExtendDao {
047
048    private static final String QUERY_LOCATION_ID_BY_LABEL_AND_LOCATION_LEVEL_IDS = "selectLocationIdByLabelAndLocationLevel";
049
050    /**
051     * Constructor used by Spring
052     */
053    @org.springframework.beans.factory.annotation.Autowired
054    public LocationDaoImpl(org.hibernate.SessionFactory sessionFactory) {
055        super();
056        setSessionFactory(sessionFactory);
057    }
058
059    @Override
060    public String getLocationLabelByLatLong(Float latitude, Float longitude) {
061        return LocationUtils.computeStatisticalRectangle(latitude, longitude);
062    }
063
064    @Override
065    public Integer getLocationIdByLabelAndLocationLevel(String locationLabel, Integer[] locationLevelIds) {
066        logger.debug("call getLocationIdByLabelAndLocationLevel()"); //$NON-NLS-1$
067        try {
068            Query query = null;
069            query = this.getSession().getNamedQuery(QUERY_LOCATION_ID_BY_LABEL_AND_LOCATION_LEVEL_IDS);
070            query.setString("locationLabel", locationLabel); //$NON-NLS-1$
071            query.setParameterList("locationLevelIds", locationLevelIds); //$NON-NLS-1$ //$NON-NLS-1$
072
073            List<Integer> locationIds = new ArrayList<Integer>();
074            for (@SuppressWarnings("unchecked")
075            Iterator<Integer> iterator = query.iterate(); iterator.hasNext();) {
076                Integer locationId = iterator.next();
077                locationIds.add(locationId);
078            }
079            if (locationIds.size() == 0) {
080                return null;
081            }
082            if (locationIds.size() > 1) {
083                throw new DataRetrievalFailureException("Too many locations found, for label : " + locationLabel);
084            }
085
086            // Return the first element in the list
087            return locationIds.get(0);
088        } catch (RuntimeException re) {
089            logger.error("getLocationIdByLabelAndLocationLevel failed", re); //$NON-NLS-1$
090            throw re;
091        }
092    }
093}