Skip to content

Commit

Permalink
#213 add initial part of the business logic
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Oct 16, 2016
1 parent ebe8d21 commit 7ae77f8
Show file tree
Hide file tree
Showing 9 changed files with 570 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main/java/alfio/config/DataSourceConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public MessageSource messageSource() {
}

@Bean
public TemplateManager getTemplateManager(Environment environment) {
return new TemplateManager(environment, getTemplateLoader(), messageSource());
public TemplateManager getTemplateManager() {
return new TemplateManager(getTemplateLoader(), messageSource());
}

@Bean
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/alfio/manager/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,8 @@ public void deleteEvent(int eventId, String username) {
eventDeleterRepository.deleteTicketCategoryText(eventId);
eventDeleterRepository.deleteTicketCategory(eventId);
eventDeleterRepository.deleteEventDescription(eventId);

eventDeleterRepository.deleteResources(eventId);

eventDeleterRepository.deleteEvent(eventId);

Expand Down
224 changes: 224 additions & 0 deletions src/main/java/alfio/manager/UploadedResourceManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This file is part of alf.io.
* <p>
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.manager;

import alfio.model.UploadedResource;
import alfio.model.modification.UploadBase64FileModification;
import alfio.repository.UploadedResourceRepository;
import alfio.util.Json;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StreamUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
@Log4j2
public class UploadedResourceManager {

public static final String ATTR_IMG_WIDTH = "width";
public static final String ATTR_IMG_HEIGHT = "height";

private final NamedParameterJdbcTemplate jdbc;
private final UploadedResourceRepository uploadedResourceRepository;

@Autowired
public UploadedResourceManager(NamedParameterJdbcTemplate jdbc, UploadedResourceRepository uploadedResourceRepository) {
this.jdbc = jdbc;
this.uploadedResourceRepository = uploadedResourceRepository;
}

public boolean hasResource(String name) {
return uploadedResourceRepository.hasResource(name);
}

public boolean hasResource(int organizationId, String name) {
return uploadedResourceRepository.hasResource(organizationId, name);
}

public boolean hasResource(int organizationId, int eventId, String name) {
return uploadedResourceRepository.hasResource(organizationId, eventId, name);
}


public void outputResource(String name, OutputStream out) {
SqlParameterSource param = new MapSqlParameterSource("name", name);
jdbc.query(uploadedResourceRepository.fileContentTemplate(name), param, rs -> {
try (InputStream is = rs.getBinaryStream("content")) {
StreamUtils.copy(is, out);
} catch (IOException e) {
throw new IllegalStateException("Error while copying data", e);
}
});
}

public void outputResource(int organizationId, String name, OutputStream out) {
SqlParameterSource param = new MapSqlParameterSource("name", name).addValue("organizationId", organizationId);
jdbc.query(uploadedResourceRepository.fileContentTemplate(organizationId, name), param, rs -> {
try (InputStream is = rs.getBinaryStream("content")) {
StreamUtils.copy(is, out);
} catch (IOException e) {
throw new IllegalStateException("Error while copying data", e);
}
});
}

public void outputResource(int organizationId, int eventId, String name, OutputStream out) {
SqlParameterSource param = new MapSqlParameterSource("name", name).addValue("organizationId", organizationId).addValue("eventId", eventId);
jdbc.query(uploadedResourceRepository.fileContentTemplate(organizationId, eventId, name), param, rs -> {
try (InputStream is = rs.getBinaryStream("content")) {
StreamUtils.copy(is, out);
} catch (IOException e) {
throw new IllegalStateException("Error while copying data", e);
}
});
}

public int saveResource(UploadBase64FileModification file) {
if (hasResource(file.getName())) {
uploadedResourceRepository.delete(file.getName());
}
LobHandler lobHandler = new DefaultLobHandler();
return jdbc.getJdbcOperations().execute(uploadedResourceRepository.uploadTemplate(file.getName()),
new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
@Override
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
setFileValues(ps, lobCreator, file, 1);
}
});

}

public int saveResource(int organizationId, UploadBase64FileModification file) {
if (hasResource(organizationId, file.getName())) {
uploadedResourceRepository.delete(organizationId, file.getName());
}
LobHandler lobHandler = new DefaultLobHandler();
return jdbc.getJdbcOperations().execute(uploadedResourceRepository.uploadTemplate(organizationId, file.getName()),
new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
@Override
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
ps.setInt(2, organizationId);
setFileValues(ps, lobCreator, file, 2);
}
});
}

public int saveResource(int organizationId, int eventId, UploadBase64FileModification file) {
if (hasResource(organizationId, eventId, file.getName())) {
uploadedResourceRepository.delete(organizationId, eventId, file.getName());
}
LobHandler lobHandler = new DefaultLobHandler();
return jdbc.getJdbcOperations().execute(uploadedResourceRepository.uploadTemplate(organizationId, eventId, file.getName()),
new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
@Override
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
ps.setInt(2, organizationId);
ps.setInt(3, eventId);
setFileValues(ps, lobCreator, file, 3);
}
});

}

private static void setFileValues(PreparedStatement ps, LobCreator lobCreator, UploadBase64FileModification file, int baseIndex) throws SQLException {
ps.setString(1, file.getName());

ps.setLong(baseIndex + 1, file.getFile().length);
lobCreator.setBlobAsBytes(ps, baseIndex + 2, file.getFile());
ps.setString(baseIndex + 3, file.getType());
ps.setString(baseIndex + 4, Json.GSON.toJson(getAttributes(file)));
}

public void deleteResource(String name) {
uploadedResourceRepository.delete(name);
}

public void deleteResource(int organizationId, String name) {
uploadedResourceRepository.delete(organizationId, name);
}

public void deleteResource(int organizationId, int eventId, String name) {
uploadedResourceRepository.delete(organizationId, eventId, name);
}

public List<UploadedResource> findAll() {
return uploadedResourceRepository.findAll();
}

public List<UploadedResource> findAll(int organizationId) {
return uploadedResourceRepository.findAll(organizationId);
}

public List<UploadedResource> findAll(int organizationId, int eventId) {
return uploadedResourceRepository.findAll(organizationId, eventId);
}

private static Map<String, String> getAttributes(UploadBase64FileModification file) {
if (!StringUtils.startsWith(file.getType(), "image/")) {
return file.getAttributes();
}

try {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(file.getFile()));
Map<String, String> attributes = new HashMap<>(file.getAttributes());
attributes.put(ATTR_IMG_WIDTH, String.valueOf(image.getWidth()));
attributes.put(ATTR_IMG_HEIGHT, String.valueOf(image.getHeight()));
return attributes;
} catch (IOException e) {
log.error("error while processing image: ", e);
return file.getAttributes();
}
}
}
56 changes: 56 additions & 0 deletions src/main/java/alfio/model/UploadedResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.model;


import alfio.util.Json;
import ch.digitalfondue.npjt.ConstructorAnnotationRowMapper.Column;
import com.google.gson.reflect.TypeToken;
import lombok.Getter;

import java.util.Collections;
import java.util.Date;
import java.util.Map;

@Getter
public class UploadedResource {

private final String name;
private final Integer organizationId;
private final Integer eventId;
private final String contentType;
private final int contentSize;
private final Date creationTime;
private final Map<String, String> attributes;

public UploadedResource(@Column("name") String name,
@Column("organization_id_fk") Integer organizationId,
@Column("event_id_fk") Integer eventId,
@Column("content_type") String contentType,
@Column("content_size") int contentSize,
@Column("creation_time") Date creationTime,
@Column("attributes") String attributes) {
this.name = name;
this.organizationId = organizationId;
this.eventId = eventId;
this.contentType = contentType;
this.contentSize = contentSize;
this.creationTime = creationTime;
Map<String, String> parsed = Json.GSON.fromJson(attributes, new TypeToken<Map<String, String>>() {}.getType());
this.attributes = parsed != null ? parsed : Collections.emptyMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Data
public class UploadBase64FileModification {

private byte[] file;
private String type;
private String name;
private Map<String, String> attributes;

public InputStream getInputStream() {
return new ByteArrayInputStream(file);
}

public Map<String, String> getAttributes() {
return attributes != null ? attributes : new HashMap<>(0);
}
}
3 changes: 3 additions & 0 deletions src/main/java/alfio/repository/EventDeleterRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,7 @@ public interface EventDeleterRepository {

@Query("delete from event where id = :eventId")
int deleteEvent(@Bind("eventId") int eventId);

@Query("delete from resource_event where event_id_fk = :eventId")
int deleteResources(@Bind("eventId") int eventId);
}

0 comments on commit 7ae77f8

Please sign in to comment.