Skip to content

Commit

Permalink
Merge pull request #172 from integratedmodelling/IM-393-Change-email-…
Browse files Browse the repository at this point in the history
…and-download-CERTIFICATE

Im 393 change email and download certificate
  • Loading branch information
kristinaBc3 committed Jun 14, 2024
2 parents 8556bbb + 49133ea commit ad7f9e0
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface MongoTagRepository extends ResourceRepository<MongoTag, String>

List<MongoTag> findAllByTagElementId(String id);

List<MongoTag> findAllByTagElementIdIsNull();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

import org.integratedmodelling.klab.api.API;
import org.integratedmodelling.klab.hub.tags.dto.TagNotification;
import org.integratedmodelling.klab.hub.tags.enums.ITagElementEnum;
import org.integratedmodelling.klab.hub.tags.payload.TagRequest;
import org.integratedmodelling.klab.hub.tags.services.TagNotificationService;
import org.integratedmodelling.klab.hub.users.dto.User;
import org.integratedmodelling.klab.hub.users.services.UserProfileService;
import org.integratedmodelling.klab.rest.HubNotificationMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import net.minidev.json.JSONObject;

@RestController
public class TagNotificationController {

Expand All @@ -39,7 +42,28 @@ public class TagNotificationController {
List<TagNotification> tagNotifications = tagNotificationService.getUserTagNotifications(user);
return ResponseEntity.status(HttpStatus.OK).body(tagNotifications);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}

/**
* Create tag notification
* @param username
* @param TagRequest
* @return
*/
@PostMapping(value = API.HUB.TAG_NOTIFICATIONS, produces = "application/json")
public ResponseEntity< ? > createUserTagNotification(@RequestBody TagRequest tagRequest) {
try {

TagNotification tagNotification = tagNotificationService.createTagNotification(
ITagElementEnum.valueOf(tagRequest.getiTagElement()), tagRequest.getiTagElementId(),
HubNotificationMessage.Type.valueOf(tagRequest.getType()), Boolean.valueOf(tagRequest.getVisible()),
tagRequest.getName(), tagRequest.getTitle(), tagRequest.getMessage());

return ResponseEntity.status(HttpStatus.OK).body(tagNotification);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}

Expand All @@ -50,13 +74,13 @@ public class TagNotificationController {
*/
@DeleteMapping(value = API.HUB.TAG_NOTIFICATION_ID)
public ResponseEntity< ? > deletetagNotification(@PathVariable("id") String id) {
JSONObject resp = new JSONObject();

try {
TagNotification tagNotification = tagNotificationService.deleteTagNotification(id);
return ResponseEntity.status(HttpStatus.OK).body(tagNotification);
} catch (Exception e) {
// resp.appendField("Message", String.format("Role %s is not valid", role));
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Objects that implementing this interface can use Tags
* The tags can be for users, groups... This interface allow all this classes to be in the MongoTag class
*
* @author kristina
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.integratedmodelling.klab.hub.tags.enums;

/**
* Use for determinate entity of MongoTag, if is NULL is generic
*
* @author kristina
*
*/
public enum ITagElementEnum {
USER, GROUP;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.integratedmodelling.klab.hub.tags.payload;

public class TagRequest {

private String id;
private String type;
private String iTagElement;
private String iTagElementId;
private String name;
private String title;
private String message;
private String visible;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getiTagElement() {
return iTagElement;
}
public void setiTagElement(String iTagElement) {
this.iTagElement = iTagElement;
}
public String getiTagElementId() {
return iTagElementId;
}
public void setiTagElementId(String iTagElementId) {
this.iTagElementId = iTagElementId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getVisible() {
return visible;
}
public void setVisible(String visible) {
this.visible = visible;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import org.integratedmodelling.klab.hub.tags.dto.ITagElement;
import org.integratedmodelling.klab.hub.tags.dto.MongoTag;
import org.integratedmodelling.klab.hub.tags.dto.TagNotification;
import org.integratedmodelling.klab.hub.tags.enums.ITagElementEnum;
import org.integratedmodelling.klab.hub.tags.enums.TagNameEnum;
import org.integratedmodelling.klab.hub.users.dto.User;
import org.integratedmodelling.klab.hub.users.services.UserService;
import org.integratedmodelling.klab.rest.HubNotificationMessage;
import org.integratedmodelling.klab.rest.HubNotificationMessage.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -32,10 +35,73 @@ public class TagNotificationService {
@Autowired
private MongoTagRepository mongoTagRepository;

@Autowired
private UserService userService;

/**
* Create TagNotification
*
* @param iTagElementEnum {@link ITagElementEnum} Element for which the notification is.
* @param id Id of element, empty if iTagElementEnum is ALL
* @param tagNotifactionType {@link HubNotificationMessage.Type} Type of notification
* @param visible Visible or not
* @param tagName To use with frontend predefined titles and messages use {@link TagNameEnum} otherwise free string
* @param title Title of notification if tagName is not a {@link TagNameEnum}
* @param message Message of notification if tagName is not a {@link TagNameEnum}
* @return
*/
public TagNotification createTagNotification(ITagElementEnum iTagElementEnum, String id,
HubNotificationMessage.Type tagNotifactionType, Boolean visible, String tagName, String title, String message) {

/* Create Mongo Tag */
MongoTag tag = new MongoTag();

tag.setName(tagName);
tag.setVisible(visible);

setITagElementById(tag, iTagElementEnum, id);

tag = mongoTagRepository.save(tag);

/* Create TagNotification */
TagNotification tagNotification = new TagNotification();

tagNotification.setTag(tag);
tagNotification.setType(tagNotifactionType);

tagNotification.setTitle(title);
tagNotification.setMessage(message);

try {
tagNotificationRepository.save(tagNotification);
} catch (Exception e) {
throw new KlabException("Error saving tag notification.", e);
}

return tagNotification;

}

private void setITagElementById(MongoTag tag, ITagElementEnum iTagElementEnum, String id) {
if (id.isEmpty() || iTagElementEnum == null) {
return;
}

tag.setTagElementId(!id.isEmpty() ? id : null);
switch(iTagElementEnum) {
case USER:
tag.setITagElement(userService.getUserById(id));
break;
case GROUP:
break;
}

}

public TagNotification createWarningUserTagNotification(User user, String tagName, Boolean visible, String title,
String message) {
MongoTag tag = new MongoTag();
tag.setTagElementId(user.getId());
tag.setTagElementId(user.getId().isEmpty() ? null : user.getId());
tag.setITagElement(user);
tag.setName(tagName);
tag.setVisible(visible);
Expand Down Expand Up @@ -65,6 +131,7 @@ public List<TagNotification> getUserTagNotifications(ITagElement iTagElement) {
List<MongoTag> listMongoTags = null;
try {
listMongoTags = mongoTagRepository.findAllByTagElementId(iTagElement.getId());
listMongoTags.addAll(mongoTagRepository.findAllByTagElementIdIsNull());
listTagNotifications = tagNotificationRepository.findAllByTagIn(listMongoTags);
} catch (Exception e) {
logger.error(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.integratedmodelling.klab.hub.users.services;

import org.integratedmodelling.klab.hub.repository.UserRepository;
import org.integratedmodelling.klab.hub.tags.dto.ITagElement;
import org.integratedmodelling.klab.hub.users.dto.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Autowired
private UserRepository userRepository;

public User getUserByUsername(String username) throws UsernameNotFoundException {

return userRepository.findByNameIgnoreCase(username).get();

}

public ITagElement getUserById(String id) {
return userRepository.findById(id).get();
}

}

0 comments on commit ad7f9e0

Please sign in to comment.