How create structure and display comments nested Spring Boot with AngularJs - java

I want create comments with sub comments nested, like hierarchy. I donĀ“t know if create structure like that
1# First structure
public class Comment{
private Long id;
private String comment;
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(nullable=true,name="parent_id")
private Comment parent; //Parent comment
#OneToMany(fetch=FetchType.EAGER)
private Set<Comment> subComments; //Sub comments
Or only creat that structure. 2# Second structure
public class Comment{
private Long id;
private String comment;
private Long parent_id;//Parent comment
If I create the second structure with parent_id, my json will be like this.
| id | comment | parent |
| 1 | comment1 | 0 |
| 2 | comment2 | 1 |
| 3 | comment3 | 2 |
| 4 | comment4 | 1 |
| 5 | comment5 | 4 |
| 6 | comment6 | 4 |
| 7 | comment7 | 6 |
| 8 | comment8 | 7 |
| 9 | comment9 | 0 |
My problem is how to create nested comments with Spring Boot and AngularJS? What the correct structure to create and display?
I want to display comments like that. My jsfiddle example about that.
Comment #1 (deepness = 0): comment 1
Reply #4 of #1 (deepness = 1): reply comment 1
Reply #8 of #4 (deepness = 2): reply of reply comment 3
Reply #9 of #8 (deepness = 3): reply of reply of reply comment 3
Comment #2 (deepness = 0): comment 2
Reply #5 of #2 (deepness = 1): reply comment 2
Comment #3 (deepness = 0): comment 3
Reply #6 of #3 (deepness = 1): reply comment 3
Reply #7 of #3 (deepness = 1): reply comment 3

Related

Spark: timestamp changes when reading from written file

I have a parquet that looks like :
------------
name | age |
------------
Tom | 12 |
------------
Mary | 15 |
Now I added a column "timestamp" to it using :
final DataFrame dfWithNewColumn = df.withColumn("timestamp", createTimestamp())
and it looks like :
------------ --------------
name | age | timestamp |
---------------------------
Tom | 12 | 1569312845998 |
---------------------------
Mary | 15 | 1569312845998 |
And I write it into a parquet :
dfWithNewColumn.write()
.partitionBy(new String[]{"name","timestamp"})
.mode(SaveMode.Append)
.parquet(parquetPath);
When I look it using spark-shell, it's in good format :
------------ --------------
name | age | timestamp |
---------------------------
Tom | 12 | 1569312845998 |
---------------------------
Mary | 15 | 1569312845998 |
But the problem is, when I read the parquet again :
public static StructType createSchema() {
final StructType schema = DataTypes.createStructType(Arrays.asList(
DataTypes.createStructField("age", DataTypes.StringType, false),
DataTypes.createStructField("name", DataTypes.StringType, false),
DataTypes.createStructField("timestamp", DataTypes.LongType, false)
));
return schema;
}
DataFrame df = sqlContext.read()
.schema(createSchema())
.parquet(parquetPath);
When I show the rows df.show(), it becomes :
------------ --------------
age | name | timestamp |
---------------------------
12 | Tom | 171798691853 |
---------------------------
15 | Mary | 171798691853 |
How is that possible ? The parquet file is OK so I assume the problem is in the reading code.
Edit :
I found the cause. This problem happens after I changed spark.sql.sources.partitionColumnTypeInference.enabled=false. How can I deal with it ?
Use Spark out of the box functions current_timestamp() which returns the current timestamp as a timestamp column. And for while reading it should be read as org.apache.spark.sql.types.TimestampType datatype.
//Write
final DataFrame dfWithNewColumn = df.withColumn("timestamp", current_timestamp())
//Read
public static StructType createSchema() {
final StructType schema = DataTypes.createStructType(Arrays.asList(
DataTypes.createStructField("age", DataTypes.StringType, false),
DataTypes.createStructField("name", DataTypes.StringType, false),
DataTypes.createStructField("timestamp", DataTypes.TimestampType, false)
));
return schema;
}

Java - building a new members block

I am new to Java Spring Boot and I am looking to create a set of admin dashboard components.
One is a "new members" in the last 28 days block.
So the user table will look something like this
id | email | password | registeredDate
1 | a#gmail.com | $2hasdhdahjasd | 12/01/2017
2 | b#gmail.com | $2hdsfdsfhgjsd | 15/01/2017
3 | c#gmail.com | $4sdfdggfdsfsd | 17/01/2017
4 | d#gmail.com | $6hasdfdahjasg | 01/02/2017
5 | e#gmail.com | $9fdfdfdahjasg | 03/02/2017
6 | f#gmail.com | $89dddfdahjasg | 07/02/2017
7 | g#gmail.com | $fghgdfdahjasg | 07/02/2017
--
so in this instance - say its February - "4+25%"
What kind of algorithms/logic do I start with to get the correct data?
// 28 days ago
Calendar thresholdPast = Calendar.getInstance();
thresholdPast.set(Calendar.HOUR_OF_DAY,0);
thresholdPast.set(Calendar.MINUTE,0);
thresholdPast.set(Calendar.SECOND,0);
thresholdPast.add(Calendar.DATE,-28);
I am using a JPA to get the data out of the system - if there is a way of creating a line that will fetch these stats?
public interface TblLoginRepository extends JpaRepository<TblLogin, String> {
long count();
List<TblLogin> findAll();
}

Restriction on #OneToMany mapping

I have a product class where there is an #OneToMany association for a list of buyers. What I want to do is that buyer search performed by the association when I search for a product, use a null constraint for the end date column of the Buyer table. How to do this in a list mapping like this, below.
// it would be something I needed cri.createCriteria("listaBuyer", "buyer).add(Restriction.isNull("finalDate"));
Example
Registered data
product code | initial date | final date |
-------------------------------------------------------
1 | 2016-28-07 | 2017-28-07 |
------------------------------------------------------
2 | 2016-10-08 | 2017-28-07 |
------------------------------------------------------
3 | 2017-28-08 | |
-----------------------------------------------------
4 | 2017-30-08 | |
Product Class
public class Product {
#OneToMany(targetEntity=Buyer.class, orphanRemoval=true, cascade={CascadeType.PERSIST,CascadeType.MERGE}, mappedBy="product")
#LazyCollection(LazyCollectionOption.FALSE)
public List<Buyer> getListaBuyer() {
if (listaBuyer == null) {
listaBuyer = new ArrayList<Buyer>();
}
return listaBuyer;
}
built criterion
Criteria cri = getSession().createCriteria(Product.class);
cri.createCriteria("status", "sta");
cri.add(Restrictions.eq("id", Product.getId()));
return cri.list();
Expected outcome
product code | initial date | final date |
-------------------------------------------------------
3 | 2017-28-08 | |
-----------------------------------------------------
4 | 2017-30-08 | |
Returned result
product code | initial date | final date |
-------------------------------------------------------
1 | 2016-28-07 | 2017-28-07 |
------------------------------------------------------
2 | 2016-10-08 | 2017-28-07 |
------------------------------------------------------
3 | 2017-28-08 | |
-----------------------------------------------------
4 | 2017-30-08 | |

MySQL SUBTRACT with SUM Query for One Single Column Same Table

Transactions_Table:
+---------+--------+-------------+--------------+-----+
| DocType | SFCode | Productname | WarrantyCode | QTY |
+---------+--------+-------------+--------------+-----+
| FP | 12 | Item | 1111-01 | 100 | -100
| FP | 12 | Item | 2222-22 | 200 |
| FP | 12 | Item | 3333-33 | 350 | -350
| LP | 12 | Item | 4444-44 | 10 |
| LP | 12 | Item | 5555-55 | 20 |
| LP | 12 | Item | 6666-66 | 35 | -35
| CAS | 12 | Item | 1111-01 | 50 | -(50 Left, show)
| CRS | 12 | Item | 3333-33 | 120 | -(230 Left, show)
| CRS | 12 | Item | 6666-66 | 35 | -(0 Left, no show)
| FPR | 12 | Item | 1111-01 | 10 | -(40 Left, show)
| LPR | 12 | Item | 5555-55 | 20 | -(0 Left, no show)
| CSR | 12 | Item | 1111-01 | 5 | -(50+5 Left, show)
| CRR | 12 | Item | 6666-66 | 5 | -(Got back 5, show)
+---------+--------+-------------+--------------+-----|
KEY:
FP: Foreign Purchase
LP: Local Purchase
CAS: Cash Sale
CRS: Credit Sale
FPR: Foreign Purchase Return
LPR: Local Purchase Return
CSR: Cash Sale Return
CRR: Credit Sale Return
There are many products but for now focussing on a single SFCode "12".
QTY is the Physical Stock PRESENT in the store, and the DocType are the transactions.
There are 2 Things I need to do with this table.
Get Current Stock which is (FP+LP+CSR+CRR) - (FPR+LPR+CAS+CRS) Note: There maybe no transaction of a particular DocType
Get Warranty Code(s) for a Product which has not been Sold Out for a particular Warranty Code. Go from Top to Bottom in Table last Column (not named) and you will get the idea.
Please suggest Java-MySql statement(s) that will help me achieve this result. Any help is appreciated.
Try something like this for #1:
SELECT SFCode, SUM(FP+LP+CSR+CRR-FPR-LPR-CAS-CRS) AS Total FROM
(SELECT SFCode,
SUM(IF(DocType = "FP", QTY, 0)) AS FP,
>>please fill out all the columns<<
FROM Transactions_Table
WHERE SFCode = "12"
GROUP BY DocType);
This is my shot at #2: (This assumes SFCode isn't an integer)
SELECT a.SFCode, a.WarrantyCode, (a.QTY-b.QTY) AS Stock FROM
(SELECT SFCode, WarrantyCode, QTY
FROM Transactions_Table
WHERE SFCode = "12"
AND DocType IN ('FP','LP','CSR','CRR')
GROUP BY WarrantyCode) AS a
LEFT JOIN
(SELECT SFCode, WarrantyCode, QTY
FROM Transactions_Table
WHERE SFCode = "12"
AND DocType IN ('FPR','LPR','CAS','CRS')
GROUP BY WarrantyCode) AS b
ON a.SFCode = b.SFCode AND a.WarrantyCode = b.WarrantyCode;
Can't really test this myself right now but this should at least give you an idea.

Getting repeated entries in table on a one-to-many relation

I'm learning jpa and decided to do a basic webapp where a User can post Messages, so a user have many messages. The problem is that when I add a message to the User's messages collection, I get duplicated entries in the Message table. (note I have two tables with the owner Id on Message, there is no join table).
This is the conversation:
First message -> Larry says: Hello John
Second message -> Larry says: Are you there?
Third Message -> John says: Yep
Fourth Message -> Larry says: ok
Fifth Message -> John says: fine
This is what I get in the screen:
Larry: Hello John
Larry Hello John
Larry: Are you there?
John: Yep
Larry: Hello John
Larry: Are you there?
Larry: ok
John: Yep
John: fine
The table content is:
+----+----------------+----------+
| id | message | owner_id |
+----+----------------+----------+
| 1 | Hello John | NULL | <- Why the NULL fields?
| 2 | Hello John | NULL |
| 3 | Are you there? | NULL |
| 4 | Yep | NULL |
| 5 | Hello John | 1 |
| 6 | Are you there? | 1 |
| 7 | ok | 1 |
| 8 | Yep | 2 |
| 9 | fine | 2 |
+----+----------------+----------+
These are the classes I have and its mappings.
#Entity
public class User implements Serializable {
#Id
#GeneratedValue
private Long id;
private String username;
private String password;
#OneToMany (fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name="owner_id")
private List<Message> messages; ...and all of its corresponding getters and setters
#Entity
public class Message implements Serializable {
#Id
#GeneratedValue
private Long id;
private String message;
#ManyToOne
private User owner; //...and all of its corresponding getters and setters
This is the relevant jsp scriptlet section which saves the user:
User user = (User)session.getAttribute("user");
user.getMessages().add(new Message(request.getParameter("message")));
FactoryDAO.getInstance().getUserDAO().update(user);
update(user) method:
public void update(User user) {
initEntityManager(); <- method in the superclass which initializes the entityManager
tx.begin();
entityManager.merge(user);
entityManager.flush();
tx.commit();
closeEntityManager();
}
Can you help me to find out what is wrong here?
Thanks!
It might have something to do with the way you're creating new User objects (not shown in your code).
Other than that, a couple of things you could try:
Use persist and not merge in the update method
Use a Set instead of a List for storing messages
Be sure to override equals and hashCode in both of your entities
I fix the problem doing as follows:
<%
User user = (User)session.getAttribute("user");
Message message = new Message(request.getParameter("message"));
message.setOwner(user);
FactoryDAO.getInstance().getMessageDAO().update(message);
%>
I have set a owner to a message instead of a message to a user.
I still don't understand why my first version didn't work as I expected.

Categories

Resources