How to make this messaging system work as desired - java

My requirement is quite straightforward, I've admin panel through that admin sends messages to all registered users of the System, User and Message Entity relationship is:
#Entity
public class Message implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String message;
#Temporal(TemporalType.TIMESTAMP)
private Date sentDate;
#ManyToMany(mappedBy = "messages", cascade = {CascadeType.MERGE})
private List<User> users;
// setter - getters methods are omitted
}
And:
#Entity
public class User implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long userId;
private String userName;
#Temporal(TemporalType.TIMESTAMP)
private Date userJoinedAt;
#ManyToMany(cascade = { CascadeType.PERSIST })
#JoinTable
private List<Message> messages;
// setter - getters methods are omitted
}
At this point I want that when a new user signs-up in the System, he should only see the admin sent messages that are sent after his sign-up time. I've tried:
public void addMessage(Message message) throws Exception {
message.setMessageSentDate(new Date());
messageService.save(message);
List<Message> messages = messageService.getAll();
List<User> registeredUsers = userService.getAll();
for (User user : registeredUsers) {
for (Message savedMessage : messages) {
if(user.getUserJoinedAt().before(savedMessage.getMessageSentDate())){
user.setMessages(messages);
userService.save(user);
}
}
}
}
But this method is not working as desired, means it adds all the messages present in Messages Entity, even if those are sent before User Joined Date.
How can I make that possible to get desired job don?
UPDATE
public void addMessage(Message message) throws Exception {
message.setMessageSentDate(new Date());
messageService.save(message);
List<Message> messages = messageService.getAll();
List<User> registeredUsers = userService.getAll();
for (User user : registeredUsers) {
// Initializing new variable to store afterJoinedMessages
List<Message> afterJoinedMessages = new CopyOnWriteArrayList<Message>();
for (Message savedMessage : messages) {
if(user.getUserJoinedAt().before(savedMessage.getMessageSentDate())){
// Here adding those messages to afterJoinedMessages List
afterJoinedMessags.add(savedMessage);
// and saving those to user
user.setMessages(afterJoinedMessags);
userService.save(user);
}
}
}
}
Would this be a better solution or the one which is given in answer?

You are adding the whole messages list whenever any of the messages matches the date condition.
Change the call user.setMessages(messages); to a new one you need to create in the User class (addMessage) which simply adds the message to the user messages list. (You can also save the user only once at the end of the loop, not in every iteration).
for (Message savedMessage : messages) {
if(user.getUserJoinedAt().before(savedMessage.getMessageSentDate())){
user.addMessage(savedMessage);
}
}
userService.save(user);
In your User class add something like :
public void addMesage(Message msg) {
messages.add(msg);
}
UPDATE
I am not aware of all the details, but as the message is being created with the current date, I'd expect it will need to be added to ALL existing users (which should have been registered before now).
So .... I'd leave the method like this : (just save the message and add it to all the users).
public void addMessage(Message message) throws Exception {
message.setMessageSentDate(new Date());
messageService.save(message);
List<User> registeredUsers = userService.getAll();
for (User user : registeredUsers) {
user.addMessage(message);
userService.save(user);
}
}

Related

Convert Java object list to entity list

I have multiple objects in my array using . If I then send this to my Spring Boot backend with axios and output the FormData beforehand, I get the following image. That fits. In the backend, however, I need this list of objects as an entity. In this case, of type List. Do I do that?
Frontend code:
let data = new FormData();
...
data.append("zugeordnet", JSON.stringify(personNamen));
await axios.post("/neuerEintrag", data,...)
React:
Backend:
#PostMapping("/neuerEintrag")
public String neuerEintrag(HttpServletRequest req,#RequestParam("zugeordnet") List<?> zugeordnet,..) {
List<User> userListe = (List<User>) zugeordnet;
for(User inListe : userListe) //ERROR here
{
System.out.println("USER :" + inListe);
}
...
}
java.lang.ClassCastException: class java.lang.String cannot be cast to class com.home.calendar.User.User
UPDATE
For completeness, here is the user entity and the complete method for a new entry.
#PostMapping("/neuerEintrag")
public String neuerEintrag(HttpServletRequest req, #RequestParam("beschreibung") String beschreibung,
#RequestParam("datum") Date datum, #RequestBody List<User> zugeordnet,
#RequestBody List<Freunde> kontaktAuswahl, #RequestParam("neuAlt") String neuAlt,
#RequestParam("kalenderId") int kalenderId) { }
The User Entity:
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
#JsonIgnoreProperties("user")
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user")
private List<Kalender> kalenderEinträge;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, List<Kalender> kalenderEinträge) {
super();
this.name = name;
this.kalenderEinträge = kalenderEinträge;
}
public List<Kalender> getKalenderEinträge() {
return kalenderEinträge;
}
[getter/setter]
Spring can't parse an unknown object.
To get it work, I suggest a new class for the "request".
#Data // lombok - this generates getter/setters/equals/hashcode for you
public class NeuerEintragRequest {
private List<User> zugeordnet;
private String beschreibung;
private int kalendarId;
// and your others fields
}
The controller can now use very type-safe objects.
#PostMapping("/neuerEintrag")
public String neuerEintrag(#RequestBody NeuerEintragRequest request) {
for(User user : request.getUserlist()) {
// a logging framework is a lot better. Try to use log4j or slf4j.
log.info("USER: {}", user);
}
...
}
Typescript
Let axios handle the typing and serializing. See this tutorial: https://masteringjs.io/tutorials/axios/post-json
To post all the needed data, you can create a new object.
// no formdata - just send the object
const data = { zugeordnet: personNamen, kalendarId: 123, beschreibung: 'abc' };
await axios.post("/neuerEintrag", data);
You can also create a interface in typescript, but this is going to much for a stackoverflow-answer. Try to learn more about spring and typescript.
Based on question & comments ,
your front end call data.append("zugeordnet", JSON.stringify(personNamen)); is converting your object to List<String> instead of List<User>.
So you can transform this List<String> to List<User> in your postMapping:
#PostMapping("/neuerEintrag")
public String neuerEintrag(HttpServletRequest req,#RequestParam("zugeordnet") List<?> zugeordnet,..) {
ObjectMapper mapper=new ObjectMapper();
for(String str:zugeordnet){
System.out.println(mapper.readValue(str, User.class));
}
...
}

Spring boot API - How to ensure no concurrency issues

I'm still in the process of learning Java / spring and I think I'm getting better. Now at this point I'm able to build a rest api BUT I'm at a lost at how to ensure I've no concurrency issues . I've read many topics regarding making the API stateless or making my POJO's immutable but I'm sure if in my case below I need to. And if I did, I'm actually unsure how my code can function by making everything final in my POJO.
If someone could help me learn here I'd be VERY grateful. Thank you for your time
Below i have a POJO called User:
#Getter
#Setter
#Document(collection = "UserProfiles")
public class User {
#Id
#JsonIgnore
private String _id;
#JsonView({ProfileViews.Intro.class, ProfileViews.Full.class})
private String userId;
#JsonView({ProfileViews.Intro.class, ProfileViews.Full.class})
private String name;
#JsonView({ProfileViews.Intro.class, ProfileViews.Full.class})
private String displayName;
#DBRef
#JsonView({ProfileViews.Full.class})
private UserInterests personalInterests;
#DBRef
#JsonIgnore
private ProfileFollows profileFollowDetails;
}
#Getter
#Setter
#Document(collection = "ProfileFollows")
public class ProfileFollows {
#Id
//Id of The Mongo Document
private String id;
//The Id of the User Profile who owns the document
private String userId;
//A list containing the Ids of the Users who have followed the Profile belonging to userId
private List<String> profileFollowedByUserIds;
//A list containing the Ids of the Profiles the current user has followed
private List<String> profileFollowingByUserList;
}
And here is my Service layer where I create and update the user
#Service
public class UserService {
#Autowired
UserDal userDal;
public User createNewUserAccount(String userId, String userName) {
//check If userId already in DB
if (checkIfUserIdExits(userId)) {
throw new UserAlreadyExistsException("Cannot create User with Id { " + userId + " }, a user with this Id already " +
"exists");
}
//Create a Empty / Base New User Object
User newUser = new User();
UserInterests userInterests = new UserInterests();
userInterests.setUserId(userId);
userInterests.setPersonalInterestsExtras(null);
userInterests.setCreatedDate(Instant.now());
userInterests.setLastUpdatedAt(Instant.now());
userInterestsDAL.save(userInterests);
newUser.setPersonalInterests(userInterests);
ProfileFollows userProfileFollows = new ProfileFollows();
userProfileFollows.setUserId(userId);
userProfileFollows.setProfileFollowedByUserIds(new ArrayList<>());
userProfileFollows.setProfileFollowingByUserList(new ArrayList<>());
newUser.setProfileFollowDetails(profileFollowsDAL.save(userProfileFollows));
newUser.setUserId(userId);
newUser.setDisplayName(generateUserDisplayName(userName));
newUser.setCreatedDate(Instant.now());
newUser.setLastUpdatedAt(Instant.now());
//save the new User Profile to the DB
return userDal.save(newUser);
}
Here is my UserDAL:
public interface UserDal {
/**
* Method to check if a user exists with a given user Id
* #param Id -- Id of user to look up where id is a string
* #return
*/
Boolean existsById(String Id);
/**
* Method to save a user to the DB
* #param user -- User object to save to the DB
* #return
*/
User save(User user);
}
My User Repository / DALImpl:
#Repository
public class UserDALImpl implements UserDal {
private final MongoTemplate mongoTemplate;
#Autowired
public UserDALImpl(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
#Override
public User save(User user) {
return mongoTemplate.save(user);
}
And lastly my controller:
#RestController
#RequestMapping("/profile")
public class CreateProfileController {
#Autowired
public CreateProfileController() {
}
#Autowired
UserService userService;
#ApiOperation(value = "Allows for the creation of a user Profile")
#PostMapping("/create")
public User createUserProfile(#RequestParam(name = "userId") String userId,
#RequestParam(name = "displayName", required = true, defaultValue = "AnonymousDev") String displayName) {
if (userId.equals("")) throw new BadRequestException("UserId cannot be blank");
if (userService.checkIfUserIdExits(userId)) {
throw new UserAlreadyExistsException("Unable to create user with Id { " + userId + " }, the " +
"userId already exists");
}
return userService.createNewUserAccount(userId, displayName);
}
}

Play 2 Java - validate form using fill and bindFromRequest in update scenario

I'm having a hard times figuring out how to do update scenario in Play 2 Java
I have
User.java model
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public Long id;
#Constraints.Required
public String email;
#Constraints.Required
public String fullname;
}
And I want to update it, so in my controller I do
public Result update(Long id) {
ObjectNode result = Json.newObject();
User employee = userService.get(id);
Form<User> userForm = formFactory.form(User.class).fill(employee);
// This won't trigger validation because it uses fill() not bind()
if (userForm.hasError()) {
result.set("message", userForm.errorsAsJson());
return badRequest(result);
}
// do update here
}
Then I try to some different approach like this
public Result update(Long id) {
ObjectNode result = Json.newObject();
User employee = userService.get(id);
Form<User> userForm = formFactory.form(User.class).fill(employee);
userForm = userForm.bindFromRequest();
// This will trigger validation but bindFromRequest will override my fill(employee) before.
if (userForm.hasError()) {
result.set("message", userForm.errorsAsJson());
return badRequest(result);
}
// do update here
}
The bindFromRequest() above will override my fill(employee). I don't want to do that because when in my request I just want to fill fullname and not my email, my email property will trigger its required validation.
So my question is, how can I only update my fullname attribute with the form i fill using existing value and still triggering validation constraints from my model?
Change userForm = userForm.bindFromRequest(); to userForm.bindFromRequest();
I have working code very similar to yours and this is the only difference I've observed.

Hibernate Model: set default #ManyToOne value of a Child Class

I have class User which has #ManyToOne field Role.
#Entity
#Table(name="USERS")
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorValue("ROLE_ADMIN")
#DiscriminatorColumn (name="ROLENAME", discriminatorType= DiscriminatorType.STRING, length=20)
public class User extends BaseEntity implements UserDetails {
// all others fields like username, password,...
#ManyToOne
#JoinColumn(name = "role_id", referencedColumnName="id")
#NotNull(message = "ROLE field is mandatory")
private Role role;
//getter and setter
}
I have many classes that extends User: UserSeller, UserClient, UserOffice....
#Entity
#DiscriminatorValue("ROLE_SELLER")
#AttributeOverride(name = "role", column = #Column(name = "role_id"))
public class UserSeller extends User {
//additional fields like CompanyId,...
//getter & setter
}
I have one panel where i can insert / edit / delete all kind of users,
but i must have also "n" panel: one for each kind of user.
When i'm in these panel, i would like to be able to insert these UserSeller without having
to put a where to choose role, but i want set this role as default.
• I tried to put a costructor in the UserSeller
#Entity
#DiscriminatorValue("ROLE_SELLER")
#AttributeOverride(name = "role", column = #Column(name = "role_id"))
public class UserSeller extends User {
#Transient
RoleService roleService;
#Transient
User user = new User();
public UserSeller()
{
super();
try
{
this.setRole(roleService.getRole("ROLE_SELLER"));
}
catch (RecordNotFoundException e)
{
}
}
but i get this error:
Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
• i tried to pass a User object to constructor:
public UserSeller(User user)
and in controller i do this:
User user = new User();
UserSeller seller = new UserSeller(user);
model.addAttribute("seller", seller);
but i get this error:
No default constructor for entity: com.machinet.model.UserVenditore
If, in UserSeller i declare again the Role field i get error about
"Redefined Column"....
Finally i found this that i though could be my solution (in
UserSeller class):
#PrePersist
public void prePersist() {
try
{
this.setRole(roleService.getRole("ROLE_SELLER"));
}
catch (RecordNotFoundException e)
{
}
}
but when i'm in the UserSeller panel and i try to add a new Seller, it doesnt' take the default role and the validation fails.
I would like to know how can i do the trick: i want that my UserSeller, UserClient,... have a default value when i insert a new record.
Do i really need to do this in the controller? is this the only way? because for a beginner like me, it doesn't look so so elegant solution:
UserVenditore venditore = new UserVenditore();
try
{
venditore.setRole(roleService.getRole("ROLE_VENDITORE"));
}
catch (RecordNotFoundException ex)
{
}
model.addAttribute("venditore", venditore);
edit: also this last solution doesn't work: it fails the validation!
Thank you for any suggestion!
for now, i found only this solution (in the ControllerClass):
in removed #Valid before #ModelAttribute
i manually set the role
i manually validate the object
#RequestMapping(value = "/add", method = RequestMethod.POST)
public String addingSeller(#ModelAttribute UserSeller seller,
BindingResult result, RedirectAttributes redirectAttrs) {
logger.info("IN: Seller/add-POST");
//set role
try
{
seller.setRole(roleService.getRole("ROLE_SELLER"));
}
catch (RecordNotFoundException ex)
{
}
//validation
validator.validate(seller, result);
if (result.hasErrors()) {
logger.info("Seller-add error: " + result.toString());
redirectAttrs.addFlashAttribute("org.springframework.validation.BindingResult.seller", result);
redirectAttrs.addFlashAttribute("seller", seller);
} else {
try
{
SellerService.addSeller(seller);
//message to the user
String message = "Seller added!";
redirectAttrs.addFlashAttribute("message", message);
redirectAttrs.addFlashAttribute("message_class", "alert-success");
}
catch (DuplicateRecordException e)
{
//the username already exists
//message to the user
String message = "Already exists an user with this USERNAME";
redirectAttrs.addFlashAttribute("message", message);
redirectAttrs.addFlashAttribute("message_class", "alert-danger");
redirectAttrs.addFlashAttribute("seller", seller);
}
}
return "redirect:/sellers/list";
}

Relationships question in hibernate

I'm learning Hibernate and Play framework (also add Java into account...). I'm having problems saving this kind of entity
#Entity
#Table(name="users")
public class User extends Model {
#Required
public String username;
#Column(name="user_displayname",nullable=true)
public String displayname;
#Password
public String user_password;
#Email
#Column(name="user_email",nullable=false,unique=true)
public String user_email;
public String user_salt;
public Date user_joindate;
#ManyToOne
#JoinTable(name="users_meta")
public UserMeta userdata;
#Required
public boolean user_isActive;
#OneToOne(targetEntity=UserPhotos.class,cascade=CascadeType.ALL)
#JoinColumn(name="id",referencedColumnName="userID")
public UserPhotos userPhoto;
#ManyToMany(cascade=CascadeType.ALL)
#JoinTable(name="links_rol2user")
public List<Rol> rol;
public User (String username, models.Pass password, String user_email) {
this.username = username;
this.user_password = password.getHashedPassword();
this.user_salt = password.getUserHash();
this.user_email = user_email;
this.user_joindate = new Date();
this.user_isActive = false;
}
This is my code when I'm registering a user
// check if the validation has errors
if(validation.hasErrors()) {
params.flash(); // add http parameters to the flash scope
validation.keep(); // keep the errors for the next request
register();
} else {
Cache.delete(uuid);
Pass pass = new Pass(password,new Date().toString());
User newUser = new User(firstName, pass, email);
UserMeta utest = new UserMeta(newUser.id);
utest.setUserTownID(pueblos);
newUser.setUserMeta(utest);
newUser.save();
Logger.info("NewUser ID : %s", newUser.getId());
// UserMeta userInfo = new UserMeta(newUser.getId());
// userInfo.setUserTownID(pueblos);
// userInfo.save();
// TODO salvar foto a null
// Confirmation left
Cache.set("thankyou", "alright!", "3mn");
thankyou();
}
I'm trying to save the userMeta, it does creates a new record when I set the userMeta object into newUser (not visible right now), but it doesn't insert the new ID created in newUser.
What kind of relation do I need? before I tweaked the code as it is now, it was a OneToOne relationship, worked quite well, but now when I was completing the register functions it kinda hit me that I needed to save userMeta object too..
If you need more info let me know, I don't know if I explained it well or not, just trying to get the hang of how Hibernate do relations, etc.
Adding UserMeta:
*/
#Entity
#Table(name="users_meta")
public class UserMeta extends Model {
#Lob
#Column(name="userBio")
public String userBio;
#Column(name="userPhotoID",nullable=true)
public Long userPhotoID = null;
#Column(name="userRoleID", nullable=false)
public Long userRoleID = 2L;
#Lob
public String userDescription;
#Column(name="userViews", nullable=false)
public Long userViews = 0L;
#Column(name="userFavoriteCount", nullable=false)
public Long userFavoriteCount = 0L;
#Column(name="userTotalComments", nullable=false)
public Long userTotalComments = 0L;
#Column(name="userTotalUploadedVideos", nullable=false)
public Long userTotalUploadedVideos = 0L;
public Long userTownID;
public Long userID;
public UserMeta() {}
public UserMeta(Long userid) {
this.userBio = "El usuario no ha escrito nada todavia!";
this.userDescription = "El usuario todavia no se ha describido!";
this.userID = userid;
}
public Long getUserTownID() {
return userTownID;
}
public void setUserTownID(Long userTownID) {
this.userTownID = userTownID;
}
}
// pass model
public class Pass {
protected String hashed;
protected String userHash;
public Pass(String passwordToHash, String salt) {
StringBuffer passSalt = new StringBuffer(passwordToHash);
this.userHash = DigestUtils.md5Hex(salt);
passSalt.append(this.userHash);
passSalt.append(Play.configuration.getProperty("application.passwordSalt"));
this.hashed = DigestUtils.sha512Hex(passSalt.toString());
}
public String getHashedPassword() {
return this.hashed;
}
public String getUserHash() {
return this.userHash;
}
}
There seems to be a lot going on there! But from what I can tell, you problem is with the id that you are passing into the UserMeta.
As you are extending Model, the id is being generated by the Model class. However, this is not set until after the entity is saved to the database (as the id is auto-generated by the database).
Therefore, because you are passing the id into the UserMeta before the User object is saved, the value of id will be null.
If you can save the User object before you create your UserMeta object, your code should work.

Categories

Resources