Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. public void setUser(
  2.             @ApiParam(value = "строка текст, логин пользователя для определения наличия пользователя в базе", required = true) @RequestParam(value = "sLogin", required = true) String sLogin,
  3.             //@ApiParam(value = "строка текст, пароль для пользователя", required = true) @RequestParam(value = "sPassword", required = true) String sPassword,
  4.             //@ApiParam(value = "строка текст, имя пользователя", required = true) @RequestParam(value = "sName", required = true) String sName,
  5.             //@ApiParam(value = "строка текст, фамилия пользователя", required = true) @RequestParam(value = "sDescription", required = true) String sDescription,
  6.             //@ApiParam(value = "строка текст, имейл пользователя, опциональный параметр", required = false) @RequestParam(value = "sEmail", required = false) String sEmail,
  7.             @ApiParam(value = "JSON-объект с параметрами: " +
  8.                     "sPassword - (обязательный при создании нового пользователя) строка текст, логин пользователя для определения наличия пользователя в базе; " +
  9.                     "sName - (обязательный) строка текст, имя пользователя; " +
  10.                     "sDescription - (обязательный) строка текст, фамилия пользователя; " +
  11.                     "sEmail - строка текст, имейл пользователя, опциональный параметр", required = true) @RequestBody String body)
  12.             throws Exception {
  13.  
  14.         log.info("Method setUser startred");
  15.         String sPassword = null;
  16.         String sName = null;
  17.         String sDescription = null;
  18.         String sEmail = null;
  19.  
  20.         if(body != null){
  21.             Map<String, Object> mBody;
  22.             try {
  23.                 mBody = (Map<String, Object>) JSONValue.parse(body);
  24.             } catch (Exception e){
  25.                 throw new IllegalArgumentException("Error parse JSON body: " + e.getMessage());
  26.             }
  27.             if(mBody != null){
  28.                 if (mBody.containsKey("sPassword")) {
  29.                     sPassword = (String) mBody.get("sPassword");
  30.                 }
  31.                 if (mBody.containsKey("sName")) {
  32.                     sName = (String) mBody.get("sName");
  33.                 } else {
  34.                     throw new Exception("The sName in RequestBody is not defined");
  35.                 }
  36.                 if (mBody.containsKey("sDescription")) {
  37.                     sDescription = (String) mBody.get("sDescription");
  38.                 } else {
  39.                     throw new Exception("The sDescription in RequestBody is not defined");
  40.                 }
  41.                 if (mBody.containsKey("sEmail")) {
  42.                     sEmail = (String) mBody.get("sEmail");
  43.                 }
  44.             }
  45.         }
  46.  
  47.         User oUser = identityService.createUserQuery().userId(sLogin).singleResult();
  48.         if (oUser == null) {
  49.             log.info("Creating new user");
  50.             oUser = identityService.newUser(sLogin);
  51.             if(sPassword == null || sPassword.equals("")){
  52.                 throw new Exception("The password for new User is not defined");
  53.             } else {
  54.                 oUser.setPassword(sPassword);
  55.             }
  56.         }
  57.         oUser.setFirstName(sName);
  58.         oUser.setLastName(sDescription);
  59.         if (sEmail != null) {
  60.             oUser.setEmail(sEmail);
  61.         }
  62.         log.info("Saving user to database");
  63.         identityService.saveUser(oUser);
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement