Room Abstract Pojo - java

I'm creating for fun an android application that tracks the spendings. I'm using Room to persist the user's data and I have POJOs that show the daily/weekly/monthly summaries.
These classes are quite similar, thus I would like to have one abstract POJO that contains the fields and extensions of it that reformat to the correct format. Something like:
public abstract class PeriodInformation {
PeriodInformation(#NonNull Calendar mCalendar, Integer mPeriodSpendingCount, Float mPeriodSpendingSum) {
this.mCalendar = mCalendar;
this.mPeriodSpendingCount = mPeriodSpendingCount;
this.mPeriodSpendingSum = mPeriodSpendingSum;
}
#ColumnInfo(name = "DateTime")
private final Calendar mCalendar;
#ColumnInfo(name = "SpendingCount")
private Integer mPeriodSpendingCount;
#ColumnInfo(name = "SpendingSum")
private Float mPeriodSpendingSum;
// Some other code, e.g., getters, equal override,...
}
Here the extension:
public class WeekInformation extends PeriodInformation{
public WeekInformation(#NonNull Calendar mCalendar, Integer mPeriodSpendingCount, Float mMonthSpendingSum) {
super(mCalendar, mPeriodSpendingCount, mMonthSpendingSum);
}
#Override
public String getPeriodRepresentation() {
//return representation;
}
}
However, I get following error message for the WeekInformation Class:
error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
So it seems that this is not possible in Room, thus I would be happy to get some suggestion how to not have to copy the same code too often.
thank you.
EDIT:
I use following DAO code to aggregate to the POJO, the column calendarDate has following format "yyyy-MM-dd'T'HH:mm:ss.SSSXXX":
#Query("SELECT date(datetime(calendarDate)) AS 'DateTime', count(uID) AS 'SpendingCount', sum(value) AS 'SpendingSum' from spending GROUP BY date(datetime(calendarDate))")
LiveData<List<DayInformation>> loadDayInformation();

I was able to make this work for me, using the Embedded annotation, allowing direct access to the fields of the embedded data type.
public class DayInformation {
#Embedded
public PeriodInformation periodInformation;
#Override
public String toString() {
return "DayInformation{" +
"periodInformation=" + periodInformation +
'}';
}
}
and
public class PeriodInformation {
PeriodInformation(Calendar timestamp,
int periodSpendingCount,
float periodSpendingSum) {
this.timestamp = timestamp;
this.periodSpendingCount = periodSpendingCount;
this.periodSpendingSum = periodSpendingSum;
}
#ColumnInfo(name = "DateTime")
public final Calendar timestamp;
#ColumnInfo(name = "SpendingCount")
public Integer periodSpendingCount;
#ColumnInfo(name = "SpendingSum")
public Float periodSpendingSum;
#Override
public String toString() {
final DateFormat dateInstance = SimpleDateFormat.getDateInstance();
String date = timestamp == null ? "null" : dateInstance.format(timestamp.getTime());
return "PeriodInformation{" +
"timestamp='" + date + '\'' +
", periodSpendingCount=" + periodSpendingCount +
", periodSpendingSum=" + periodSpendingSum +
'}';
}
}
plus
#Entity
public class Spending {
#PrimaryKey(autoGenerate = true)
public int uid;
#ColumnInfo(name = "calendarDate")
public Calendar timestamp;
#ColumnInfo(name = "value")
public float value;
public Spending(#NonNull Calendar timestamp, float value) {
this.timestamp = timestamp;
this.value = value;
}
#Override
public String toString() {
final DateFormat dateInstance = SimpleDateFormat.getDateInstance();
String date = timestamp == null ? "null" : dateInstance.format(timestamp.getTime());
return "Spending{" +
"uid=" + uid +
", timestamp='" + date + '\'' +
", value=" + value +
'}';
}
}
and a DAO
#Dao
public interface SpendingDao {
#Insert
void insertAll(Spending... spendings);
#Query("SELECT * FROM spending")
LiveData<List<Spending>> findAll();
#Query("SELECT calendarDate AS 'DateTime', count(uID) AS 'SpendingCount', sum(value) AS 'SpendingSum' from spending GROUP BY date(datetime(calendarDate))")
LiveData<List<DayInformation>> loadDayInformation();
}
gives the following output
aggregated data is
DayInformation{periodInformation=PeriodInformation{timestamp='Jun 26, 2018', periodSpendingCount=8, periodSpendingSum=184.0}}
spending data is Spending{uid=1, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=2, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=3, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=4, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=5, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=6, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=7, timestamp='Jun 26, 2018', value=23.0}
spending data is Spending{uid=8, timestamp='Jun 26, 2018', value=23.0}

Related

Unable to store Date in a specific format in database

reading json data from database, and converting it into a object in java.
Data in Database :- {"id":123,"expiryDate":"2024-02-10"}
NOTE :- I am trying to use this data in Google Web Toolkit (GWT)
converting above data using object mapper.
Object consuming above json TestJson.java
Class TestJson {
private Long id,
private Date expiryDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id= id;
}
public Date getExpiryDate() {
return expiryDate;
}
public void setExpiryDate(Date expiryDate) {
this.expiryDate = expiryDate;
}
public String toJSONString() {
return "{" +
"\"id\" : \"" + id + "\"," +
"\"expiryDate\" : \"" + expiryDate + "\"}";
}
}
When the Json data is converted then the value of "Expiry Date" is "Sat Feb 10 05:30:00 IST 2024".
But I want it in the same format as it is in JSON.
Since I was trying to save/parse the object in Google Web Toolkit (GWT) so SimpleDateFormat was creating issue, as GWT doesn't supports SimpleDateFormat.
But below worked for me
import com.google.gwt.i18n.shared.DateTimeFormat;
import com.google.gwt.i18n.shared.DefaultDateTimeFormatInfo;
String pattern = "yyyy-MM-dd"; /*your pattern here*/
DefaultDateTimeFormatInfo info = new DefaultDateTimeFormatInfo();
DateTimeFormat df = new DateTimeFormat(pattern, info) {};
df.format(expiryDate)
Output :-
{"id":123,"expiryDate" : "2024-02-10"}

How to deserialize a String to a Date with Morphia

I have a Mongo collection with objects of this format:
{
id: 1,
date: "2020-08-06T12:00:00Z",
...
}
I have Java code that needs to read from this collection but never writes to it. The process that writes to this collection is not owned by me so I can't necessarily change the format of that date string. I initially tried to model my Java Morphia object like this:
#Entity public class MyDocument {
#Id
private Integer id;
private Date date;
...
}
This did not work because Morphia didn't know how to deserialize that date format into a Date object. The solution that I came up with was treating the date as a String on the POJO and then having a getDate() method that did the actual deserialization. I am wondering, is there a better way for me to do this? I know if you're using Jackson you can annotate certain fields with #JsonDeserialize and pass a deserializer so I was wondering if there was something similar for Morphia.
My solution (which feels suboptimal to me):
#Entity public class MyDocument {
#Id
private Integer id;
private String date;
...
private Date getDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
return dateFormat.parse(date);
} catch (Exception ex) {
return null;
}
}
}
You can go ahead and create a simple converter extending the TypeConverter like so:
public class DateConverter extends TypeConverter {
private static final String FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private final SimpleDateFormat simpleDateFormat;
public DateConverter() {
super(Date.class);
this.simpleDateFormat = new SimpleDateFormat(FORMAT);
}
#Override
public Object decode(Class<?> targetClass, Object fromDBObject, MappedField optionalExtraInfo) {
try {
return simpleDateFormat.parse(((String) fromDBObject));
} catch (ParseException e) {
return null;
}
}
}
The go ahead and register your formatter for your document entity like so:
#Entity("Documents")
#Converters(DateConverter.class)
public class Document {
#Id
private Integer id;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
private Date date;
public Date getDate() { return date; }
public void setDate(Date date) { this.date = date; }
#Override
public String toString() {
return "Document{" +
"id=" + id +
", date=" + date +
'}';
}
}
This will effectively tell Morphia to decode the database incoming values via parsing the string with the desired pattern, resulting directly into a concrete Date object without any additional conversion logic.

Android Room Base64

I am trying to save a base64 value into my sqlite database using Room and for some reason it's not saving. Well, i'm assuming it's not saving because when I try to read the table that has the base64 column, it returns values for all the other columns except the base64 column. What am I doing wrong?
My Entity:
#Entity(tableName = "healthCareWorkerInformation",
foreignKeys = #ForeignKey(
entity = HealthCareWorker.class,
parentColumns = {"id"},
childColumns = {"hcwId"},
onDelete = ForeignKey.CASCADE),
indices = #Index(
value = {"hcwId"}))
public class HealthCareWorkersInformation {
#PrimaryKey(autoGenerate = true)
#ColumnInfo(name = "hcwInfoId")
private long id;
private long hcwId;
//#ColumnInfo(typeAffinity = ColumnInfo.BLOB)
private String base64Image;
private String updatedAt = new SimpleDateFormat("dd MM yyyy, HH:mm",
Locale.getDefault()).format(Calendar.getInstance().getTime());
public HealthCareWorkersInformation() {
}
#Ignore
public HealthCareWorkersInformation(long hcwId) {
this.hcwId = hcwId;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getHcwId() {
return hcwId;
}
public void setHcwId(long hcwId) {
this.hcwId = hcwId;
}
public String getBase64Image() {
return base64Image;
}
public void setBase64Image(String base64Image) {
this.base64Image = base64Image;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
}
My DAO:
#Insert
void insertHealthCareWorkerInformation(HealthCareWorkersInformation healthCareWorkersInformation);
#Query("SELECT * FROM HEALTHCAREWORKERINFORMATION")
LiveData<List<HealthCareWorkerInformation>> getHCWInfo();
Sample data I send through:
{"consentGiven":null,"hcwId":1,"patiendId":1,"name":"Ben","lastName":"Ben","dateOfBirth":"4/9/2019","phoneNumber":"+271234567","base64Image":"data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAGQASwDASIAAhEBAxEB/8QAHgAAAgEFAQEBAAAAAAAAAAAAAgMEAAEFBgcICQr/xABSEAACAQIEAwUGAwYDBAYHCAMBAhEDIQAEEjEFQVEGEyJhcQeBkaGx8AgywRQjQtHh8QkVUhYXM2IKGCQlQ4I0NVNjcpKiGSZEVHODlLOjstT/xAAcAQADAAMBAQEAAAAAAAAAAAABAgMABAYFBwj/xAA6EQABAwIEAgYJAgYDAQAAAAABAAIRAyEEEjFBBVEGEyJhkaEUFTJCUnGBwdGx8AczQ4LC8RYjJFP/2gAMAwEAAhEDEQA/APn4stpu5P5ove17/wBsNpVCaqKSph9Ji1xb79ThLMaKtrqAQLrG4j19/wCuGy6AWbSwHiHNuR+X3GN7qyQtAO2CcrjSrhdJMEyLx8fX4YZQd6FmVg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av3ZdY009IjSDbYfpEb4BFkpdFkln7yqNSgVGBmLaRO5Hu+uFDSE1AhKX+keEHp9/zw16aLQ8IaoRaCoOxi88r4jVG8WjQWJB1AXME9beXPnhspmBdJYgFMq1EZGen+bbVMBh9/McsK/agFRSzlV/i1CN9vf8Yta+L1GUnSATMEgCQ3MDf7+WFrS8KeIu+uCZuDO3reMYBus6wgQdFeqoesgf95LSBpJIsYN+d/ngtRqoRqKlWMaZgCQbn34UKhrZkyxEiVBIA57fKfdgqY0UwQz2FmjrEn7/AFwpaUAQXdxQXWpqYFXZYki4AtvsYv5YoVYdlU09QAFjFzM+fL5YCqVqp+7puAAQTGnTy5bjzxThnpFCQxMgiALR87zOCW3lEOTlbWg1srFSVLdb3m28+WL0n7qowUaZEmRpPL0Bt064SlZ97+M6vE0GIv7p9d/hRrGkDLmUW4U2G0z5b7/2wgzGqJAF08Or1KisYUjSADus2tHp5+/DBUNJID01ggMzGJMiAbRyj+8YXSpaWVJk6ZB6nz8/rOAMU0aoq3AiIuR0vz914wDJdCME2Te8UqxANiN7CNM4RQr0qjaSBYQFnSBY38vvpgxpcvLaHBEEHSN4gTa4PrfCKxCsJ1K3kd+vx6YzKNFMEi6u9Ukggt4DDGffv/TnhUh6kr+URt05394xdyHp6g4LMSJEExvMX8z8MBTYIupmlnmCRMCeg92FAIEBWaRaVYVitQqrSbm14B+/ucJVtKgEwTYnkPvocFVQMpbQr0yLgGY5+/4xE4X3n78M0sxBZiAbA7H764LQJss0MndXqVUamqmOhuZI6fX5YXUYPU5lgIuDEc7bzMYo2Rify6jChY5mxM7+fr0wuA+u2u+mQLAC8nyNx74wbAJmvl9kKOiESiwvIEjUNzFvLFhXCTKpqkDSwuLW+/sCGDui6YUsSb3AgiMXZwCzhQ1QgA2gjoPL76YO8IsbLb/NKet+0VdAEgMSREfTyuMC16heZAEg6rrfb12+HwWGWrBckEtYAfwjb5/p"}
Everything else gets saved except for the base64 column. Please assist.
The problem is the semicolon in data:image/png;base64, in combination with " double quotes.
The most easy would be to save the base64 string without that prefix, which breaks the syntax and then assume it is all PNG images (or add a further field, which indicates the encoding of each image).
This problem isn't specific to Room, but specific to Java with SQLite, because that semicolon terminates the statement (which Room will generate). In Java, one can use ' single-quotes only for the primitive data-type char, while the complex data-type String excepts " double-quotes. The only way to get around this limitation, is not trying to save a String containing a ;.
To provide an example of what I mean:
private String base64String = null;
private String base64Type = "png";
public String getBase64Image() {
if(this.base64String != null) {
return "data:image/" + this.base64Type + ";base64," + this.base64String;
} else {
return null;
}
}

How to parse JSON in Java using a high performance parser?

My input is an array with approx. 2,000 elements received every second, sometimes several times per second. Every element of the array has 3 Big Decimals.
Jackson (com.fasterxml) is the slowest part of otherwise sub-millisecond app and takes 15 milliseconds (avg). The slow function is objectMapper.readValue(text, MyDto.class);
When using a custom JSON parsing algorithm based on substring, it takes microseconds. With ObjectMapper it's 15 milliseconds.
Parsing JSON via substring is a bad practice because the code is verbose and prone to bugs.
What would you use for JSON parsing? The requirement is a very fast algorithm.
https://github.com/ngs-doo/dsl-json I found DSL json, but don't know hot to make it parse a String with JSON into my DTO. I haven't found a simple fast algorithm to parse JSON from String into a DTO.
EDIT: The input to be parsed is at:
https://pastebin.com/831YtBdq
Code:
public class BitstampOrderBook {
private long timestamp;
private List<List<BigDecimal>> bids;
private List<List<BigDecimal>> asks;
public BitstampOrderBook() {
}
public BitstampOrderBook(long timestamp, List<List<BigDecimal>> bids, List<List<BigDecimal>> asks) {
this.timestamp = timestamp;
this.bids = bids;
this.asks = asks;
}
public long getTimestamp() {
return timestamp;
}
public List<List<BigDecimal>> getBids() {
return bids;
}
public List<List<BigDecimal>> getAsks() {
return asks;
}
}
public class BitstampOrder {
private BigDecimal price;
private BigDecimal amount;
private String datetime;
private int id;
#SerializedName("order_type")
private int orderType;
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public String getDatetime() {
return datetime;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getOrderType() {
return orderType;
}
public void setOrderType(int orderType) {
this.orderType = orderType;
}
#Override
public String toString() {
return "BitStampOrder{" +
"price=" + price +
", amount=" + amount +
", datetime='" + datetime + '\'' +
", id='" + id + '\'' +
", orderType='" + orderType + '\'' +
'}';
}
}
Main:
BitstampOrderBook orderBook = objectMapper.readValue(jsonString, BitstampOrderBook.class);
JProfiler (Jackson):
https://i.imgur.com/mjdbDQe.png
EDIT 2:
JProfiler (Gson):
https://i.imgur.com/WcHVhhd.png
As can be seen from JProfiler, Gson is several times faster than Jackson. What would be even faster than Gson?
Do not use strings as input, parse buffered bytes that you read from the wire or disks immediately instead.
Do not bind to BigDecimal, use double primitive instead + efficient formatter according to your rules if you will need to serialize its value back to JSON with the same precision.
Use 2-field class for your inner arrays of price/amount pairs.
Switch to Scala and use jsoniter-scala (DECLAIMER: I'm a contributor to this amazing library).
Done all these steps and you will get more than 400Mb per second speed when parsing of your samples in one thread.
See results of GeoJSONReading which parses mostly the same kind of JSON (arrays of 2-value sub-arrays of numbers) here.

spring data mongodb LocalDateTime conversion

I have got a simple Model & Repository classes as shown below:
StudentModel class:
#Document(collection = "student")
public final class StudentModel {
#Id
private final String name;
private final LocalDateTime joiningDateTime;
public StudentModel(String name, LocalDateTime joiningDateTime) {
this.name = name;
this.joiningDateTime = joiningDateTime;
}
public String getName() {
return name;
}
public LocalDateTime getJoiningDateTime() {
return joiningDateTime;
}
#Override
public String toString() {
return "StudentModel{" +
"name='" + name + '\'' +
", joiningDateTime=" + joiningDateTime +
'}';
}
}
StudentRepository class:
#Repository
public interface StudentRepository extends MongoRepository<StudentModel, String> {
}
StudentTestingService class:
#Service
public class StudentTestingService {
#Autowired
private StudentRepository studentRepository;
#PostConstruct
public void init() {
studentRepository.save(new StudentModel("JOHN",
LocalDateTime.of(2018, 4, 15, 9, 30, 0)));//line1
System.out.println(" student saved to database !!!!!!! ");//line2
StudentModel student = studentRepository.findOne("JOHN");//line3
System.out.println("Joining DATE :"+student.getJoiningDateTime());//line4
}
}
I run the above code inside a spring boot application (server runs at BST timezone).
As you could see above, my StudentTestingService class stores the joiningDateTime for the student (in BST) as "15-APR-2018 09:30:00" (line1 above)
which is being saved inside the MongoDB database with the GMT time (i.e., "15-APR-2018 08:30:00") as shown in the below screen shot:
Now, when I query the record (at line3) and print it (line4), it prints in BST (though inside MongoDB database it is being stored as GMT time).
So, my question is, how and where does inside "spring-data-mongodb" code, these time conversions (locat time to GMT & GMT to localtime again) being handled/coded?
This seems quite basic and I am sure I am missing something here & lost.
Could you please point me to the "spring-data-mongodb" code base for this? If these conversions are not being handled inside "spring-data-mongodb", where does these being handled i.e., is it inside "mongo-java-driver" library classes ?
Versions:
Spring boot version: 1.5.10
Mongo DB version: 3.4.9

Categories