101 lines
4.6 KiB
Java
101 lines
4.6 KiB
Java
import java.io.File;
|
|
import java.nio.file.Files;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class PopulateLivestreamRaw {
|
|
public static void main(String[] args) {
|
|
try {
|
|
Class.forName("oracle.jdbc.OracleDriver");
|
|
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/sisvietnam", "sisvietnam", "sisvietnam");
|
|
|
|
// Get tag ID
|
|
long tagId = 6351; // Hardcoded from CheckTag
|
|
|
|
// Delete old tags
|
|
PreparedStatement delTagStmt = conn.prepareStatement("DELETE FROM sis_post_tag WHERE post_id IN (SELECT id FROM sis_post WHERE category_id IS NULL) OR tag_id = ?");
|
|
delTagStmt.setLong(1, tagId);
|
|
delTagStmt.executeUpdate();
|
|
|
|
// Delete old posts
|
|
PreparedStatement delStmt = conn.prepareStatement("DELETE FROM sis_post WHERE id = 6450 OR id >= 6600");
|
|
delStmt.executeUpdate();
|
|
|
|
String baseDir = "/home/x79/sisvietnamvn_01/Media/Livestream";
|
|
String uploadDir = "/home/x79/sisvietnamvn_01/sisvietnamvn_main/src/main/resources/static/upload/livestream";
|
|
new File(uploadDir).mkdirs();
|
|
|
|
long currentId = 6600;
|
|
|
|
for (int i = 1; i <= 4; i++) {
|
|
String folder = baseDir + "/Tuan" + i;
|
|
File f = new File(folder);
|
|
if (!f.exists()) continue;
|
|
|
|
String title = "Tuần " + i;
|
|
String excerpt = "Tuần " + i;
|
|
StringBuilder content = new StringBuilder();
|
|
String metaDescRaw = "";
|
|
String imageUrl = "";
|
|
|
|
File contentFile = new File(folder, "content.txt");
|
|
if (contentFile.exists()) {
|
|
List<String> lines = Files.readAllLines(contentFile.toPath()).stream().filter(l -> !l.trim().isEmpty()).collect(Collectors.toList());
|
|
if (lines.size() > 0) title = lines.get(0);
|
|
if (lines.size() > 1) excerpt = lines.get(1);
|
|
for (int j = 2; j < lines.size(); j++) {
|
|
content.append(lines.get(j)).append("<br>");
|
|
}
|
|
}
|
|
|
|
File embedFile = new File(folder, "embed.txt");
|
|
if (!embedFile.exists()) embedFile = new File(folder, "url.txt");
|
|
if (embedFile.exists()) {
|
|
metaDescRaw = Files.readString(embedFile.toPath()); // Keep entire raw iframe
|
|
}
|
|
|
|
for (File img : f.listFiles()) {
|
|
if (img.getName().endsWith(".jpg") || img.getName().endsWith(".png")) {
|
|
File dest = new File(uploadDir, "tuan" + i + "_" + img.getName());
|
|
Files.copy(img.toPath(), dest.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
|
|
imageUrl = "/upload/livestream/" + dest.getName();
|
|
break;
|
|
}
|
|
}
|
|
|
|
String slug = "livestream-tuan-" + i;
|
|
|
|
PreparedStatement insertStmt = conn.prepareStatement(
|
|
"INSERT INTO sis_post (id, slug, title, excerpt, content, meta_description, featured_image, category_id, created_date, status, layout, created_by) " +
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, NULL, SYSDATE, 'PUBLISHED', 'STANDARD', 'admin')"
|
|
);
|
|
insertStmt.setLong(1, currentId);
|
|
insertStmt.setString(2, slug);
|
|
insertStmt.setString(3, title);
|
|
insertStmt.setString(4, excerpt);
|
|
insertStmt.setString(5, content.toString());
|
|
insertStmt.setString(6, metaDescRaw);
|
|
insertStmt.setString(7, imageUrl);
|
|
insertStmt.executeUpdate();
|
|
|
|
PreparedStatement tagStmt = conn.prepareStatement("INSERT INTO sis_post_tag (post_id, tag_id) VALUES (?, ?)");
|
|
tagStmt.setLong(1, currentId);
|
|
tagStmt.setLong(2, tagId);
|
|
tagStmt.executeUpdate();
|
|
|
|
currentId++;
|
|
System.out.println("Inserted Tuan " + i);
|
|
}
|
|
|
|
conn.close();
|
|
System.out.println("Done.");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|