Skip to content

Commit

Permalink
Store table in database for redirect service
Browse files Browse the repository at this point in the history
  • Loading branch information
mingan666 committed Dec 29, 2022
1 parent 9655e7e commit c7017a3
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
1 change: 1 addition & 0 deletions remotesync-api/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target/
persistence.xml
24 changes: 23 additions & 1 deletion remotesync-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
are made available under the terms of the GNU Public License v2.0
which accompanies this distribution, and is available at
http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
Contributors:
Matthieu Helleboid - initial API and implementation
-->
Expand All @@ -13,7 +13,29 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>remotesync-api</artifactId>
<name>Piwigo Remote Sync API</name>

<properties>
<hibernate.version>5.6.14.Final</hibernate.version>
<mariadb.version>3.0.6</mariadb.version>
</properties>

<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core-jakarta</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>${mariadb.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

<dependency>
<groupId>args4j</groupId>
<artifactId>args4j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* are made available under the terms of the GNU Public License v2.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
*
* Contributors:
* Matthieu Helleboid - initial API and implementation
******************************************************************************/
Expand All @@ -20,6 +20,7 @@
import org.piwigo.remotesync.api.Constants;
import org.piwigo.remotesync.api.ISyncConfiguration;
import org.piwigo.remotesync.api.cache.LegacyCache;
import org.piwigo.remotesync.menalto.Importer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

protected SyncDirectoryWalker(ISyncConfiguration syncConfiguration) {
super(null, Constants.IMAGE_EXTENSIONS_FILTER, -1);
this.syncConfiguration = syncConfiguration;
importer = new Importer(syncConfiguration.getDirectory());
}

@Override
Expand All @@ -55,7 +58,9 @@ protected void handleDirectoryStart(File directory, int depth, Collection<File>
// FIXME : ignore me
}
logger.info("Creating album for " + directory);
legacyCache.addAlbum(createAlbum(directory, parentAlbumId));
Integer albumId = createAlbum(directory, parentAlbumId);
legacyCache.addAlbum(albumId);
importer.addAlbum(directory.getAbsolutePath(), albumId);
}
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.piwigo.remotesync.menalto;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.NamedQueries;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Table;

@Entity
@Table(name = "import_item")
@NamedQueries({
@NamedQuery(name = "ImportItem.findAll", query = "select t from ImportItem t")
})
public class ImportItem
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer myId;

@Column(name = "path", nullable = false, length = 255)
private String myPath;

@Column(name = "album_id", nullable = false)
private Integer myAlbumId;

@Column(name = "image_id", nullable = true)
private Integer myImageId;

public Integer getId()
{
return myId;
}

public void setId(Integer id)
{
myId = id;
}

public String getPath()
{
return myPath;
}

public ImportItem setPath(String path)
{
myPath = path;
return this;
}

public Integer getAlbumId()
{
return myAlbumId;
}

public ImportItem setAlbumId(Integer albumId)
{
myAlbumId = albumId;
return this;
}

public Integer getImageId()
{
return myImageId;
}

public ImportItem setImageId(Integer imageId)
{
myImageId = imageId;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.piwigo.remotesync.menalto;

import jakarta.persistence.EntityManager;

public class Importer
{
private final EntityManager myEntityManager;
private final String myStartDirectory;

public Importer(String directory)
{
PersistenceManager persistenceManager = new PersistenceManager();
myEntityManager = persistenceManager.getEntityManager();
myStartDirectory = directory;
}

public void addAlbum(String path, Integer albumId)
{
addImportItem(new ImportItem().setPath(getRelativePath(path)).setAlbumId(albumId));
}

public void addImage(String path, Integer albumId, Integer imageId)
{
addImportItem(new ImportItem().setPath(getRelativePath(path)).setAlbumId(albumId).setImageId(imageId));
}

private String getRelativePath(String path)
{
String relative = path.replace(myStartDirectory, "");
return relative = relative.startsWith("/") ? relative.substring(1) : relative;
}

private void addImportItem(ImportItem importItem)
{
myEntityManager.getTransaction().begin();
myEntityManager.persist(importItem);
myEntityManager.getTransaction().commit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.piwigo.remotesync.menalto;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;

public class PersistenceManager
{
public static final String PERSISTENCE_UNIT_NAME = "gallery3";

private final EntityManagerFactory myFactory;
private final EntityManager myEntityManager;

public PersistenceManager()
{
try
{
myFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
myEntityManager = myFactory.createEntityManager();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}

public void shutdown()
{
if (myEntityManager != null && myEntityManager.isOpen())
{
myEntityManager.close();
}

if (myFactory != null && myFactory.isOpen())
{
myFactory.close();
}
}

public EntityManager getEntityManager()
{
if (myEntityManager != null && myEntityManager.isOpen())
{
return myEntityManager;
}
throw new RuntimeException("No database connection for persistence unit: " + PERSISTENCE_UNIT_NAME);
}
}
15 changes: 15 additions & 0 deletions remotesync-api/src/main/resources/META-INF/persistence.xml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<persistence-unit name="gallery3">
<class>se.rutgers.gallery.persistence.menalto.ImportItem</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mariadb://<host>>/gallery3"/>
<property name="javax.persistence.jdbc.user" value="<username>"/>
<property name="javax.persistence.jdbc.password" value="<password>"/>
<property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MariaDB103Dialect"/>
</properties>
</persistence-unit>
</persistence>

0 comments on commit c7017a3

Please sign in to comment.