View Javadoc
1   /**
2    * This file Copyright (c) 2016-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.config.registry;
35  
36  import lombok.AccessLevel;
37  import lombok.AllArgsConstructor;
38  import lombok.Data;
39  
40  /**
41   * Builds {@link DefinitionProvider.Problem}. It is not possible to instantiate this builder directly, but
42   * {@link DefinitionProvider.Problem Problem} provides convenience static methods for that.
43   *
44   * @see DefinitionProvider.Problem#severe()
45   * @see DefinitionProvider.Problem#major()
46   * @see DefinitionProvider.Problem#minor()
47   * @see DefinitionProvider.Problem#fromTransformationProblem(info.magnolia.transformer.TransformationProblem)
48   */
49  public class DefinitionProviderProblemBuilder {
50  
51      private DefinitionProvider.Problem.SeverityType severityType;
52  
53      private DefinitionProvider.Problem.Type type;
54  
55      private String title;
56  
57      private String details;
58  
59      private String location = "/";
60  
61      private Exception relatedException;
62  
63      DefinitionProviderProblemBuilder() {
64      }
65  
66      public DefinitionProviderProblemBuilder withSeverityType(DefinitionProvider.Problem.SeverityType severityType) {
67          this.severityType = severityType;
68          return this;
69      }
70  
71      public DefinitionProviderProblemBuilder withType(DefinitionProvider.Problem.Type type) {
72          this.type = type;
73          return this;
74      }
75  
76      public DefinitionProviderProblemBuilder withTitle(String title) {
77          this.title = title;
78          return this;
79      }
80  
81      public DefinitionProviderProblemBuilder withDetails(String details) {
82          this.details = details;
83          return this;
84      }
85  
86      public DefinitionProviderProblemBuilder withRelatedException(Exception relatedException) {
87          this.relatedException = relatedException;
88          return this;
89      }
90  
91      /**
92       * @param location the relative location of the problem, within the definition;
93       * transformation problems on definition root (e.g. failure to resolve bean class) may set location as "/".
94       */
95      public DefinitionProviderProblemBuilder withLocation(String location) {
96          this.location = location;
97          return this;
98      }
99  
100     public DefinitionProvider.Problem build() {
101         return new ProblemImpl(severityType, type, location, title, details, relatedException);
102     }
103 
104     @Data
105     @AllArgsConstructor(access = AccessLevel.PRIVATE)
106     private class ProblemImpl implements DefinitionProvider.Problem {
107         private SeverityType severityType;
108         private Type type;
109         private String location;
110         private String title;
111         private String details;
112         private Exception relatedException;
113 
114         @Override
115         public String toString() {
116             // It should be sufficient to have the problem title only in some cases, but 'null' as detailed messages might not.
117             // Therefore we then log empty problem details also in order to make log-analysis easier (instead of omitting it).
118             return String.format("\nSeverity type:   %s " +
119                                  "\nTitle:           %s: [%s] - %s " +
120                                  "\nProblem details: %s", severityType.name(), location, type.getCaption(), title, details == null ? "" : details);
121         }
122     }
123 }