| Back | Main view
Content management using IMiS/Storage Connector JAVA
Product: | IMiS/Storage Connector JAVA |
Release: | 10.2.2110 |
Date: | 09/02/2022 |
Case:
Content management using IMiS/Storage Connector JAVA
Description:
This example demonstrates operations which reads, creates, updates and deletes entity's content on the IMiS/ARChive Server.
Below is a description of operations for the content management.
1. Read content part
Operation reads content part with specified identifier.
JAVA
IArchive archive = IMIS_ARCHIVE;
String id= "<entity-identifier>";
String contentPartId = "<content-part-identifier>";
IDocument document = null;
try {
// Open document in read mode
document = archive.openDocument(EntityIdKind.INTERNAL, id, null, AccessMode.READ);
// Find content part with identifier
IContent content = document.getContent();
for (IContentPart contentPart : content.getParts()) {
if (contentPart.getId().equals(contentPartId)) {
// Read content part and print content
InputStream inputStream = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
inputStream = contentPart.openDataInputStream();
byte[] buffer = new byte[4096];
int n;
while (-1 < (n = inputStream.read(buffer, 0, buffer.length)))
outputStream.write(buffer, 0, n);
byte[] bytes = outputStream.toByteArray();
System.out.println(new String(bytes, "UTF-8"));
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
finally {
if (null != inputStream) {
try {
inputStream.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
finally {
// Close document
if (null != document) {
try {
document.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
Result
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tempus dapibus efficitur.
2. Create content part
Operation creates new content part in document and adds it to the end of the content part list.
JAVA
IArchive archive = IMIS_ARCHIVE;
String id= "<entity-identifier>";
String contentType = "text/plain";
String description = "lorem.txt";
InputStream inputStream = <input stream of the new content part>;
IDocument document = null;
try {
// Open document in read-write mode
document = mArchive.openDocument(EntityIdKind.INTERNAL, id, null, AccessMode.READ_WRITE);
// Create content part
IContentPart contentPart = document.createContentPart(contentType);
// Set content part description
contentPart.setDescription(description);
OutputStream outputStream = null;
try {
// Open content part output stream and write input stream
outputStream = contentPart.openDataOutputStream(false);
byte[] buffer = new byte[4096];
int n;
while (-1 != (n = inputStream.read(buffer)))
outputStream.write(buffer, 0, n);
outputStream.flush();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
finally {
if (null != outputStream) {
try {
outputStream.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
// Add content part to the end of content parts list
IContent content = document.getContent();
IReadOnlyList<IContentPart> contentParts = content.getParts();
List<IContentPart> newContentParts = new ArrayList<IContentPart>();
for (IContentPart part : contentParts)
newContentParts.add(part);
newContentParts.add(contentPart);
content.setParts(newContentParts);
// Save document
document.save();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
finally {
// Close document
if (null != document) {
try {
document.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
3. Update content part
Operation updates content part in document with specified content part identifier.
JAVA
IArchive archive = IMIS_ARCHIVE;
String id= "<entity-identifier>";
String contentPartId= "<content part identifier>";
String description = "lorem2.txt";
InputStream inputStream = <input stream of the new content part>;
IDocument document = null;
try {
// Open document in read-write mode
document = archive.openDocument(EntityIdKind.INTERNAL, id, null, AccessMode.READ_WRITE);
// Find content part to update
IContentPart contentPart = null;
IContent content = document.getContent();
List<IContentPart> newContentParts = new ArrayList<IContentPart>();
for (IContentPart part : content.getParts()) {
if (part.getId().equals(contentPartId))
contentPart = part;
newContentParts.add(part);
}
if (null == contentPart) {
System.out.println("Content part not found");
return;
}
// Set content part new description
contentPart.setDescription(description);
// Overwrite content part content
OutputStream outputStream = null;
try {
// Open content part output stream and write input stream
outputStream = contentPart.openDataOutputStream(false);
byte[] buffer = new byte[4096];
int n;
while (-1 != (n = inputStream.read(buffer)))
outputStream.write(buffer, 0, n);
outputStream.flush();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
finally {
if (null != outputStream) {
try {
outputStream.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
// Set content parts list
content.setParts(newContentParts);
// Save document
document.save();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
finally {
// Close document
if (null != document) {
try {
document.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
4. Delete content part
Operation deletes content part in document with specified content part identifier.
JAVA
IArchive archive = IMIS_ARCHIVE;
String id= "<entity-identifier>";
String contentPartId = "<content part identifier>";
IDocument document = null;
try {
// Open document in read-write mode
document = archive.openDocument(EntityIdKind.INTERNAL, id, null, AccessMode.READ_WRITE);
IContent content = document.getContent();
List<IContentPart> newContentParts = new ArrayList<IContentPart>();
// Add parts to list and skip part which will be deleted
for (IContentPart part : content.getParts()) {
if (!part.getId().equals(contentPartId))
newContentParts.add(part);
}
// Set content parts list
content.setParts(newContentParts);
// Save document
document.save();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
finally {
// Close document
if (null != document) {
try {
document.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
Related Documents:
| Back | Main view