Skip to content

Commit

Permalink
#213 refactor: move some models in the correct package, move methods …
Browse files Browse the repository at this point in the history
…in correct util class
  • Loading branch information
syjer committed Oct 28, 2016
1 parent 105e059 commit 4f4c4e2
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/main/java/alfio/controller/ReservationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import alfio.controller.support.SessionUtil;
import alfio.controller.support.TicketDecorator;
import alfio.manager.*;
import alfio.manager.support.OrderSummary;
import alfio.model.OrderSummary;
import alfio.manager.support.PaymentResult;
import alfio.manager.system.ConfigurationManager;
import alfio.model.*;
Expand Down Expand Up @@ -393,7 +393,7 @@ public String handleReservation(@PathVariable("eventName") String eventName,
if (!ticketReservation.get().getValidity().after(new Date())) {
bindingResult.reject(ErrorsCode.STEP_2_ORDER_EXPIRED);
}
final TicketReservationManager.TotalPrice reservationCost = ticketReservationManager.totalReservationCostWithVAT(reservationId);
final TotalPrice reservationCost = ticketReservationManager.totalReservationCostWithVAT(reservationId);
if(!paymentForm.isPostponeAssignment() && !ticketRepository.checkTicketUUIDs(reservationId, paymentForm.getTickets().keySet())) {
bindingResult.reject(ErrorsCode.STEP_2_MISSING_ATTENDEE_DATA);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import alfio.manager.PaymentManager;
import alfio.manager.TicketReservationManager;
import alfio.manager.i18n.I18nManager;
import alfio.manager.support.OrderSummary;
import alfio.model.OrderSummary;
import alfio.manager.user.UserManager;
import alfio.model.*;
import alfio.model.modification.*;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/alfio/controller/form/PaymentForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package alfio.controller.form;

import alfio.manager.PaypalManager;
import alfio.manager.TicketReservationManager;
import alfio.model.CustomerName;
import alfio.model.Event;
import alfio.model.TicketFieldConfiguration;
import alfio.model.TotalPrice;
import alfio.model.result.ValidationResult;
import alfio.model.transaction.PaymentProxy;
import alfio.util.ErrorsCode;
Expand Down Expand Up @@ -76,7 +76,7 @@ public boolean hasPaypalTokens() {
return StringUtils.isNotBlank(paypalPayerID) && StringUtils.isNotBlank(paypalPaymentId);
}

public void validate(BindingResult bindingResult, TicketReservationManager.TotalPrice reservationCost, Event event,
public void validate(BindingResult bindingResult, TotalPrice reservationCost, Event event,
List<TicketFieldConfiguration> fieldConf) {

List<PaymentProxy> allowedPaymentMethods = event.getAllowedPaymentProxies();
Expand Down
38 changes: 3 additions & 35 deletions src/main/java/alfio/controller/support/TemplateProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import com.openhtmltopdf.DOMBuilder;
import com.openhtmltopdf.pdfboxout.PdfBoxRenderer;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.jsoup.Jsoup;
import org.springframework.core.io.ClassPathResource;
Expand Down Expand Up @@ -81,7 +79,7 @@ public static PDFTemplateGenerator buildPDFTicket(Locale language,
FileUploadManager fileUploadManager) {

return () -> {
Optional<ImageData> imageData = extractImageModel(event, fileUploadManager);
Optional<TemplateResource.ImageData> imageData = extractImageModel(event, fileUploadManager);
Map<String, Object> model = TemplateResource.buildModelForTicketPDF(organization, event, ticketReservation, ticketCategory, ticket, imageData);
String page = templateManager.renderTemplate(event, TemplateResource.TICKET_PDF, model, language);
return prepareItextRenderer(page);
Expand All @@ -103,48 +101,18 @@ public static PdfBoxRenderer prepareItextRenderer(String page) {
return renderer;
}

static Optional<ImageData> extractImageModel(Event event, FileUploadManager fileUploadManager) {
static Optional<TemplateResource.ImageData> extractImageModel(Event event, FileUploadManager fileUploadManager) {
if(event.getFileBlobIdIsPresent()) {
return fileUploadManager.findMetadata(event.getFileBlobId()).map((metadata) -> {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fileUploadManager.outputFile(metadata.getId(), baos);
return fillWithImageData(metadata, baos.toByteArray());
return TemplateResource.fillWithImageData(metadata, baos.toByteArray());
});
} else {
return Optional.empty();
}
}

@Getter
@AllArgsConstructor
public static class ImageData {
private final String eventImage;
private final Integer imageWidth;
private final Integer imageHeight;
}

static ImageData fillWithImageData(FileBlobMetadata m, byte[] image) {

Map<String, String> attributes = m.getAttributes();
if (attributes.containsKey(FileUploadManager.ATTR_IMG_WIDTH) && attributes.containsKey(FileUploadManager.ATTR_IMG_HEIGHT)) {
final int width = Integer.parseInt(attributes.get(FileUploadManager.ATTR_IMG_WIDTH));
final int height = Integer.parseInt(attributes.get(FileUploadManager.ATTR_IMG_HEIGHT));
//in the PDF the image can be maximum 300x150
int resizedWidth = width;
int resizedHeight = height;
if (resizedHeight > 150) {
resizedHeight = 150;
resizedWidth = width * resizedHeight / height;
}
if (resizedWidth > 300) {
resizedWidth = 300;
resizedHeight = height * resizedWidth / width;
}
return new ImageData("data:" + m.getContentType() + ";base64," + Base64.getEncoder().encodeToString(image), resizedWidth, resizedHeight);
}
return new ImageData(null, null, null);
}

public static PartialTicketPDFGenerator buildPartialPDFTicket(Locale language,
Event event,
TicketReservation ticketReservation,
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/alfio/manager/FileUploadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
@Log4j2
public class FileUploadManager {

public static final String ATTR_IMG_WIDTH = "width";
public static final String ATTR_IMG_HEIGHT = "height";
private final NamedParameterJdbcTemplate jdbc;
private final FileUploadRepository repository;

Expand Down Expand Up @@ -117,8 +115,8 @@ private Map<String, String> getAttributes(UploadBase64FileModification file) {
try {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(file.getFile()));
Map<String, String> attributes = new HashMap<>();
attributes.put(ATTR_IMG_WIDTH, String.valueOf(image.getWidth()));
attributes.put(ATTR_IMG_HEIGHT, String.valueOf(image.getHeight()));
attributes.put(FileBlobMetadata.ATTR_IMG_WIDTH, String.valueOf(image.getWidth()));
attributes.put(FileBlobMetadata.ATTR_IMG_HEIGHT, String.valueOf(image.getHeight()));
return attributes;
} catch (IOException e) {
log.error("error while processing image: ", e);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/alfio/manager/PaypalManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package alfio.manager;

import alfio.controller.form.UpdateTicketOwnerForm;
import alfio.manager.support.OrderSummary;
import alfio.manager.support.SummaryRow;
import alfio.model.OrderSummary;
import alfio.model.SummaryRow;
import alfio.manager.system.ConfigurationManager;
import alfio.model.CustomerName;
import alfio.model.Event;
Expand Down
13 changes: 2 additions & 11 deletions src/main/java/alfio/manager/TicketReservationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import alfio.repository.user.OrganizationRepository;
import alfio.util.*;
import alfio.util.TemplateManager.TemplateOutput;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
Expand Down Expand Up @@ -125,14 +124,6 @@ public static class InvalidSpecialPriceTokenException extends RuntimeException {

}

@Data
public static class TotalPrice {
private final int priceWithVAT;
private final int VAT;
private final int discount;
private final int discountAppliedCount;
}

@Autowired
public TicketReservationManager(EventRepository eventRepository,
OrganizationRepository organizationRepository,
Expand Down Expand Up @@ -704,8 +695,8 @@ List<SummaryRow> extractSummary(String reservationId, Event event, Locale locale
String formattedSingleAmount = "-" + (promo.getDiscountType() == DiscountType.FIXED_AMOUNT ? formatCents(promo.getDiscountAmount()) : (promo.getDiscountAmount()+"%"));
summary.add(new SummaryRow(formatPromoCode(promo, ticketRepository.findTicketsInReservation(reservationId)),
formattedSingleAmount,
reservationCost.discountAppliedCount,
formatCents(reservationCost.discount), reservationCost.discount, SummaryRow.SummaryType.PROMOTION_CODE));
reservationCost.getDiscountAppliedCount(),
formatCents(reservationCost.getDiscount()), reservationCost.getDiscount(), SummaryRow.SummaryType.PROMOTION_CODE));
});
return summary;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/alfio/model/FileBlobMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
@Getter
public class FileBlobMetadata {

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

private final String id;
private final String name;
private final int contentSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
* 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.support;
package alfio.model;

import alfio.manager.TicketReservationManager;
import lombok.Data;

import java.util.List;

@Data
public class OrderSummary {
private final TicketReservationManager.TotalPrice originalTotalPrice;
private final TotalPrice originalTotalPrice;
private final List<SummaryRow> summary;
private final boolean free;
private final String totalPrice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* 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.support;
package alfio.model;

import lombok.Data;

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/alfio/model/TotalPrice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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 lombok.Data;

@Data
public class TotalPrice {
private final int priceWithVAT;
private final int VAT;
private final int discount;
private final int discountAppliedCount;
}
47 changes: 37 additions & 10 deletions src/main/java/alfio/util/TemplateResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@
* 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.util;

import alfio.controller.support.TemplateProcessor;
import alfio.manager.TicketReservationManager;
import alfio.manager.support.OrderSummary;
import alfio.manager.support.SummaryRow;
import alfio.model.*;
import alfio.model.modification.SendCodeModification;
import alfio.model.transaction.PaymentProxy;
import alfio.model.user.Organization;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.time.ZonedDateTime;
import java.util.*;

import static alfio.util.ImageUtil.createQRCode;


public enum TemplateResource {
GOOGLE_ANALYTICS("/alfio/templates/google-analytics.ms", false, "text/plain", TemplateManager.TemplateOutput.TEXT),

Expand Down Expand Up @@ -141,7 +139,6 @@ public Map<String, Object> prepareSampleModel(Organization organization, Event e
}



public String getSavedName(Locale locale) {
return name() + "_" + locale.getLanguage() + ".ms";
}
Expand Down Expand Up @@ -186,7 +183,7 @@ private static Map<String, Object> prepareSampleDataForConfirmationEmail(Organiz
TicketReservation reservation = sampleTicketReservation();
Optional<String> vat = Optional.of("VAT-NR");
List<Ticket> tickets = Collections.singletonList(sampleTicket());
OrderSummary orderSummary = new OrderSummary(new TicketReservationManager.TotalPrice(1000, 80, 0, 0),
OrderSummary orderSummary = new OrderSummary(new TotalPrice(1000, 80, 0, 0),
Collections.singletonList(new SummaryRow("Ticket", "10.00", 1, "10.00", 1000, SummaryRow.SummaryType.TICKET)), false, "10.00", "0.80", false, false);
String reservationUrl = "http://your-domain.tld/reservation-url/";
String reservationShortId = "597e7e7b";
Expand Down Expand Up @@ -221,7 +218,7 @@ public static Map<String, Object> prepareModelForConfirmationEmail(Organization
ZonedDateTime confirmationTimestamp = Optional.ofNullable(reservation.getConfirmationTimestamp()).orElseGet(ZonedDateTime::now);
model.put("confirmationDate", confirmationTimestamp.withZoneSameInstant(event.getZoneId()));

if(reservation.getValidity() != null) {
if (reservation.getValidity() != null) {
model.put("expirationDate", ZonedDateTime.ofInstant(reservation.getValidity().toInstant(), event.getZoneId()));
}

Expand Down Expand Up @@ -295,8 +292,8 @@ public static Map<String, Object> buildModelForTicketHasBeenCancelled(Organizati
}

// used by TICKET_PDF
public static Map<String, Object> buildModelForTicketPDF(Organization organization, Event event, TicketReservation ticketReservation, TicketCategory ticketCategory, Ticket ticket, Optional<TemplateProcessor.ImageData> imageData) {
String qrCodeText = ticket.ticketCode(event.getPrivateKey());
public static Map<String, Object> buildModelForTicketPDF(Organization organization, Event event, TicketReservation ticketReservation, TicketCategory ticketCategory, Ticket ticket, Optional<ImageData> imageData) {
String qrCodeText = ticket.ticketCode(event.getPrivateKey());
//
Map<String, Object> model = new HashMap<>();
model.put("ticket", ticket);
Expand Down Expand Up @@ -337,4 +334,34 @@ public static Map<String, Object> buildModelForWaitingQueueReservationEmail(Orga
return model;
}


public static ImageData fillWithImageData(FileBlobMetadata m, byte[] image) {

Map<String, String> attributes = m.getAttributes();
if (attributes.containsKey(FileBlobMetadata.ATTR_IMG_WIDTH) && attributes.containsKey(FileBlobMetadata.ATTR_IMG_HEIGHT)) {
final int width = Integer.parseInt(attributes.get(FileBlobMetadata.ATTR_IMG_WIDTH));
final int height = Integer.parseInt(attributes.get(FileBlobMetadata.ATTR_IMG_HEIGHT));
//in the PDF the image can be maximum 300x150
int resizedWidth = width;
int resizedHeight = height;
if (resizedHeight > 150) {
resizedHeight = 150;
resizedWidth = width * resizedHeight / height;
}
if (resizedWidth > 300) {
resizedWidth = 300;
resizedHeight = height * resizedWidth / width;
}
return new ImageData("data:" + m.getContentType() + ";base64," + Base64.getEncoder().encodeToString(image), resizedWidth, resizedHeight);
}
return new ImageData(null, null, null);
}

@Getter
@AllArgsConstructor
public static class ImageData {
private final String eventImage;
private final Integer imageWidth;
private final Integer imageHeight;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public void testCoverityFindingDivisionByZero() {
}
private void assertDimensionsUnder300x150(Pair<String, String> p) {
Map<String, String> parameters = new HashMap<>();
parameters.put(FileUploadManager.ATTR_IMG_WIDTH, p.getLeft());
parameters.put(FileUploadManager.ATTR_IMG_HEIGHT, p.getRight());
parameters.put(FileBlobMetadata.ATTR_IMG_WIDTH, p.getLeft());
parameters.put(FileBlobMetadata.ATTR_IMG_HEIGHT, p.getRight());
FileBlobMetadata metadata = mock(FileBlobMetadata.class);
when(metadata.getAttributes()).thenReturn(parameters);
Event e = mock(Event.class);
Expand Down
Loading

0 comments on commit 4f4c4e2

Please sign in to comment.