Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 8a60bc88

Přidáno uživatelem stepanekp před asi 3 roky(ů)

Structure of the antipattern service and repository edited

Zobrazit rozdíly:

src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/detecting/detectors/AntiPatternDetector.java
13 13
 * you should implements all method correctly, see example TooLongSprintDetector.java
14 14
 */
15 15
public interface AntiPatternDetector {
16
    /**
17
     * Method for setting anti-pattern to the detector
18
     *
19
     * @param antiPattern ap to set
20
     */
21
    void setAntiPattern(AntiPattern antiPattern);
22

  
23
    /**
24
     * Method for getting name of the json file with anti-pattern details
25
     *
26
     * @return json file name
27
     */
28
    String getJsonFileName();
16 29

  
17 30
    /**
18 31
     * Method for getting AP like model class for manipulating on UI.
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/detecting/detectors/BusinessAsUsualDetectorImpl.java
15 15

  
16 16
    private final Logger LOGGER = LoggerFactory.getLogger(BusinessAsUsualDetectorImpl.class);
17 17

  
18
    private AntiPatternService antiPatternService = new AntiPatternServiceImpl();
19

  
20 18
    public final String configJsonFileName = "BusinessAsUsual.json";
21 19

  
22
    private AntiPattern antiPattern = antiPatternService.getAntiPatternFromJsonFile(configJsonFileName);
20
    private AntiPattern antiPattern;
23 21

  
24 22
    private final List<String> SQL_FILE_NAMES = Arrays.asList(
25 23
            "set_project_id.sql",
......
38 36
        return Arrays.asList(((String) antiPattern.getThresholds().get("searchSubstringsWithRetrospective").getValue()).split("\\|\\|"));
39 37
    }
40 38

  
39
    @Override
40
    public String getJsonFileName(){
41
        return this.configJsonFileName;
42
    }
43

  
44
    @Override
45
    public void setAntiPattern(AntiPattern antiPattern) {
46
        this.antiPattern = antiPattern;
47
    }
48

  
41 49
    @Override
42 50
    public AntiPattern getAntiPatternModel() {
43 51
        return this.antiPattern;
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/detecting/detectors/LongOrNonExistentFeedbackLoopsDetectorImpl.java
18 18

  
19 19
    private final Logger LOGGER = LoggerFactory.getLogger(LongOrNonExistentFeedbackLoopsDetectorImpl.class);
20 20

  
21
    private AntiPatternService antiPatternService = new AntiPatternServiceImpl();
22

  
23 21
    public final String configJsonFileName = "LongOrNonExistentFeedbackLoops.json";
24 22

  
25
    private AntiPattern antiPattern = antiPatternService.getAntiPatternFromJsonFile(configJsonFileName);
23
    private AntiPattern antiPattern;
26 24

  
27 25
    private final List<String> SQL_FILE_NAMES = Arrays.asList(
28 26
            "set_project_id.sql",
......
49 47
        return Arrays.asList(((String) antiPattern.getThresholds().get("searchSubstringsWithFeedbackLoop").getValue()).split("\\|\\|"));
50 48
    }
51 49

  
50
    @Override
51
    public String getJsonFileName(){
52
        return this.configJsonFileName;
53
    }
54

  
55
    @Override
56
    public void setAntiPattern(AntiPattern antiPattern) {
57
        this.antiPattern = antiPattern;
58
    }
59

  
52 60
    @Override
53 61
    public AntiPattern getAntiPatternModel() {
54 62
        return this.antiPattern;
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/detecting/detectors/NinetyNinetyRuleDetectorImpl.java
15 15

  
16 16
    private final Logger LOGGER = LoggerFactory.getLogger(BusinessAsUsualDetectorImpl.class);
17 17

  
18
    private AntiPatternService antiPatternService = new AntiPatternServiceImpl();
19

  
20 18
    public final String configJsonFileName = "NinetyNinetyRule.json";
21 19

  
22
    private AntiPattern antiPattern = antiPatternService.getAntiPatternFromJsonFile(configJsonFileName);
20
    private AntiPattern antiPattern;
23 21

  
24 22
    private final List<String> SQL_FILE_NAMES = Arrays.asList(
25 23
            "set_project_id.sql",
......
36 34
        return ((PositiveInteger) antiPattern.getThresholds().get("maxBadDivisionLimit").getValue()).intValue();
37 35
    }
38 36

  
37
    @Override
38
    public String getJsonFileName(){
39
        return this.configJsonFileName;
40
    }
41

  
42
    @Override
43
    public void setAntiPattern(AntiPattern antiPattern) {
44
        this.antiPattern = antiPattern;
45
    }
46

  
39 47
    @Override
40 48
    public AntiPattern getAntiPatternModel() {
41 49
        return this.antiPattern;
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/detecting/detectors/RoadToNowhereDetectorImpl.java
19 19

  
20 20
    private final Logger LOGGER = LoggerFactory.getLogger(SpecifyNothingDetectorImpl.class);
21 21

  
22
    private AntiPatternService antiPatternService = new AntiPatternServiceImpl();
23

  
24 22
    public final String configJsonFileName = "RoadToNowhere.json";
25 23

  
26
    private AntiPattern antiPattern = antiPatternService.getAntiPatternFromJsonFile(configJsonFileName);
24
    private AntiPattern antiPattern;
27 25

  
28 26
    private final List<String> SQL_FILE_NAMES = Arrays.asList(
29 27
            "set_project_id.sql",
......
48 46
        return Arrays.asList(((String) antiPattern.getThresholds().get("searchSubstringsWithProjectPlan").getValue()).split("\\|\\|"));
49 47
    }
50 48

  
49
    @Override
50
    public String getJsonFileName(){
51
        return this.configJsonFileName;
52
    }
53

  
54
    @Override
55
    public void setAntiPattern(AntiPattern antiPattern) {
56
        this.antiPattern = antiPattern;
57
    }
58

  
51 59
    @Override
52 60
    public AntiPattern getAntiPatternModel() {
53 61
        return this.antiPattern;
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/detecting/detectors/SpecifyNothingDetectorImpl.java
19 19

  
20 20
    private final Logger LOGGER = LoggerFactory.getLogger(SpecifyNothingDetectorImpl.class);
21 21

  
22
    private AntiPatternService antiPatternService = new AntiPatternServiceImpl();
23

  
24 22
    public final String configJsonFileName = "SpecifyNothing.json";
25 23

  
26
    private AntiPattern antiPattern = antiPatternService.getAntiPatternFromJsonFile(configJsonFileName);
24
    private AntiPattern antiPattern;
27 25

  
28 26
    private final List<String> SQL_FILE_NAMES = Arrays.asList(
29 27
            "set_project_id.sql",
......
51 49
        return Arrays.asList(((String) antiPattern.getThresholds().get("searchSubstringsWithProjectSpecification").getValue()).split("\\|\\|"));
52 50
    }
53 51

  
52
    @Override
53
    public String getJsonFileName(){
54
        return this.configJsonFileName;
55
    }
56

  
57
    @Override
58
    public void setAntiPattern(AntiPattern antiPattern) {
59
        this.antiPattern = antiPattern;
60
    }
61

  
54 62
    @Override
55 63
    public AntiPattern getAntiPatternModel() {
56 64
        return this.antiPattern;
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/detecting/detectors/TooLongSprintDetectorImpl.java
14 14

  
15 15
    private final Logger LOGGER = LoggerFactory.getLogger(TooLongSprintDetectorImpl.class);
16 16

  
17
    private AntiPatternService antiPatternService = new AntiPatternServiceImpl();
18

  
19 17
    public final String configJsonFileName = "TooLongSprint.json";
20 18

  
21
    private AntiPattern antiPattern = antiPatternService.getAntiPatternFromJsonFile(configJsonFileName);
19
    private AntiPattern antiPattern;
22 20

  
23 21
    private final List<String> SQL_FILE_NAMES = Arrays.asList(
24 22
            "set_project_id.sql",
......
29 27
    // sql queries loaded from sql file
30 28
    private List<String> sqlQueries;
31 29

  
30
    @Override
31
    public String getJsonFileName(){
32
        return this.configJsonFileName;
33
    }
34

  
35
    @Override
36
    public void setAntiPattern(AntiPattern antiPattern) {
37
        this.antiPattern = antiPattern;
38
    }
39

  
32 40
    @Override
33 41
    public AntiPattern getAntiPatternModel() {
34 42
        return this.antiPattern;
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/detecting/detectors/VaryingSprintLengthDetectorImpl.java
18 18

  
19 19
    private final Logger LOGGER = LoggerFactory.getLogger(VaryingSprintLengthDetectorImpl.class);
20 20

  
21
    private AntiPatternService antiPatternService = new AntiPatternServiceImpl();
22

  
23 21
    public final String configJsonFileName = "VaryingSprintLength.json";
24 22

  
25
    private AntiPattern antiPattern = antiPatternService.getAntiPatternFromJsonFile(configJsonFileName);
23
    private AntiPattern antiPattern;
26 24

  
27 25
    private final List<String> SQL_FILE_NAMES = Arrays.asList(
28 26
            "set_project_id.sql",
......
41 39
        return ((PositiveInteger) this.antiPattern.getThresholds().get("maxIterationChanged").getValue()).intValue();
42 40
    }
43 41

  
42
    @Override
43
    public String getJsonFileName(){
44
        return this.configJsonFileName;
45
    }
46

  
47
    @Override
48
    public void setAntiPattern(AntiPattern antiPattern) {
49
        this.antiPattern = antiPattern;
50
    }
51

  
44 52
    @Override
45 53
    public AntiPattern getAntiPatternModel() {
46 54
        return this.antiPattern;
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/repository/AntiPatternRepository.java
1 1
package cz.zcu.fav.kiv.antipatterndetectionapp.repository;
2 2

  
3
import com.fasterxml.jackson.databind.JsonNode;
3 4
import cz.zcu.fav.kiv.antipatterndetectionapp.detecting.detectors.AntiPatternDetector;
5
import cz.zcu.fav.kiv.antipatterndetectionapp.model.AntiPattern;
6
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Threshold;
7
import cz.zcu.fav.kiv.antipatterndetectionapp.model.types.Percentage;
8
import cz.zcu.fav.kiv.antipatterndetectionapp.model.types.PositiveFloat;
9
import cz.zcu.fav.kiv.antipatterndetectionapp.model.types.PositiveInteger;
10
import cz.zcu.fav.kiv.antipatterndetectionapp.utils.JsonParser;
4 11
import org.reflections.Reflections;
5 12
import org.slf4j.Logger;
6 13
import org.slf4j.LoggerFactory;
......
22 29
public class AntiPatternRepository implements ServletContextAware {
23 30

  
24 31
    private static final String QUERY_DIR = "/queries/";
32
    private static final String AP_DIR = "/antipatterns/";
25 33
    private final Logger LOGGER = LoggerFactory.getLogger(AntiPatternRepository.class);
26 34
    private ServletContext servletContext;
27
    private Map<Long, AntiPatternDetector> antiPatternDetectors = init();
35
    private Map<Long, AntiPatternDetector> antiPatternDetectors;
36

  
37
    /**
38
     * This method load all queries from files for each AP.
39
     *
40
     * @param servletContext servlet context
41
     */
42
    @Override
43
    public void setServletContext(ServletContext servletContext) {
44
        this.servletContext = servletContext;
45

  
46
        // initialize all detectors
47
        this.antiPatternDetectors = initDetectors();
48

  
49
        // initialize sql queries
50
        initSqlQueries();
51
    }
52

  
28 53

  
29 54
    /**
30 55
     * The method that loads all available detectors that implement
......
34 59
     *
35 60
     * @return map of all AP
36 61
     */
37
    private Map<Long, AntiPatternDetector> init() {
62
    private Map<Long, AntiPatternDetector> initDetectors() {
38 63
        LOGGER.info("-------START CREATING DETECTORS WITH REFLECTION-------");
64

  
39 65
        Map<Long, AntiPatternDetector> antiPatterns = new HashMap<>();
40 66
        try {
41 67
            Reflections reflections = new Reflections("cz.zcu.fav.kiv.antipatterndetectionapp");
42 68
            Set<Class<? extends AntiPatternDetector>> subTypes = reflections.getSubTypesOf(AntiPatternDetector.class);
43 69
            for (Class<? extends AntiPatternDetector> subType : subTypes) {
44 70
                AntiPatternDetector antiPatternDetector = subType.getDeclaredConstructor().newInstance();
71

  
72
                // loading anti-pattern from json file and linking it to the detector file
73
                antiPatternDetector.setAntiPattern(getAntiPatternFromJsonFile(antiPatternDetector.getJsonFileName()));
74

  
45 75
                antiPatterns.putIfAbsent(antiPatternDetector.getAntiPatternModel().getId(), antiPatternDetector);
46 76
                LOGGER.info("Creating detector " + antiPatternDetector.getAntiPatternModel().getPrintName());
47 77
            }
......
49 79
            e.printStackTrace();
50 80
            LOGGER.error("Cannot get all detectors with reflection", e);
51 81
        }
52
        LOGGER.info("-------FINISHED CREATING DETECTORS WITH REFLECTION-------");
53
        return antiPatterns;
54
    }
55 82

  
56
    /**
57
     * Get all loaded AP from global value.
58
     *
59
     * @return list of AP
60
     */
61
    public List<AntiPatternDetector> getAllAntiPatterns() {
62
        return new ArrayList<>(this.antiPatternDetectors.values());
63
    }
83
        LOGGER.info("-------FINISHED CREATING DETECTORS WITH REFLECTION-------");
64 84

  
65
    /**
66
     * Get AP by given ID.
67
     *
68
     * @param id AP ID
69
     * @return AP
70
     */
71
    public AntiPatternDetector getAntiPatternById(Long id) {
72
        return this.antiPatternDetectors.getOrDefault(id, null);
85
        return antiPatterns;
73 86
    }
74 87

  
75 88
    /**
76
     * This method load all queries from files for each AP.
77 89
     *
78
     * @param servletContext servlet context
79 90
     */
80
    @Override
81
    public void setServletContext(ServletContext servletContext) {
82
        this.servletContext = servletContext;
91
    private void initSqlQueries(){
83 92
        LOGGER.info("-------START READING SQL FROM FILES-------");
93

  
84 94
        for (AntiPatternDetector antiPatternDetector : getAllAntiPatterns()) {
85 95
            LOGGER.info("Reading sql files for AP " + antiPatternDetector.getAntiPatternModel().getPrintName());
86 96
            antiPatternDetector.setSqlQueries(loadSqlFile(antiPatternDetector.getSqlFileNames()));
87

  
88 97
        }
98

  
89 99
        LOGGER.info("-------FINISHED READING SQL FROM FILES-------");
90 100
    }
91 101

  
102

  
92 103
    /**
93 104
     * Method for loading list of sql files from given list of files
94 105
     *
......
100 111

  
101 112
        // walk through all sql filenames and load all sql queries
102 113
        for (String fileName : fileNames) {
103
            LOGGER.info("Reading sql query from file name " + fileName);
114

  
115
            LOGGER.info("Reading sql query from file " + fileName);
116

  
104 117
            try {
105 118
                URL test = servletContext.getResource(QUERY_DIR + fileName);
106 119
                BufferedReader read = new BufferedReader(
......
113 126
                }
114 127
                read.close();
115 128
            } catch (IOException e) {
116
                LOGGER.warn("Cannot read sql from file " + fileName, e);
129
                LOGGER.error("Cannot read sql from file " + fileName);
117 130
                return queries;
118 131
            }
119 132
        }
120 133
        return queries;
121 134
    }
135

  
136
    /**
137
     *
138
     * @param jsonFileName
139
     * @return
140
     */
141
    public AntiPattern getAntiPatternFromJsonFile(String jsonFileName){
142
        String json = "";   // json configuration file
143
        JsonNode node = null;
144

  
145
        LOGGER.info("Reading anti-pattern from json file " + jsonFileName);
146

  
147
        try {
148
            URL url = servletContext.getResource(AP_DIR + jsonFileName);
149
            BufferedReader read = new BufferedReader(
150
                    new InputStreamReader(url.openStream()));
151

  
152
            String line;
153
            while ((line = read.readLine()) != null) {
154
                json += line;
155
            }
156
            node = JsonParser.parse(json);
157
        } catch (IOException e) {
158
            LOGGER.warn("Cannot read anti-pattern from json file " + jsonFileName);
159
            return null;
160
        }
161

  
162
        Long APid = Long.parseLong(node.get("id").asText());
163
        String APPrintName = node.get("printName").asText();
164
        String APName = node.get("name").asText();
165
        String APDescription = node.get("description").asText();
166
        String APCatalogueFileName = node.get("catalogueFileName") != null ? node.get("catalogueFileName").asText() : null;
167

  
168
        Map<String, Threshold> APMap = new HashMap<>();
169

  
170
        JsonNode array = node.get("thresholds");
171
        Threshold<?> tmpThreshold = null;
172

  
173
        for(int i = 0; i < array.size(); i++){
174
            JsonNode tmpNode = array.get(i);
175

  
176
            String thresholdName = tmpNode.get("thresholdName").asText();
177
            String thresholdType = tmpNode.get("thresholdType").asText();
178

  
179
            JsonNode thresholdNode = tmpNode.get("threshold");
180

  
181
            String tThresholdName = thresholdNode.get("thresholdName").asText();
182
            String tThresholdPrintName = thresholdNode.get("thresholdPrintName").asText();
183
            String tThresholdDescription = thresholdNode.get("thresholdDescription").asText();
184
            String tThresholdErrorMess = thresholdNode.get("thresholdErrorMess").asText();
185
            String tThresholdValue = thresholdNode.get("thresholdValue").asText();
186

  
187
            tmpThreshold = getThreshold(thresholdType, tThresholdName, tThresholdPrintName, tThresholdDescription, tThresholdErrorMess, tThresholdValue);
188

  
189
            APMap.put(thresholdName, tmpThreshold);
190
        }
191

  
192
        AntiPattern newAP = new AntiPattern(APid, APPrintName, APName, APDescription, APMap, APCatalogueFileName);
193

  
194
        return newAP;
195
    }
196

  
197
    /**
198
     * Get all loaded AP from global value.
199
     *
200
     * @return list of AP
201
     */
202
    public List<AntiPatternDetector> getAllAntiPatterns() {
203
        return new ArrayList<>(this.antiPatternDetectors.values());
204
    }
205

  
206
    /**
207
     * Get AP by given ID.
208
     *
209
     * @param id AP ID
210
     * @return AP
211
     */
212
    public AntiPatternDetector getAntiPatternById(Long id) {
213
        return this.antiPatternDetectors.getOrDefault(id, null);
214
    }
215

  
216
    /**
217
     *
218
     * @param thresholdType
219
     * @param name
220
     * @param printName
221
     * @param description
222
     * @param errorMessage
223
     * @param value
224
     * @return
225
     */
226
    private Threshold<?> getThreshold(String thresholdType, String name, String printName, String description, String errorMessage, String value){
227

  
228
        if(thresholdType.equals("Percentage")){
229
            return new Threshold<>(name, printName, description, errorMessage, new Percentage(Float.parseFloat(value)));
230
        }
231
        else if(thresholdType.equals("PositiveFloat")){
232
            return new Threshold<>(name, printName, description, errorMessage, new PositiveFloat(Float.parseFloat(value)));
233
        }
234
        else if(thresholdType.equals("PositiveInteger")){
235
            return new Threshold<>(name, printName, description, errorMessage, new PositiveInteger(Integer.parseInt(value)));
236
        }
237
        else if(thresholdType.equals("String")){
238
            return new Threshold<>(name, printName, description, errorMessage, value);
239
        }
240

  
241
        return null;
242
    }
243

  
122 244
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/AntiPatternService.java
38 38

  
39 39
    AntiPattern setErrorMessages(AntiPattern antiPattern, List<String> wrongParameters);
40 40

  
41
    AntiPattern getAntiPatternFromJsonFile(String fileName);
42

  
43 41
    String getDescriptionFromCatalogue(long id);
44 42
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/AntiPatternServiceImpl.java
205 205
        return setErrorMessages(antiPatterns, wrongParameters).get(0);
206 206
    }
207 207

  
208
    @Override
209
    public AntiPattern getAntiPatternFromJsonFile(String jsonFileName){
210
        String json = "";   // json configuration file
211
        JsonNode node = null;
212
        try {
213
            json = new String(Files.readAllBytes(Paths.get(new FileSystemResource("").getFile().getAbsolutePath() + "\\src\\main\\webapp\\antipatterns\\" + jsonFileName)));
214
            node = JsonParser.parse(json);
215
        } catch (IOException e) {
216
            e.printStackTrace();
217
        }
218

  
219
        Long APid = Long.parseLong(node.get("id").asText());
220
        String APPrintName = node.get("printName").asText();
221
        String APName = node.get("name").asText();
222
        String APDescription = node.get("description").asText();
223
        String APCatalogueFileName = node.get("catalogueFileName") != null ? node.get("catalogueFileName").asText() : null;
224

  
225
        Map<String, Threshold> APMap = new HashMap<>();
226

  
227
        JsonNode array = node.get("thresholds");
228
        Threshold<?> tmpThreshold = null;
229

  
230
        for(int i = 0; i < array.size(); i++){
231
            JsonNode tmpNode = array.get(i);
232

  
233
            String thresholdName = tmpNode.get("thresholdName").asText();
234
            String thresholdType = tmpNode.get("thresholdType").asText();
235

  
236
            JsonNode thresholdNode = tmpNode.get("threshold");
237

  
238
            String tThresholdName = thresholdNode.get("thresholdName").asText();
239
            String tThresholdPrintName = thresholdNode.get("thresholdPrintName").asText();
240
            String tThresholdDescription = thresholdNode.get("thresholdDescription").asText();
241
            String tThresholdErrorMess = thresholdNode.get("thresholdErrorMess").asText();
242
            String tThresholdValue = thresholdNode.get("thresholdValue").asText();
243

  
244
            tmpThreshold = getThreshold(thresholdType, tThresholdName, tThresholdPrintName, tThresholdDescription, tThresholdErrorMess, tThresholdValue);
245

  
246
            APMap.put(thresholdName, tmpThreshold);
247
        }
248

  
249
        AntiPattern newAP = new AntiPattern(APid, APPrintName, APName, APDescription, APMap, APCatalogueFileName);
250

  
251
        return newAP;
252
    }
253

  
254
    private Threshold<?> getThreshold(String thresholdType, String name, String printName, String description, String errorMessage, String value){
255

  
256
        if(thresholdType.equals("Percentage")){
257
            return new Threshold<>(name, printName, description, errorMessage, new Percentage(Float.parseFloat(value)));
258
        }
259
        else if(thresholdType.equals("PositiveFloat")){
260
            return new Threshold<>(name, printName, description, errorMessage, new PositiveFloat(Float.parseFloat(value)));
261
        }
262
        else if(thresholdType.equals("PositiveInteger")){
263
            return new Threshold<>(name, printName, description, errorMessage, new PositiveInteger(Integer.parseInt(value)));
264
        }
265
        else if(thresholdType.equals("String")){
266
            return new Threshold<>(name, printName, description, errorMessage, value);
267
        }
268

  
269
        return null;
270
    }
271

  
272 208
    @Override
273 209
    public String getDescriptionFromCatalogue(long id) {
274 210
        AntiPattern ap = this.getAntiPatternById(id).getAntiPatternModel();

Také k dispozici: Unified diff