So my front end colleague has successfully made this cool calendar using angular.uicalendar, that I'm trying to accommodate on the back end using java persistence and Spring MVC. The Calendar object itself has a lot of information and he's passing it as a nested object.
The parent object seems to have a “Start” and “End” which are LocalDateTimes, and a title which is a string. Then within that, there is an Email object (boolean sendEmail, int time (as in how long until you want it to be sent)) Then on that same layer there’s a text object which contains two variables that do virtually the same thing, but for texts, and also another called notification, which also does the same, but whether to post it on the website or not. Here's a screenshot of how it looks on the front end side if that helps.
Screenshot of Calendar Object
So I'm trying to replicate the object on my end so that I can store/edit the objects and I'm confused about how to proceed. My independent research suggests I can use an "#Embedded" annotation? One intriguing suggestion I had was "making the parameter into a Hashmap." Apparently Spring should be able to parse it into that if I don't have a special class prepared to store it. But I'm sure what that would look like. Could I even store the object in the database with this HashMap hanging off the end of it? Currently my calendarEvent class looks like so, and it's my attempt to make sense of what this Object would look like:
package com.theironyard.Entities;
import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
/**
* Created by macbookair on 12/16/15.
*/
#Entity
#Table (name = "events")
public class CalendarEvent {
#Id
#GeneratedValue
public int id;
#Column(nullable = false)
public LocalDateTime start;
#Column(nullable = false)
public LocalDateTime end;
#Column(nullable = false)
public String title;
//possibly use #Embedded here ?
public Object email() {
boolean send;
int time; // hours before sending email
return email();
}
public Object text() {
boolean send;
int time;
return text();
}
public Object notification() {
boolean send;
int time;
return notification();
}
public CalendarEvent(int id, LocalDateTime start, LocalDateTime end, String title) {
this.id = id;
this.start = start;
this.end = end;
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public LocalDateTime getStart() {
return start;
}
public void setStart(LocalDateTime start) {
this.start = start;
}
public LocalDateTime getEnd() {
return end;
}
public void setEnd(LocalDateTime end) {
this.end = end;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
So I guess in summary: What's the best way to store this object? Can I store the main object in the database with a HashMap hanging off of it?
Thanks everybody.
Related
Hello i am currently studying and we got a task , we need to modify a video-shop wepsite and we got catalog class and some other classes to help it run with spring like catalogcontroller catalogdataintitlizer etc , here is a cut of the code
`#Entity
#Table(name = "COMMENTS")
public class Comment implements Serializable {
private static final long serialVersionUID = -7114101035786254953L;
private #Id #GeneratedValue long id;
private String text;
static int rating;
static double r_total;
private LocalDateTime date;
#SuppressWarnings("unused")
private Comment() {}
public Comment(String text, int rating, LocalDateTime dateTime ) {
this.text = text;
this.rating = rating;
this.date = dateTime;
` that was from a class called Comment and in this class there are the rating we need to get the Average rating from the rating on every video "note : when someone open a video you can leave a comment with a rating and we need to always add up all the rating that the user leave and then divide it on the number of users ,
`#Entity
public class Disc extends Product {
public static enum DiscType {
BLURAY, DVD;
}
private List<Comment> comments = new ArrayList<>();
#SuppressWarnings({ "unused", "deprecation" })
private Disc() {}
public Disc(String name, String image, Money price, String genre, DiscType type) {
super(name, price);
this.image = image;
this.genre = genre;
this.type = type;
}
public String getGenre() {
return genre;
}
public void addComment(Comment comment) {
comments.add(comment);
}
public Iterable<Comment> getComments() {
return comments;
}`
and here is the other class that might help too here we save the comments to a arraylist "note : i didnt type the code " we just got this project and we need to change few things and i have been spending alot alot of time trying to solve this problem but still no use
i tried to make object from disc in comment and use some of his methods but sadly i got whitelist error from spring when i press on any video so i tried to make the rating static and try to grab it from the other class it worked but when i change from video to other video i end up adding all videos togther " it stacks " i am really new to java and i dont really know what to do now xd ty in advance
To run the application i use tomcat 8.5.50 package in war.
i use spring 5.2 version.
in my code i want to use LocalDataTime like this:
#Entity
#Table(name="meals")
public class Meal {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id")
private Integer id;
#Column(name = "date_time")
#Convert(converter = MealConverter.class)
private LocalDateTime datetime;
#Column(name = "description")
private String description;
#Column(name = "calories")
private int calories;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public LocalDateTime getDatetime() {
return datetime;
}
public void setDatetime(LocalDateTime datetime) {
this.datetime = datetime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getCalories() {
return calories;
}
public void setCalories(int calories) {
this.calories = calories;
}
}
my Converter:
#Converter(autoApply = true)
public class MealConverter implements AttributeConverter<Meal, String> {
private static final String SEPARATOR = ", ";
#Override
public String convertToDatabaseColumn(Meal meal) {
StringBuilder sb = new StringBuilder();
sb.append(meal.getCalories()).append(SEPARATOR)
.append(meal.getDatetime())
.append(SEPARATOR)
.append(meal.getDescription());
return sb.toString();
}
#Override
public Meal convertToEntityAttribute(String dbData) {
String[] rgb = dbData.split(SEPARATOR);
Meal meal = new Meal(Integer.valueOf(rgb[0]),
LocalDateTime(valueOf(rgb[1]),
rgb[2],
rgb[3]);
return meal;
}
}
I am trying to use the converter in the convertToEntityAttribute method but the compiler does not allow me to do this. What needs to be fixed in my Converter?
Meal meal = new Meal(Integer.valueOf(rgb[0]),
LocalDateTime(valueOf(rgb[1]),
rgb[2],
rgb[3]);
Your Meal class doesn’t seem to have any explicit constructor, so you cannot pass arguments to new Meal(). You seem to be trying to pass two arguments. You may want to create a suitable constructor, or you may want to pass the two values into the Meal using setters after the object has been created.
LocalDateTime is a class, but you seem to try to call it as a method with three arguments. If that’s java.time.LocalDateTime, you probably intended LocalDateTime.of(someArguemts), but there isn’t any three-argument of method of that class. If you explain better what result you expect, we can guide you better.
As the first argument to LocalDateTime you have a call to a valueOf method that doesn’t seem to be declared in your class. You may have intended Integer.valueOf as in the preceding line.
If you are trying to use your RGB values for initializing a date (don’t know what sense that might make), be aware that if your RGB values go up to 255, this will likely fail with an exception since month numbers only go up to 12 and day of month up to 31.
I am far from sure that the following is correct or does what you want it to do, but it’s a guess at what you may be after.
#Override
public Meal convertToEntityAttribute(String dbData) {
String[] fields = dbData.split(SEPARATOR);
Meal meal = new Meal();
meal.setCalories(Integer.parseInt(fields[0]));
meal.setDateTime(LocalDateTime.parse(fields[1]));
meal.setDescription(fields[2]);
return meal;
}
I am trying to do the opposite of your convertToDatabaseColumn method. I have discarded the variable name rgb because I didn’t see how it couldn’t be misleading here.
I have a small difficulty with ebeans.
I used to create items with the following code, populating the addDate:
#MappedSuperclass
public abstract class GenericModel extends Model {
#Id
protected Long id;
#UpdatedTimestamp
#Version
private Date lastUpdate;
#CreatedTimestamp
protected Date addDate;
public Long getId(){
return id;
}
public Date getLastUpdate(){
return lastUpdate;
}
public Date getAddDate(){
return addDate;
}
}
However, as I need to synchronise the data with an offline device, I changed it to:
#MappedSuperclass
public abstract class GenericModel extends Model {
#Id
protected Long id;
#UpdatedTimestamp
#Version
private Date lastUpdate;
protected Date addDate;
public Long getId(){
return id;
}
public Date getLastUpdate(){
return lastUpdate;
}
public Date getAddDate(){
return addDate;
}
#WhenCreated
private void addCreateTimestamp(){
if(this.addDate == null) this.addDate = new Date();
}
}
But this doesn't work, as there is an exception that the addDate cannot be null when inserting.
Is there another way of populating the addDate is not set (for the synchronisation I sometimes I need to set the addDate manually so it is the same as the master).
[EDIT:] this problem doesn't seem to show up on my development system with men db, but when deployed with MySql
#WhenCreated is just an alias for #CreatedTimestamp. What are you expecting putting this annotation before the addCreateTimestamp() method. This method is never executed in your case, so the addDate is never set.
Try this:
#MappedSuperclass
public abstract class GenericModel extends Model {
// ...
protected Date addDate;
// ...
#Override
public void save() {
addCreateTimestamp();
super.save();
}
private void addCreateTimestamp(){
if(this.addDate == null) this.addDate = new Date();
}
// ...
I implemented a Java web service (JAX-RS API Jersey implementation). There is an entity:
#XmlRootElement
public class TestPhoto extends Photo {
public enum Type {
BEFORE, AFTER, ADDON
}
private User author;
#XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
private LocalDateTime createdTime;
private Type type;
public TestPhoto() {
super();
}
public TestPhoto(Long id, String key, String path, User author, LocalDateTime createdTime, Type type) {
super(id, key, path);
this.author = author;
this.createdTime = createdTime;
this.type = type;
}
public User getAuthor() {
return author;
}
public void setAuthor(User author) {
this.author = author;
}
public LocalDateTime getCreatedTime() {
return createdTime;
}
public void setCreatedTime(LocalDateTime createdTime) {
this.createdTime = createdTime;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
}
When I am trying to retrieve a list of such entities, I get an error:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 8 counts of IllegalAnnotationExceptions
Class has two properties of the same name "createdTime"
this problem is related to the following location:
at public java.time.LocalDateTime com.test.TestPhoto.getCreatedTime()
at com.test.TestPhoto
at public java.util.List com.test.TestAccount.getAddonPhotos()
at com.test.TestAccount
this problem is related to the following location:
at private java.time.LocalDateTime com.test.TestPhoto.createdTime
at com.test.TestPhoto
at public java.util.List com.test.TestAccount.getAddonPhotos()
at com.test.TestAccount
I know how to fix this. For instance, I could rename private fields and add _ as a prefix. Or I could add a #XmlAccessorType(XmlAccessType.FIELD) annotation to a field.
I see also, that there is something wrong with #XmlJavaTypeAdapter(LocalDateTimeAdapter.class) annotation. If comment it out everything works fine. But that part of functionality won't work in this case.
What I am trying to understand is why this happened in the first place. The code worked perfectly fine and then just stopped and started throwing such exceptions. Should this code have worked in the first place? Or is it completely wrong?
I have an object extending SugarRecord that looks like this:
public class SavedDraft extends SugarRecord {
private String name;
private String difficulty;
private int sport_id;
private LocalActivity localActivity;
public SavedDraft() {
}
public SavedDraft(String name, String difficulty, int ID, LocalActivity localActivity) {
this.name = name;
this.difficulty = difficulty;
this.sport_id = ID;
this.localActivity = localActivity;
}
}
The problem is that I always get a null object when I try to get the localActivity object from the database (see: SavedDraft.findById(SavedDraft.class, 1).getLocalActivity()), and I'm just wondering if it's possible to save objects as parameters in SugarORM at all.
This would be a relationship and you would need the LocalActivity to extend SugarRecord also.
See the documentation of Book and Author: http://satyan.github.io/sugar/creation.html