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 com.fasterxml.jackson.databind.JsonNode;
22  import com.fasterxml.jackson.databind.node.ObjectNode;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Objects;
26  import java.util.Optional;
27  import java.util.stream.Collectors;
28  
29  public class OJPAJSONAuditConfDAO extends AbstractJPAJSONLoggerDAO {
30  
31      protected static class OMessageCriteriaBuilder extends JSONMessageCriteriaBuilder {
32  
33          protected Optional<String> jsonExprItem(final JsonNode logger, final String field) {
34              return logger.has(field)
35                      ? Optional.of("@.logger." + field + " == \"" + logger.get(field).asText() + "\"")
36                      : Optional.empty();
37          }
38  
39          @Override
40          protected String doBuild(final List<ObjectNode> containers) {
41              if (entityKey != null) {
42                  query.append(andIfNeeded()).append('(').
43                          append("JSON_VALUE(").append(AUDIT_ENTRY_MESSAGE_COLUMN).
44                          append(", '$.before' RETURNING VARCHAR2(32767)) LIKE '%").
45                          append(entityKey).append("%' OR ").
46                          append("JSON_VALUE(").append(AUDIT_ENTRY_MESSAGE_COLUMN).
47                          append(", '$.input' RETURNING VARCHAR2(32767)) LIKE '%").
48                          append(entityKey).append("%' OR ").
49                          append("JSON_VALUE(").append(AUDIT_ENTRY_MESSAGE_COLUMN).
50                          append(", '$.output' RETURNING VARCHAR2(32767)) LIKE '%").
51                          append(entityKey).append("%')");
52              }
53  
54              if (!containers.isEmpty()) {
55                  query.append(andIfNeeded()).append('(').
56                          append(containers.stream().filter(container -> container.has("logger")).map(container -> {
57                              JsonNode logger = container.get("logger");
58  
59                              List<String> clauses = new ArrayList<>();
60                              jsonExprItem(logger, "type").ifPresent(clauses::add);
61                              jsonExprItem(logger, "category").ifPresent(clauses::add);
62                              jsonExprItem(logger, "subcategory").ifPresent(clauses::add);
63                              jsonExprItem(logger, "result").ifPresent(clauses::add);
64                              jsonExprItem(logger, "event").ifPresent(clauses::add);
65  
66                              return "JSON_EXISTS(MESSAGE, '$[*]?(" + String.join(" && ", clauses) + ")')";
67                          }).filter(Objects::nonNull).collect(Collectors.joining(" OR "))).
68                          append(')');
69              }
70  
71              return query.toString();
72          }
73      }
74  
75      @Override
76      protected MessageCriteriaBuilder messageCriteriaBuilder(final String entityKey) {
77          return new OMessageCriteriaBuilder().entityKey(entityKey);
78      }
79  }