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 org.apache.commons.lang3.BooleanUtils;
22  import org.apache.commons.lang3.StringUtils;
23  import org.apache.commons.lang3.tuple.Pair;
24  import org.apache.syncope.common.lib.types.AttrSchemaType;
25  import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
26  import org.apache.syncope.core.persistence.api.entity.AnyUtils;
27  import org.apache.syncope.core.persistence.api.entity.PlainAttrValue;
28  import org.apache.syncope.core.persistence.api.entity.PlainSchema;
29  
30  public class OJPAJSONAnyDAO extends AbstractJPAJSONAnyDAO {
31  
32      public OJPAJSONAnyDAO(final PlainSchemaDAO plainSchemaDAO) {
33          super(plainSchemaDAO);
34      }
35  
36      @Override
37      protected String queryBegin(final String table) {
38          return "SELECT DISTINCT id FROM " + view(table) + ' ';
39      }
40  
41      @Override
42      protected Object getAttrValue(
43              final PlainSchema schema,
44              final PlainAttrValue attrValue,
45              final boolean ignoreCaseMatch) {
46  
47          return schema.getType() == AttrSchemaType.Boolean
48                  ? BooleanUtils.toStringTrueFalse(attrValue.getBooleanValue())
49                  : schema.getType() == AttrSchemaType.String && ignoreCaseMatch
50                  ? StringUtils.lowerCase(attrValue.getStringValue())
51                  : attrValue.getValue();
52      }
53  
54      @Override
55      protected String attrValueMatch(
56              final AnyUtils anyUtils,
57              final PlainSchema schema,
58              final PlainAttrValue attrValue,
59              final boolean ignoreCaseMatch) {
60  
61          StringBuilder query = new StringBuilder("plainSchema = ? AND ");
62  
63          Pair<String, Boolean> schemaInfo = schemaInfo(schema.getType(), ignoreCaseMatch);
64          query.append(schemaInfo.getRight() ? "LOWER(" : "");
65  
66          if (schema.isUniqueConstraint()) {
67              query.append("u").append(schemaInfo.getLeft());
68          } else {
69              query.append("JSON_VALUE(").append(schemaInfo.getLeft()).append(", '$[*]')");
70          }
71  
72          query.append(schemaInfo.getRight() ? ")" : "").
73                  append(" = ").
74                  append(schemaInfo.getRight() ? "LOWER(" : "").
75                  append('?').append(schemaInfo.getRight() ? ")" : "");
76  
77          return query.toString();
78      }
79  }