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  import org.apache.commons.lang.StringUtils;
24  
25  import de.smartics.exceptions.code.NumberCode;
26  import de.smartics.exceptions.core.Code;
27  import de.smartics.exceptions.report.data.ExceptionCodeReportItem;
28  import de.smartics.exceptions.report.data.StoredExceptionCodesReport;
29  import de.smartics.exceptions.report.sort.MixedComponentCodeComparator;
30  import de.smartics.maven.exceptions.AbstractSinkReportGenerator;
31  
32  /**
33   * Simple generator that generates with a Maven sink. The codes are sorted by
34   * the component ID and within that by their number code.
35   */
36  public class ComponentCodeSortedSinkReportGenerator extends
37      AbstractSinkReportGenerator
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    // --- members --------------------------------------------------------------
44  
45    // ****************************** Initializer *******************************
46  
47    // ****************************** Constructors ******************************
48  
49    /**
50     * Default constructor.
51     */
52    public ComponentCodeSortedSinkReportGenerator()
53    {
54    }
55  
56    // ****************************** Inner Classes *****************************
57  
58    // ********************************* Methods ********************************
59  
60    // --- init -----------------------------------------------------------------
61  
62    // --- get&set --------------------------------------------------------------
63  
64    // --- business -------------------------------------------------------------
65  
66    @Override
67    protected List<ExceptionCodeReportItem> getItems(
68        final StoredExceptionCodesReport report)
69    {
70      final List<ExceptionCodeReportItem> items =
71          new ArrayList<ExceptionCodeReportItem>(report.getItems());
72      Collections.sort(items, new MixedComponentCodeComparator());
73      return items;
74    }
75  
76    @Override
77    protected String getCurrentSelection(final ExceptionCodeReportItem item)
78    {
79      final Code code = item.getCode();
80  
81      if (code instanceof NumberCode)
82      {
83        final NumberCode numberCode = (NumberCode) code;
84        final String codeMajorNumber =
85            String.valueOf(numberCode.getMajorNumber());
86        return codeMajorNumber;
87      }
88  
89      return code.getComponentId();
90    }
91  
92    @Override
93    protected boolean hasSectionChanged(String currentSection,
94        final ExceptionCodeReportItem item)
95    {
96      final Code code = item.getCode();
97  
98      boolean headerChanged;
99      if (code instanceof NumberCode)
100     {
101       final NumberCode numberCode = (NumberCode) code;
102       final String codeMajorNumber =
103           String.valueOf(numberCode.getMajorNumber());
104       headerChanged = !ObjectUtils.equals(currentSection, codeMajorNumber);
105     }
106     else
107     {
108       final String componentId = code.getComponentId();
109       headerChanged = !ObjectUtils.equals(currentSection, componentId);
110     }
111 
112     return headerChanged;
113   }
114 
115   @Override
116   protected String getCodeTitle(final String headLine,
117       final ExceptionCodeReportItem item)
118   {
119     final String title;
120     if (headLine != null)
121     {
122       title = headLine;
123     }
124     else
125     {
126       final String componentId = item.getCode().getComponentId();
127       if (StringUtils.isNotBlank(componentId))
128       {
129         title = componentId;
130       }
131       else
132       {
133         final String code = item.getCode().getCode();
134         title = code;
135       }
136     }
137 
138     return title;
139   }
140 
141   // --- object basics --------------------------------------------------------
142 
143 }