View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.common.lib.to;
20  
21  import java.util.Collection;
22  import org.apache.commons.lang3.StringUtils;
23  import org.apache.commons.lang3.builder.ToStringBuilder;
24  import org.apache.syncope.common.lib.BaseBean;
25  import org.apache.syncope.common.lib.types.ResourceOperation;
26  import org.apache.syncope.common.lib.types.TraceLevel;
27  
28  public class ProvisioningReport implements BaseBean {
29  
30      private static final long serialVersionUID = 9201119472070963385L;
31  
32      public enum Status {
33  
34          SUCCESS,
35          IGNORE,
36          FAILURE
37  
38      }
39  
40      private String message;
41  
42      private Status status;
43  
44      private String anyType;
45  
46      private ResourceOperation operation;
47  
48      private String key;
49  
50      private String name;
51  
52      private String uidValue;
53  
54      public String getMessage() {
55          return message;
56      }
57  
58      public void setMessage(final String message) {
59          this.message = message;
60      }
61  
62      public String getName() {
63          return name;
64      }
65  
66      public void setName(final String name) {
67          this.name = name;
68      }
69  
70      public String getKey() {
71          return key;
72      }
73  
74      public void setKey(final String key) {
75          this.key = key;
76      }
77  
78      public Status getStatus() {
79          return status;
80      }
81  
82      public void setStatus(final Status status) {
83          this.status = status;
84      }
85  
86      public String getAnyType() {
87          return anyType;
88      }
89  
90      public void setAnyType(final String anyType) {
91          this.anyType = anyType;
92      }
93  
94      public ResourceOperation getOperation() {
95          return operation;
96      }
97  
98      public void setOperation(final ResourceOperation operation) {
99          this.operation = operation;
100     }
101 
102     public String getUidValue() {
103         return uidValue;
104     }
105 
106     public void setUidValue(final String uidValue) {
107         this.uidValue = uidValue;
108     }
109 
110     /**
111      * Human readable report string, using the given trace level.
112      *
113      * @param level trace level
114      * @return String for certain levels, null for level NONE
115      */
116     public String getReportString(final TraceLevel level) {
117         if (level == TraceLevel.SUMMARY) {
118             // No per entry log in this case.
119             return null;
120         } else if (level == TraceLevel.FAILURES && status == Status.FAILURE) {
121             // only report failures
122             return String.format("Failed %s (key/name): %s/%s with message: %s", operation, key, name, message);
123         } else {
124             // All
125             return String.format("%s %s (key/name): %s/%s %s", operation, status, key, name,
126                     StringUtils.isBlank(message)
127                     ? ""
128                     : "with message: " + message);
129         }
130     }
131 
132     /**
133      * Helper method to invoke logging per provisioning result, for the given trace level.
134      *
135      * @param results provisioning results
136      * @param level trace level
137      * @return report as string
138      */
139     public static String generate(final Collection<ProvisioningReport> results, final TraceLevel level) {
140         StringBuilder sb = new StringBuilder();
141         results.forEach(result -> sb.append(result.getReportString(level)).append('\n'));
142         return sb.toString();
143     }
144 
145     @Override
146     public String toString() {
147         return new ToStringBuilder(this).
148                 append(message).
149                 append(status).
150                 append(anyType).
151                 append(operation).
152                 append(key).
153                 append(name).
154                 append(uidValue).
155                 build();
156     }
157 }