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.Collection;
22  import java.util.List;
23  import javax.persistence.TypedQuery;
24  import org.apache.syncope.core.persistence.api.dao.DerSchemaDAO;
25  import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
26  import org.apache.syncope.core.persistence.api.entity.AnyTypeClass;
27  import org.apache.syncope.core.persistence.api.entity.DerSchema;
28  import org.apache.syncope.core.persistence.jpa.entity.JPADerSchema;
29  
30  public class JPADerSchemaDAO extends AbstractDAO<DerSchema> implements DerSchemaDAO {
31  
32      protected final ExternalResourceDAO resourceDAO;
33  
34      public JPADerSchemaDAO(final ExternalResourceDAO resourceDAO) {
35          this.resourceDAO = resourceDAO;
36      }
37  
38      @Override
39      public DerSchema find(final String key) {
40          return entityManager().find(JPADerSchema.class, key);
41      }
42  
43      @Override
44      public List<DerSchema> findByAnyTypeClasses(final Collection<AnyTypeClass> anyTypeClasses) {
45          StringBuilder queryString = new StringBuilder("SELECT e FROM ").
46                  append(JPADerSchema.class.getSimpleName()).
47                  append(" e WHERE ");
48          anyTypeClasses.forEach(anyTypeClass -> queryString.
49                  append("e.anyTypeClass.id='").
50                  append(anyTypeClass.getKey()).append("' OR "));
51  
52          TypedQuery<DerSchema> query = entityManager().createQuery(
53                  queryString.substring(0, queryString.length() - 4), DerSchema.class);
54  
55          return query.getResultList();
56      }
57  
58      @Override
59      public List<DerSchema> findByKeyword(final String keyword) {
60          TypedQuery<DerSchema> query = entityManager().createQuery(
61                  "SELECT e FROM " + JPADerSchema.class.getSimpleName() + " e"
62                  + " WHERE e.id LIKE :keyword", DerSchema.class);
63          query.setParameter("keyword", keyword);
64          return query.getResultList();
65      }
66  
67      @Override
68      public List<DerSchema> findAll() {
69          TypedQuery<DerSchema> query = entityManager().createQuery(
70                  "SELECT e FROM " + JPADerSchema.class.getSimpleName() + " e", DerSchema.class);
71          return query.getResultList();
72      }
73  
74      @Override
75      public DerSchema save(final DerSchema schema) {
76          ((JPADerSchema) schema).map2json();
77          return entityManager().merge(schema);
78      }
79  
80      @Override
81      public void delete(final String key) {
82          final DerSchema schema = find(key);
83          if (schema == null) {
84              return;
85          }
86  
87          resourceDAO.deleteMapping(key);
88  
89          if (schema.getAnyTypeClass() != null) {
90              schema.getAnyTypeClass().getDerSchemas().remove(schema);
91          }
92  
93          entityManager().remove(schema);
94      }
95  }