View Javadoc
1   package net.sumaris.core.dao.referential.location;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core
6    * %%
7    * Copyright (C) 2018 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Preconditions;
26  import net.sumaris.core.dao.technical.hibernate.HibernateDaoSupport;
27  import net.sumaris.core.model.referential.Status;
28  import net.sumaris.core.model.referential.StatusEnum;
29  import net.sumaris.core.model.referential.location.LocationLevel;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.stereotype.Repository;
33  
34  import javax.persistence.NoResultException;
35  import javax.persistence.TypedQuery;
36  import javax.persistence.criteria.CriteriaBuilder;
37  import javax.persistence.criteria.CriteriaQuery;
38  import javax.persistence.criteria.ParameterExpression;
39  import javax.persistence.criteria.Root;
40  
41  @Repository("locationLevelDao")
42  public class LocationLevelDaoImpl extends HibernateDaoSupport implements LocationLevelDao {
43  
44      /** Logger. */
45      private static final Logger log =
46              LoggerFactory.getLogger(LocationLevelDaoImpl.class);
47  
48      @Override
49      public LocationLevel./../../../../net/sumaris/core/model/referential/location/LocationLevel.html#LocationLevel">LocationLevel create(LocationLevel level) {
50          Preconditions.checkNotNull(level);
51          //Preconditions.checkNotNull(level.getId());
52          Preconditions.checkNotNull(level.getLabel());
53          Preconditions.checkNotNull(level.getName());
54  
55  
56          // Default value
57          if (level.getStatus() == null) {
58              level.setStatus(load(Status.class, StatusEnum.ENABLE.getId()));
59          }
60  
61          getEntityManager().persist(level);
62  
63          return level;
64      }
65  
66      @Override
67      public LocationLevel findByLabel(final String label) {
68  
69          try {
70              return getByLabel(label);
71          }
72          catch(NoResultException e) {
73              return null;
74          }
75          catch(Exception e) {
76              return null;
77          }
78      }
79  
80      @Override
81      public LocationLevel getByLabel(final String label) {
82          CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
83          CriteriaQuery<LocationLevel> query = builder.createQuery(LocationLevel.class);
84          Root<LocationLevel> root = query.from(LocationLevel.class);
85  
86          ParameterExpression<String> labelParam = builder.parameter(String.class);
87  
88          query.select(root)
89                  .where(builder.equal(root.get(LocationLevel.Fields.LABEL), labelParam));
90  
91          TypedQuery<LocationLevel> q = getEntityManager().createQuery(query)
92                  .setParameter(labelParam, label);
93          return q.getSingleResult();
94      }
95  
96      /* -- protected methods -- */
97  
98  }