Revize 1bb59448
Přidáno uživatelem Jiri Trefil před téměř 2 roky(ů)
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java | ||
---|---|---|
4 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.ConfigRepository; |
5 | 5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserConfigurationJoinRepository; |
6 | 6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.ConfigurationService; |
7 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
|
7 | 8 |
import org.springframework.beans.factory.annotation.Autowired; |
9 |
import org.springframework.http.HttpStatus; |
|
8 | 10 |
import org.springframework.http.ResponseEntity; |
9 | 11 |
import org.springframework.web.bind.annotation.*; |
10 | 12 |
|
13 |
import java.util.HashMap; |
|
11 | 14 |
import java.util.List; |
15 |
import java.util.Map; |
|
12 | 16 |
|
13 | 17 |
/** |
14 | 18 |
* @Author Jiri Trefil |
... | ... | |
26 | 30 |
return response; |
27 | 31 |
} |
28 | 32 |
|
29 |
@PostMapping(value="/get_configuration")
|
|
30 |
public ResponseEntity<String> getUserConfigurations(@RequestBody User user){ |
|
33 |
@PostMapping(value="/configuration") |
|
34 |
public ResponseEntity<String> getUserConfigurations(@RequestBody User user) {
|
|
31 | 35 |
ResponseEntity<String> response = this.configurationService.getUserConfigurations(user); |
32 | 36 |
return response; |
33 | 37 |
} |
34 | 38 |
|
39 |
@PostMapping(value="/configuration_name") |
|
40 |
public ResponseEntity<String> getConfigurationNames(@RequestBody User user) { |
|
41 |
Map<String, Object> json = new HashMap<>(); |
|
42 |
List<String> configuration = this.configurationService.getConfigurationNames(user); |
|
43 |
if(configuration == null) { |
|
44 |
json.put("message", "internal sever error"); |
|
45 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json), HttpStatus.INTERNAL_SERVER_ERROR); |
|
46 |
} |
|
47 |
json.put("message", "ok"); |
|
48 |
json.put("configuration_names", configuration); |
|
49 |
String jsonString = JSONBuilder.buildJSON(json); |
|
50 |
return new ResponseEntity<>(jsonString, HttpStatus.OK); |
|
51 |
} |
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
35 | 56 |
|
36 | 57 |
|
37 | 58 |
|
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/UserConfigurationJoin.java | ||
---|---|---|
15 | 15 |
public class UserConfigurationJoin { |
16 | 16 |
@EmbeddedId |
17 | 17 |
private UserConfigKey id; |
18 |
|
|
18 |
private String configurationName; |
|
19 | 19 |
|
20 | 20 |
public UserConfigurationJoin(){ |
21 | 21 |
} |
22 | 22 |
|
23 |
public UserConfigurationJoin(UserConfigKey id){ |
|
23 |
|
|
24 |
public UserConfigurationJoin(UserConfigKey id, String configurationName){ |
|
25 |
this.configurationName = configurationName; |
|
24 | 26 |
this.id = id; |
25 | 27 |
} |
28 |
|
|
29 |
public String getConfigurationName() { |
|
30 |
return configurationName; |
|
31 |
} |
|
32 |
|
|
26 | 33 |
public UserConfigKey getUserConfigKey(){ |
27 | 34 |
return this.id; |
28 | 35 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/ConfigRepository.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.Configuration; |
4 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.UserConfigKey; |
5 |
import org.springframework.beans.factory.annotation.Value; |
|
5 | 6 |
import org.springframework.data.jpa.repository.JpaRepository; |
6 | 7 |
import org.springframework.data.jpa.repository.Modifying; |
7 | 8 |
import org.springframework.data.jpa.repository.Query; |
... | ... | |
10 | 11 |
import java.util.List; |
11 | 12 |
@Repository |
12 | 13 |
public interface ConfigRepository extends JpaRepository<Configuration,Integer> { |
14 |
@Value("${default_user_id}") |
|
15 |
int defaultUserId = 0; |
|
13 | 16 |
Configuration findConfigurationById(int id); |
14 | 17 |
Configuration findConfigurationByConfigHash(String hash); |
15 | 18 |
//query to get all public configurations |
... | ... | |
22 | 25 |
List<Configuration> getAllUserConfigurations(int userId); |
23 | 26 |
|
24 | 27 |
|
28 |
@Query("select userconfig.configurationName from UserConfigurationJoin userconfig where userconfig.id.userId = ?1 or userconfig.id.userId=2 ") |
|
29 |
List<String> getAllUserConfigurationNames(int userId); |
|
30 |
|
|
31 |
|
|
32 |
|
|
25 | 33 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationService.java | ||
---|---|---|
3 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
4 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.UserConfiguration; |
5 | 5 |
import org.springframework.http.ResponseEntity; |
6 |
|
|
6 |
import java.util.List; |
|
7 | 7 |
public interface ConfigurationService { |
8 | 8 |
//upload configuration |
9 | 9 |
ResponseEntity<String> addConfiguration(Configuration cfg); |
... | ... | |
13 | 13 |
//get all configurations available to user |
14 | 14 |
ResponseEntity<String> getUserConfigurations(User user); |
15 | 15 |
|
16 |
List<String> getConfigurationNames(User user); |
|
16 | 17 |
|
17 | 18 |
|
18 | 19 |
|
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/ConfigurationServiceImplementation.java | ||
---|---|---|
100 | 100 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.BAD_REQUEST); |
101 | 101 |
} |
102 | 102 |
//save the relation between user and configuration |
103 |
this.userConfigurationJoinRepository.save(new UserConfigurationJoin(key)); |
|
103 |
this.userConfigurationJoinRepository.save(new UserConfigurationJoin(key,configuration.getConfigurationName()));
|
|
104 | 104 |
json.put("message","configuration added to collection."); |
105 | 105 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.OK); |
106 | 106 |
} |
... | ... | |
119 | 119 |
String jsonString = JSONBuilder.buildJSON(json); |
120 | 120 |
return new ResponseEntity<>(jsonString,HttpStatus.OK); |
121 | 121 |
} |
122 |
|
|
123 |
@Override |
|
124 |
public List<String> getConfigurationNames(User user) { |
|
125 |
final String userName = user.getName(); |
|
126 |
if(userName == null){ |
|
127 |
return null; |
|
128 |
} |
|
129 |
//client can only send his name - he obviously does not know his id in db, we have to query that |
|
130 |
User userInfo = this.userService.getUserByName(userName); |
|
131 |
//fetch all configurations this particular user can see |
|
132 |
//ie all public configs + configurations uploaded by this particular user |
|
133 |
List<String> configurationNames = this.configurationRepository.getAllUserConfigurationNames(userInfo.getId()); |
|
134 |
return configurationNames; |
|
135 |
} |
|
122 | 136 |
} |
src/main/resources/application.properties | ||
---|---|---|
31 | 31 |
# security |
32 | 32 |
auth.realm.login=http://localhost:8081/login |
33 | 33 |
auth.realm.logout=http://localhost:8081/logout |
34 |
auth.realm.authenticate=http://localhost:8081/authenticate |
|
34 |
auth.realm.authenticate=http://localhost:8081/authenticate |
|
35 |
|
|
36 |
#default configuration user |
|
37 |
default_user_id = 2 |
Také k dispozici: Unified diff
#10523 vytvoreni default usera pro konfigurace. mirny refactor konfiguracni service