Skip to content

Commit

Permalink
Need to work on saving
Browse files Browse the repository at this point in the history
  • Loading branch information
feminaexlux committed Aug 15, 2013
1 parent b052c8c commit f9c2e40
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 94 deletions.
8 changes: 4 additions & 4 deletions gallery-struts2.iml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/${project.build.directory}/classes" />
<excludeFolder url="file://$MODULE_DIR$/${project.build.directory}/test-classes" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:servlet-api:2.5" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet.jsp:jsp-api:2.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.struts:struts2-core:2.3.4.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.struts.xwork:xwork-core:2.3.4.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.1" level="project" />
Expand All @@ -67,8 +67,8 @@
<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: org.freemarker:freemarker:2.3.19" level="project" />
<orderEntry type="library" name="Maven: commons-fileupload:commons-fileupload:1.2.2" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.0.1" 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" />
<orderEntry type="library" name="Maven: org.apache.struts:struts2-spring-plugin:2.3.4.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:3.2.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:3.2.3.RELEASE" level="project" />
Expand Down
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<struts-version>2.3.4.1</struts-version>
<log4j-version>2.0-beta8</log4j-version>
<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>
<spring-version>3.2.3.RELEASE</spring-version>
<mysql-version>5.1.25</mysql-version>
Expand All @@ -20,6 +22,18 @@
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>

<!-- Apache -->

Expand Down Expand Up @@ -48,6 +62,16 @@
<artifactId>commons-codec</artifactId>
<version>${codec-version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${fileupload-version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${io-version}</version>
</dependency>

<!-- Hibernate -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.feminaexlux.gallery.struts2.dao.ResourceDAO;
import net.feminaexlux.gallery.struts2.dao.UserDAO;
import net.feminaexlux.gallery.struts2.service.AlbumService;
import net.feminaexlux.gallery.struts2.service.ImageService;
import net.feminaexlux.gallery.struts2.service.ResourceService;
import net.feminaexlux.gallery.struts2.service.UserService;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -37,6 +38,12 @@ public AlbumService albumService() {
return new AlbumService();
}

@Bean
@Scope("prototype")
public ImageService imageService() {
return new ImageService();
}

@Bean
@Scope("prototype")
public UserService userService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.support.SharedEntityManagerBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.sql.DriverManager;
Expand All @@ -39,7 +37,7 @@ public EntityManagerFactory entityManagerFactory() throws SQLException {
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setShowSql(false);
jpaVendorAdapter.setShowSql(true);
jpaVendorAdapter.setDatabase(Database.MYSQL);
return jpaVendorAdapter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.feminaexlux.gallery.struts2.model.Album;
import net.feminaexlux.gallery.struts2.model.Image;
import net.feminaexlux.gallery.struts2.service.AlbumService;
import net.feminaexlux.gallery.struts2.service.ImageService;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -17,10 +18,13 @@ public class Administration extends Controller {

@Autowired
private AlbumService albumService;
@Autowired
private ImageService imageService;

private int albumId;
private File upload;
private String uploadFileName;
private String uploadContentType;

public String upload() {
if (upload != null && albumId > 0) {
Expand All @@ -33,6 +37,9 @@ public String upload() {
image.setImage(byteArray);
image.setThumbnail(byteArray);
image.setName(uploadFileName);
image.setContentType(uploadContentType);

imageService.save(image);
} catch (IOException ioException) {
LOG.error("Error with upload {}\n{}", uploadFileName, ioException);
}
Expand All @@ -53,7 +60,15 @@ public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

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

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

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;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

public class ImageLoader extends Controller {
private static final Logger LOG = LogManager.getLogger(ImageLoader.class);

@Autowired
private ImageService imageService;

private int imageId;
private String imageType;

public String image() {
if (imageId > 0 && StringUtility.isNotEmpty(imageType)) {
try {
Image image = imageService.getImage(imageId, imageType);
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Content-Disposition", "attachment;filename=" + image.getName());
response.setContentType(image.getContentType());
response.setContentLength(image.getImage().length);

OutputStream outputStream = response.getOutputStream();
outputStream.write(image.getImage());
outputStream.flush();
outputStream.close();
} catch (IOException ioException) {
LOG.error("Error uploading file {}\n{}", imageId, ioException);
}
}

return SUCCESS;
}

public String thumbnail() {
return SUCCESS;
}

public void setImageId(int imageId) {
this.imageId = imageId;
}

public void setImageType(String imageType) {
this.imageType = imageType;
}
}
10 changes: 10 additions & 0 deletions src/main/java/net/feminaexlux/gallery/struts2/model/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Image extends Resource implements Linkable {
private Album album;
private byte[] image;
private byte[] thumbnail;
private String contentType;
private String description;
private String slug;

Expand Down Expand Up @@ -46,6 +47,15 @@ public void setThumbnail(byte[] thumbnail) {
this.thumbnail = thumbnail;
}

@Column(name = "content_type", nullable = false, length = 50)
public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

@Column(nullable = false)
public String getDescription() {
return description;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.feminaexlux.gallery.struts2.service;

import net.feminaexlux.gallery.struts2.model.Image;
import net.feminaexlux.gallery.struts2.model.ResourceType;

import java.util.List;

public class ImageService extends ResourceService {
public Image getImage(int id) {
return super.get(id, ResourceType.IMAGE, Image.class);
}

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

public List<Image> getAll() {
return resourceDAO.findAll(Image.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public static boolean isEmpty(String check) {
return StringUtils.isEmpty(StringUtils.trimToNull(check));
}

public static boolean isNotEmpty(String check) {
return !isEmpty(check);
}

public static String encrypt(String password, User potentialUser) {
if (isEmpty(password) || potentialUser == null) {
throw new InvalidParameterException();
Expand Down
Loading

0 comments on commit f9c2e40

Please sign in to comment.