2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.auditing.impl;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Objects;
26 import org.apache.commons.lang3.StringUtils;
27 import org.javatuples.Pair;
28 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
29 import org.openecomp.sdc.be.model.User;
30 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
31 import org.openecomp.sdc.common.api.Constants;
33 public abstract class AuditBaseEventFactory implements AuditEventFactory {
35 private AuditingActionEnum action;
37 protected AuditBaseEventFactory(AuditingActionEnum action) {
38 this.action = Objects.requireNonNull(action);
41 protected AuditBaseEventFactory() {
44 public static String buildUserNameExtended(User user) {
48 StringBuilder builder = new StringBuilder();
49 appendIfNotEmpty(user.getUserId(), builder);
50 String firstName = replaceNullNameWithEmpty(user.getFirstName());
51 String lastName = replaceNullNameWithEmpty(user.getLastName());
52 if (appendIfNotEmpty(firstName, builder)) {
53 appendIfNotEmpty(lastName, builder, " ");
55 appendIfNotEmpty(lastName, builder);
57 appendIfNotEmpty(user.getEmail(), builder);
58 appendIfNotEmpty(user.getRole(), builder);
59 return builder.toString();
62 private static boolean appendIfNotEmpty(String value, StringBuilder builder) {
63 return appendIfNotEmpty(value, builder, ", ");
66 protected static boolean appendIfNotEmpty(String value, StringBuilder builder, String delimiter) {
67 if (StringUtils.isEmpty(value)) {
70 addDelimiterIfNeeded(builder, delimiter);
71 builder.append(value);
75 private static void addDelimiterIfNeeded(StringBuilder builder, String delimiter) {
76 if (builder.length() > 0) {
77 builder.append(delimiter);
81 protected static String buildUserName(User user) {
82 if (user == null || StringUtils.isEmpty(user.getUserId())) {
83 return StringUtils.EMPTY;
85 StringBuilder sb = new StringBuilder();
86 String firstName = replaceNullNameWithEmpty(user.getFirstName());
87 if (!firstName.isEmpty()) {
91 String lastName = replaceNullNameWithEmpty(user.getLastName());
92 if (!lastName.isEmpty()) {
95 sb.append("(").append(user.getUserId()).append(")");
99 private static String buildValue(String value) {
101 return StringUtils.EMPTY;
106 protected static String replaceNullNameWithEmpty(String name) {
107 if (name != null && !name.trim().contains(Constants.NULL_STRING)) {
110 return StringUtils.EMPTY;
113 public AuditingActionEnum getAction() {
118 //TODO implement in derived classes for ci testing
119 public List<Pair<String, String>> getQueryParams() {
120 return Collections.emptyList();
124 public final String getLogMessage() {
125 return String.format(getLogPattern(), getLogArgs());
128 private Object[] getLogArgs() {
129 return Arrays.stream(getLogMessageParams()).map(AuditBaseEventFactory::buildValue).toArray(String[]::new);
132 public abstract String getLogPattern();
134 public abstract String[] getLogMessageParams();