-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserController.java
301 lines (226 loc) · 10.8 KB
/
UserController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.socibby.controller;
import com.socibby.model.Category;
import com.socibby.model.Organization;
import com.socibby.model.Photo;
import com.socibby.model.User;
import com.socibby.service.CategoryService;
import com.socibby.service.OrganizationService;
import com.socibby.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Controller
@RequestMapping("/user")
public class UserController {
private final UserService userService;
private final CategoryService categoryService;
private final OrganizationService organizationService;
@Autowired
public UserController(UserService userService, CategoryService categoryService, OrganizationService organizationService) {
this.userService = userService;
this.categoryService = categoryService;
this.organizationService = organizationService;
}
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register(@ModelAttribute("registerUser") User user, HttpSession session, ModelMap modelMap) {
User validUser = (User) session.getAttribute("user");
Organization organization = (Organization) session.getAttribute("organization");
if (organization != null) {
return "home/multiAccount";
}
if (validUser != null) {
List<Organization> organizationByUser = organizationService.getOrganizationByUser(validUser);
modelMap.addAttribute("organizations", organizationByUser);
return "home/dashboard";
}
return "user/register";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String save(@ModelAttribute("registerUser") @Valid User user, BindingResult bindingResult, ModelMap modelMap,
@RequestParam("your_picture") MultipartFile file, HttpSession session) {
if (bindingResult.hasErrors()) {
return "user/register";
}
imageUploadOperations(user.getUsername(), file);
String filePath = user.getUsername() + File.separator + "profile.png";
Photo photo = new Photo(filePath);
user.setPhoto(photo);
Set<Category> categorySet = user.getCategorySet();
Set<Category> realCategorySet = new HashSet<Category>();
assignCategory(categorySet, realCategorySet, user);
userService.insertUser(user);
session.setAttribute("user", user);
List<Organization> organizationByUser = organizationService.getOrganizationByUser(user);
modelMap.addAttribute("organizations", organizationByUser);
return "home/dashboard";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginPage(@ModelAttribute("user") User user, HttpSession session, ModelMap modelMap) {
Organization organization = (Organization) session.getAttribute("organization");
User validUser = (User) session.getAttribute("user");
if (organization != null) {
return "home/multiAccount";
}
if (validUser != null) {
List<Organization> organizationByUser = organizationService.getOrganizationByUser(validUser);
modelMap.addAttribute("organizations", organizationByUser);
return "home/dashboard";
}
return "user/login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute("user") User user, ModelMap modelMap, HttpSession session) {
String username = user.getUsername();
String password = user.getPassword();
User validUser = userService.validateUser(username, password);
if (validUser != null) {
session.setAttribute("user", validUser);
List<Organization> organizationByUser = organizationService.getOrganizationByUser(validUser);
modelMap.addAttribute("organizations", organizationByUser);
return "home/dashboard";
}
modelMap.addAttribute("errorLogin", "Giriş bilgileriniz geçersiz !");
return "user/login";
}
@RequestMapping(value = "/profile", method = RequestMethod.GET)
public String profilePage(@ModelAttribute("updateUser") User user, HttpSession session, ModelMap modelMap) {
User validUser = (User) session.getAttribute("user");
if (validUser != null) {
modelMap.addAttribute("userInformation", validUser);
return "user/profile";
}
return "index";
}
@RequestMapping(value = "/update/{userId}", method = RequestMethod.GET)
public String updateProfile(@PathVariable("userId") Long id, HttpSession session, ModelMap modelMap) {
User validUser = (User) session.getAttribute("user");
if (validUser != null) {
if (validUser.getId() == id) {
User user = userService.getUserById(id);
modelMap.addAttribute("userInformation", validUser);
modelMap.addAttribute("user", user);
return "user/editProfile";
} else {
modelMap.addAttribute("userInformation", validUser);
return "user/profile";
}
}
return "index";
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String updateUser(@ModelAttribute("user") User user, HttpSession session, ModelMap modelMap) {
User validUser = (User) session.getAttribute("user");
user.setId(validUser.getId());
user.setUsername(validUser.getUsername());
user.setPhoto(validUser.getPhoto());
Set<Category> categorySet = user.getCategorySet();
Set<Category> realCategorySet = new HashSet<Category>();
assignCategory(categorySet, realCategorySet, user);
System.out.println(user);
userService.updateUser(user);
session.setAttribute("user", user);
modelMap.addAttribute("userInformation", user);
modelMap.addAttribute("user", user);
return "user/profile";
}
@RequestMapping()
public String home(HttpSession session, ModelMap modelMap) {
User validUser = (User) session.getAttribute("user");
if (validUser != null) {
List<Organization> organizationByUser = organizationService.getOrganizationByUser(validUser);
modelMap.addAttribute("organizations", organizationByUser);
return "home/dashboard";
}
return "index";
}
private void imageUploadOperations(String userName, MultipartFile file) {
// TOMCAT_HOME_PATH --> /opt/tomcat
String TOMCAT_HOME_PATH = System.getProperty("catalina.home");
String IMAGES_FOLDER_NAME = "images";
// " /opt/tomcat + / + images " folder path
String IMAGES_FOLDER_PATH = TOMCAT_HOME_PATH + File.separator + IMAGES_FOLDER_NAME;
File IMAGES_DIRECTORY = new File(IMAGES_FOLDER_PATH);
String IMAGES_DIRECTORY_ABSOLUTE_PATH = IMAGES_DIRECTORY.getAbsolutePath() + File.separator;
if (file.isEmpty()) {
System.out.println("IMAGE IS EMPTY");
String USER_FOLDER_NAME = IMAGES_DIRECTORY_ABSOLUTE_PATH + userName;
File USER_DIRECTORY = new File(USER_FOLDER_NAME);
if (!USER_DIRECTORY.exists()) {
System.out.println("KULLANICIYA AIT FOLDER OLUŞTURULDU --> PATH : " + USER_FOLDER_NAME);
USER_DIRECTORY.mkdirs();
}
try {
// ---> /opt/tomcat/images/default.png
File defaultFile = new File(IMAGES_DIRECTORY_ABSOLUTE_PATH + "default.png");
BufferedImage bufferedImage = ImageIO.read(defaultFile);
String uploadedFilePath = USER_FOLDER_NAME + File.separator + "profile.png";
// e.g omerfarukgenc34/profile.png
ImageIO.write(bufferedImage, "png", new File(uploadedFilePath));
} catch (Exception e) {
System.out.println("Hata olduuuuuuuuuu!");
}
// File/image is not empty
} else {
if (!IMAGES_DIRECTORY.exists()) {
System.out.println("IMAGES KLASORU OLUŞTURULDU --> PATH : " + IMAGES_DIRECTORY_ABSOLUTE_PATH);
IMAGES_DIRECTORY.mkdirs();
}
String USER_FOLDER_NAME = IMAGES_DIRECTORY_ABSOLUTE_PATH + userName;
File USER_DIRECTORY = new File(USER_FOLDER_NAME);
if (!USER_DIRECTORY.exists()) {
System.out.println("KULLANICIYA AIT FOLDER OLUŞTURULDU --> PATH : " + USER_FOLDER_NAME);
USER_DIRECTORY.mkdirs();
}
try {
String fileName = "profile.png";
String uploadedFilePath = USER_FOLDER_NAME + File.separator + fileName;
// e.g omerfarukgenc34/profile.png
File image = new File(uploadedFilePath);
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(image));
stream.write(file.getBytes());
stream.close();
} catch (Exception e) {
System.out.println("Hata");
}
}
}
private void assignCategory(Set<Category> categorySet, Set<Category> realCategorySet, User user) {
// realCategorySet kullanıcının seçmiş olduğu kategorilerin gerçek değeriyle dolduruldu.
for (Category category : categorySet) {
String categoryName = category.getName();
Category categoryByName = categoryService.getCategoryByName(categoryName);
realCategorySet.add(categoryByName);
}
user.getCategorySet().removeAll(categorySet);
// Tüm kategoriler sırasıyla gezilip kullanıcıya hepsi teker teker eklendi.
for (Category category : realCategorySet) {
user.getCategorySet().add(category);
category.getUserSet().add(user);
categoryService.updateCategory(category);
}
}
@ModelAttribute("categoryList")
public List<String> prepareCategoryList() {
List<Category> allCategory = categoryService.getAllCategory();
List<String> categoryNameList = new ArrayList<String>();
for (Category category : allCategory) {
String categoryName = category.getName();
categoryNameList.add(categoryName);
}
return categoryNameList;
}
}