source: trunk/autoquest-core-usability-evaluation/src/main/java/de/ugoe/cs/autoquest/usability/result/UsabilityProblemDescriptionResolver.java @ 1217

Last change on this file since 1217 was 1217, checked in by adeicke, 11 years ago
  • Added proper formating and JavaDoc?.
  • Several renaming refactorings.
  • Property svn:mime-type set to text/plain
File size: 7.7 KB
RevLine 
[1138]1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.usability.result;
16
17import java.io.File;
18import java.io.IOException;
19import java.net.URISyntaxException;
20import java.util.EnumMap;
21import java.util.Map;
22import java.util.Map.Entry;
23
24import jodd.props.Props;
25
26import com.google.common.base.Optional;
27import com.google.common.base.Predicate;
28import com.google.common.collect.Iterables;
29import com.google.common.collect.Maps;
30
31import de.ugoe.cs.util.console.Console;
32
33/**
34 * <p>
[1217]35 * Helper class, which creates a {@link UsabilityProblemDescription} for a usability rule.
[1138]36 * </p>
37 *
38 * @author Alexander Deicke
39 */
[1217]40public class UsabilityProblemDescriptionResolver {
[1138]41
[1217]42    /**
43     * <p>
44     * .properties file, which contains all details concerning a usability defect.
45     * </p>
46     */
[1138]47    private final String defectDescriptionFile = "defects.props";
48
49    /**
50     * <p>
[1217]51     * Creates a defect description for a {@link UsabilityRule}.
[1138]52     * </p>
53     *
54     * @param name
[1217]55     *            of usability rule
56     * @return defect description for usability rule
[1138]57     */
[1217]58    public UsabilityProblemDescription descriptionFor(String usabilityRuleName) {
[1138]59        Props allProperties = initProperties();
60        Map<String, String> usabilityRuleProperties =
61            allUsabilityRuleProperties(allProperties, usabilityRuleName);
62        return createUsabilityDefect(usabilityRuleProperties);
63    }
64
65    /**
66     * <p>
[1217]67     * Initializes the properties, which are used to create the defect description.
[1138]68     * </p>
69     *
[1217]70     * @return properties needed to create defect description
[1138]71     */
72    private Props initProperties() {
73        Optional<File> defectDescriptionFile = getDefectDescriptionFile();
74        Props props = new Props();
75        props.setEscapeNewLineValue("\n");
76        props.setValueTrimLeft(true);
77        if (defectDescriptionFile.isPresent()) {
78            loadProperties(defectDescriptionFile, props);
79        }
80        return props;
81    }
82
83    /**
84     * <p>
[1217]85     * Loads the .properties file from the system.
[1138]86     * </p>
87     *
[1217]88     * @return iff present, {@link File} object of the .properties file
[1138]89     */
90    private Optional<File> getDefectDescriptionFile() {
91        try {
92            return Optional.fromNullable(new File(ClassLoader
93                .getSystemResource(defectDescriptionFile).toURI()));
94        }
95        catch (URISyntaxException e) {
96            Console.printerr("Error while loading defect description file.");
97            Console.logException(e);
98            return Optional.absent();
99        }
100    }
101
[1217]102    /**
103     *
104     * <p>
105     * Loads the values from the .properties.
106     * </p>
107     *
108     * @param defectDescriptionFile
109     *            .properties file
110     * @param props
111     *            object, which stores the loaded values
112     */
[1138]113    private void loadProperties(Optional<File> defectDescriptionFile, Props props) {
114        try {
115            props.load(defectDescriptionFile.get());
116        }
117        catch (IOException e) {
118            Console.logException(e);
119        }
120    }
121
122    /**
123     * <p>
[1217]124     * Returns all existing properties for a given usability rule.
[1138]125     * </p>
126     *
[1217]127     * @param allProperties
128     *            all properties available
129     * @param usabilityRuleName
130     *            name of usability rule
131     * @return all properties of certain usability rule
[1138]132     */
133    private Map<String, String> allUsabilityRuleProperties(Props allProperties,
[1217]134                                                           String usabilityRuleName)
[1138]135    {
136        Map<String, String> usabilityRuleProperties = Maps.newHashMap();
137        allProperties.extractSubProps(usabilityRuleProperties, usabilityRuleName + ".*");
138        return usabilityRuleProperties;
139    }
140
141    /**
142     * <p>
[1217]143     * Creates the usability defect.
[1138]144     * </p>
145     *
146     * @param usabilityRuleProperties
[1217]147     *            all properties needed for creation.
148     * @return defect description for a usability rule
[1138]149     */
[1217]150    private UsabilityProblemDescription createUsabilityDefect(Map<String, String> usabilityRuleProperties)
151    {
[1138]152        String description =
[1217]153            Iterables.getOnlyElement(Maps
154                .filterKeys(usabilityRuleProperties, descriptionProperty()).values());
155        EnumMap<UsabilityProblemSeverityLevel, Double> severity =
[1138]156            getSeverityMap(usabilityRuleProperties);
[1217]157        return new UsabilityProblemDescription(description, severity);
[1138]158    }
159
160    /**
161     * <p>
[1217]162     * Gets the description property.
[1138]163     * </p>
164     *
[1217]165     * @return description property
[1138]166     */
167    private Predicate<String> descriptionProperty() {
168        return new Predicate<String>() {
169
170            public boolean apply(String key) {
171                return key.endsWith("description");
172            }
173
174        };
175    }
[1217]176
[1138]177    /**
178     * <p>
[1217]179     * Creates severity level map for defect description, by matching all entried from .properties
180     * file to corresponding {@link UsabilityProblemSeverityLevel}.
[1138]181     * </p>
182     *
183     * @param usabilityRuleProperties
[1217]184     *            all properties of certain usability rule
185     * @return assignment of {@link UsabilityProblemSeverityLevel} and corresponding threshold
[1138]186     */
[1217]187    private EnumMap<UsabilityProblemSeverityLevel, Double> getSeverityMap(Map<String, String> usabilityRuleProperties)
[1138]188    {
[1217]189        EnumMap<UsabilityProblemSeverityLevel, Double> severityMap =
190            Maps.newEnumMap(UsabilityProblemSeverityLevel.class);
[1138]191        Map<String, String> allSeverityProperties =
192            Maps.filterEntries(usabilityRuleProperties, allSeverityProperties());
193        for (Entry<String, String> severityProperty : allSeverityProperties.entrySet()) {
[1217]194            UsabilityProblemSeverityLevel severityLevel =
[1138]195                getSeverityLevel(severityProperty.getKey());
196            Double rule = Double.valueOf(severityProperty.getValue());
197            severityMap.put(severityLevel, rule);
198        }
199        return severityMap;
200    }
201
202    /**
203     * <p>
[1217]204     * Matches severity level from .properties file against {@link UsabilityProblemSeverityLevel}.
[1138]205     * </p>
206     *
[1217]207     * @param severityProperty
208     *            severity level from .properties file
209     * @return matching {@link UsabilityProblemSeverityLevel}
[1138]210     */
[1217]211    private UsabilityProblemSeverityLevel getSeverityLevel(String severityProperty) {
[1138]212        int startSeverityLevel = severityProperty.lastIndexOf(".") + 1;
[1217]213        String severityLevelIdentifier =
[1138]214            severityProperty.substring(startSeverityLevel).toUpperCase();
[1217]215        return UsabilityProblemSeverityLevel.valueOf(severityLevelIdentifier);
[1138]216    }
217
218    /**
219     * <p>
[1217]220     * Gets the severity level properties.
[1138]221     * </p>
222     *
[1217]223     * @return severity level
[1138]224     */
225    private Predicate<Entry<String, String>> allSeverityProperties() {
226        return new Predicate<Map.Entry<String, String>>() {
227
228            public boolean apply(Map.Entry<String, String> entry) {
229                return entry.getKey().contains("severity");
230            }
231
232        };
233    }
234
235}
Note: See TracBrowser for help on using the repository browser.