11-14-2008 02:28 AM
11-14-2008 10:59 AM
11-14-2008 11:34 AM
@Override
public Document loadDocument(String documentId) throws DocumentNotFoundException {
try {
Predicate predicate = buildPredicate(buildReference(documentId));
Content content = WebServiceFactory.getContentService().read(predicate, Constants.PROP_CONTENT)[0];
if (content == null) {
throw new DocumentNotFoundException("The document's content was empty");
}
InputStream contentStream = ContentUtils.getContentAsInputStream(content);
DocumentType contentType = DocumentType.getDocumentTypeForMimeType(content.getFormat().getMimetype());
Node documentNode = WebServiceFactory.getRepositoryService().get(predicate)[0];
String name = null;
String createdBy = null;
Map<UserDefinedDocumentProperty, String[]> userDefinedProperties = new HashMap<UserDefinedDocumentProperty, String[]>();
for (NamedValue namedValue : documentNode.getProperties()) {
String value = namedValue.getValue();
String valueName = namedValue.getName();
String strippedValueName = namedValue.getName().substring(namedValue.getName().lastIndexOf("}") + 1);
if (valueName.equals(Constants.PROP_NAME)) {
name = value;
} else if (valueName.equals(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, PreDefinedDocumentProperty.CREATED_BY.getPropertyName()))) {
createdBy = value;
}
try {
UserDefinedDocumentProperty property = UserDefinedDocumentProperty.getUserDefinedDocumentPropertyByPropertyName(strippedValueName);
userDefinedProperties.put(property, namedValue.getValues());
} catch (EnumConstantNotPresentException e) {
// nothing to do, there are many Alfresco properties that we don't care about
}
}
Folder parent = getParentFolder(documentId);
return new Document(documentId, contentStream, name, contentType, createdBy, isVersionable(documentNode), parent, userDefinedProperties);
} catch (ContentFault e) {
logger.info(e.getMessage(), e);
throw new DocumentNotFoundException(e);
} catch (RemoteException e) {
logger.info(e.getMessage(), e);
throw new DocumentNotFoundException(e);
}
}
userDefinedProperties.put(property, namedValue.getValues());
@Override
public String saveDocument(Document document, String revisionNote) throws DocumentManagementException {
if (document.getContent() == null) {
throw new DocumentManagementException("The document must contain some content");
}
ContentFormat format = new ContentFormat(document.getType().getMimeType(), ENCODING);
List<NamedValue> properties = new ArrayList<NamedValue>();
for (UserDefinedDocumentProperty property : document.getUserDefinedProperties().keySet()) {
NamedValue newValue = new NamedValue();
newValue.setName(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, property.getPropertyName()));
newValue.setIsMultiValue(true);
newValue.setValues(document.getUserDefinedProperties().get(property));
properties.add(newValue);
}
if (document.getDocumentId() == null || document.getDocumentId().isEmpty()) {
if (document.getName() == null || document.getName().isEmpty()) {
throw new DocumentManagementException("The document's name must be supplied");
}
ParentReference parentReference = getParentReferenceById(document.getName(), document.getContainingFolder().getUuid());
properties.add(Utils.createNamedValue(Constants.PROP_NAME, document.getName()));
properties.add(Utils.createNamedValue(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, PreDefinedDocumentProperty.CREATED_BY.getPropertyName()), activeEmployee.getEmployeeId().toString()));
CML cml = buildCMLForCreate(parentReference, properties.toArray(new NamedValue[0]), Constants.TYPE_CONTENT);
try {
UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);
Reference newContentNode = result[0].getDestination();
Content content = WebServiceFactory.getContentService().write(newContentNode, Constants.PROP_CONTENT, ContentUtils.convertToByteArray(document.getContent()), format);
if (document.isVersionable()) {
makeVersionable(content.getNode());
}
return content.getNode().getUuid();
} catch (Exception e) {
logger.info(e.getMessage(), e);
throw new IllegalStateException(e);
}
}
try {
// checkout a working copy
Predicate itemsToCheckOut = buildPredicate(buildReference(document.getDocumentId()));
CheckoutResult checkOutResult = WebServiceFactory.getAuthoringService().checkout(itemsToCheckOut, null);
Reference workingCopyReference = checkOutResult.getWorkingCopies()[0];
// write the new content
Predicate predicate = buildPredicate(workingCopyReference);
WebServiceFactory.getContentService().write(workingCopyReference, Constants.PROP_CONTENT, ContentUtils.convertToByteArray(document.getContent()), format);
// update properties
CML cml = buildCMLForUpdate(properties.toArray(new NamedValue[0]), predicate, document.getDocumentId());
WebServiceFactory.getRepositoryService().update(cml);
// check it back in
NamedValue[] comments = new NamedValue[] {
Utils.createNamedValue(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, PreDefinedDocumentProperty.REVISION_NOTE.getPropertyName()), revisionNote),
Utils.createNamedValue(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, PreDefinedDocumentProperty.UPDATED_BY.getPropertyName()), activeEmployee.getEmployeeId().toString())
};
WebServiceFactory.getAuthoringService().checkin(predicate, comments, false);
return document.getDocumentId();
} catch (Exception e) {
logger.info(e.getMessage(), e);
throw new DocumentManagementException(e);
}
}
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.