View Javadoc

1   /*
2    * Copyright 2007-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.exceptions.report;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.apache.commons.lang.ObjectUtils;
23  
24  import de.smartics.exceptions.code.NumberCode;
25  import de.smartics.exceptions.core.Code;
26  import de.smartics.exceptions.report.data.ExceptionCodeReportItem;
27  import de.smartics.exceptions.report.data.StoredExceptionCodesReport;
28  import de.smartics.exceptions.report.sort.MixedCodeComparator;
29  import de.smartics.maven.exceptions.AbstractSinkReportGenerator;
30  
31  /**
32   * Simple generator that generates with a Maven sink. The codes are sorted by
33   * the major/minor/micro number code only. The component ID is not considered
34   * for sorting.
35   */
36  public class CodeSortedSinkReportGenerator extends AbstractSinkReportGenerator
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    // --- members --------------------------------------------------------------
43  
44    // ****************************** Initializer *******************************
45  
46    // ****************************** Constructors ******************************
47  
48    /**
49     * Default constructor.
50     */
51    public CodeSortedSinkReportGenerator()
52    {
53    }
54  
55    // ****************************** Inner Classes *****************************
56  
57    // ********************************* Methods ********************************
58  
59    // --- init -----------------------------------------------------------------
60  
61    // --- get&set --------------------------------------------------------------
62  
63    // --- business -------------------------------------------------------------
64  
65    @Override
66    protected List<ExceptionCodeReportItem> getItems(
67        final StoredExceptionCodesReport report)
68    {
69      final List<ExceptionCodeReportItem> items =
70          new ArrayList<ExceptionCodeReportItem>(report.getItems());
71      Collections.sort(items, new MixedCodeComparator());
72      return items;
73    }
74  
75    @Override
76    protected String getCurrentSelection(final ExceptionCodeReportItem item)
77    {
78      final Code code = item.getCode();
79  
80      if (code instanceof NumberCode)
81      {
82        final NumberCode numberCode = (NumberCode) code;
83        final String codeMajorNumber =
84            String.valueOf(numberCode.getMajorNumber());
85        return codeMajorNumber;
86      }
87  
88      return code.getComponentId();
89    }
90  
91    @Override
92    protected boolean hasSectionChanged(String currentSection,
93        final ExceptionCodeReportItem item)
94    {
95      final Code code = item.getCode();
96  
97      boolean headerChanged;
98      if (code instanceof NumberCode)
99      {
100       final NumberCode numberCode = (NumberCode) code;
101       final String codeMajorNumber =
102           String.valueOf(numberCode.getMajorNumber());
103       headerChanged = !ObjectUtils.equals(currentSection, codeMajorNumber);
104     }
105     else
106     {
107       final String componentId = code.getComponentId();
108       headerChanged = !ObjectUtils.equals(currentSection, componentId);
109     }
110 
111     return headerChanged;
112   }
113 
114   @Override
115   protected String getCodeTitle(final String headLine,
116       final ExceptionCodeReportItem item)
117   {
118     final String title;
119     if (headLine != null)
120     {
121       title = headLine;
122     }
123     else
124     {
125       final String code = item.getCode().getCode();
126       title = code;
127     }
128 
129     return title;
130   }
131 
132   // --- object basics --------------------------------------------------------
133 
134 }