-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store table in database for redirect service
- Loading branch information
Showing
9 changed files
with
221 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/target/ | ||
persistence.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
remotesync-api/src/main/java/org/piwigo/remotesync/menalto/ImportItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
remotesync-api/src/main/java/org/piwigo/remotesync/menalto/Importer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
remotesync-api/src/main/java/org/piwigo/remotesync/menalto/PersistenceManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
remotesync-api/src/main/resources/META-INF/persistence.xml.example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |