Skip to content

Commit 88fbfcb

Browse files
committed
Store table in database for redirect service
1 parent 9655e7e commit 88fbfcb

File tree

9 files changed

+221
-12
lines changed

9 files changed

+221
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

remotesync-api/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target/
2+
persistence.xml

remotesync-api/pom.xml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
are made available under the terms of the GNU Public License v2.0
55
which accompanies this distribution, and is available at
66
http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7-
7+
88
Contributors:
99
Matthieu Helleboid - initial API and implementation
1010
-->
@@ -13,7 +13,29 @@
1313
<modelVersion>4.0.0</modelVersion>
1414
<artifactId>remotesync-api</artifactId>
1515
<name>Piwigo Remote Sync API</name>
16+
17+
<properties>
18+
<hibernate.version>5.6.14.Final</hibernate.version>
19+
<mariadb.version>3.0.6</mariadb.version>
20+
</properties>
21+
1622
<dependencies>
23+
<dependency>
24+
<groupId>org.hibernate</groupId>
25+
<artifactId>hibernate-core-jakarta</artifactId>
26+
<version>${hibernate.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.mariadb.jdbc</groupId>
30+
<artifactId>mariadb-java-client</artifactId>
31+
<version>${mariadb.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.commons</groupId>
35+
<artifactId>commons-lang3</artifactId>
36+
<version>3.12.0</version>
37+
</dependency>
38+
1739
<dependency>
1840
<groupId>args4j</groupId>
1941
<artifactId>args4j</artifactId>

remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/ConnectedWalker.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* are made available under the terms of the GNU Public License v2.0
55
* which accompanies this distribution, and is available at
66
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7-
*
7+
*
88
* Contributors:
99
* Matthieu Helleboid - initial API and implementation
1010
******************************************************************************/
@@ -19,6 +19,7 @@
1919
import org.piwigo.remotesync.api.exception.ClientServerException;
2020
import org.piwigo.remotesync.api.request.PwgCategoriesAddRequest;
2121
import org.piwigo.remotesync.api.request.PwgImagesUploadRequest;
22+
import org.piwigo.remotesync.menalto.Importer;
2223
import org.slf4j.Logger;
2324
import org.slf4j.LoggerFactory;
2425

@@ -28,15 +29,15 @@ public class ConnectedWalker extends SyncDirectoryWalker {
2829

2930
private AuthenticatedWSClient client;
3031
private ProgressLinker linker = new ProgressLinker();
31-
32+
3233
protected ConnectedWalker(ISyncConfiguration syncConfiguration) {
3334
super(syncConfiguration);
3435
}
3536

3637
protected void connect() throws IOException {
3738
if (client != null)
3839
return;
39-
40+
4041
try {
4142
logger.info("Connecting... ");
4243
client = new AuthenticatedWSClient(syncConfiguration).login();
@@ -51,7 +52,7 @@ protected void connect() throws IOException {
5152
protected void disconnect() {
5253
if (client == null)
5354
return;
54-
55+
5556
try {
5657
logger.info("Disconnecting... ");
5758
client.logout();
@@ -61,15 +62,15 @@ protected void disconnect() {
6162
logger.error("Unable to disconnect : " + e.getMessage(), e);
6263
}
6364
}
64-
65+
6566
protected AuthenticatedWSClient getClient() {
6667
return (client);
6768
}
68-
69+
6970
public ProgressLinker getProgressLinker() {
7071
return (linker);
7172
}
72-
73+
7374
public void setSyncConfiguration(ISyncConfiguration conf)
7475
{
7576
this.syncConfiguration = conf;
@@ -99,7 +100,7 @@ protected Integer createAlbum(File directory, Integer parentAlbumId) throws IOEx
99100
}
100101

101102
@Override
102-
protected Integer createImage(File file, Integer albumId) throws IOException {
103+
protected Integer createImage(File file, Integer albumId) throws IOException {
103104
try {
104105
linker.fillImageDetails(file);
105106
linker.addImage();

remotesync-api/src/main/java/org/piwigo/remotesync/api/sync/SyncDirectoryWalker.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* are made available under the terms of the GNU Public License v2.0
55
* which accompanies this distribution, and is available at
66
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7-
*
7+
*
88
* Contributors:
99
* Matthieu Helleboid - initial API and implementation
1010
******************************************************************************/
@@ -20,6 +20,7 @@
2020
import org.piwigo.remotesync.api.Constants;
2121
import org.piwigo.remotesync.api.ISyncConfiguration;
2222
import org.piwigo.remotesync.api.cache.LegacyCache;
23+
import org.piwigo.remotesync.menalto.Importer;
2324
import org.slf4j.Logger;
2425
import org.slf4j.LoggerFactory;
2526

@@ -30,10 +31,12 @@ public abstract class SyncDirectoryWalker extends DirectoryWalker<File> {
3031
protected ISyncConfiguration syncConfiguration;
3132
protected File startDirectory;
3233
protected Map<File, LegacyCache> legacyCaches = new HashMap<File, LegacyCache>();
34+
private Importer importer;
3335

3436
protected SyncDirectoryWalker(ISyncConfiguration syncConfiguration) {
3537
super(null, Constants.IMAGE_EXTENSIONS_FILTER, -1);
3638
this.syncConfiguration = syncConfiguration;
39+
importer = new Importer(syncConfiguration.getDirectory());
3740
}
3841

3942
@Override
@@ -55,7 +58,9 @@ protected void handleDirectoryStart(File directory, int depth, Collection<File>
5558
// FIXME : ignore me
5659
}
5760
logger.info("Creating album for " + directory);
58-
legacyCache.addAlbum(createAlbum(directory, parentAlbumId));
61+
Integer albumId = createAlbum(directory, parentAlbumId);
62+
legacyCache.addAlbum(albumId);
63+
importer.addAlbum(directory.getAbsolutePath(), albumId);
5964
}
6065
}
6166

@@ -72,7 +77,9 @@ protected void handleFile(File file, int depth, java.util.Collection<File> resul
7277
// FIXME : ignore me
7378
}
7479
logger.info("Uploading " + file.getName() + " in album with ID " + albumId);
75-
legacyCache.addImage(file, createImage(file, albumId));
80+
Integer imageId = createImage(file, albumId);
81+
legacyCache.addImage(file, imageId);
82+
importer.addImage(file.getAbsolutePath(), albumId, imageId);
7683
}
7784
}
7885

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.piwigo.remotesync.menalto;
2+
3+
import jakarta.persistence.Column;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.GeneratedValue;
6+
import jakarta.persistence.GenerationType;
7+
import jakarta.persistence.Id;
8+
import jakarta.persistence.NamedQueries;
9+
import jakarta.persistence.NamedQuery;
10+
import jakarta.persistence.Table;
11+
12+
@Entity
13+
@Table(name = "import_item")
14+
@NamedQueries({
15+
@NamedQuery(name = "ImportItem.findAll", query = "select t from ImportItem t")
16+
})
17+
public class ImportItem
18+
{
19+
@Id
20+
@GeneratedValue(strategy = GenerationType.IDENTITY)
21+
@Column(name = "id", nullable = false)
22+
private Integer myId;
23+
24+
@Column(name = "path", nullable = false, length = 255)
25+
private String myPath;
26+
27+
@Column(name = "album_id", nullable = false)
28+
private Integer myAlbumId;
29+
30+
@Column(name = "image_id", nullable = true)
31+
private Integer myImageId;
32+
33+
public Integer getId()
34+
{
35+
return myId;
36+
}
37+
38+
public void setId(Integer id)
39+
{
40+
myId = id;
41+
}
42+
43+
public String getPath()
44+
{
45+
return myPath;
46+
}
47+
48+
public ImportItem setPath(String path)
49+
{
50+
myPath = path;
51+
return this;
52+
}
53+
54+
public Integer getAlbumId()
55+
{
56+
return myAlbumId;
57+
}
58+
59+
public ImportItem setAlbumId(Integer albumId)
60+
{
61+
myAlbumId = albumId;
62+
return this;
63+
}
64+
65+
public Integer getImageId()
66+
{
67+
return myImageId;
68+
}
69+
70+
public ImportItem setImageId(Integer imageId)
71+
{
72+
myImageId = imageId;
73+
return this;
74+
}
75+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.piwigo.remotesync.menalto;
2+
3+
import jakarta.persistence.EntityManager;
4+
5+
public class Importer
6+
{
7+
private final EntityManager myEntityManager;
8+
private final String myStartDirectory;
9+
10+
public Importer(String directory)
11+
{
12+
PersistenceManager persistenceManager = new PersistenceManager();
13+
myEntityManager = persistenceManager.getEntityManager();
14+
myStartDirectory = directory;
15+
}
16+
17+
public void addAlbum(String path, Integer albumId)
18+
{
19+
addImportItem(new ImportItem().setPath(getRelativePath(path)).setAlbumId(albumId));
20+
}
21+
22+
public void addImage(String path, Integer albumId, Integer imageId)
23+
{
24+
addImportItem(new ImportItem().setPath(getRelativePath(path)).setAlbumId(albumId).setImageId(imageId));
25+
}
26+
27+
private String getRelativePath(String path)
28+
{
29+
String relative = path.replace(myStartDirectory, "");
30+
return relative = relative.startsWith("/") ? relative.substring(1) : relative;
31+
}
32+
33+
private void addImportItem(ImportItem importItem)
34+
{
35+
myEntityManager.getTransaction().begin();
36+
myEntityManager.persist(importItem);
37+
myEntityManager.getTransaction().commit();
38+
}
39+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.piwigo.remotesync.menalto;
2+
3+
import jakarta.persistence.EntityManager;
4+
import jakarta.persistence.EntityManagerFactory;
5+
import jakarta.persistence.Persistence;
6+
7+
public class PersistenceManager
8+
{
9+
public static final String PERSISTENCE_UNIT_NAME = "gallery3";
10+
11+
private final EntityManagerFactory myFactory;
12+
private final EntityManager myEntityManager;
13+
14+
public PersistenceManager()
15+
{
16+
try
17+
{
18+
myFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
19+
myEntityManager = myFactory.createEntityManager();
20+
}
21+
catch (Exception e)
22+
{
23+
throw new RuntimeException(e);
24+
}
25+
}
26+
27+
public void shutdown()
28+
{
29+
if (myEntityManager != null && myEntityManager.isOpen())
30+
{
31+
myEntityManager.close();
32+
}
33+
34+
if (myFactory != null && myFactory.isOpen())
35+
{
36+
myFactory.close();
37+
}
38+
}
39+
40+
public EntityManager getEntityManager()
41+
{
42+
if (myEntityManager != null && myEntityManager.isOpen())
43+
{
44+
return myEntityManager;
45+
}
46+
throw new RuntimeException("No database connection for persistence unit: " + PERSISTENCE_UNIT_NAME);
47+
}
48+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
3+
<persistence-unit name="gallery3">
4+
<class>se.rutgers.gallery.persistence.menalto.ImportItem</class>
5+
<properties>
6+
<property name="javax.persistence.jdbc.url" value="jdbc:mariadb://<host>>/gallery3"/>
7+
<property name="javax.persistence.jdbc.user" value="<username>"/>
8+
<property name="javax.persistence.jdbc.password" value="<password>"/>
9+
<property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver"/>
10+
<property name="hibernate.show_sql" value="true"/>
11+
<property name="hibernate.format_sql" value="true"/>
12+
<property name="hibernate.dialect" value="org.hibernate.dialect.MariaDB103Dialect"/>
13+
</properties>
14+
</persistence-unit>
15+
</persistence>

0 commit comments

Comments
 (0)