Skip to content
91 changes: 91 additions & 0 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ecs160</groupId>
<artifactId>annotations</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.json</include>
</includes>
<excludes>
<exclude>desktop.ini</exclude>
</excludes>
</resource>
<resource>
<directory>target/generated-sources/annotations</directory>
<excludes>
<exclude>desktop.ini</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<transformers>
<transformer>
<mainClass>com.ecs160.MyApp</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.ecs160.MyApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.ecs160.hw1.SocialMediaAnalyzerDriver</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
</properties>
</project>
112 changes: 112 additions & 0 deletions newpost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.ecs160;

import com.google.gson.annotations.SerializedName;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

@Persistable
public class Post {

@PersistableId
@SerializedName("cid")
private String postId;

@PersistableField
@SerializedName("post")
private PostData post; // ✅ `record` 现在在 `post` 里面

@PersistableField
@SerializedName("thread")
private Thread thread; // ✅ `replies` 仍然在 `thread` 里

@LazyLoad
@PersistableListField(className = "Post")
@SerializedName("replyIds")
private List<String> replyIds;

private transient List<Post> replies;

public String getPostId() {
return (post != null) ? post.getCid() : null;
}

// ✅ `record` 现在在 `post` 里
public String getPostContent() {
if (post != null && post.getRecord() != null) {
return maskUrls(post.getRecord().getText());
}
return null;
}

private String maskUrls(String text) {
if (text == null) return null;
return text.replaceAll("(https?://\\S+|\\S+\\.\\S+)", "[link]");
}

// ✅ 解析 `thread.replies`
public List<Post> getReplies() {
if (replies == null && thread != null && thread.getReplies() != null) {
replies = thread.getReplies();
}
return replies;
}

public void setReplies(List<Post> replies) {
if (replies != null) {
this.replies = replies;
this.replyIds = replies.stream().map(Post::getPostId).toList();
}
}

@Override
public String toString() {
return "Post{" +
"postId=" + getPostId() +
", postContent='" + getPostContent() + '\'' +
", replies=" + (replyIds != null ? replyIds : "[]") +
'}';
}

// ✅ `Thread` 仍然保留,因为 `replies` 在 `thread` 里
public static class Thread {
@SerializedName("post")
private PostData post;

@SerializedName("replies")
private List<Post> replies; // ✅ `replies` 仍然在 `thread` 里

public PostData getPost() {
return post;
}

public List<Post> getReplies() {
return replies;
}
}

public static class PostData {
@SerializedName("cid")
private String cid;

@SerializedName("record")
private Record record; // ✅ `record` 现在在 `post` 里

public String getCid() {
return cid;
}

public Record getRecord() {
return record;
}
}

public static class Record {
@SerializedName("text")
private String text;

public String getText() {
return text;
}
}
}
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.ecs160.MyApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/ecs160/JsonLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.ecs160;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

public class JsonLoader {
private static final Gson gson = new Gson();

public static List<Post> loadPosts(String filePath) throws IOException {
try (FileReader reader = new FileReader(filePath)) {
JsonObject jsonObject = gson.fromJson(reader, JsonObject.class);
JsonArray feedArray = jsonObject.getAsJsonArray("feed");
Type listType = new TypeToken<List<Post>>() {}.getType();
List<Post> posts = gson.fromJson(feedArray, listType);


/*
for (int i = 0; i < Math.min(posts.size(), 10); i++) {
System.out.println("加载 Post ID: " + posts.get(i).getPostId());
}
*/
return posts;
}
}
}
98 changes: 93 additions & 5 deletions src/main/java/com/ecs160/MyApp.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,101 @@
package com.ecs160;


import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import com.ecs160.persistence.Session;
import java.util.Scanner;
import java.util.List;

public class MyApp {
public static void main(String[] args) throws FileNotFoundException, URISyntaxException, NoSuchFieldException {
public static void main(String[] args) {


Session.getredisSession().CleanDataBase();

try {
String filePath = "src/main/resources/input.json";
List<Post> posts = JsonLoader.loadPosts(filePath);
int count = 0;
Session session = Session.getredisSession();
for (Post post : posts) {

session.add(post);
//TestIfGotPostsFromJson(count,post);
}
session.persistAll();

//PrintFist10Posts(posts);

UserInput();

} catch (Exception e) {
e.printStackTrace();
}

}
}


public static void UserInput() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("\nPlease Enter Post ID (or type 'exit' to quit): ");
String inputPostId = scanner.nextLine().trim();

if (inputPostId.equalsIgnoreCase("exit")) {
System.out.println("Exiting UserInput mode...");
break;
}

// 加载 Post
Post post = (Post) Session.getredisSession().load(Post.class, inputPostId);

if (post != null) {
// 成功查询到 Post
System.out.println("\nLoad Post Successfully...");
System.out.println("Post Content: " + post.getPostContent());
System.out.println("Reply IDs: " + (post.getReplyIds() != null ? post.getReplyIds() : "[]"));
List<String> replyTexts = post.getReplyTexts();
if (!replyTexts.isEmpty()) {
System.out.println(" Replies:");
for (String replyText : replyTexts) {
System.out.println(" " + replyText);
}
} else {
System.out.println(" No Replies Found.");
}
} else {
System.out.println("Post Not Found. Please try again.");
}
}
scanner.close();
}

public static void TestIfGotPostsFromJson(int count, Post post) {
if (count <=20) {
System.out.println("\n========== DEBUG: Loading Posts =========="+ " Number Of "+ count);
System.out.println("\uD83D\uDCCC" + post);
System.out.println("✅ Debug: if have replies In the Posts: " + post.getReplyIds());
}
}

public static void PrintFist10Posts(List<Post> posts) {
int J= 0;
for (int i = 0; i < Math.min(posts.size(), 2); i++) {
String postId = posts.get(i).getPostId();

Post loadedPost = (Post) Session.getredisSession().load(Post.class, postId);
if (loadedPost != null) {

System.out.println("\n========== DEBUG: Loading Posts From Redis ==========");

System.out.println("✅ Post ID: " + loadedPost.getPostId());
System.out.println("✅ Post Content: " + loadedPost.getPostContent());
System.out.println("✅ Reply IDs from Redis: " +
(loadedPost.getReplyIds() != null ? loadedPost.getReplyIds() : "[]"));


//System.out.println("\uD83D\uDCCC" + loadedPost.getReplyTexts());
}
}
}

}
Loading