Author: dcosse Date: 2014-06-23 14:27:25 +0200 (Mon, 23 Jun 2014) New Revision: 251 Url: http://forge.codelutin.com/projects/faxtomail/repository/revisions/251 Log: refs #4656 ajout de l'impression des details d'une demande Added: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/odt/ trunk/faxtomail-ui-swing/src/main/resources/pdf/ Modified: trunk/faxtomail-ui-swing/pom.xml trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/AttachmentToPrintChooserUIHandler.java trunk/pom.xml Modified: trunk/faxtomail-ui-swing/pom.xml =================================================================== --- trunk/faxtomail-ui-swing/pom.xml 2014-06-20 16:37:20 UTC (rev 250) +++ trunk/faxtomail-ui-swing/pom.xml 2014-06-23 12:27:25 UTC (rev 251) @@ -282,6 +282,12 @@ <groupId>javax.activation</groupId> <artifactId>activation</artifactId> </dependency> + + <dependency> + <groupId>org.apache.pdfbox</groupId> + <artifactId>pdfbox</artifactId> + </dependency> + </dependencies> <!-- TODO --> Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/AttachmentToPrintChooserUIHandler.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/AttachmentToPrintChooserUIHandler.java 2014-06-20 16:37:20 UTC (rev 250) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/AttachmentToPrintChooserUIHandler.java 2014-06-23 12:27:25 UTC (rev 251) @@ -26,17 +26,33 @@ import com.franciaflex.faxtomail.persistence.entities.Attachment; import com.franciaflex.faxtomail.persistence.entities.AttachmentFile; +import com.franciaflex.faxtomail.persistence.entities.AttachmentFileImpl; import com.franciaflex.faxtomail.ui.swing.content.demande.DemandeUIModel; import com.franciaflex.faxtomail.ui.swing.util.AbstractFaxToMailUIHandler; import com.franciaflex.faxtomail.ui.swing.util.Cancelable; import com.franciaflex.faxtomail.ui.swing.util.FaxToMailUIUtil; +import com.google.common.base.Joiner; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.io.Closeables; import jaxx.runtime.validator.swing.SwingValidator; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.pdfbox.examples.fdf.PrintFields; +import org.apache.pdfbox.examples.fdf.SetField; +import org.apache.pdfbox.pdmodel.PDDocument; import javax.swing.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.Map; import static org.nuiton.i18n.I18n.t; @@ -49,6 +65,11 @@ private static final Log log = LogFactory.getLog(AttachmentToPrintChooserUIHandler.class); + protected Map<String, String> pdfFieldValues; + + protected DateFormat dayFormat = new SimpleDateFormat("dd/MM/yyyy"); + + @Override public void afterInit(AttachmentToPrintChooserUI attachmentToPrintChooserUI) { initUI(attachmentToPrintChooserUI); @@ -56,14 +77,32 @@ AttachmentToPrintChooserUIModel model = getModel(); DemandeUIModel demand = model.getDemand(); - JPanel attachmentPanel = ui.getAttachmentPanel(); JCheckBox checkBox = new JCheckBox(t("faxtomail.chooseMailFolder.element"), false); - attachmentPanel.add(checkBox); + ui.getAttachmentPanel().add(checkBox); + final AttachmentFile demandFile = new AttachmentFileImpl(); + demandFile.setFilename("demand_details"); + byte[] demandContent = getDocumentContent(); + demandFile.setContent(demandContent); + + checkBox.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(ItemEvent e) { + log.info("state changed " + e.getStateChange()); + if (e.getStateChange() == ItemEvent.SELECTED) { + getModel().addAttachmentToPrint(demandFile); + } else { + getModel().removeAttachmentToPrint(demandFile); + } + } + }); + for (Attachment attachment : demand.getAttachment()) { - //TODO kmorin 20140617 check if not null - createCheckBox(attachment.getOriginalFile()); + if (attachment.getOriginalFile() != null) { + createCheckBox(attachment.getOriginalFile()); + } + final AttachmentFile file = attachment.getEditedFile(); if (file != null) { createCheckBox(file); @@ -72,7 +111,8 @@ } protected void createCheckBox(final AttachmentFile attachmentFile) { - JCheckBox checkBox = new JCheckBox(attachmentFile.getFilename(), true); + String attachmentName = attachmentFile != null ? attachmentFile.getFilename() : ""; + JCheckBox checkBox = new JCheckBox(attachmentName, true); ui.getAttachmentPanel().add(checkBox); getModel().addAttachmentToPrint(attachmentFile); @@ -110,7 +150,91 @@ public void print() { for (AttachmentFile attachmentFile : getModel().getAttachmentsToPrint()) { - FaxToMailUIUtil.print(attachmentFile, true); + if (StringUtils.isNotBlank(attachmentFile.getFilename())) { + FaxToMailUIUtil.print(attachmentFile, true); + } } } + + public byte[] getDocumentContent() { + pdfFieldValues = Maps.newHashMap(); + List<InputStream> streams = Lists.newArrayList(); + PDDocument resultDocument; + + byte[] result = null; + + try { + + InputStream emptyPage = getClass().getResourceAsStream("/pdf/demande.pdf"); + streams.add(emptyPage); + resultDocument = PDDocument.load(emptyPage); + SetField fields = new SetField(); + if (log.isTraceEnabled()) { + log.trace("fields in document are:"); + PrintFields printFields = new PrintFields(); + printFields.printFields(resultDocument); + } + + loadDocumentDedails(); + + + if (log.isDebugEnabled()) { + log.debug("will fill form document:\n" + pdfFieldValues); + } + + for (Map.Entry<String, String> field : pdfFieldValues.entrySet()) { + String fieldName = field.getKey(); + String fieldValue = field.getValue(); + fields.setField(resultDocument, fieldName, fieldValue); + } + + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + resultDocument.save(out); + resultDocument.close(); + + result = out.toByteArray(); + + + } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error("", e); + } + } finally { + for (InputStream is : streams) { + try { + Closeables.close(is, false); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("An exception occurred", e); + } + } + } + } + return result; + } + + protected void loadDocumentDedails() { + DemandeUIModel demand = getModel().getDemand(); + setField("demand", demand.getObject()); + setField("receivedDate", dayFormat.format(demand.getReceptionDate())); + setField("sender", demand.getSender()); + setField("object", demand.getObject()); + setField("clientCode", demand.getClientCode()); + setField("demandType", (demand.getDemandType() == null) ? "" : demand.getDemandType().getLabel()); + setField("priority", (demand.getPriority() == null) ? "" : demand.getPriority().getLabel()); + setField("projectReference", demand.getProjectReference()); + setField("companyReference", demand.getCompanyReference()); + setField("etatAttente", (demand.getEtatAttente() == null) ? "" : demand.getEtatAttente().getLabel()); + setField("status", (demand.getDemandStatus() == null) ? "" : demand.getDemandStatus().getLabel()); + setField("takenBy", (demand.getTakenBy() == null) ? "" : (demand.getTakenBy().getLogin() + " " + demand.getTakenBy().getFirstName() + " " + demand.getTakenBy().getLastName())); + } + + protected void setField(String fieldName, String... value) { + if (value == null) { + pdfFieldValues.put(fieldName, ""); + } else { + pdfFieldValues.put(fieldName, Joiner.on(" ").skipNulls().join(value)); + } + } } Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2014-06-20 16:37:20 UTC (rev 250) +++ trunk/pom.xml 2014-06-23 12:27:25 UTC (rev 251) @@ -135,6 +135,7 @@ <maven.compiler.target>1.7</maven.compiler.target> <signatureArtifactId>java17</signatureArtifactId> <signatureVersion>1.0</signatureVersion> + <pdfboxVersion>1.6.0</pdfboxVersion> <!-- Do not upgrade to version 1.7.x or 1.8.x because some classes are missing --> </properties> <repositories> @@ -624,6 +625,12 @@ <artifactId>unboundid-ldapsdk</artifactId> <version>2.3.6</version> </dependency> + + <dependency> + <groupId>org.apache.pdfbox</groupId> + <artifactId>pdfbox</artifactId> + <version>${pdfboxVersion}</version> + </dependency> </dependencies> </dependencyManagement>