Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Retrieve an IContentManager

The IPortalContext has an IContentManagerFactory which can be used to create an IContentManager. To retrieve an IContentManager based on a connection, see the following code snippet for an example:

...

Which connectionName to use depends on your project's configuration.

Creating new content

The following code snippet shows how to create new content based on a given string.

Code Block
languagejava
InputStream stream = new ByteArrayInputStream(contentString.getBytes("UTF-8"));
IUserData userData = context.getSessionScope().getActiveUser();
CustomContentProperties properties = new CustomContentProperties();
properties.setProperty("customProperty", new StringValue("value"));
String contentName = "content name";
String contentType = "text/plain";
Long processId = null;
String caseId = null;

if(contentManager instanceof IChannelContentManager channelContentManager) {
  // Using the Java NIO-based API introduced in Blueriq 17.4
  GUID contentId = contentManagerchannelContentManager.create(Channels.newChannel(stream), contentName, contentType, processId, caseId, properties, context.getSessionScope().getActiveUser()); userData);
} else {
  // Using the stream-based API that was deprecated in Blueriq 17.4 and might be released in a future release
  GUID contentId = contentManager.create(stream, contentName, contentType, processId, caseId, properties, userData); 
}

The properties object may be used to store additional properties with the content. The contentId will be required to read, update or delete this content at a later time.

Read metadata

To retrieve file metadata, including custom properties, you may use the readMetadata method, for example:

Code Block
languagejava
IContentMetadata metadata = contentManager.readMetadata(contentId, context.getSessionScope().getActiveUser(), true);
IValue propertyValue = metadata.getProperties().getProperty("customProperty");

Read content

The following code snippet shows how to retrieve the string that was created in the previous example.

Code Block
languagejava
InputStream streamIUserData userData = context.getSessionScope().getActiveUser();
int size = (int) contentManager.readDatareadMetadata(contentId, context.getSessionScope().getActiveUser(), false).getSize();
String contentString = new String(StreamUtil.getBytes(stream), "UTF-8");

To retrieve the custom property you may use the readMetadata method, for example:

Code Block
languagejava
IContentMetadata metadata;

if(contentManager instanceof IChannelContentManager channelContentManager) {
  // Using the Java NIO-based API introduced in Blueriq 17.4
  ByteBuffer content = ByteBuffer.allocate(size);

  // Be sure to close the channel after use to prevent files from being locked
  try (ReadableByteChannel channel = contentManager.readMetadatagetReadableByteChannel(contentId, context.getSessionScope().getActiveUser());
StringValue propertyValue = metadata.getProperties().getProperty("customProperty"); userData)) {
    channel.read(content);
  }
  contentString = new String(content.array(), StandardCharsets.UTF_8);
} else {
  // Using the stream-based API that was deprecated in Blueriq 17.4 and might be released in a future release 
  byte[] content = new byte[size];
  contentManager.readBytes(contentId, 0, content, 0, size, userData);
  contentString = new String(content, StandardCharsets.UTF_8); 
}


Update existing content

It is possible to update only the data, only properties or both. The following code snippet shows how to update all at once, overloads exists which do not require all arguments:

Code Block
languagejava
InputStream stream = new ByteArrayInputStream(newContentString.getBytes("UTF-8"));
IUserData userData = context.getSessionScope().getActiveUser();
CustomContentProperties properties = new CustomContentProperties();
properties.setProperty("customProperty", new StringValue("newValue"));
String contentName = "new content name";
String contentType = "text/plain";
Long processId = null;
String caseId = null;

if(contentManager instanceof IChannelContentManager channelContentManager) {
  // Using the Java NIO-based API introduced in Blueriq 17.4
  contentManager.update(contentId, Channels.newChannel(stream), contentName, contentType, processId, caseId, properties, context.getSessionScope().getActiveUser()); userData);
} else {
 // Using the stream-based API that was deprecated in Blueriq 17.4 and might be released in a future release  
 contentManager.update(contentId, stream, contentName, contentType, processId, caseId, properties, userData);
}

Delete content

The following example shows how to delete the content made in the previous example:

Code Block
languagejava
 contentManager.delete(contentId);

Move content

The following example shows how to move content between content managers:

Code Block
languagejava
// Get the active user data
IUserData userData = context.getSessionScope().getActiveUser();

// Pre-emptive check if the both content managers are the of the same implemation so we can check if can move the files.
// This is should also be done in the move function of the IContentManager.

if (sourceContentManager.getImplementation().getClass().equals(targetImplementation.getImplementation().getClass())) {
   // Both content managers require the same implementation to perform a direct move.
   GUID newContentId = sourceContentManager.move(targetContentManager, contentId, userData);
}