Friday, May 7, 2010

Bulk PDF or file Upload with IBM WCM

Hi All

Now I know there's been a lot of request for a bulk up-loader using the API for WCM.
So you as the old saying goes ...ask and you shall receive.

String libraryName = null;
Workspace workspace = null;
DocumentId authTempId = null;
DocumentId workflowId = null;
String destinationSitearea = null;

void uploadFiles(JspWriter out) {
File baseDir = new File(destFolder);
try {
if (!baseDir.exists()) {
System.out.println("Bulk uploader: path does not exist - " + destFolder);
return;
}
File[] buDirs = baseDir.listFiles();
for (int i = 0; i <>
if (buDirs[i].isFile()) {
continue;
}
// you need to get the sitearea destination first
File textFile = new File(buDirs[i], "sitearea.txt");
if (!textFile.exists()) {
continue; // textFile contains destination sitearea
}
destinationSitearea = getDestinationSitearea(textFile);
if (destinationSitearea == null) {
continue; // nonsensical destination area retrieved
}

File[] files = buDirs[i].listFiles();
for (int m = 0; m <>
if (files[m].isDirectory()) {
// files[m].delete();
continue;
}
boolean hasUpdated = false;
if (files[m].getName().toLowerCase().endsWith(".pdf")) {
hasUpdated = updateWCMPage(files[m], out);
}
if (hasUpdated) {
out.println("File uploaded: " + files[m].getAbsolutePath() + "
");
files[m].delete();
} else {
if (files[m].getName().toLowerCase().endsWith(".pdf")) {
out.println("Failed to update file '" + files[m] + "' into wcm
");
}
}
}
}

} catch (SecurityException se) {
} catch (IOException ioe) {
}
}

boolean updateWCMPage(File file, JspWriter out) {
boolean hasUpdated = false;
try {
try {
if (workspace == null) {
workspace = WCM_API.getRepository().getWorkspace(wcmUserName, wcmUserPassword);
}
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary(libraryName));
DocumentIdIterator contentIterator = workspace.findContentByPath(destinationSitearea + file.getName());

if (contentIterator.hasNext()) {
DocumentId contentId = (DocumentId) contentIterator.next();
Content content = (Content) workspace.getById(contentId);

TextComponent txtName = (TextComponent) content.getComponent("Name");
txtName.setText(file.getName());
FileComponent fileComponent = (FileComponent) content.getComponent("File");

FileInputStream fin = new FileInputStream(file);
byte[] fileContent = new byte[(int) file.length()];
fin.read(fileContent);

if (!workspace.isLocked(contentId)) {
fileComponent.setFile(file.getName(), fileContent);
content.setComponent("File", fileComponent);
content.setComponent("Name", txtName);

String[] errors = workspace.save(content);
for (int j = 0; j <>
out.println("Error saving page" + errors[j]);
}
if (errors.length == 0) {
hasUpdated = true;
out.println("Content page '" + destinationSitearea + file.getName() + "' has been updated
");
} else {
StringBuffer buffer = new StringBuffer();
for (int k = 0; k <>
buffer.append(errors[k]);
}
out.println("Errors updating content page '" + destinationSitearea + file.getName() + "' " + buffer.toString() + "
");
}
} else {
out.println("Contet page '" + file.getName() + "' is locked currently locked by another user.
");
}

} else { // page does not exist
DocumentIdIterator siteareaIterator = workspace.findContentByPath(destinationSitearea);
DocumentId defContentId = null;
DocumentId siteareaId = null;
boolean validPath = false;
if (siteareaIterator.hasNext()) {
defContentId = siteareaIterator.nextId();
Content defContent = (Content) workspace.getById(defContentId);
defContent.getDirectParent(); // used to get sitearea
siteareaId = defContent.getDirectParent();
validPath = true;
}

if (validPath) {
setAuthoringTemplateId(out);
setWorkflowId(out);
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary(libraryName));
Content addedPage = workspace.createContent(authTempId, siteareaId, null, ChildPosition.END);
addedPage.setWorkflowId(workflowId);
System.out.println("trying to process '" + file.getName() + "'");
String pageName = file.getName();
pageName = pageName.replaceAll("[^a-zA-Z._-]", "");
addedPage.setName(pageName);
if (!addedPage.isPublished()) {
if (addedPage.isDraft()) {
out.println("Content page '" + destinationSitearea + pageName + "' is in the draft state
");
TextComponent txtName = (TextComponent) addedPage.getComponent("Name");
txtName.setText(file.getName());
FileComponent fileComponent = (FileComponent) addedPage.getComponent("File");

FileInputStream fin = new FileInputStream(file);
byte[] fileContent = new byte[(int) file.length()];
fin.read(fileContent);

fileComponent.setFile(file.getName(), fileContent);
addedPage.setComponent("File", fileComponent);
addedPage.setComponent("Name", txtName);
addedPage.addHistoryLogEntry("New page created: " + file.getName());

String[] errors = workspace.save(addedPage);
for (int j = 0; j <>
System.out.println(errors[j]);
}
addedPage.nextWorkflowStage();
out.println("Content page '" + destinationSitearea + pageName + "' moved to the approved state
");
addedPage.nextWorkflowStage();
out.println("Content page '" + destinationSitearea + pageName + "' moved to the published state
");
hasUpdated = true;
}
}
} else {
out.println("Error: Please ensure your sitearea '" + destinationSitearea + "' has its default content set");
}
}

} catch (OperationFailedException ofe) {
out.println("Error: Operation failed: " + ofe.getMessage() + "
");
} catch (ServiceNotAvailableException snae) {
out.println("Error: Service not available: " + snae.getMessage() + "
");
} catch (DocumentRetrievalException dre) {
out.println("Error: Unable to retrieve document: " + dre.getMessage() + "
");
} catch (AuthorizationException ae) {
out.println("Error: Authorization problem: " + ae.getMessage() + "
");
} catch (DocumentCreationException dce) {
out.println("Error: Unable to create document: " + dce.getMessage() + "
");
} catch (IllegalDocumentTypeException idte) {
out.println("Error: Wrong document type: " + idte.getMessage() + "
");
} catch (PropertyRetrievalException pre) {
out.println("Error: Unable to get document workflow stage: " + pre.getMessage() + "
");
} catch (NoMoreWorkflowStagesException nmwfse) {
out.println("Error: " + nmwfse.getMessage() + "
");
} catch (DocumentSaveException dse) {
out.println("Error: Unable to save new document: " + dse.getMessage());
} catch (DuplicateChildException dce) {
out.println("Error: Duplicate page: " + dce.getMessage());
} catch (ComponentNotFoundException cne) {
out.println("Error: Authoring template element not found: " + cne.getMessage());
} catch (IllegalTypeChangeException ile) {
out.println("Error: Illegal AT type: " + ile.getMessage());
}
} catch (IOException ioe) {
}
return hasUpdated;
}

String getDestinationSitearea(File file) {
String sitearea = null;
try {
FileInputStream fin = new FileInputStream(file);
byte[] fileContent = new byte[(int) file.length()];
fin.read(fileContent);
String content = new String(fileContent);
content = content.toLowerCase().trim();
if (!content.startsWith("/")) {
content = "/" + content;
}
if (content.endsWith("\n")) {
content = content.replaceAll("\n", "");
}
if (!content.endsWith("/")) {
content = content + "/";
}
String[] segments = content.split("/"); //obtain library name from path
libraryName = segments[1];
sitearea = content;

} catch (IOException ioe) {
}
return sitearea;
}

void setAuthoringTemplateId(JspWriter out) {
try {
try {
if (authTempId == null) {
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("documents"));
DocumentIdIterator docIds = workspace.findByType(DocumentTypes.AuthoringTemplate);
while (docIds.hasNext()) {
authTempId = (DocumentId) docIds.next();
AuthoringTemplate at = (AuthoringTemplate) workspace.getById(authTempId);
String name = at.getName();
if (name.equalsIgnoreCase("at-file-resource")) {
break;
}
}
}
} catch (DocumentRetrievalException dre) {
out.println("Error: Unable to retrieve document: " + dre.getMessage() + "
");
} catch (AuthorizationException ae) {
out.println("Error: Authorization problem: " + ae.getMessage() + "
");
}
} catch (IOException ioe) {
}
}

void setWorkflowId(JspWriter out) {
try {
try {
if (workflowId == null) {
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("documents"));
DocumentIdIterator docIds = workspace.findByType(DocumentTypes.Workflow);
while (docIds.hasNext()) {
workflowId = (DocumentId) docIds.next();
Workflow wf = (Workflow) workspace.getById(workflowId);
String name = wf.getName();
if (name.equalsIgnoreCase("wf-documents")) {
break;
}
}
}
} catch (DocumentRetrievalException dre) {
out.println("Error: Unable to retrieve document: " + dre.getMessage() + "
");
} catch (AuthorizationException ae) {
out.println("Error: Authorization problem: " + ae.getMessage() + "
");
}
} catch (IOException ioe) {
}
}

boolean isEmpty(String value) {
if (value == null || value.trim().length() == 0) {
return true;
} else {
return false;
}
}

%>



Yours in Websphere
Jerome Slinger

1 comment:

  1. This looks great! I’ve been searching for a bulk uploaded for a while! Are you able to provide the source? There appears to be some missing closing or opening brackets. Look forward to your reply!

    ReplyDelete

Generate reports from Opportunities using a Visualforce Page in Salesforce

  Step 1: Create a Visualforce Page Go to the Setup menu in Salesforce. Search for "Visualforce Pages" in the Quick Find box and c...