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=========================================================
21 package org.openecomp.sdc.be.auditing.impl;
23 import org.apache.commons.lang.StringUtils;
24 import org.javatuples.Pair;
25 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
26 import org.openecomp.sdc.be.model.User;
27 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
28 import org.openecomp.sdc.common.api.Constants;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Objects;
34 public abstract class AuditBaseEventFactory implements AuditEventFactory {
36 private AuditingActionEnum action;
38 public AuditBaseEventFactory(AuditingActionEnum action) {
39 this.action = Objects.requireNonNull(action);
42 public AuditBaseEventFactory() {}
44 public AuditingActionEnum getAction() {
48 public static String buildUserNameExtended(User user) {
53 StringBuilder builder = new StringBuilder();
54 appendIfNotEmpty(user.getUserId(), builder);
56 String firstName = replaceNullNameWithEmpty(user.getFirstName());
57 String lastName = replaceNullNameWithEmpty(user.getLastName());
59 if (appendIfNotEmpty(firstName, builder)) {
60 appendIfNotEmpty(lastName, builder, " ");
63 appendIfNotEmpty(lastName, builder);
65 appendIfNotEmpty(user.getEmail(), builder);
66 appendIfNotEmpty(user.getRole(), builder);
68 return builder.toString();
71 private static boolean appendIfNotEmpty(String value, StringBuilder builder) {
72 return appendIfNotEmpty(value, builder, ", ");
75 protected static boolean appendIfNotEmpty(String value, StringBuilder builder, String delimiter) {
76 if (StringUtils.isEmpty(value)) {
79 addDelimiterIfNeeded(builder, delimiter);
80 builder.append(value);
84 private static void addDelimiterIfNeeded(StringBuilder builder, String delimiter) {
85 if (builder.length() > 0) {
86 builder.append(delimiter);
90 protected static String buildUserName(User user) {
91 if (user == null || StringUtils.isEmpty(user.getUserId())) {
92 return StringUtils.EMPTY;
94 StringBuilder sb = new StringBuilder();
95 String firstName = replaceNullNameWithEmpty(user.getFirstName());
96 if (!firstName.isEmpty()) {
100 String lastName = replaceNullNameWithEmpty(user.getLastName());
101 if (!lastName.isEmpty()) {
104 sb.append("(").append(user.getUserId()).append(")");
105 return sb.toString();
108 private static String buildValue(String value) {
110 return StringUtils.EMPTY;
115 protected static String replaceNullNameWithEmpty(String name) {
116 if (name != null && !name.trim().contains(Constants.NULL_STRING)) {
119 return StringUtils.EMPTY;
123 //TODO implement in derived classes for ci testing
124 public List<Pair<String, String>> getQueryParams() {
125 return Collections.emptyList();
129 public final String getLogMessage() {
130 return String.format(getLogPattern(), getLogArgs());
133 private Object[] getLogArgs() {
134 return Arrays.stream(getLogMessageParams())
135 .map(AuditBaseEventFactory::buildValue)
136 .toArray(String[]::new);
139 public abstract String getLogPattern();
141 public abstract String[] getLogMessageParams();