Skip to content

Commit

Permalink
Foreign key constraints when inserting... need to figure this out
Browse files Browse the repository at this point in the history
  • Loading branch information
feminaexlux committed Sep 1, 2013
1 parent f9c2e40 commit 5f804cd
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 275 deletions.
5 changes: 3 additions & 2 deletions gallery-struts2.iml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<orderEntry type="library" name="Maven: javassist:javassist:3.11.0.GA" level="project" />
<orderEntry type="library" name="Maven: asm:asm:3.3" level="project" />
<orderEntry type="library" name="Maven: asm:asm-commons:3.3" level="project" />
<orderEntry type="library" name="Maven: asm:asm-tree:3.3" level="project" />
<orderEntry type="library" name="Maven: org.freemarker:freemarker:2.3.19" level="project" />
<orderEntry type="library" name="Maven: commons-fileupload:commons-fileupload:1.3" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.4" level="project" />
Expand All @@ -81,15 +82,15 @@
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.0-beta8" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-core:2.0-beta8" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.8" level="project" />
<orderEntry type="library" name="Maven: org.hibernate:hibernate-core:4.2.3.Final" level="project" />
<orderEntry type="library" name="Maven: org.hibernate:hibernate-core:4.2.4.Final" level="project" />
<orderEntry type="library" name="Maven: antlr:antlr:2.7.7" level="project" />
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.1.0.GA" level="project" />
<orderEntry type="library" name="Maven: dom4j:dom4j:1.6.1" level="project" />
<orderEntry type="library" name="Maven: org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:1.0.1.Final" level="project" />
<orderEntry type="library" name="Maven: org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final" level="project" />
<orderEntry type="library" name="Maven: org.hibernate.common:hibernate-commons-annotations:4.0.2.Final" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.15.0-GA" level="project" />
<orderEntry type="library" name="Maven: org.hibernate:hibernate-entitymanager:4.2.3.Final" level="project" />
<orderEntry type="library" name="Maven: org.hibernate:hibernate-entitymanager:4.2.4.Final" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:3.2.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-tx:3.2.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-orm:3.2.3.RELEASE" level="project" />
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<codec-version>1.8</codec-version>
<fileupload-version>1.3</fileupload-version>
<io-version>2.4</io-version>
<hibernate-version>4.2.3.Final</hibernate-version>
<hibernate-version>4.2.4.Final</hibernate-version>
<spring-version>3.2.3.RELEASE</spring-version>
<mysql-version>5.1.25</mysql-version>
<junit-version>4.11</junit-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class Administration extends Controller {
private static final Logger LOG = LogManager.getLogger(Administration.class);
Expand All @@ -26,6 +27,21 @@ public class Administration extends Controller {
private String uploadFileName;
private String uploadContentType;

private Map<String, String> form;

public String saveAlbum() {
if (form != null && !form.isEmpty()) {
Album album = new Album();
album.setName(form.get("album_name"));
album.setDescription(form.get("album_description"));
albumService.save(album);
} else {
addActionError("Album details are missing");
}

return SUCCESS;
}

public String upload() {
if (upload != null && albumId > 0) {
try {
Expand Down Expand Up @@ -64,6 +80,14 @@ public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

public Map<String, String> getForm() {
return form;
}

public void setForm(Map<String, String> form) {
this.form = form;
}

public List<Album> getAlbums() {
return albumService.getAll();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ public <T extends Resource> T save(T resource) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();

entityManager.merge(resource);
entityManager.flush();

entityManager.getTransaction().commit();
try {
entityManager.merge(resource);
entityManager.flush();

entityManager.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
entityManager.getTransaction().rollback();
}

return resource;
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/net/feminaexlux/gallery/struts2/model/Album.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
import javax.persistence.*;

@Entity
@Table(name = "album")
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id"),
@PrimaryKeyJoinColumn(name = "type", referencedColumnName = "type")
@DiscriminatorValue(ResourceType.ALBUM)
@SecondaryTable(name = "album", pkJoinColumns = {
@PrimaryKeyJoinColumn(name = "album_id", referencedColumnName = "resource_id"),
@PrimaryKeyJoinColumn(name = "album_type", referencedColumnName = "resource_type")
})
public class Album extends Resource implements Linkable {
private Album parent;
private String description;
private String slug;

public Album() {
this.key = new ResourceKey(0, ResourceType.ALBUM);
}

@ManyToOne
@JoinColumns({
@JoinColumn(name = "parent_id", referencedColumnName = "id"),
@JoinColumn(name = "parent_type", referencedColumnName = "type")
@JoinColumn(table = "album", name = "album_parent_id", referencedColumnName = "resource_id"),
@JoinColumn(table = "album", name = "album_parent_type", referencedColumnName = "resource_type")
})
public Album getParent() {
return parent;
Expand All @@ -26,7 +30,7 @@ public void setParent(Album parent) {
this.parent = parent;
}

@Column(nullable = false)
@Column(table = "album", name = "album_description", nullable = false)
public String getDescription() {
return description;
}
Expand All @@ -35,7 +39,7 @@ public void setDescription(String description) {
this.description = description;
}

@Column(nullable = false, length = 100)
@Column(table = "album", name = "album_slug", nullable = false, length = 100)
@Override
public String getSlug() {
return slug;
Expand Down
30 changes: 19 additions & 11 deletions src/main/java/net/feminaexlux/gallery/struts2/model/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
import javax.persistence.*;

@Entity
@Table(name = "image")
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id"),
@PrimaryKeyJoinColumn(name = "type", referencedColumnName = "type")
@DiscriminatorValue(ResourceType.IMAGE)
@SecondaryTable(name = "image", pkJoinColumns = {
@PrimaryKeyJoinColumn(name = "image_id", referencedColumnName = "resource_id"),
@PrimaryKeyJoinColumn(name = "image_type", referencedColumnName = "resource_type")
})
public class Image extends Resource implements Linkable {
public Image() {
this.key = new ResourceKey(0, ResourceType.IMAGE);
}

public Image(String type) {
this.key = new ResourceKey(0, type);
}

private Album album;
private byte[] image;
private byte[] thumbnail;
Expand All @@ -18,8 +26,8 @@ public class Image extends Resource implements Linkable {

@ManyToOne
@JoinColumns({
@JoinColumn(name = "album_id", referencedColumnName = "id"),
@JoinColumn(name = "album_type", referencedColumnName = "type")
@JoinColumn(table = "image", name = "image_album_id", referencedColumnName = "resource_id"),
@JoinColumn(table = "image", name = "image_album_type", referencedColumnName = "resource_type")
})
public Album getAlbum() {
return album;
Expand All @@ -29,7 +37,7 @@ public void setAlbum(Album parent) {
this.album = parent;
}

@Column(nullable = false)
@Column(table = "image", name = "image_content", nullable = false)
public byte[] getImage() {
return image;
}
Expand All @@ -38,7 +46,7 @@ public void setImage(byte[] image) {
this.image = image;
}

@Column(nullable = false)
@Column(table = "image", name = "image_thumbnail", nullable = false)
public byte[] getThumbnail() {
return thumbnail;
}
Expand All @@ -47,7 +55,7 @@ public void setThumbnail(byte[] thumbnail) {
this.thumbnail = thumbnail;
}

@Column(name = "content_type", nullable = false, length = 50)
@Column(table = "image", name = "image_content_type", nullable = false, length = 50)
public String getContentType() {
return contentType;
}
Expand All @@ -56,7 +64,7 @@ public void setContentType(String contentType) {
this.contentType = contentType;
}

@Column(nullable = false)
@Column(table = "image", name = "image_description", nullable = false)
public String getDescription() {
return description;
}
Expand All @@ -65,7 +73,7 @@ public void setDescription(String description) {
this.description = description;
}

@Column(nullable = false, length = 100)
@Column(table = "image", name = "image_slug", nullable = false, length = 50)
@Override
public String getSlug() {
return slug;
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/net/feminaexlux/gallery/struts2/model/Resource.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package net.feminaexlux.gallery.struts2.model;

import org.hibernate.annotations.DiscriminatorOptions;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "resource")
@Inheritance(strategy = InheritanceType.JOINED)
@Inheritance
@DiscriminatorColumn(name = "resource_type")
@DiscriminatorOptions(insert = false)
public class Resource {
protected ResourceKey key;
protected String name;
Expand All @@ -32,14 +36,15 @@ public int getId() {
}

@Transient
public ResourceType getType() {
public String getType() {
if (key == null) {
return null;
}

return key.getType();
}

@Column(name = "resource_name", nullable = false, length = 50)
public String getName() {
return name;
}
Expand All @@ -48,6 +53,8 @@ public void setName(String name) {
this.name = name;
}

@Column(name = "resource_created")
@Temporal(TemporalType.TIMESTAMP)
public Date getCreated() {
return created;
}
Expand All @@ -56,6 +63,8 @@ public void setCreated(Date created) {
this.created = created;
}

@Column(name = "resource_updated")
@Temporal(TemporalType.TIMESTAMP)
public Date getUpdated() {
return updated;
}
Expand All @@ -64,6 +73,8 @@ public void setUpdated(Date updated) {
this.updated = updated;
}

@Column(name = "resource_deleted")
@Temporal(TemporalType.TIMESTAMP)
public Date getDeleted() {
return deleted;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package net.feminaexlux.gallery.struts2.model;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import java.io.Serializable;

@Embeddable
public class ResourceKey implements Serializable {
private int id;
private ResourceType type;
private String type;

public ResourceKey() {
}

public ResourceKey(int id, String type) {
this.id = id;
this.type = new ResourceType(type);
this.type = type;
}

@Column(name = "resource_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
Expand All @@ -26,13 +29,12 @@ public void setId(int id) {
this.id = id;
}

@ManyToOne
@JoinColumn(name = "type", referencedColumnName = "type")
public ResourceType getType() {
@Column(name = "resource_type", length = 50)
public String getType() {
return type;
}

public void setType(ResourceType type) {
public void setType(String type) {
this.type = type;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,8 @@
package net.feminaexlux.gallery.struts2.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "resource_type")
public class ResourceType {
public static final String ALBUM = "Album";
public static final String COMIC = "Comic";
public static final String IMAGE = "Image";
public static final String NSFW = "Nsfw";
public static final String TAG = "Tag";
public static final String ADMIN = "Admin";

private String type;

public ResourceType() {
}

public ResourceType(String type) {
this.type = type;
}

@Id
@Column(name = "type", nullable = false, length = 50)
public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ResourceType that = (ResourceType) o;

if (type != null ? !type.equals(that.type) : that.type != null) return false;

return true;
}

@Override
public int hashCode() {
return type != null ? type.hashCode() : 0;
}

@Override
public String toString() {
return type;
}
public static final String USER = "User";
}
Loading

0 comments on commit 5f804cd

Please sign in to comment.