View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.persistence.jpa.dao;
20  
21  import java.util.List;
22  import javax.persistence.TypedQuery;
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.syncope.core.persistence.api.dao.FIQLQueryDAO;
25  import org.apache.syncope.core.persistence.api.entity.FIQLQuery;
26  import org.apache.syncope.core.persistence.api.entity.user.User;
27  import org.apache.syncope.core.persistence.jpa.entity.JPAFIQLQuery;
28  
29  public class JPAFIQLQueryDAO extends AbstractDAO<FIQLQuery> implements FIQLQueryDAO {
30  
31      @Override
32      public FIQLQuery find(final String key) {
33          return entityManager().find(JPAFIQLQuery.class, key);
34      }
35  
36      @Override
37      public List<FIQLQuery> findByOwner(final User user, final String target) {
38          StringBuilder queryString = new StringBuilder("SELECT e FROM ").
39                  append(JPAFIQLQuery.class.getSimpleName()).append(" e WHERE e.owner=:user");
40          if (StringUtils.isNotBlank(target)) {
41              queryString.append(" AND e.target=:target");
42          }
43  
44          TypedQuery<FIQLQuery> query = entityManager().createQuery(queryString.toString(), FIQLQuery.class);
45          query.setParameter("user", user);
46          if (StringUtils.isNotBlank(target)) {
47              query.setParameter("target", target);
48          }
49  
50          return query.getResultList();
51      }
52  
53      @Override
54      public List<FIQLQuery> findAll() {
55          TypedQuery<FIQLQuery> query = entityManager().createQuery(
56                  "SELECT e FROM " + JPAFIQLQuery.class.getSimpleName() + " e ", FIQLQuery.class);
57          return query.getResultList();
58      }
59  
60      @Override
61      public FIQLQuery save(final FIQLQuery fiqlQuery) {
62          return entityManager().merge(fiqlQuery);
63      }
64  
65      @Override
66      public void delete(final FIQLQuery fiqlQuery) {
67          entityManager().remove(fiqlQuery);
68      }
69  
70      @Override
71      public void delete(final String key) {
72          FIQLQuery fiqlQuery = find(key);
73          if (fiqlQuery == null) {
74              return;
75          }
76  
77          delete(fiqlQuery);
78      }
79  }