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.LocationClassification;
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("locationClassificationDao")
42  public class LocationClassificationDaoImpl extends HibernateDaoSupport implements LocationClassificationDao {
43  
44      /** Logger. */
45      private static final Logger log =
46              LoggerFactory.getLogger(LocationClassificationDaoImpl.class);
47  
48      @Override
49      public LocationClassification./../net/sumaris/core/model/referential/location/LocationClassification.html#LocationClassification">LocationClassification create(LocationClassification classification) {
50          Preconditions.checkNotNull(classification);
51          //Preconditions.checkNotNull(classification.getId());
52          Preconditions.checkNotNull(classification.getLabel());
53          Preconditions.checkNotNull(classification.getName());
54  
55          // Default value
56          if (classification.getStatus() == null) {
57              classification.setStatus(load(Status.class, StatusEnum.ENABLE.getId()));
58          }
59  
60          getEntityManager().persist(classification);
61  
62          return classification;
63      }
64  
65      @Override
66      public LocationClassification findByLabel(final String label) {
67  
68          try {
69              return getByLabel(label);
70          }
71          catch(NoResultException e) {
72              return null;
73          }
74          catch(Exception e) {
75              return null;
76          }
77      }
78  
79      @Override
80      public LocationClassification getByLabel(final String label) {
81          CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
82          CriteriaQuery<LocationClassification> query = builder.createQuery(LocationClassification.class);
83          Root<LocationClassification> root = query.from(LocationClassification.class);
84  
85          ParameterExpression<String> labelParam = builder.parameter(String.class);
86  
87          query.select(root)
88                  .where(builder.equal(root.get(LocationClassification.Fields.LABEL), labelParam));
89  
90          TypedQuery<LocationClassification> q = getEntityManager().createQuery(query)
91                  .setParameter(labelParam, label);
92          return q.getSingleResult();
93      }
94  
95      /* -- protected methods -- */
96  
97  }