Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / auditing / impl / AuditBaseEventFactory.java
1 package org.openecomp.sdc.be.auditing.impl;
2
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;
9
10 import java.util.Collections;
11 import java.util.List;
12
13 public abstract class AuditBaseEventFactory implements AuditEventFactory {
14
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;
19
20     public AuditBaseEventFactory(AuditingActionEnum action) {
21         this.action = action;
22     }
23
24     public AuditingActionEnum getAction() {
25         return action;
26     }
27
28     public static String buildUserNameExtended(User user) {
29         if (user == null) {
30             return null;
31         }
32
33         StringBuilder builder = new StringBuilder();
34         appendIfNotEmpty(user.getUserId(), builder);
35
36         String firstName = replaceNullNameWithEmpty(user.getFirstName());
37         String lastName = replaceNullNameWithEmpty(user.getLastName());
38
39         if (appendIfNotEmpty(firstName, builder)) {
40             appendIfNotEmpty(lastName, builder, " ");
41         }
42         else {
43             appendIfNotEmpty(lastName, builder);
44         }
45         appendIfNotEmpty(user.getEmail(), builder);
46         appendIfNotEmpty(user.getRole(), builder);
47
48         return builder.toString();
49     }
50
51     private static boolean appendIfNotEmpty(String value, StringBuilder builder) {
52         return appendIfNotEmpty(value, builder, ", ");
53     }
54
55     protected static boolean appendIfNotEmpty(String value, StringBuilder builder, String delimiter) {
56         if (StringUtils.isEmpty(value)) {
57             return false;
58         }
59         addDelimiterIfNeeded(builder, delimiter);
60         builder.append(value);
61         return true;
62     }
63
64     private static void addDelimiterIfNeeded(StringBuilder builder, String delimiter) {
65         if (builder.length() > 0) {
66             builder.append(delimiter);
67         }
68     }
69
70     protected static String buildUserName(User user) {
71         if (user == null || user.getUserId() == null || user.getUserId().isEmpty()) {
72             return Constants.EMPTY_STRING;
73         }
74         StringBuilder sb = new StringBuilder();
75         String firstName = replaceNullNameWithEmpty(user.getFirstName());
76         if (!firstName.isEmpty()) {
77             sb.append(firstName);
78             sb.append(" ");
79         }
80         String lastName = replaceNullNameWithEmpty(user.getLastName());
81         if (!lastName.isEmpty()) {
82             sb.append(lastName);
83         }
84         sb.append("(").append(user.getUserId()).append(")");
85         return sb.toString();
86     }
87
88     public static String buildValue(String value) {
89         if (value == null) {
90             return Constants.EMPTY_STRING;
91         }
92         return value;
93     }
94
95     protected static String replaceNullNameWithEmpty(String name) {
96         if (name != null && !name.trim().contains(Constants.NULL_STRING)) {
97             return name;
98         }
99         return Constants.EMPTY_STRING;
100     }
101
102     @Override
103     //TODO implement in derived classes for ci testing
104     public List<Pair<String, String>> getQueryParams() {
105         return Collections.emptyList();
106     }
107
108     @Override
109     public String getAuditingEsType() {
110         return this.action.getAuditingEsType();
111     }
112
113
114 }