Skip to content

Commit

Permalink
Changing database structure to be hibernate friendly :(
Browse files Browse the repository at this point in the history
  • Loading branch information
feminaexlux committed Sep 7, 2013
1 parent 251ed16 commit 65b5066
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ public class Administration extends Controller {

private Map<String, String> form;

// Models
private List<Album> albums;
private List<Image> images;

@Override
public String execute() throws Exception {
albums = albumService.getAll();
images = imageService.getAll();

return SUCCESS;
}

public String saveAlbum() {
if (form != null && !form.isEmpty()) {
Album album = new Album();
Expand All @@ -54,6 +66,7 @@ public String upload() {
image.setThumbnail(byteArray);
image.setName(uploadFileName);
image.setContentType(uploadContentType);
image.setDescription(form.get("image_description"));

imageService.save(image);
} catch (IOException ioException) {
Expand Down Expand Up @@ -89,10 +102,10 @@ public void setForm(Map<String, String> form) {
}

public List<Album> getAlbums() {
return albumService.getAll();
return albums;
}

public List<Image> getImages() {
return imageService.getAll();
return images;
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
package net.feminaexlux.gallery.struts2.controller;

import net.feminaexlux.gallery.struts2.model.Album;
import net.feminaexlux.gallery.struts2.model.Resource;
import net.feminaexlux.gallery.struts2.service.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;

public class Gallery extends Controller {
public static final int ALBUM_ID = 2;
import java.util.List;

public class Gallery extends Controller {
@Autowired
private AlbumService albumService;

private Album album;
private List<Album> albums;

@Override
public String execute() throws Exception {
album = albumService.getAlbum(ALBUM_ID);
albums = albumService.getAll();

return SUCCESS;
}

public String save() {
album = albumService.getAlbum(ALBUM_ID);
album.setDescription("Test");
album.setName("Test");

albumService.save(album);

return SUCCESS;
}

public String revert() {
album = albumService.getAlbum(ALBUM_ID);
album.setDescription("Original Art");
album.setName("Originals");

albumService.save(album);

return SUCCESS;
public Album getAlbum() {
return album;
}

public Resource getAlbum() {
return album;
public List<Album> getAlbums() {
return albums;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.feminaexlux.gallery.struts2.model.Image;
import net.feminaexlux.gallery.struts2.service.ImageService;
import net.feminaexlux.gallery.struts2.utility.StringUtility;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
Expand All @@ -19,12 +18,11 @@ public class ImageLoader extends Controller {
private ImageService imageService;

private int imageId;
private String imageType;

public String image() {
if (imageId > 0 && StringUtility.isNotEmpty(imageType)) {
if (imageId > 0) {
try {
Image image = imageService.getImage(imageId, imageType);
Image image = imageService.getImage(imageId);
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Content-Disposition", "attachment;filename=" + image.getName());
response.setContentType(image.getContentType());
Expand All @@ -49,8 +47,4 @@ public String thumbnail() {
public void setImageId(int imageId) {
this.imageId = imageId;
}

public void setImageType(String imageType) {
this.imageType = imageType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public T save(T resource) {

try {
if (resource.getId() == 0) {
// FIXME: hacky way of doing things
Query query = entityManager.createNativeQuery("SELECT MAX(resource_id) FROM resource");
int id = (Integer) query.getSingleResult();
resource.setId(id + 1);

entityManager.persist(resource);
} else {
entityManager.merge(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
@Entity
@DiscriminatorValue(ResourceType.ALBUM)
@SecondaryTable(name = "album", pkJoinColumns = {
@PrimaryKeyJoinColumn(name = "album_id", referencedColumnName = "resource_id"),
@PrimaryKeyJoinColumn(name = "album_type", referencedColumnName = "resource_type")
@PrimaryKeyJoinColumn(name = "album_id", referencedColumnName = "resource_id")
})
public class Album extends Resource implements Linkable {
@ManyToOne
@JoinColumns({
@JoinColumn(table = "album", name = "album_parent_id", referencedColumnName = "resource_id"),
@JoinColumn(table = "album", name = "album_parent_type", referencedColumnName = "resource_type")
})
@JoinColumn(table = "album", name = "album_parent_id", referencedColumnName = "resource_id")
private Album parent;

@Column(table = "album", name = "album_description", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
@Entity
@DiscriminatorValue(ResourceType.IMAGE)
@SecondaryTable(name = "image", pkJoinColumns = {
@PrimaryKeyJoinColumn(name = "image_id", referencedColumnName = "resource_id"),
@PrimaryKeyJoinColumn(name = "image_type", referencedColumnName = "resource_type")
@PrimaryKeyJoinColumn(name = "image_id", referencedColumnName = "resource_id")
})
public class Image extends Resource implements Linkable {
@ManyToOne
@JoinColumns({
@JoinColumn(table = "image", name = "image_album_id", referencedColumnName = "resource_id"),
@JoinColumn(table = "image", name = "image_album_type", referencedColumnName = "resource_type")
})
@JoinColumn(table = "image", name = "image_album_id", referencedColumnName = "resource_id")
private Album album;

@Column(table = "image", name = "image_content", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
public class Resource implements Serializable {
@Id
@Column(name = "resource_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected int id;

@Id
@Column(name = "resource_type", length = 50)
protected String type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
@Entity
@DiscriminatorValue(ResourceType.USER)
@SecondaryTable(name = "user", pkJoinColumns = {
@PrimaryKeyJoinColumn(name = "user_id", referencedColumnName = "resource_id"),
@PrimaryKeyJoinColumn(name = "user_type", referencedColumnName = "resource_type")
@PrimaryKeyJoinColumn(name = "user_id", referencedColumnName = "resource_id")
})
public class User extends Resource {
@Column(table = "user", name = "user_login", nullable = false, length = 50)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.feminaexlux.gallery.struts2.dao.ImageDAO;
import net.feminaexlux.gallery.struts2.model.Image;
import net.feminaexlux.gallery.struts2.model.ResourceType;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
Expand All @@ -10,8 +11,8 @@ public class ImageService extends ResourceService<Image> {
@Autowired
private ImageDAO imageDAO;

public Image getImage(final int id, final String type) {
return super.get(id, type, Image.class);
public Image getImage(final int id) {
return super.get(id, ResourceType.IMAGE, Image.class);
}

public List<Image> getAll() {
Expand Down
26 changes: 10 additions & 16 deletions src/main/resources/sql/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ USE `gallery`;
DROP TABLE IF EXISTS `album`;
CREATE TABLE IF NOT EXISTS `album` (
`album_id` INT(10) NOT NULL,
`album_type` VARCHAR(50) NOT NULL,
`album_description` TEXT NOT NULL,
`album_slug` VARCHAR(50) NOT NULL,
`album_parent_id` INT(10) DEFAULT NULL,
`album_parent_type` VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (`album_id`, `album_type`),
PRIMARY KEY (`album_id`),
UNIQUE KEY `album_slug` (`album_slug`),
KEY `FK2_album_parent` (`album_parent_id`, `album_parent_type`),
CONSTRAINT `FK1_album_resource` FOREIGN KEY (`album_id`, `album_type`) REFERENCES `resource` (`resource_id`, `resource_type`),
CONSTRAINT `FK2_album_parent` FOREIGN KEY (`album_parent_id`, `album_parent_type`) REFERENCES `album` (`album_id`, `album_type`)
KEY `FK1_album_parent` (`album_parent_id`),
CONSTRAINT `FK1_album_parent` FOREIGN KEY (`album_parent_id`) REFERENCES `album` (`album_id`)
)
ENGINE =InnoDB
DEFAULT CHARSET =utf8;
Expand All @@ -41,19 +38,17 @@ CREATE TABLE IF NOT EXISTS `album` (
DROP TABLE IF EXISTS `image`;
CREATE TABLE IF NOT EXISTS `image` (
`image_id` INT(10) NOT NULL,
`image_type` VARCHAR(50) NOT NULL,
`image_description` TEXT NOT NULL,
`image_slug` VARCHAR(50) NOT NULL,
`image_content_type` VARCHAR(50) NOT NULL,
`image_content` MEDIUMBLOB NOT NULL,
`image_thumbnail` MEDIUMBLOB NOT NULL,
`image_album_id` INT(10) NOT NULL,
`image_album_type` VARCHAR(50) NOT NULL,
PRIMARY KEY (`image_id`, `image_type`),
PRIMARY KEY (`image_id`),
UNIQUE KEY `image_slug` (`image_slug`),
KEY `FK2_image_album` (`image_album_id`, `image_album_type`),
CONSTRAINT `FK1_image_resource` FOREIGN KEY (`image_id`, `image_type`) REFERENCES `resource` (`resource_id`, `resource_type`),
CONSTRAINT `FK2_image_album` FOREIGN KEY (`image_album_id`, `image_album_type`) REFERENCES `album` (`album_id`, `album_type`)
KEY `FK2_image_album` (`image_album_id`),
CONSTRAINT `FK1_image_resource` FOREIGN KEY (`image_id`) REFERENCES `resource` (`resource_id`),
CONSTRAINT `FK2_image_album` FOREIGN KEY (`image_album_id`) REFERENCES `album` (`album_id`)
)
ENGINE =InnoDB
DEFAULT CHARSET =utf8;
Expand All @@ -70,7 +65,7 @@ CREATE TABLE IF NOT EXISTS `resource` (
`resource_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`resource_updated` TIMESTAMP NULL DEFAULT NULL,
`resource_deleted` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`resource_id`, `resource_type`),
PRIMARY KEY (`resource_id`),
UNIQUE KEY `resource_type_resource_name` (`resource_type`, `resource_name`)
)
ENGINE =InnoDB
Expand All @@ -83,12 +78,11 @@ CREATE TABLE IF NOT EXISTS `resource` (
DROP TABLE IF EXISTS `user`;
CREATE TABLE IF NOT EXISTS `user` (
`user_id` INT(10) NOT NULL,
`user_type` VARCHAR(50) NOT NULL,
`user_login` VARCHAR(50) NOT NULL,
`user_password` VARCHAR(50) NOT NULL,
PRIMARY KEY (`user_id`, `user_type`),
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_login` (`user_login`),
CONSTRAINT `FK1_user_resource` FOREIGN KEY (`user_id`, `user_type`) REFERENCES `resource` (`resource_id`, `resource_type`)
CONSTRAINT `FK1_user_resource` FOREIGN KEY (`user_id`) REFERENCES `resource` (`resource_id`)
)
ENGINE =InnoDB
DEFAULT CHARSET =utf8;
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/struts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@
/view/administration/administration.jsp
</result>
</action>

<action name="ImageLoader" class="net.feminaexlux.gallery.struts2.controller.ImageLoader">
<result>
/view/gallery/image.jsp
</result>
</action>
</package>
</struts>
18 changes: 12 additions & 6 deletions web/view/administration/administration.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
<body>
<h1>Administration</h1>

<s:iterator value="images" var="image">
<li>
${image.album.name}: ${image.name}
</li>
</s:iterator>
<ul>
<s:iterator value="images" var="image_container">
<s:url action="ImageLoader" method="image" var="image_data">
<s:param name="imageId">
${image_container.id}
</s:param>
</s:url>
<li>${image_container.name}</li>
<li><img src="${image_data}"></li>
</s:iterator>
</ul>

<form action="<s:url action="Administration" method="saveAlbum" />" method="post">
<label>Name: <input type="text" name="form.album_name"></label>
Expand All @@ -28,7 +34,7 @@
</option>
</s:iterator>
</select>
<textarea name="description"></textarea>
<label>Image description: <textarea name="form.image_description"></textarea></label>
<input type="file" name="upload">
<input type="submit" value="Upload Image">
</form>
Expand Down
15 changes: 5 additions & 10 deletions web/view/gallery/gallery.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Gallery - ${album.slug}</title>
<title>Gallery</title>
</head>
<body>
<h1>${album.type} (${album.id}): ${album.name}</h1>
${album.description}
<s:iterator value="albums" var="album">
<h1>${album.type} (${album.id}): ${album.name}</h1>

<form action="<s:url action="Gallery" method="save" />" method="POST">
<input type="submit" value="Save"/>
</form>

<form action="<s:url action="Gallery" method="revert" />" method="POST">
<input type="submit" value="Revert"/>
</form>
<p>${album.description}</p>
</s:iterator>

<a href="<s:url action="Login" />">Login</a>
</body>
Expand Down
10 changes: 10 additions & 0 deletions web/view/gallery/image.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Gallery</title>
</head>
<body>
</body>
</html>

0 comments on commit 65b5066

Please sign in to comment.