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;
}
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);
}
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;
}
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);
}
}
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;
}
Aggregations