Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / auditing / impl / AuditBaseEventFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.auditing.impl;
22
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;
29
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 {
35
36     private AuditingActionEnum action;
37
38     public AuditBaseEventFactory(AuditingActionEnum action) {
39         this.action = Objects.requireNonNull(action);
40     }
41
42     public AuditBaseEventFactory() {}
43
44     public AuditingActionEnum getAction() {
45         return action;
46     }
47
48     public static String buildUserNameExtended(User user) {
49         if (user == null) {
50             return null;
51         }
52
53         StringBuilder builder = new StringBuilder();
54         appendIfNotEmpty(user.getUserId(), builder);
55
56         String firstName = replaceNullNameWithEmpty(user.getFirstName());
57         String lastName = replaceNullNameWithEmpty(user.getLastName());
58
59         if (appendIfNotEmpty(firstName, builder)) {
60             appendIfNotEmpty(lastName, builder, " ");
61         }
62         else {
63             appendIfNotEmpty(lastName, builder);
64         }
65         appendIfNotEmpty(user.getEmail(), builder);
66         appendIfNotEmpty(user.getRole(), builder);
67
68         return builder.toString();
69     }
70
71     private static boolean appendIfNotEmpty(String value, StringBuilder builder) {
72         return appendIfNotEmpty(value, builder, ", ");
73     }
74
75     protected static boolean appendIfNotEmpty(String value, StringBuilder builder, String delimiter) {
76         if (StringUtils.isEmpty(value)) {
77             return false;
78         }
79         addDelimiterIfNeeded(builder, delimiter);
80         builder.append(value);
81         return true;
82     }
83
84     private static void addDelimiterIfNeeded(StringBuilder builder, String delimiter) {
85         if (builder.length() > 0) {
86             builder.append(delimiter);
87         }
88     }
89
90     protected static String buildUserName(User user) {
91         if (user == null || StringUtils.isEmpty(user.getUserId())) {
92             return StringUtils.EMPTY;
93         }
94         StringBuilder sb = new StringBuilder();
95         String firstName = replaceNullNameWithEmpty(user.getFirstName());
96         if (!firstName.isEmpty()) {
97             sb.append(firstName);
98             sb.append(" ");
99         }
100         String lastName = replaceNullNameWithEmpty(user.getLastName());
101         if (!lastName.isEmpty()) {
102             sb.append(lastName);
103         }
104         sb.append("(").append(user.getUserId()).append(")");
105         return sb.toString();
106     }
107
108     private static String buildValue(String value) {
109         if (value == null) {
110             return StringUtils.EMPTY;
111         }
112         return value;
113     }
114
115     protected static String replaceNullNameWithEmpty(String name) {
116         if (name != null && !name.trim().contains(Constants.NULL_STRING)) {
117             return name;
118         }
119         return StringUtils.EMPTY;
120     }
121
122     @Override
123     //TODO implement in derived classes for ci testing
124     public List<Pair<String, String>> getQueryParams() {
125         return Collections.emptyList();
126     }
127
128     @Override
129     public final String getLogMessage() {
130        return String.format(getLogPattern(), getLogArgs());
131     }
132
133     private Object[] getLogArgs() {
134         return Arrays.stream(getLogMessageParams())
135                 .map(AuditBaseEventFactory::buildValue)
136                 .toArray(String[]::new);
137     }
138
139     public abstract String getLogPattern();
140
141     public abstract String[] getLogMessageParams();
142
143
144
145
146 }