View Javadoc
1   package org.nuiton.eugene.models.extension.tagvalue;
2   
3   /*
4    * #%L
5    * EUGene :: EUGene
6    * %%
7    * Copyright (C) 2004 - 2015 CodeLutin
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Joiner;
26  import org.nuiton.eugene.models.object.ObjectModelClass;
27  import org.nuiton.eugene.models.object.ObjectModelPackage;
28  
29  import java.util.LinkedHashSet;
30  import java.util.Set;
31  
32  /**
33   * When a missing tag value is detected.
34   *
35   * Created on 15/08/15.
36   *
37   * @author Tony Chemit - chemit@codelutin.com
38   * @since 3.0
39   */
40  public class MissingTagValueException extends RuntimeException {
41  
42      private static final long serialVersionUID = 1L;
43  
44      protected final String tagValueName;
45  
46      protected final String tagValueValue;
47  
48      protected final String prefixMessage;
49  
50      protected final Set<String> stack;
51  
52      public MissingTagValueException(String tagValueName, String tagValueValue, String prefixMessage, ObjectModelPackage aPackage, ObjectModelClass aClass) {
53          this.tagValueName = tagValueName;
54          this.tagValueValue = tagValueValue;
55          this.prefixMessage = prefixMessage;
56          this.stack = getUsageStack(tagValueName, tagValueValue, aPackage, aClass);
57      }
58  
59      @Override
60      public String toString() {
61  
62          return "\n\n" + prefixMessage
63                  + "\n=========================================================================================="
64                  + "\n" + Joiner.on("\n").join(stack)
65                  + "\n==========================================================================================";
66  
67      }
68  
69      /**
70       * Build the stack of usage of the given tag value.
71       *
72       * Order of usage is : model, packages (from root to final package), then class.
73       *
74       * @param tagValueName  tag value name
75       * @param tagValueValue tag value
76       * @param aPackage      package used
77       * @param aClass        class used
78       * @return the orderer set of stack usage.
79       */
80      protected LinkedHashSet<String> getUsageStack(String tagValueName, String tagValueValue, ObjectModelPackage aPackage, ObjectModelClass aClass) {
81          String suffix = ".tagvalue." + tagValueName + "=" + tagValueValue;
82          LinkedHashSet<String> stack = new LinkedHashSet<>();
83  
84          stack.add("model" + suffix);
85  
86          addPackageTagValue(aPackage, suffix, stack);
87          stack.add(aClass.getQualifiedName() + ".class" + suffix);
88  
89          return stack;
90      }
91  
92      protected void addPackageTagValue(ObjectModelPackage aPackage, String suffix, Set<String> stack) {
93  
94          if (aPackage.getParentPackage() != null) {
95              addPackageTagValue(aPackage.getParentPackage(), suffix, stack);
96          }
97          stack.add("package." + aPackage.getName() + suffix);
98  
99      }
100 
101 }