Bugfixes for Exporters, Exporter for Note assets

git-svn-id: https://svn.libreccm.org/ccm/trunk@5731 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2018-11-08 19:01:55 +00:00
parent 45f4bca4ab
commit 256fcdba3d
10 changed files with 411 additions and 34 deletions

View File

@ -1,44 +1,169 @@
package org.librecms.assets;
import com.arsdigita.cms.ContentItem;
import com.arsdigita.cms.FileAsset;
import com.arsdigita.cms.contentassets.FileAttachment;
import com.arsdigita.kernel.KernelConfig;
import com.arsdigita.persistence.DataCollection;
import com.arsdigita.persistence.DataObject;
import com.arsdigita.util.UncheckedWrapperException;
import com.arsdigita.web.WebConfig;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.librecms.contentsection.AbstractBinaryAssetsExporter;
import org.libreccm.export.AbstractDomainObjectsExporter;
import org.libreccm.export.IdSequence;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class FileAssetsExporter
extends AbstractBinaryAssetsExporter<FileAttachment> {
extends AbstractDomainObjectsExporter<ContentItem> {
@Override
protected void exportBinaryAssetProperties(
final FileAttachment asset,
final JsonGenerator jsonGenerator)
throws IOException {
// Nothing
}
@Override
public Class<FileAttachment> exportsType() {
return FileAttachment.class;
public Class<ContentItem> exportsType() {
return ContentItem.class;
}
@Override
public String exportsBaseDataObjectType() {
return FileAttachment.BASE_DATA_OBJECT_TYPE;
return ContentItem.BASE_DATA_OBJECT_TYPE;
}
@Override
public String convertsToType() {
return "org.librecms.assets.FileAsset";
}
@Override
protected boolean includeSubTypes() {
return true;
}
@Override
protected List<String> exportDomainObject(
final ContentItem item,
final Path targetDir) {
final DataCollection fileAttachments = FileAttachment
.getAttachments(item);
final List<String> uuids = new ArrayList<>();
while (fileAttachments.next()) {
final String uuid = exportFileAsset(
fileAttachments.getDataObject(),
targetDir);
uuids.add(uuid);
}
return uuids;
}
private String exportFileAsset(final DataObject dataObj,
final Path targetDir) {
final FileAttachment fileAttachment = new FileAttachment(dataObj);
final String uuid = generateFileAssetUuid(fileAttachment);
final Path targetFilePath = generateTargetFilePath(targetDir, uuid);
final File targetFile = targetFilePath.toFile();
final JsonFactory jsonFactory = new JsonFactory();
try (JsonGenerator jsonGenerator = jsonFactory
.createGenerator(targetFile, JsonEncoding.UTF8)) {
jsonGenerator.writeStartObject();
jsonGenerator.writeNumberField("objectId",
IdSequence.getInstance().nextId());
jsonGenerator.writeStringField("uuid", uuid);
jsonGenerator.writeStringField("displayName",
fileAttachment.getDisplayName());
jsonGenerator.writeObjectFieldStart("description");
jsonGenerator.writeStringField(
KernelConfig.getConfig().getDefaultLanguage(),
fileAttachment.getDescription());
jsonGenerator.writeEndObject();
jsonGenerator.writeStringField(
"mimeType",
fileAttachment.getMimeType().toString());
jsonGenerator.writeStringField("fileName",
fileAttachment.getName());
jsonGenerator.writeObjectFieldStart("title");
jsonGenerator.writeStringField(
KernelConfig.getConfig().getDefaultLanguage(),
fileAttachment.getName());
jsonGenerator.writeEndObject();
jsonGenerator.writeBinaryField("data", fileAttachment.getContent());
jsonGenerator.writeNumberField("size", fileAttachment.getSize());
jsonGenerator.writeEndObject();
} catch (IOException ex) {
throw new UncheckedWrapperException(ex);
}
return uuid;
}
protected static String generateFileAssetUuid(
final FileAttachment fileAttachment) {
final byte[] uuidSource = String.format(
"%s/files/%s",
WebConfig.getInstanceOf().getSiteName(),
fileAttachment.getOID().toString())
.getBytes(StandardCharsets.UTF_8);
final String uuid = UUID.nameUUIDFromBytes(uuidSource).toString();
return uuid;
}
// extends AbstractBinaryAssetsExporter<FileAttachment> {
//
// @Override
// protected void exportBinaryAssetProperties(
// final FileAttachment asset,
// final JsonGenerator jsonGenerator)
// throws IOException {
//
// // Nothing
// }
//
// @Override
// public Class<FileAttachment> exportsType() {
//
// return FileAttachment.class;
// }
//
// @Override
// public String exportsBaseDataObjectType() {
//
// return FileAttachment.BASE_DATA_OBJECT_TYPE;
// }
//
// @Override
// public String convertsToType() {
//
// return "org.librecms.assets.FileAsset";
// }
}

View File

@ -65,28 +65,32 @@ public class FileAttachmentsExporter
while (fileAttachments.next()) {
sortKey++;
final String uuid = exportFileAttachment(fileAttachments
.getDataObject(),
listUuid,
sortKey,
targetDir);
final String uuid = exportFileAttachment(
item,
fileAttachments.getDataObject(),
listUuid,
sortKey,
targetDir);
attachmentUuids.add(uuid);
}
return attachmentUuids;
}
private String exportFileAttachment(final DataObject dataObj,
private String exportFileAttachment(final ContentItem item,
final DataObject dataObj,
final String listUuid,
final long sortKey,
final Path targetDir) {
final FileAttachment fileAttachment = new FileAttachment(dataObj);
final String fileAssetUuid = generateUuid(fileAttachment);
final String fileAssetUuid = FileAssetsExporter
.generateFileAssetUuid(fileAttachment);
final byte[] uuidSource = String.format(
"%s/files/%s",
"%s/%s/files/%s",
WebConfig.getInstanceOf().getSiteName(),
item.getOID().toString(),
fileAttachment.getOID().toString())
.getBytes(StandardCharsets.UTF_8);
final String uuid = UUID.nameUUIDFromBytes(uuidSource).toString();

View File

@ -30,6 +30,7 @@ import com.arsdigita.persistence.DataObject;
import com.arsdigita.runtime.DomainInitEvent;
import org.libreccm.export.ExportManager;
import org.librecms.assets.ImageAttachmentListsExporter;
import org.librecms.assets.ImageAttachmentsExporter;
/**
@ -79,6 +80,9 @@ public class ItemImageAttachmentInitializer extends ContentAssetInitializer {
ExportManager
.getInstance()
.registerExporter(new ImageAttachmentsExporter());
ExportManager
.getInstance()
.registerExporter(new ImageAttachmentListsExporter());
}

View File

@ -0,0 +1,33 @@
package org.librecms.assets;
import com.arsdigita.cms.ContentItem;
import com.arsdigita.cms.contentassets.ItemImageAttachment;
import com.arsdigita.persistence.DataCollection;
import org.librecms.contentsection.AbstractAttachmentListsExporter;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class ImageAttachmentListsExporter
extends AbstractAttachmentListsExporter {
@Override
protected String getListName() {
return "images";
}
@Override
protected boolean hasList(final ContentItem contentItem) {
final DataCollection images = ItemImageAttachment
.getImageAttachments(contentItem);
final boolean result = images.next();
images.close();
return result;
}
}

View File

@ -70,11 +70,11 @@ public class ImageAttachmentsExporter
while (imageAttachments.next()) {
sortKey++;
final String uuid = exportImageAttachment(imageAttachments
.getDataObject(),
listUuid,
sortKey,
targetDir);
final String uuid = exportImageAttachment(
imageAttachments.getDataObject(),
listUuid,
sortKey,
targetDir);
attachmentsUuids.add(uuid);
}

View File

@ -0,0 +1,32 @@
package org.librecms.assets;
import com.arsdigita.cms.ContentItem;
import com.arsdigita.cms.contentassets.Note;
import com.arsdigita.persistence.DataCollection;
import org.librecms.contentsection.AbstractAttachmentListsExporter;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class SideNoteAttachmentListsExporter
extends AbstractAttachmentListsExporter {
@Override
protected String getListName() {
return "notes";
}
@Override
protected boolean hasList(final ContentItem item) {
final DataCollection attachments = Note.getNotes(item);
final boolean result = attachments.next();
attachments.close();
return result;
}
}

View File

@ -0,0 +1,122 @@
package org.librecms.assets;
import com.arsdigita.cms.ContentItem;
import com.arsdigita.cms.contentassets.Note;
import com.arsdigita.persistence.DataCollection;
import com.arsdigita.persistence.DataObject;
import com.arsdigita.util.UncheckedWrapperException;
import com.arsdigita.web.WebConfig;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.libreccm.export.AbstractDomainObjectsExporter;
import org.libreccm.export.IdSequence;
import org.librecms.contentsection.AbstractAttachmentListsExporter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class SideNoteAttachmentsExporter
extends AbstractDomainObjectsExporter<ContentItem>{
@Override
public Class<ContentItem> exportsType() {
return ContentItem.class;
}
@Override
public String exportsBaseDataObjectType() {
return ContentItem.BASE_DATA_OBJECT_TYPE;
}
@Override
public String convertsToType() {
return "org.librecms.contentsection.ItemAttachment";
}
@Override
protected boolean includeSubTypes() {
return true;
}
@Override
protected List<String> exportDomainObject(final ContentItem item,
final Path targetDir) {
final String listUuid = AbstractAttachmentListsExporter
.generateListUuid(item, "notes");
final List<String> attachmentsUuids = new ArrayList<>();
final DataCollection noteAttachments = Note.getNotes(item);
long sortKey = 0;
while(noteAttachments.next()) {
sortKey++;
final String uuid = exportNoteAttachment(
noteAttachments.getDataObject(),
listUuid,
sortKey,
targetDir);
}
return attachmentsUuids;
}
private String exportNoteAttachment(final DataObject dataObj,
final String listUuid,
final long sortKey,
final Path targetDir) {
final Note note = new Note(dataObj);
final String noteUuid = generateUuid(note);
final byte[] uuidSource = String.format(
"%s/notes/%s",
WebConfig.getInstanceOf().getSiteName(),
note.getOID().toString())
.getBytes(StandardCharsets.UTF_8);
final String uuid = UUID.nameUUIDFromBytes(uuidSource).toString();
final Path targetFilePath = generateTargetFilePath(targetDir, uuid);
final File targetFile = targetFilePath.toFile();
final JsonFactory jsonFactory = new JsonFactory();
try (JsonGenerator jsonGenerator = jsonFactory
.createGenerator(targetFile, JsonEncoding.UTF8)) {
jsonGenerator.writeStartObject();
jsonGenerator.writeNumberField("attachmentId",
IdSequence.getInstance().nextId());
jsonGenerator.writeStringField("uuid", uuid);
jsonGenerator.writeStringField("attachmentList", listUuid);
jsonGenerator.writeStringField("asset", noteUuid);
jsonGenerator.writeNumberField("sortKey", sortKey);
jsonGenerator.writeEndObject();
} catch(IOException ex) {
throw new UncheckedWrapperException(ex);
}
return noteUuid;
}
}

View File

@ -0,0 +1,56 @@
package org.librecms.assets;
import com.arsdigita.cms.contentassets.Note;
import com.arsdigita.util.UncheckedWrapperException;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.libreccm.core.AbstractCcmObjectsExporter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class SideNotesExporter
extends AbstractCcmObjectsExporter<Note> {
@Override
public Class<Note> exportsType() {
return Note.class;
}
@Override
public String exportsBaseDataObjectType() {
return Note.BASE_DATA_OBJECT_TYPE;
}
@Override
public String convertsToType() {
return "org.librecms.assets.SideNote";
}
@Override
protected void exportObjectProperties(final Note note,
final JsonGenerator jsonGenerator)
throws IOException {
jsonGenerator.writeObjectFieldStart("title");
jsonGenerator.writeStringField(
note.getOwner().getLanguage(),
note.getDisplayName());
jsonGenerator.writeEndObject();
jsonGenerator.writeObjectFieldStart("text");
jsonGenerator.writeStringField(
note.getOwner().getLanguage(),
note.getContent());
jsonGenerator.writeEndObject();
}
}

View File

@ -11,6 +11,7 @@ import java.io.IOException;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
* @param <T>
*/
public abstract class AbstractBinaryAssetsExporter<T extends BinaryAsset>
extends AbstractAssetsExporter<T>{

View File

@ -36,7 +36,7 @@ public class PhasesExporter extends AbstractDomainObjectsExporter<Phase> {
@Override
public String convertsToType() {
return "org.librcms.lifecycle.Phase";
return "org.librecms.lifecycle.Phase";
}
@Override