Projekt

Obecné

Profil

« Předchozí | Další » 

Revize e9a96fd8

Přidáno uživatelem Václav Hrabík před více než 1 rok

https://kivprogrammers.atlassian.net/browse/TSP2-23 soubory Bp Adama Šmucra do SPADE

Zobrazit rozdíly:

pom.xml
77 77
	</dependencyManagement>
78 78

  
79 79
	<dependencies>
80

  
80
		<dependency>
81
			<groupId>org.projectlombok</groupId>
82
			<artifactId>lombok</artifactId>
83
			<version>1.18.30</version>
84
			<scope>provided</scope>
85
		</dependency>
81 86
		<dependency>
82 87
			<groupId>org.springframework.boot</groupId>
83 88
			<artifactId>spring-boot-starter</artifactId>
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/AppController.java
3 3

  
4 4
import cz.zcu.fav.kiv.antipatterndetectionapp.detecting.AntiPatternManager;
5 5
import cz.zcu.fav.kiv.antipatterndetectionapp.model.AntiPattern;
6
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
6 7
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Query;
7 8
import cz.zcu.fav.kiv.antipatterndetectionapp.model.QueryResult;
8 9
import cz.zcu.fav.kiv.antipatterndetectionapp.service.AntiPatternService;
9 10
import cz.zcu.fav.kiv.antipatterndetectionapp.service.ConfigurationService;
10 11
import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService;
11 12
import cz.zcu.fav.kiv.antipatterndetectionapp.service.UserAccountService;
13
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.ProjectDto;
14
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.converters.ClassToDto;
15
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.converters.ProjectToDto;
12 16
import org.jsoup.Jsoup;
13 17
import org.jsoup.safety.Safelist;
14 18
import org.slf4j.Logger;
......
55 59
    @Autowired
56 60
    private ConfigurationService configurationService;
57 61

  
62
    private final ClassToDto<Project, ProjectDto> classToDto = new ProjectToDto();
63

  
58 64
    @GetMapping("/")
59 65
    public String mainPage(Model model) {
60
        model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
66
        model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
61 67
        return "main-page";
62 68
    }
63 69

  
......
71 77
     */
72 78
    @GetMapping("/detect")
73 79
    public String index(Model model) {
74
        model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
80
        model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
75 81
        LOGGER.info("@GetMapping(\"/\") - Accessing main page");
76 82
        return "index";
77 83
    }
......
85 91
     */
86 92
    @GetMapping("/projects/{id}")
87 93
    public String getProjectById(@PathVariable Long id, Model model) {
88
        model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
94
        model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
89 95
        model.addAttribute("project", projectService.getProjectById(id));
90 96
        LOGGER.info("@GetMapping(\"projects/" + id + ")");
91 97
        return "project";
......
97 103
     */
98 104
    @GetMapping("/anti-patterns")
99 105
    public @ResponseBody List<AntiPattern> getAllAntiPatterns(Model model) {
100
        model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
106
        model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
101 107
        LOGGER.info("GetMapping(\"/anti-patterns\") and obtaining all APs.");
102 108
        return antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns());
103 109
    }
......
112 118
    @GetMapping("/anti-patterns/{id}")
113 119
    public String getAntiPatternById(@PathVariable Long id, Model model, HttpSession session) {
114 120
        String currentConfigurationName = configurationGetFromSession(session);
115
        model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
121
        model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
116 122
        model.addAttribute("antiPattern", antiPatternService.getAntiPatternById(id).getAntiPatternModel());
117 123
        model.addAttribute("description", antiPatternService.getDescriptionFromCatalogue(id));
118 124
        model.addAttribute("operationalizationText", antiPatternService.getOperationalizationText(antiPatternService.getAntiPatternById(id).getAntiPatternModel().getName()));
......
140 146

  
141 147
        if (currentConfigurationName == null) {
142 148
            model.addAttribute("errorMessage", "No configuration was found!");
143
            model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
149
            model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
144 150
            return "index";
145 151
        }
146 152

  
147 153
        if (selectedProjects == null) {
148 154
            model.addAttribute("errorMessage", "No project selected." +
149 155
                    " Select at least one project.");
150
            model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
156
            model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
151 157
            LOGGER.warn("@PostMapping(\"/analyze\") - Processing requirements for analyzing selected projects and AP with an error. " +
152 158
                    "selectedProjects are null!");
153 159
            return "index";
......
156 162
        if (selectedAntiPatterns == null) {
157 163
            model.addAttribute("errorMessage", "No anti-pattern selected." +
158 164
                    " Select at least one anti-pattern.");
159
            model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
165
            model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
160 166
            LOGGER.warn("@PostMapping(\"/analyze\") - Processing requirements for analyzing selected projects and AP with an error. " +
161 167
                    "selectedAntiPatterns are null!");
162 168
            return "index";
......
170 176
        antiPatternService.saveResults(results);
171 177
        antiPatternService.setConfigurationChanged(false);
172 178

  
173
        model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
179
        model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
174 180
        model.addAttribute("queryResults", results);
175 181
        model.addAttribute("recalculationTime", DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalTime.now()));
176 182

  
......
192 198
        }
193 199
        antiPatternService.setConfigurationChanged(false);
194 200

  
195
        model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
201
        model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
196 202
        model.addAttribute("queryResults", antiPatternService.getResults());
197 203
        model.addAttribute("recalculationTime", DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalTime.now()));
198 204

  
......
207 213
     */
208 214
    @GetMapping("/about")
209 215
    public String about(Model model) {
210
        model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
216
        model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
211 217
        LOGGER.info("@GetMapping(\"/about\") - Accessing about page");
212 218
        return "about";
213 219
    }
......
220 226
     */
221 227
    @GetMapping("/configuration")
222 228
    public String configuration(Model model, HttpSession session) {
223
        model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
229
        model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
224 230
        model.addAttribute("antiPatterns", antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns()));
225 231

  
226 232
        String currentConfigurationName = configurationGetFromSession(session);
......
245 251

  
246 252
        String currentConfigurationName = configurationGetFromSession(session);
247 253

  
248
        Query query = new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns()));
254
        Query query = new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns()));
249 255

  
250 256
        List<String> wrongParameters = configurationService.saveNewConfiguration(query.getAntiPatterns(), currentConfigurationName, antiPatternNames, thresholdNames, thresholdValues, true);
251 257

  
......
283 289
                                    @RequestParam(value = "configuration-save-as-input") String newConfigName,
284 290
                                    HttpSession session, RedirectAttributes redirectAttributes) {
285 291

  
286
        Query query = new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns()));
292
        Query query = new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns()));
287 293
        String currentConfigurationName = configurationGetFromSession(session);
288 294

  
289 295
        List<String> allConfigurationNames = configurationService.getAllConfigurationNames();
......
439 445
     */
440 446
    @GetMapping("/login")
441 447
    public String login(Model model) {
442
        model.addAttribute("query", new Query(projectService.getAllProjects(), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
448
        model.addAttribute("query", new Query(classToDto.convert(projectService.getAllProjects()), antiPatternService.antiPatternsToModel(antiPatternService.getAllAntiPatterns())));
443 449
        return "login";
444 450
    }
445 451

  
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/management/EnumController.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.controller.management;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.Constants;
4
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
5
import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService;
6
import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.enums.*;
7
import cz.zcu.fav.kiv.antipatterndetectionapp.utils.Utils;
8
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.FormObject;
9
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.ProjectInstance;
10
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.interfaces.Classification;
11
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.interfaces.EnumType;
12
import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.Service;
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.data.util.Pair;
17
import org.springframework.stereotype.Controller;
18
import org.springframework.ui.Model;
19
import org.springframework.web.bind.annotation.GetMapping;
20
import org.springframework.web.bind.annotation.PostMapping;
21
import org.springframework.web.bind.annotation.RequestParam;
22
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
23

  
24
import javax.annotation.PostConstruct;
25
import javax.servlet.http.HttpSession;
26
import java.util.*;
27

  
28
/**
29
 * This class contains all endpoints of enums.html
30
 */
31
@Controller
32
public class EnumController {
33

  
34
    private final Logger LOGGER = LoggerFactory.getLogger(EnumController.class);
35

  
36
    @Autowired
37
    private PriorityService priorityService;
38

  
39
    @Autowired
40
    private SeverityService severityService;
41

  
42
    @Autowired
43
    private RelationService  relationService;
44

  
45
    @Autowired
46
    private ResolutionService resolutionService;
47

  
48
    @Autowired
49
    private RoleService roleService;
50

  
51
    @Autowired
52
    private StatusService statusService;
53

  
54
    @Autowired
55
    private WuTypeService wuTypeService;
56

  
57
    @Autowired
58
    private PriorityClassificationService priorityClassificationService;
59

  
60
    @Autowired
61
    private SeverityClassificationService severityClassificationService;
62

  
63
    @Autowired
64
    private StatusClassificationService statusClassificationService;
65

  
66
    @Autowired
67
    private RoleClassificationService roleClassificationService;
68

  
69
    @Autowired
70
    private ResolutionClassificationService resolutionClassificationService;
71

  
72
    @Autowired
73
    private WuTypeClassificationService wuTypeClassificationService;
74

  
75
    @Autowired
76
    private RelationClassificationService relationClassificationService;
77

  
78
    @Autowired
79
    private ProjectService projectService;
80

  
81
    private Map<String, Pair<Service, EnumService>> services;
82

  
83
    /**
84
     * Method initializing map of enum
85
     * services and their classifications
86
     */
87
    @PostConstruct
88
    private void initializeMap() {
89
        services = Map.ofEntries(
90
                Map.entry("Priority", Pair.of(priorityClassificationService, priorityService)),
91
                Map.entry("Severity", Pair.of(severityClassificationService, severityService)),
92
                Map.entry("Status", Pair.of(statusClassificationService, statusService)),
93
                Map.entry("Role", Pair.of(roleClassificationService, roleService)),
94
                Map.entry("Resolution", Pair.of(resolutionClassificationService, resolutionService)),
95
                Map.entry("WuType", Pair.of(wuTypeClassificationService, wuTypeService)),
96
                Map.entry("Relation", Pair.of(relationClassificationService, relationService))
97
        );
98
    }
99

  
100
    /**
101
     * Method for showing enums in tabs
102
     * @param model         Object for passing data to the UI
103
     * @param project       Selected project for showing enums
104
     * @param redirectAttrs Attributes for redirection
105
     * @param session       Session with attributes
106
     * @return              HTML template
107
     */
108
    @GetMapping("/management/enums")
109
    public String changeTab(Model model,
110
                            @RequestParam(value = "selectedProject", required = false) Long project,
111
                            RedirectAttributes redirectAttrs,
112
                            HttpSession session) {
113

  
114
        // First open in session
115
        if(project == null && session.getAttribute("project") == null) {
116
            model.addAttribute("projects", projectService.getAllProjects());
117
            LOGGER.info("@GetMapping(\"/management/enums\") - Accessing page");
118
            return "management/enums";
119
        }
120

  
121
        // Project selected
122
        if(project != null) {
123
            session.setAttribute("project", project);
124
            Utils.sessionRecreate(session);
125
        }
126

  
127
        project = (long) session.getAttribute("project");
128

  
129
        Project foundedProject = projectService.getProjectById(project);
130
        if(foundedProject == null) {
131
            if(!Objects.equals(project, Constants.DEFAULT_ID)) {
132
                redirectAttrs.addFlashAttribute("errorMessage", "Project not found");
133
                LOGGER.info("@GetMapping(\"/management/enums\")- Project not found");
134
            } else {
135
                LOGGER.info("@GetMapping(\"/management/enums\") - Accessing page");
136
            }
137
            model.addAttribute("projects", projectService.getAllProjects());
138
            return "management/enums";
139
        }
140

  
141
        // For each project instance of project, get all enums
142
        for(Map.Entry<String, Pair<Service, EnumService>> entry : services.entrySet()) {
143

  
144
            Set<EnumType> enums = new HashSet<>(); // Enums of current enum type
145
            Service service = entry.getValue().getFirst(); // Service for current enum type
146

  
147
            // For each project instance, get all enums for current enum type
148
            for(ProjectInstance projectInstance : foundedProject.getProjectInstances()) {
149
                Set<EnumType> enumsByName = (Set<EnumType>) projectInstance.getEnumsByName(entry.getKey());
150
                if(enumsByName != null) {
151
                    enums.addAll(enumsByName);
152
                }
153
            }
154

  
155
            // Create classification pairs with enums
156
            List<Pair<Classification, List<EnumType>>> pairs = createClassPairs(service, enums);
157

  
158
            //Calculate rowspan for superclasses
159
            List<Integer> superClassSpan = calculateSpan(pairs);
160

  
161
            model.addAttribute(entry.getKey(), pairs);
162
            model.addAttribute(entry.getKey() + "All", service.getAllClasses());
163
            model.addAttribute(entry.getKey() + "Super", superClassSpan);
164
        }
165

  
166
        model.addAttribute("projects", projectService.getAllProjects());
167
        model.addAttribute("result", new FormObject());
168
        return "management/enums";
169
    }
170

  
171
    /**
172
     * Method for changing enum class for selected enums
173
     * @param model         Object for passing data to the UI
174
     * @param selectedEnums Selected enums for change of their class
175
     * @param selectedClass Selected class for enums
176
     * @param enumName      Name of the current selected tab
177
     * @param redirectAttrs Attributes for redirection
178
     * @return              Html template
179
     */
180
    @PostMapping(value = "/changeEnums")
181
    public String changeEnums(Model model,
182
                              @RequestParam(value = "selectedEnums", required = false) List<Long> selectedEnums,
183
                              @RequestParam(value = "selectedClass", required = false) Long selectedClass,
184
                              @RequestParam(value = "enumName", required = false) String enumName,
185
                              RedirectAttributes redirectAttrs) {
186

  
187
        if(selectedEnums == null || selectedEnums.isEmpty()) {
188
            redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Nothing was selected");
189
            LOGGER.info("@PostMapping(\"/management/enums\") - Nothing was selected");
190
            return "redirect:/management/enums";
191
        }
192

  
193
        for(Long id : selectedEnums) {
194
            Pair<Service, EnumService> pair = services.get(enumName);
195
            if(pair != null) {
196
                Service service = pair.getFirst();
197
                EnumService enumService = pair.getSecond();
198

  
199
                EnumType enumType = enumService.getEnumById(id);
200
                enumType.setClassId(service.getClassById(selectedClass));
201
                enumService.saveEnum(enumType);
202
            }
203
        }
204

  
205
        redirectAttrs.addFlashAttribute("successMessage", "All selected enums (" + selectedEnums.size() + ") were successfully assigned");
206
        redirectAttrs.addFlashAttribute("activeTab", enumName);
207
        return "redirect:/management/enums";
208
    }
209

  
210
    /**
211
     * Method creating pairs with enum type
212
     * classification and list of relevant enums
213
     * @param service   Enum service
214
     * @param enums     Set of enums in specific project
215
     * @return          List of pairs with enum class and relevant enums
216
     */
217
    private List<Pair<Classification, List<EnumType>>> createClassPairs(Service service, Set<EnumType> enums) {
218

  
219
        List<Pair<Classification, List<EnumType>>> pairs = new ArrayList<>(); // List with enum types classes and their enums
220
        for(Classification enumClass : service.getAllClasses()) {
221
            Set<EnumType> enumSet = new HashSet<>(); // For each enum classification create new list for enums
222

  
223
            //If enum class is equal to current class, add enum into list
224
            for(EnumType e : enums) {
225
                if(Objects.equals(e.getClassId().getId(), enumClass.getId())) {
226
                    enumSet.add(e);
227
                }
228
            }
229

  
230
            //If enum set has some enums in it, add pair to the final list
231
            if(!enumSet.isEmpty()) {
232
                List<EnumType> enumTypes = new ArrayList<>(enumSet);
233
                Collections.sort(enumTypes, Comparator.comparing(EnumType::getName, String.CASE_INSENSITIVE_ORDER));
234
                Pair<Classification, List<EnumType>> newPair = Pair.of(enumClass, enumTypes);
235
                pairs.add(newPair);
236
            }
237
        }
238
        return pairs;
239
    }
240

  
241
    /**
242
     * Method calculating rowspan for superclasses
243
     * @param pairs     Classification and list with enums
244
     * @return          List with sizes of rowspan for each classification
245
     */
246
    private List<Integer> calculateSpan(List<Pair<Classification, List<EnumType>>> pairs) {
247
        List<Integer> superClassSpan = new ArrayList<>(); // Superclass rowspan for current enum type
248
        String preSuperClass = null;
249
        int preListSize = 0;
250
        int lastI = 0;
251
        for(int i = 0; i < pairs.size(); i++) {
252
            Pair<Classification, List<EnumType>> currentPair = pairs.get(i);
253
            String currentSuperClass = currentPair.getFirst().getSuperClass();
254
            int currentListSize = currentPair.getSecond().size();
255
            if(preSuperClass == null || !currentSuperClass.contentEquals(preSuperClass)) {
256
                superClassSpan.add(i, currentListSize);
257
                lastI = i;
258
            } else if(currentSuperClass.contentEquals(preSuperClass)) {
259
                superClassSpan.set(lastI, preListSize + currentListSize);
260
                superClassSpan.add(i, 0);
261
                preListSize = preListSize + currentListSize;
262
                continue;
263
            }
264
            preSuperClass = currentSuperClass;
265
            preListSize = currentListSize;
266
        }
267

  
268
        return superClassSpan;
269
    }
270
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/management/PersonController.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.controller.management;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.Constants;
4
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
5
import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService;
6
import cz.zcu.fav.kiv.antipatterndetectionapp.utils.Utils;
7
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.Person;
8
import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.PersonService;
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.stereotype.Controller;
13
import org.springframework.ui.Model;
14
import org.springframework.web.bind.annotation.GetMapping;
15
import org.springframework.web.bind.annotation.PostMapping;
16
import org.springframework.web.bind.annotation.RequestParam;
17
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
18

  
19
import javax.servlet.http.HttpSession;
20
import java.util.Collections;
21
import java.util.Comparator;
22
import java.util.List;
23
import java.util.Objects;
24

  
25
/**
26
 * This class contains all endpoints of person.html
27
 */
28
@Controller
29
public class PersonController {
30

  
31
    private final Logger LOGGER = LoggerFactory.getLogger(PersonController.class);
32

  
33
    @Autowired
34
    private PersonService personService;
35

  
36
    @Autowired
37
    private ProjectService projectService;
38

  
39
    /**
40
     * Method for showing people and their identities
41
     * @param model         Object for passing data to the UI
42
     * @param project       Selected project for showing people
43
     * @param redirectAttrs Attributes for redirection
44
     * @param session       Session with attributes
45
     * @return              HTML template
46
     */
47
    //[JT] vyhodit http session
48
    @GetMapping("/management/person")
49
    public String people(Model model,
50
                         @RequestParam(value = "selectedProject", required = false) Long project,
51
                         RedirectAttributes redirectAttrs,
52
                         HttpSession session) {
53

  
54
        // [JT] tohle taky hodit pryc
55
        //session pro nas neni platna - posilat v body requestu z klienta
56
        if(project == null && session.getAttribute("project") == null) {
57
            model.addAttribute("projects", projectService.getAllProjects());
58
            LOGGER.info("@GetMapping(\"/management/person\") - Accessing page");
59
            //[JT] prepsat do reactu
60
            return "management/person";
61
        }
62
        //[JT] tohle pryc
63
        // Project selected
64
        if(project != null) {
65
            session.setAttribute("project", project);
66
            Utils.sessionRecreate(session);
67
        }
68
        //[JT] nerozumim
69
        project = (long) session.getAttribute("project");
70

  
71
        Project foundedProject = projectService.getProjectById(project);
72
        //[JT] logika do service
73
        if(foundedProject == null) {
74
            if(!Objects.equals(project, Constants.DEFAULT_ID)) {
75
                redirectAttrs.addFlashAttribute("errorMessage", "Project not found");
76
                LOGGER.info("@GetMapping(\"/management/person\")- Project not found");
77
            } else {
78
                LOGGER.info("@GetMapping(\"/management/person\") - Accessing page");
79
            }
80
            //[JT] do reactu
81
            model.addAttribute("projects", projectService.getAllProjects());
82
            return "management/person";
83
        }
84

  
85
        List<Person> people = foundedProject.getPeople();
86
        Collections.sort(people, Comparator.comparing(Person::getName, String.CASE_INSENSITIVE_ORDER));
87
        //[JT] hodit do json response body
88
        model.addAttribute("people", people);
89
        model.addAttribute("projects", projectService.getAllProjects());
90
        LOGGER.info("@GetMapping(\"/management/person\") - Accessing page");
91

  
92
        return "management/person";
93
    }
94

  
95
    /**
96
     * Method for merging people in same project
97
     * @param model                 Object for passing data to the UI
98
     * @param selectedPeople        People selected by user for merge
99
     * @param personName            Name of the new person for merge
100
     * @param redirectAttrs         Attributes for redirection
101
     * @return                      HTML template
102
     */
103
    @PostMapping(value = "/changeIdentities")
104
    public String mergeIdentities(Model model,
105
                                  @RequestParam(value = "selectedBox", required = false) List<Person> selectedPeople,
106
                                  @RequestParam(value = "personName", required = false) List<String> personName,
107
                                  @RequestParam(value = "radioBtn", required = false) Integer radioBtn,
108
                                  @RequestParam(value = "submitId", required = false) Long submitId,
109
                                  RedirectAttributes redirectAttrs) {
110

  
111
        // Nothing was selected
112
        if(selectedPeople == null || selectedPeople.isEmpty()) {
113
            redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Nothing was selected");
114
            LOGGER.info("@PostMapping(\"/management/person\") - Nothing was selected or name was empty");
115

  
116
        } else {
117

  
118
            Person newPerson;
119
            if(Objects.equals(submitId, Constants.DEFAULT_ID)) { // Creating new person
120

  
121
                if(personName == null || personName.isEmpty() || radioBtn == null || personName.get(radioBtn).isEmpty()) { // Person name is not set
122
                    redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Name of the new person is empty");
123
                    LOGGER.info("@PostMapping(\"/management/person\") - Name of the new person is empty");
124
                    return "redirect:/management/person";
125
                }
126

  
127
                Person createdPerson = new Person(personName.get(radioBtn), selectedPeople.get(0).getProject());
128
                newPerson = personService.savePerson(createdPerson);
129

  
130
            } else { // Merging to existing person
131
                newPerson = personService.getPersonById(submitId);
132
            }
133

  
134
            if(newPerson != null) { // Person was found in database
135

  
136
                if(selectedPeople.contains(newPerson)) {
137
                    redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Person cannot be merged with itself");
138
                    LOGGER.info("@PostMapping(\"/management/person\") - Person cannot be merged with itself");
139
                    return "redirect:/management/person";
140
                }
141

  
142
                for(Person person : selectedPeople) { // Relation update for every selected person
143
                    personService.updatePersonRelations(person.getId(), newPerson.getId());
144
                    personService.deletePerson(person.getId());
145
                }
146

  
147
                redirectAttrs.addFlashAttribute("successMessage", "All identities of selected person were merged to person " + newPerson.getName());
148
                LOGGER.info("@PostMapping(\"/management/person\") - Selected identities were merged to person with id " + submitId);
149

  
150
            } else {
151
                redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Selected person was not found");
152
                LOGGER.info("@PostMapping(\"/management/person\") - Selected person with id " + submitId + " was not found");
153
            }
154

  
155
        }
156
        return "redirect:/management/person";
157
    }
158
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/management/ProjectController.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.controller.management;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.Constants;
4
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
5
import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService;
6
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.types.Node;
7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Controller;
11
import org.springframework.ui.Model;
12
import org.springframework.web.bind.annotation.GetMapping;
13
import org.springframework.web.bind.annotation.PostMapping;
14
import org.springframework.web.bind.annotation.RequestParam;
15
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
16

  
17
import java.util.ArrayList;
18
import java.util.List;
19
import java.util.Objects;
20

  
21
/**
22
 * This class contains all endpoints of projects.html
23
 */
24
@Controller
25
public class ProjectController {
26

  
27
    private final Logger LOGGER = LoggerFactory.getLogger(ProjectController.class);
28

  
29
    @Autowired
30
    private ProjectService projectService;
31

  
32
    /**
33
     * Method for visualization of project hierarchy
34
     * @param model     Object for passing data to the UI
35
     * @return          HTML template
36
     */
37
    @GetMapping("/management/projects")
38
    public String projects(Model model) {
39
        List<Node> parents = new ArrayList<>();
40
        List<Project> parentProjects = projectService.getParentProjects();
41
        for(Project parentProject : parentProjects) {
42
            Node n = new Node();
43
            n.project = parentProject;
44
            n.children = new ArrayList<>();
45
            parents.add(n);
46
        }
47

  
48
        for(Node n : parents) {
49
            n.children = calculate(n.project);
50
        }
51

  
52
        model.addAttribute("parents", parents);
53
        LOGGER.info("@GetMapping(\"/management/projects\") - Accessing page");
54
        return "management/projects";
55
    }
56

  
57
    /**
58
     * Method for changing superproject of selected projects
59
     * @param model                 Object for passing data to the UI
60
     * @param selectedProjects      Selected projects for changing their parent project
61
     * @param selectedSuperProject  Selected project for being parent project
62
     * @param projectName           Name of new created project that will take a place as parent project
63
     * @param redirectAttrs         Redirect attributes
64
     * @return                      HTML template
65
     */
66
    @PostMapping(value = "/superProjectChange")
67
    public String projectChange(Model model,
68
                                @RequestParam(value = "selectedBox", required = false) List<Project> selectedProjects,
69
                                @RequestParam(value = "submitId", required = false) Long selectedSuperProject,
70
                                @RequestParam(value = "projectName", required = false) String projectName,
71
                                RedirectAttributes redirectAttrs) {
72

  
73
        // Validity check
74
        if(selectedProjects == null) {
75
            redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Nothing was selected before submission");
76
            LOGGER.warn("@PostMapping(\"/management/projects\") - Cannot proceed: Submission went wrong");
77

  
78
        } else if(projectService.getProjectById(selectedSuperProject) == null && !Objects.equals(selectedSuperProject, Constants.DEFAULT_ID)) {
79
            redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Superproject was not found");
80
            LOGGER.warn("@PostMapping(\"/management/projects\") - Cannot proceed: Submission went wrong");
81

  
82
        } else if(!Objects.equals(selectedSuperProject, Constants.DEFAULT_ID) && selectedProjects.contains(projectService.getProjectById(selectedSuperProject))) {
83
            if(selectedProjects.size() == 1) {
84
                selectedProjects.get(0).setSuperProject(null);
85
                projectService.saveProject(selectedProjects.get(0));
86
                redirectAttrs.addFlashAttribute("successMessage", "The project was set up as a parent project");
87
            } else {
88
                redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Project cannot be superproject for self");
89
                LOGGER.warn("@PostMapping(\"/management/projects\") - Cannot proceed: Submission went wrong");
90
            }
91

  
92
        } else {
93
            // User creating new superproject
94
            if(Objects.equals(selectedSuperProject, Constants.DEFAULT_ID)) {
95
                if(projectName == null || projectName.isEmpty()) {
96
                    redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Name was not found");
97
                    LOGGER.warn("@PostMapping(\"/management/projects\") - Cannot proceed: Submission went wrong");
98
                } else {
99
                    Project project = new Project(projectName);
100
                    Project newProject = projectService.saveProject(project);
101
                    for(Project subProject : selectedProjects) {
102
                        subProject.setSuperProject(newProject);
103
                        projectService.saveProject(subProject);
104
                    }
105
                    redirectAttrs.addFlashAttribute("successMessage", "Selected projects (" + selectedProjects.size() + ") were assign to " + newProject.getName());
106
                }
107
                return "redirect:/management/projects/";
108
            }
109

  
110
            // Check if there is no cycle
111
            List<Project> childProjects = new ArrayList<>();
112
            for(Project project : selectedProjects) {
113
                addChildProject(project, childProjects);
114
            }
115
            if(childProjects.contains(projectService.getProjectById(selectedSuperProject))) {
116
                redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Some selected projects cannot be assign - Selected superproject cannot be superior to its parent");
117
                LOGGER.warn("@PostMapping(\"/management/projects\") - Cannot proceed: Submission went wrong");
118
                return "redirect:/management/projects/";
119
            }
120

  
121
            // Changing superproject of all selected projects
122
            for(Project project : selectedProjects) {
123
                project.setSuperProject(projectService.getProjectById(selectedSuperProject));
124
                projectService.saveProject(project);
125
            }
126
            redirectAttrs.addFlashAttribute("successMessage", "Selected projects (" + selectedProjects.size() + ") were assign to " + projectService.getProjectById(selectedSuperProject).getName());
127
        }
128

  
129
        return "redirect:/management/projects/";
130
    }
131

  
132
    /**
133
     * Method for searching all subordinate projects of selected project
134
     * @param project       Project for which the search will be done
135
     * @param childProjects Collection of all subordinate projects
136
     */
137
    private void addChildProject(Project project, List<Project> childProjects) {
138
        for(Project childProject : projectService.getSubordinateProjectsTo(project.getId())) {
139
            childProjects.add(childProject);
140
            addChildProject(childProject, childProjects);
141
        }
142
    }
143

  
144
    /**
145
     * Method creating the hierarchy of projects by recursion
146
     * @param p Project for which the hierarchy will be generated
147
     * @return  List of project's children with their hierarchy
148
     */
149
    private ArrayList<Node> calculate(Project p) {
150
        ArrayList<Node> nodes = new ArrayList<>();
151
        List<Project> projects = projectService.getSubordinateProjectsTo(p.getId());
152
        for(Project project : projects) {
153
            Node n = new Node();
154
            n.project = project;
155
            n.children = calculate(project);
156
            nodes.add(n);
157
        }
158
        return nodes;
159
    }
160
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/management/ReleaseController.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.controller.management;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService;
4
import cz.zcu.fav.kiv.antipatterndetectionapp.utils.Utils;
5
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.Commit;
6
import cz.zcu.fav.kiv.antipatterndetectionapp.repository.managment.CommitRepository;
7
import cz.zcu.fav.kiv.antipatterndetectionapp.repository.managment.ConfigurationPersonRelationRepository;
8
import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.BranchService;
9
import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.CommitService;
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.stereotype.Controller;
14
import org.springframework.ui.Model;
15
import org.springframework.web.bind.annotation.GetMapping;
16
import org.springframework.web.bind.annotation.PostMapping;
17
import org.springframework.web.bind.annotation.RequestParam;
18
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
19

  
20
import javax.servlet.http.HttpSession;
21

  
22
/**
23
 * This class contains all endpoints of release.html
24
 */
25
@Controller
26
public class ReleaseController {
27

  
28
    private final Logger LOGGER = LoggerFactory.getLogger(ReleaseController.class);
29

  
30
    @Autowired
31
    private ProjectService projectService;
32

  
33
    @Autowired
34
    private CommitService commitService;
35

  
36
    @Autowired
37
    private BranchService branchService;
38

  
39
    @Autowired
40
    private CommitRepository commitRepository;
41

  
42
    @Autowired
43
    private ConfigurationPersonRelationRepository configurationPersonRelationRepository;
44

  
45
    /**
46
     * Method for visualization of released configurations
47
     * @param model     Object for passing data to the UI
48
     * @param project   Selected project for showing releases
49
     * @param session   Session with attributes
50
     * @return          HTML template
51
     */
52
    @GetMapping("/management/release")
53
    public String releases(Model model,
54
                           @RequestParam(value = "selectedProject", required = false) Long project,
55
                           HttpSession session) {
56

  
57
        // First open in session
58
        if(project == null && session.getAttribute("project") == null) {
59
            model.addAttribute("projects", projectService.getAllProjects());
60
            LOGGER.info("@GetMapping(\"/management/release\") - Accessing page");
61
            return "management/release";
62
        }
63

  
64
        // Project selected
65
        if(project != null) {
66
            session.setAttribute("project", project);
67
            Utils.sessionRecreate(session);
68
        }
69

  
70
        project = (long) session.getAttribute("project");
71
        model.addAttribute("projectId", project);
72

  
73
        model.addAttribute("projects", projectService.getAllProjects());
74
        model.addAttribute("commits", commitRepository.getReleasedCommitsByProject(project));
75
        return "management/release";
76
    }
77

  
78
    /**
79
     * Method opening specific commit based on identifier
80
     * @param model         Object for passing data to the UI
81
     * @param identifier    Identifier of commit for visualisation
82
     * @param redirectAttrs Attributes for redirection
83
     * @param session       Session with attributes
84
     * @return              HTML template
85
     */
86
    @PostMapping(value = "/openCommit")
87
    public String openCommit(Model model,
88
                             @RequestParam(value = "identifier", required = false) String identifier,
89
                             RedirectAttributes redirectAttrs,
90
                             HttpSession session) {
91

  
92
        Commit commit = commitService.getCommitByIdentifier((long) session.getAttribute("project"), identifier);
93
        if(commit == null) {
94
            redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Commit doesn't exist");
95
            LOGGER.info("@PostMapping(\"/management/release/\") - Commit not found");
96
            return "redirect:/management/release";
97
        }
98

  
99
        model.addAttribute("selectedCommit", commit);
100
        model.addAttribute("committedBy", configurationPersonRelationRepository.getCommittedRelation(commit.getId()));
101
        return "management/commit";
102
    }
103

  
104
    /**
105
     * Method changing release attribute of specific commit
106
     * @param model         Object for passing data to the UI
107
     * @param identifier    Identifier of commit for attribute change
108
     * @param release       Value of attribute release
109
     * @param redirectAttrs Attributes for redirection
110
     * @param session       Session with attributes
111
     * @return              HTML template
112
     */
113
    @PostMapping(value = "/changeRelease")
114
    public String changeRelease(Model model,
115
                                @RequestParam(value = "identifier", required = false) String identifier,
116
                                @RequestParam(value = "release", required = false) boolean release,
117
                                RedirectAttributes redirectAttrs,
118
                                HttpSession session) {
119

  
120
        Commit commit = commitService.getCommitByIdentifier((long) session.getAttribute("project"), identifier);
121
        if(commit == null) {
122
            redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Commit doesn't exist");
123
            LOGGER.info("@PostMapping(\"/management/release/\") - Commit not found");
124
            return "redirect:/management/release";
125
        }
126

  
127
        if(release != commit.isRelease()) {
128
            commit.setRelease(release);
129
            commitService.saveCommit(commit);
130
        }
131

  
132
        LOGGER.info("@PostMapping(\"/management/person\") - Commit " + commit.getIdentifier() + " were unreleased");
133
        model.addAttribute("selectedCommit", commit);
134
        model.addAttribute("committedBy", configurationPersonRelationRepository.getCommittedRelation(commit.getId()));
135
        return "management/commit";
136
    }
137

  
138
    /**
139
     * Method for marking commit as not released
140
     * @param model         Object for passing data to the UI
141
     * @param submitId      ID of commit for unreleasing
142
     * @param redirectAttrs Attributes for redirection
143
     * @return
144
     */
145
    @PostMapping(value = "/unRelease")
146
    public String unRelease(Model model,
147
                            @RequestParam(value = "submitId", required = false) Long submitId,
148
                            RedirectAttributes redirectAttrs) {
149

  
150
        Commit commit = commitService.getCommitById(submitId);
151
        if(commit == null) {
152
            redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Submitted commit id doesn't exists");
153
            LOGGER.info("@PostMapping(\"/management/release/\") - Commit not found");
154
            return "redirect:/management/release";
155
        }
156

  
157
        commit.setRelease(false);
158
        commitService.saveCommit(commit);
159

  
160
        redirectAttrs.addFlashAttribute("successMessage", "Commit " + commit.getIdentifier() + " were unreleased");
161
        LOGGER.info("@PostMapping(\"/management/person\") - Commit " + commit.getIdentifier() + " were unreleased");
162
        return "redirect:/management/release";
163
    }
164

  
165
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/management/SegmentActivityController.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.controller.management;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.Constants;
4
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
5
import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService;
6
import cz.zcu.fav.kiv.antipatterndetectionapp.utils.Utils;
7
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.Activity;
8
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.Category;
9
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.ProjectInstance;
10
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.WorkUnit;
11
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.enums.WuType;
12
import cz.zcu.fav.kiv.antipatterndetectionapp.repository.managment.ActivityRepository;
13
import cz.zcu.fav.kiv.antipatterndetectionapp.repository.managment.WorkUnitRepository;
14
import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.ActivityService;
15
import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.CategoryService;
16
import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.WorkUnitService;
17
import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.enums.WuTypeService;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.stereotype.Controller;
22
import org.springframework.ui.Model;
23
import org.springframework.web.bind.annotation.GetMapping;
24
import org.springframework.web.bind.annotation.PostMapping;
25
import org.springframework.web.bind.annotation.RequestParam;
26
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
27

  
28
import javax.servlet.http.HttpSession;
29
import java.util.*;
30

  
31
/**
32
 * This class contains all endpoints of segment-activity.html
33
 */
34
@Controller
35
public class SegmentActivityController {
36

  
37
    private final Logger LOGGER = LoggerFactory.getLogger(SegmentActivityController.class);
38

  
39
    @Autowired
40
    private ProjectService projectService;
41

  
42
    @Autowired
43
    private ActivityService activityService;
44

  
45
    @Autowired
46
    private WorkUnitService workUnitService;
47

  
48
    @Autowired
49
    private CategoryService categoryService;
50

  
51
    @Autowired
52
    private WuTypeService wuTypeService;
53

  
54
    @Autowired
55
    private ActivityRepository activityRepository;
56

  
57
    @Autowired
58
    private WorkUnitRepository workUnitRepository;
59

  
60
    /**
61
     * Method for visualization of activities and work units
62
     * @param model         Object for passing data to the UI
63
     * @param project       Selected project for showing workunits and activities
64
     * @param redirectAttrs Attributes for redirection
65
     * @param session       Session with attributes
66
     * @return              HTML template
67
     */
68
    @GetMapping("/management/segment-activity")
69
    public String changeProject(Model model,
70
                                @RequestParam(value = "selectedProject", required = false) Long project,
71
                                RedirectAttributes redirectAttrs,
72
                                HttpSession session) {
73

  
74
        // Remove activity if different project selected
75
        if(project != null && session.getAttribute("project") != project) {
76
            Utils.sessionRecreate(session);
77
        }
78

  
79
        // First open in session
80
        if(project == null && session.getAttribute("project") == null) {
81
            model.addAttribute("projects", projectService.getAllProjects());
82
            LOGGER.info("@GetMapping(\"/management/segment-activity\") - Accessing page");
83
            return "management/segment-activity";
84
        }
85

  
86
        // Project selected
87
        if(project != null) {
88
            session.setAttribute("project", project);
89
        }
90

  
91
        project = (long) session.getAttribute("project");
92

  
93
        Project foundedProject = projectService.getProjectById(project);
94

  
95
        if(foundedProject == null) {
96
            if(!Objects.equals(project, Constants.DEFAULT_ID)) {
97
                redirectAttrs.addFlashAttribute("errorMessage", "Project not found");
98
                LOGGER.info("@GetMapping(\"/management/segment-activity\")- Project not found");
99
            } else {
100
                session.removeAttribute("activity");
101
                LOGGER.info("@GetMapping(\"/management/segment-activity\") - Accessing page");
102
            }
103
            model.addAttribute("projects", projectService.getAllProjects());
104
            return "management/segment-activity";
105
        }
106

  
107
        applyFilter(model, foundedProject, session);
108

  
109
        model.addAttribute("activities", foundedProject.getActivities());
110
        model.addAttribute("projects", projectService.getAllProjects());
111
        return "management/segment-activity";
112
    }
113

  
114
    /**
115
     * Method for selection of activity
116
     * @param model         Object for passing data to the UI
117
     * @param activityId    Selected activity for assigning
118
     * @param redirectAttrs Attributes for redirection
119
     * @param session       Session with attributes
120
     * @return              HTML template
121
     */
122
    @PostMapping("/selectActivity")
123
    public String selectActivity(Model model,
124
                                 @RequestParam(value = "submitId", required = false) Long activityId,
125
                                 RedirectAttributes redirectAttrs,
126
                                 HttpSession session) {
127

  
128
        Activity activity = activityService.getActivityById(activityId);
129
        if(activity == null) {
130
            redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Activity not found");
131
            LOGGER.info("@GetMapping(\"/management/segment-activity\")- Activity not found");
132
            return "redirect:/management/segment-activity";
133
        }
134

  
135
        session.setAttribute("activity", activity);
136
        LOGGER.info("@GetMapping(\"/management/segment-activity\") - Accessing page");
137
        return "redirect:/management/segment-activity";
138
    }
139

  
140
    /**
141
     * Method for reselection of activity (removing activity from session)
142
     * @param session   Session with attributes
143
     * @return          HTML template
144
     */
145
    @PostMapping("/reselectActivity")
146
    public String reselectActivity(HttpSession session) {
147
        session.removeAttribute("activity");
148
        return "redirect:/management/segment-activity";
149
    }
150

  
151
    /**
152
     * Method adding category to the filter for current work units
153
     * @param model         Object for passing data to the UI
154
     * @param categoryId    Identifier of type that will be added
155
     * @param session       Session with attributes
156
     * @return              HTML template
157
     */
158
    @PostMapping("/addCategoryFilter")
159
    public String addCategory(Model model,
160
                              @RequestParam(value = "selectedCategory", required = false) Long categoryId,
161
                              HttpSession session) {
162

  
163
        List<Category> categoryFilter = (List<Category>) session.getAttribute("categoryFilter");
164
        if(categoryFilter == null) {
165
            categoryFilter = new ArrayList<>();
166
        }
167

  
168
        Category category = categoryService.getCategoryById(categoryId);
169
        if(category != null) {
170
            categoryFilter.add(category);
171
        }
172
        session.setAttribute("categoryFilter", categoryFilter);
173
        return "redirect:/management/segment-activity";
174
    }
175

  
176
    /**
177
     * Method removing category from filter for current work units
178
     * @param model         Object for passing data to the UI
179
     * @param categoryId    Identifier of category that will be removed
180
     * @param session       Session with attributes
181
     * @return              HTML template
182
     */
183
    @PostMapping("/removeCategoryFilter")
184
    public String removeCategory(Model model,
185
                                 @RequestParam(value = "selectedCategory", required = false) Long categoryId,
186
                                 HttpSession session) {
187

  
188

  
189
        List<Category> categoryFilter = (List<Category>) session.getAttribute("categoryFilter");
190
        if(categoryFilter == null) {
191
            return "redirect:/management/segment-activity";
192
        }
193

  
194
        Category category = categoryService.getCategoryById(categoryId);
195
        if(category != null) {
196
            categoryFilter.remove(category);
197
        }
198
        session.setAttribute("categoryFilter", categoryFilter);
199
        return "redirect:/management/segment-activity";
200
    }
201

  
202
    /**
203
     * Method adding type to the filter for current work units
204
     * @param model     Object for passing data to the UI
205
     * @param typeId    Identifier of type that will be added
206
     * @param session   Session with attributes
207
     * @return          HTML template
208
     */
209
    @PostMapping("/addTypeFilter")
210
    public String addType(Model model,
211
                          @RequestParam(value = "selectedType", required = false) Long typeId,
212
                          HttpSession session) {
213

  
214
        List<WuType> typeFilter = (List<WuType>) session.getAttribute("typeFilter");
215
        if(typeFilter == null) {
216
            typeFilter = new ArrayList<>();
217
        }
218

  
219
        WuType wuType = wuTypeService.getWuTypeById(typeId);
220
        if(wuType != null) {
221
            typeFilter.add(wuType);
222
        }
223
        session.setAttribute("typeFilter", typeFilter);
224
        return "redirect:/management/segment-activity";
225
    }
226

  
227
    /**
228
     * Method removing type from filter for current work units
229
     * @param model     Object for passing data to the UI
230
     * @param typeId    Identifier of type that will be removed
231
     * @param session   Session with attributes
232
     * @return          HTML template
233
     */
234
    @PostMapping("/removeTypeFilter")
235
    public String removeType(Model model,
236
                             @RequestParam(value = "selectedType", required = false) Long typeId,
237
                             HttpSession session) {
238

  
239
        List<WuType> typeFilter = (List<WuType>) session.getAttribute("typeFilter");
240
        if(typeFilter == null) {
241
            return "redirect:/management/segment-activity";
242
        }
243

  
244
        WuType wuType = wuTypeService.getWuTypeById(typeId);
245
        if(wuType != null) {
246
            typeFilter.remove(wuType);
247
        }
248
        session.setAttribute("typeFilter", typeFilter);
249
        return "redirect:/management/segment-activity";
250
    }
251

  
252
    /**
253
     * Method assigning activity to selected work units
254
     * @param model         Object for passing data to the UI
255
     * @param selectedUnits Selected work units
256
     * @param redirectAttrs Attributes for redirection
257
     * @param session       Session with attributes
258
     * @return              HTML template
259
     */
260
    @PostMapping("/assignActivity")
261
    public String assignActivity(Model model,
262
                                 @RequestParam(value = "selectedUnits", required = false) List<WorkUnit> selectedUnits,
263
                                 RedirectAttributes redirectAttrs,
264
                                 HttpSession session) {
265

  
266
        if(selectedUnits == null || selectedUnits.isEmpty()) {
267
            selectedUnits = (List<WorkUnit>) session.getAttribute("selectedWU");
268
            session.removeAttribute("selectedWU");
269
            if(selectedUnits == null || selectedUnits.isEmpty()) {
270
                redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Nothing was selected");
271
                LOGGER.info("@PostMapping(\"/management/segment-activity\" - Nothing was selected");
272
                return "redirect:/management/segment-activity";
273
            }
274
        }
275

  
276
        Activity activity = (Activity) session.getAttribute("activity");
277
        if(activity != null) {
278

  
279
            for(WorkUnit workUnit : selectedUnits) {
280
                workUnit.setActivity(activity);
281
                workUnitService.saveWorkUnit(workUnit);
282
            }
283

  
284
        } else {
285
            redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Error occurred while getting selected activity");
286
            LOGGER.info("@PostMapping(\"/management/segment-activity\" - Session has problem with activity extract");
287
            return "redirect:/management/segment-activity";
288
        }
289

  
290
        redirectAttrs.addFlashAttribute("successMessage", "Activity was assigned to all selected work units (" + selectedUnits.size() + ")");
291
        LOGGER.info("@PostMapping(\"/management/segment-activity\" - Activity was assigned to all selected work units");
292
        return "redirect:/management/segment-activity";
293
    }
294

  
295
    /**
296
     * Method testing if there are some work units with activities
297
     * @param model         Object for passing data to the UI
298
     * @param selectedUnits Selected work units
299
     * @param redirectAttrs Attributes for redirection
300
     * @param session       Session with attributes
301
     * @return              HTML template
302
     */
303
    @PostMapping("/testWU")
304
    public String testWU(Model model,
305
                         @RequestParam(value = "selectedUnits", required = false) List<WorkUnit> selectedUnits,
306
                         RedirectAttributes redirectAttrs,
307
                         HttpSession session) {
308

  
309
        if(selectedUnits == null || selectedUnits.isEmpty()) {
310
            redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Nothing was selected");
311
            LOGGER.info("@PostMapping(\"/management/segment-activity\" - Nothing was selected");
312
            return "redirect:/management/segment-activity";
313
        }
314

  
315
        List<Long> ids = new ArrayList<>();
316
        boolean notEmpty = false;
317
        for(WorkUnit workUnit : selectedUnits) {
318
            ids.add(workUnit.getId());
319
            if(workUnit.getActivity() != null) {
320
                notEmpty = true;
321
            }
322
        }
323

  
324
        if(notEmpty) {
325
            session.setAttribute("selectedWU", selectedUnits);
326
            redirectAttrs.addFlashAttribute("selectedWU", selectedUnits);
327
            redirectAttrs.addFlashAttribute("selectedIds", ids);
328
            return "redirect:/management/segment-activity";
329
        }
330

  
331
        return assignActivity(model, selectedUnits, redirectAttrs, session);
332
    }
333

  
334
    /**
335
     * Method for applying filters defined by user into model
336
     * @param model             Object for passing data to the UI
337
     * @param foundedProject    Selected project for showing template
338
     * @param session           Session with attributes
339
     */
340
    private void applyFilter(Model model,
341
                             Project foundedProject,
342
                             HttpSession session) {
343

  
344
        Set<Category> categories = new HashSet<>();
345
        Set<WuType> types = new HashSet<>();
346
        for(ProjectInstance instance : foundedProject.getProjectInstances()) {
347
            categories.addAll(instance.getCategories());
348
            types.addAll(instance.getWuTypes());
349
        }
350

  
351
        //Remove categories in filter
352
        List<Category> categoryList = new ArrayList<>(categories);
353
        List<Category> categoryFilter = (List<Category>) session.getAttribute("categoryFilter");
354
        if(categoryFilter != null) {
355
            categoryFilter.forEach(categoryList::remove);
356
        }
357

  
358
        List<WuType> wuTypeList = new ArrayList<>(types);
359
        List<WuType> wuTypeFilter = (List<WuType>) session.getAttribute("typeFilter");
360
        if(wuTypeFilter != null) {
361
            wuTypeFilter.forEach(wuTypeList::remove);
362
        }
363

  
364
        //Sort categories and types
365
        Collections.sort(categoryList, Comparator.comparing(Category::getName, String.CASE_INSENSITIVE_ORDER));
366
        Collections.sort(wuTypeList, Comparator.comparing(WuType::getName, String.CASE_INSENSITIVE_ORDER));
367

  
368
        List<Long> categoryFilterId = new ArrayList<>();
369
        List<Long> wuTypeFilterId = new ArrayList<>();
370

  
371
        if(categoryFilter != null) {
372
            for(Category category : categoryFilter) {
373
                categoryFilterId.add(category.getId());
374
            }
375
        }
376

  
377
        if(wuTypeFilter != null) {
378
            for(WuType type : wuTypeFilter) {
379
                wuTypeFilterId.add(type.getId());
380
            }
381
        }
382

  
383
        if(session.getAttribute("categoryFilter") != null && !categoryFilter.isEmpty() && session.getAttribute("typeFilter") != null && !wuTypeFilter.isEmpty()) {
384
            model.addAttribute("workunits", workUnitRepository.getFiltredWorkUnits(foundedProject.getId(), categoryFilterId, wuTypeFilterId, categoryFilterId.size()));
385
        } else if(session.getAttribute("categoryFilter") != null && !categoryFilter.isEmpty()) {
386
            model.addAttribute("workunits", workUnitRepository.getCategoryFiltredWorkUnits(foundedProject.getId(), categoryFilterId, categoryFilterId.size()));
387
        } else if(session.getAttribute("typeFilter") != null && !wuTypeFilter.isEmpty()) {
388
            model.addAttribute("workunits", workUnitRepository.getTypeFiltredWorkUnits(foundedProject.getId(), wuTypeFilterId));
389
        } else {
390
            model.addAttribute("workunits", foundedProject.getWorkUnits());
391
        }
392

  
393
        model.addAttribute("categories", categoryList);
394
        model.addAttribute("types", wuTypeList);
395
    }
396
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/management/SegmentCategoryController.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.controller.management;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.Constants;
4
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project;
5
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.*;
6
import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService;
7
import cz.zcu.fav.kiv.antipatterndetectionapp.service.managment.*;
8
import cz.zcu.fav.kiv.antipatterndetectionapp.utils.Utils;
9
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.interfaces.DatabaseObject;
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.data.util.Pair;
14
import org.springframework.stereotype.Controller;
15
import org.springframework.ui.Model;
16
import org.springframework.web.bind.annotation.GetMapping;
17
import org.springframework.web.bind.annotation.PostMapping;
18
import org.springframework.web.bind.annotation.RequestParam;
19
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
20

  
21
import javax.servlet.http.HttpSession;
22
import java.util.*;
23

  
24
/**
25
 * This class contains all endpoints of segment-category.html
26
 */
27
@Controller
28
public class SegmentCategoryController {
29

  
30
    private final Logger LOGGER = LoggerFactory.getLogger(SegmentCategoryController.class);
31

  
32
    @Autowired
33
    private CategoryService categoryService;
34

  
35
    @Autowired
36
    private IterationService iterationService;
37

  
38
    @Autowired
39
    private PhaseService phaseService;
40

  
41
    @Autowired
42
    private ActivityService activityService;
43

  
44
    @Autowired
45
    private ProjectService projectService;
46

  
47
    @Autowired
48
    private ProjectInstanceService projectInstanceService;
49

  
50
    @Autowired
51
    private WorkUnitService workUnitService;
52

  
53
    /**
54
     * Method for showing category page for specific project
55
     * @param model         Object for passing data to the UI
56
     * @param project       Selected project for showing categories
57
     * @param redirectAttrs Attributes for redirection
58
     * @return              HTML template
59
     */
60
    @GetMapping("/management/segment-category")
61
    public String category(Model model,
62
                                @RequestParam(value = "selectedProject", required = false) Long project,
63
                                RedirectAttributes redirectAttrs,
64
                                HttpSession session) {
65

  
66
        // First open in session
67
        if(project == null && session.getAttribute("project") == null) {
68
            model.addAttribute("projects", projectService.getAllProjects());
69
            LOGGER.info("@GetMapping(\"/management/segment-category\") - Accessing page");
70
            return "management/segment-category";
71
        }
72

  
73
        // Project selected
74
        if(project != null) {
75
            session.setAttribute("project", project);
76
            Utils.sessionRecreate(session);
77
        }
78

  
79
        project = (long) session.getAttribute("project");
80

  
81
        Project foundedProject = projectService.getProjectById(project);
82
        if(foundedProject == null) {
83
            if(!Objects.equals(project, Constants.DEFAULT_ID)) {
84
                redirectAttrs.addFlashAttribute("errorMessage", "Project not found");
85
                LOGGER.info("@GetMapping(\"/management/segment-category/\") - Project not found");
86
            } else {
87
                LOGGER.info("@GetMapping(\"/management/segment-category/\") - Accessing page");
88
            }
89
            model.addAttribute("projects", projectService.getAllProjects());
90
            return "management/segment-category";
91
        }
92

  
93
        //Add all categories into model
94
        List<Category> categories = new ArrayList<>();
95
        for(ProjectInstance projectInstance : foundedProject.getProjectInstances()) {
96
            categories.addAll(projectInstance.getCategories());
97
        }
98
        Collections.sort(categories, Comparator.comparing(Category::getName, String.CASE_INSENSITIVE_ORDER));
99

  
100
        model.addAttribute("categories", categories);
101
        model.addAttribute("projects", projectService.getAllProjects());
102
        LOGGER.info("@GetMapping(\"/management/segment-category/\") - Accessing page");
103

  
104
        return "/management/segment-category";
105
    }
106

  
107
    /**
108
     * Method preparing selected categories for transformation into defined type
109
     * @param model                 Object for passing data to the UI
110
     * @param selectedCategories    List of selected categories
111
     * @param submitType            Type of required attribute (1 - Iteration, 2 - Phase, 3 - Activity)
112
     * @param submitId              String with type of required attribute and id of one selected category
113
     * @param redirectAttrs         Attributes for redirection
114
     * @return                      HTML template
115
     */
116
    @PostMapping(value = "/categoryChange")
117
    public String categoryChange(Model model,
118
                                 @RequestParam(value = "selectedBox", required = false) List<Category> selectedCategories,
119
                                 @RequestParam(value = "submitType", required = false) Integer submitType,
120
                                 @RequestParam(value = "submitId", required = false) String submitId,
121
                                 RedirectAttributes redirectAttrs) {
122

  
123
        if(selectedCategories != null && submitType != null) { // Selected submit
124

  
125
            if(submitType <= 0 || submitType > Constants.SUBMIT_TYPES) { // ERROR: Unknown submitType
126
                redirectAttrs.addFlashAttribute("errorMessage", "ERROR: Application error occurred");
127
                LOGGER.warn("@PostMapping(\"/management/segment-category\") - Cannot proceed: Unknown submitType " + submitType);
128
                return "redirect:/management/segment-category";
129
            }
130

  
131
        } else if(submitId != null) { //Inline submit
132

  
133
            Pair<Integer, Long> resultPair = extractTypeAndId(submitId);
... Rozdílový soubor je zkrácen, protože jeho délka přesahuje max. limit.

Také k dispozici: Unified diff