1 package org.openecomp.sdc.be.auditing.impl;
3 import org.apache.commons.lang.StringUtils;
4 import org.javatuples.Pair;
5 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
6 import org.openecomp.sdc.be.model.User;
7 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
8 import org.openecomp.sdc.common.api.Constants;
10 import java.util.Collections;
11 import java.util.List;
13 public abstract class AuditBaseEventFactory implements AuditEventFactory {
15 //TODO imanzon: Check if requestId and serviceInstanceId fields are required for all tables.
16 //Currently they are included even if they ahs null value. If they should not appear then
17 //createTable code should be updated so that they need to be removed from the tables
18 private AuditingActionEnum action;
20 public AuditBaseEventFactory(AuditingActionEnum action) {
24 public AuditingActionEnum getAction() {
28 public static String buildUserNameExtended(User user) {
33 StringBuilder builder = new StringBuilder();
34 appendIfNotEmpty(user.getUserId(), builder);
36 String firstName = replaceNullNameWithEmpty(user.getFirstName());
37 String lastName = replaceNullNameWithEmpty(user.getLastName());
39 if (appendIfNotEmpty(firstName, builder)) {
40 appendIfNotEmpty(lastName, builder, " ");
43 appendIfNotEmpty(lastName, builder);
45 appendIfNotEmpty(user.getEmail(), builder);
46 appendIfNotEmpty(user.getRole(), builder);
48 return builder.toString();
51 private static boolean appendIfNotEmpty(String value, StringBuilder builder) {
52 return appendIfNotEmpty(value, builder, ", ");
55 protected static boolean appendIfNotEmpty(String value, StringBuilder builder, String delimiter) {
56 if (StringUtils.isEmpty(value)) {
59 addDelimiterIfNeeded(builder, delimiter);
60 builder.append(value);
64 private static void addDelimiterIfNeeded(StringBuilder builder, String delimiter) {
65 if (builder.length() > 0) {
66 builder.append(delimiter);
70 protected static String buildUserName(User user) {
71 if (user == null || user.getUserId() == null || user.getUserId().isEmpty()) {
72 return Constants.EMPTY_STRING;
74 StringBuilder sb = new StringBuilder();
75 String firstName = replaceNullNameWithEmpty(user.getFirstName());
76 if (!firstName.isEmpty()) {
80 String lastName = replaceNullNameWithEmpty(user.getLastName());
81 if (!lastName.isEmpty()) {
84 sb.append("(").append(user.getUserId()).append(")");
88 public static String buildValue(String value) {
90 return Constants.EMPTY_STRING;
95 protected static String replaceNullNameWithEmpty(String name) {
96 if (name != null && !name.trim().contains(Constants.NULL_STRING)) {
99 return Constants.EMPTY_STRING;
103 //TODO implement in derived classes for ci testing
104 public List<Pair<String, String>> getQueryParams() {
105 return Collections.emptyList();
109 public String getAuditingEsType() {
110 return this.action.getAuditingEsType();