| 1 | |
package org.jtheque.films.services.impl; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import net.sf.jasperreports.engine.JRException; |
| 20 | |
import net.sf.jasperreports.engine.JRExporterParameter; |
| 21 | |
import net.sf.jasperreports.engine.JasperCompileManager; |
| 22 | |
import net.sf.jasperreports.engine.JasperExportManager; |
| 23 | |
import net.sf.jasperreports.engine.JasperFillManager; |
| 24 | |
import net.sf.jasperreports.engine.JasperPrint; |
| 25 | |
import net.sf.jasperreports.engine.JasperPrintManager; |
| 26 | |
import net.sf.jasperreports.engine.JasperReport; |
| 27 | |
import net.sf.jasperreports.engine.design.JasperDesign; |
| 28 | |
import net.sf.jasperreports.engine.export.JRPrintServiceExporter; |
| 29 | |
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter; |
| 30 | |
import net.sf.jasperreports.engine.xml.JRXmlLoader; |
| 31 | |
import org.jtheque.core.managers.Managers; |
| 32 | |
import org.jtheque.core.managers.language.ILanguageManager; |
| 33 | |
import org.jtheque.core.managers.log.IJThequeLogger; |
| 34 | |
import org.jtheque.core.managers.log.Logger; |
| 35 | |
import org.jtheque.core.managers.view.able.IViewManager; |
| 36 | |
import org.jtheque.films.persistence.od.able.Film; |
| 37 | |
import org.jtheque.films.services.able.ICoverService; |
| 38 | |
import org.jtheque.films.services.impl.utils.cover.Format; |
| 39 | |
import org.jtheque.films.services.impl.utils.file.exports.datasources.FilmsDatasource; |
| 40 | |
import org.jtheque.films.services.impl.utils.file.jt.FileFilterFactory; |
| 41 | |
import org.jtheque.films.utils.Constants; |
| 42 | |
import org.jtheque.utils.collections.ArrayUtils; |
| 43 | |
|
| 44 | |
import javax.print.attribute.AttributeSet; |
| 45 | |
import javax.print.attribute.HashPrintRequestAttributeSet; |
| 46 | |
import javax.print.attribute.standard.Copies; |
| 47 | |
import javax.print.attribute.standard.JobName; |
| 48 | |
import java.awt.Image; |
| 49 | |
import java.util.HashMap; |
| 50 | |
import java.util.Map; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public final class CoverService implements ICoverService { |
| 58 | |
private final Format[] formats; |
| 59 | |
|
| 60 | |
private Image image; |
| 61 | |
private JasperPrint print; |
| 62 | |
private Film film; |
| 63 | |
private Format format; |
| 64 | |
|
| 65 | |
@Logger |
| 66 | |
private IJThequeLogger logger; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public CoverService() { |
| 72 | 0 | super(); |
| 73 | |
|
| 74 | 0 | formats = new Format[4]; |
| 75 | |
|
| 76 | 0 | formats[0] = new Format("cover.formats.dvd.standard", Constants.Report.FORM002_DVD_STANDARD); |
| 77 | 0 | formats[1] = new Format("cover.formats.dvd.slim", Constants.Report.FORM002_DVD_SLIM); |
| 78 | 0 | formats[2] = new Format("cover.formats.cd.standard", Constants.Report.FORM002_CD_STANDARD); |
| 79 | 0 | formats[3] = new Format("cover.formats.cd.slim", Constants.Report.FORM002_CD_SLIM); |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
@Override |
| 83 | |
public Format[] getFormats() { |
| 84 | 0 | return ArrayUtils.copyOf(formats); |
| 85 | |
} |
| 86 | |
|
| 87 | |
@Override |
| 88 | |
public Image getReportImage(Film film, Format format) { |
| 89 | 0 | if (film != null && (this.film != film || this.format != format)) { |
| 90 | 0 | this.film = film; |
| 91 | 0 | this.format = format; |
| 92 | |
|
| 93 | |
try { |
| 94 | 0 | JasperDesign jasperDesign = JRXmlLoader.load(getClass().getClassLoader().getResourceAsStream("org/jtheque/films/reports/" + format.getForm())); |
| 95 | 0 | JasperReport report = JasperCompileManager.compileReport(jasperDesign); |
| 96 | |
|
| 97 | 0 | Map<String, Object> parameters = new HashMap<String, Object>(3); |
| 98 | 0 | parameters.put("REPORT_REALIZER_HEADER", Managers.getManager(ILanguageManager.class).getMessage("cover.realizer")); |
| 99 | 0 | parameters.put("REPORT_ACTORS_HEADER", Managers.getManager(ILanguageManager.class).getMessage("cover.actors")); |
| 100 | 0 | parameters.put("REPORT_DVD_LOGO", getClass().getClassLoader().getResourceAsStream("org/jtheque/films/images/dvd-logo-h.gif")); |
| 101 | |
|
| 102 | 0 | print = JasperFillManager.fillReport(report, parameters, new FilmsDatasource(film)); |
| 103 | |
|
| 104 | 0 | image = JasperPrintManager.printPageToImage(print, 0, 1.0f); |
| 105 | 0 | } catch (JRException e) { |
| 106 | 0 | logger.error(e); |
| 107 | 0 | } |
| 108 | |
} |
| 109 | |
|
| 110 | 0 | return image; |
| 111 | |
} |
| 112 | |
|
| 113 | |
@Override |
| 114 | |
public void printCurrentReport() { |
| 115 | |
try { |
| 116 | 0 | AttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); |
| 117 | |
|
| 118 | 0 | printRequestAttributeSet.add(new Copies(1)); |
| 119 | 0 | printRequestAttributeSet.add(new JobName("JTheque Print Report Job", null)); |
| 120 | |
|
| 121 | 0 | JRPrintServiceExporter exporter = new JRPrintServiceExporter(); |
| 122 | 0 | exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); |
| 123 | 0 | exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet); |
| 124 | 0 | exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE); |
| 125 | 0 | exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE); |
| 126 | 0 | exporter.exportReport(); |
| 127 | 0 | } catch (JRException e) { |
| 128 | 0 | logger.error(e); |
| 129 | 0 | } |
| 130 | 0 | } |
| 131 | |
|
| 132 | |
@Override |
| 133 | |
public void exportCurrentReportToPDF() { |
| 134 | 0 | String filePath = Managers.getManager(IViewManager.class).chooseFile(FileFilterFactory.getFileFilter(Constants.Files.FileType.PDF)); |
| 135 | |
|
| 136 | |
try { |
| 137 | 0 | JasperExportManager.exportReportToPdfFile(print, filePath); |
| 138 | 0 | } catch (JRException e) { |
| 139 | 0 | logger.error(e); |
| 140 | 0 | } |
| 141 | 0 | } |
| 142 | |
} |