Search in sources :

Example 1 with Attachment

use of net.parostroj.timetable.model.Attachment in project grafikon by jub77.

the class FileLoadSaveAttachments method getAttachment.

private Attachment getAttachment(Pair<LSAttachment, OutputTemplate> pair, byte[] bytes) {
    AttachmentType type = AttachmentType.valueOf(pair.first.getType());
    Attachment attachment = null;
    if (type == AttachmentType.BINARY) {
        attachment = new Attachment(pair.first.getName(), bytes);
    } else {
        String text = new String(bytes, TEXT_ENCODING);
        attachment = new Attachment(pair.first.getName(), text);
    }
    return attachment;
}
Also used : AttachmentType(net.parostroj.timetable.model.Attachment.AttachmentType) Attachment(net.parostroj.timetable.model.Attachment)

Example 2 with Attachment

use of net.parostroj.timetable.model.Attachment in project grafikon by jub77.

the class FileLoadSaveAttachments method load.

public void load(ZipInputStream zipInput, ZipEntry entry) throws IOException {
    String ref = entry.getName().substring(location.length());
    Pair<LSAttachment, OutputTemplate> pair = templateMap.get(ref);
    byte[] bytes = ByteStreams.toByteArray(zipInput);
    Attachment attachment = getAttachment(pair, bytes);
    pair.second.getAttachments().add(attachment);
}
Also used : OutputTemplate(net.parostroj.timetable.model.OutputTemplate) Attachment(net.parostroj.timetable.model.Attachment)

Example 3 with Attachment

use of net.parostroj.timetable.model.Attachment in project grafikon by jub77.

the class EditAttachmentsDialog method createNew.

@Override
protected Attachment createNew(String name) {
    int result = chooser.showOpenDialog(this);
    Attachment attachment = null;
    if (result == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        try {
            byte[] bytes = Files.toByteArray(file);
            attachment = new Attachment(file.getName(), bytes);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            GuiComponentUtils.showError("Error reading file", this);
        }
    }
    return attachment;
}
Also used : Attachment(net.parostroj.timetable.model.Attachment) IOException(java.io.IOException) File(java.io.File)

Example 4 with Attachment

use of net.parostroj.timetable.model.Attachment in project grafikon by jub77.

the class FileLoadSaveAttachments method save.

public void save(ZipOutputStream zipOutput) throws IOException {
    for (Map.Entry<String, Attachment> entry : attachmentMap.entrySet()) {
        String reference = entry.getKey();
        Attachment attachment = entry.getValue();
        ZipEntry zipEntry = new ZipEntry(location + reference);
        byte[] bytes = this.getBytes(attachment);
        zipEntry.setSize(bytes.length);
        zipOutput.putNextEntry(zipEntry);
        zipOutput.write(bytes);
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) Attachment(net.parostroj.timetable.model.Attachment) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with Attachment

use of net.parostroj.timetable.model.Attachment in project grafikon by jub77.

the class LSOutputTemplate method createOutputTemplate.

public OutputTemplate createOutputTemplate(PartFactory partFactory, Function<String, ObjectWithId> mapping, FileLoadSaveAttachments flsAttachments) throws LSException {
    OutputTemplate outputTemplate = partFactory.createOutputTemplate(id);
    if (name != null) {
        // name mapped to key
        outputTemplate.setKey(name);
    }
    if (this.template != null) {
        outputTemplate.setTemplate(this.template.createTextTemplate());
    }
    if (this.script != null) {
        outputTemplate.setScript(this.script.createScript());
    }
    outputTemplate.getAttributes().add(attributes.createAttributes(mapping));
    // process attachments
    if (attachments != null) {
        for (LSAttachment attachment : attachments) {
            if (attachment.getRef() != null) {
                if (flsAttachments == null) {
                    throw new LSException("Attachment loader cannot be null");
                }
                flsAttachments.addForLoad(attachment, outputTemplate);
            } else {
                // process inline data
                if (attachment.getBinaryData() != null) {
                    outputTemplate.getAttachments().add(new Attachment(attachment.getName(), attachment.getBinaryData()));
                } else if (attachment.getTextData() != null) {
                    outputTemplate.getAttachments().add(new Attachment(attachment.getName(), attachment.getTextData()));
                }
            }
        }
    }
    return outputTemplate;
}
Also used : OutputTemplate(net.parostroj.timetable.model.OutputTemplate) Attachment(net.parostroj.timetable.model.Attachment) LSException(net.parostroj.timetable.model.ls.LSException)

Aggregations

Attachment (net.parostroj.timetable.model.Attachment)5 OutputTemplate (net.parostroj.timetable.model.OutputTemplate)2 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ZipEntry (java.util.zip.ZipEntry)1 AttachmentType (net.parostroj.timetable.model.Attachment.AttachmentType)1 LSException (net.parostroj.timetable.model.ls.LSException)1