use of org.alfresco.opencmis.dictionary.TypeDefinitionWrapper in project alfresco-repository by Alfresco.
the class AlfrescoCmisServiceImpl method createDocument.
@Override
public String createDocument(String repositoryId, final Properties properties, String folderId, final ContentStream contentStream, VersioningState versioningState, final List<String> policies, final Acl addAces, final Acl removeAces, ExtensionsData extension) {
checkRepositoryId(repositoryId);
// get the parent folder node ref
final CMISNodeInfo parentInfo = getOrCreateFolderInfo(folderId, "Parent folder");
// get name and type
final String name = connector.getNameProperty(properties, null);
final String objectTypeId = connector.getObjectTypeIdProperty(properties);
final TypeDefinitionWrapper type = connector.getTypeForCreate(objectTypeId, BaseTypeId.CMIS_DOCUMENT);
connector.checkChildObjectType(parentInfo, type.getTypeId());
DocumentTypeDefinition docType = (DocumentTypeDefinition) type.getTypeDefinition(false);
if ((docType.getContentStreamAllowed() == ContentStreamAllowed.NOTALLOWED) && (contentStream != null)) {
throw new CmisConstraintException("This document type does not support content!");
}
if ((docType.getContentStreamAllowed() == ContentStreamAllowed.REQUIRED) && (contentStream == null)) {
throw new CmisConstraintException("This document type does requires content!");
}
if (!docType.isVersionable() && (versioningState != VersioningState.NONE)) {
throw new CmisConstraintException("This document type is not versionable!");
}
versioningState = getDocumentDefaultVersioningState(versioningState, type);
FileInfo fileInfo = connector.getFileFolderService().create(parentInfo.getNodeRef(), name, type.getAlfrescoClass());
NodeRef nodeRef = fileInfo.getNodeRef();
connector.setProperties(nodeRef, type, properties, new String[] { PropertyIds.NAME, PropertyIds.OBJECT_TYPE_ID });
connector.applyPolicies(nodeRef, type, policies);
connector.applyACL(nodeRef, type, addAces, removeAces);
// handle content
if (contentStream != null) {
// write content
String mimeType = parseMimeType(contentStream);
String encoding = getEncoding(contentStream.getStream(), mimeType);
ContentWriter writer = connector.getFileFolderService().getWriter(nodeRef);
writer.setMimetype(mimeType);
writer.setEncoding(encoding);
writer.putContent(contentStream.getStream());
}
connector.extractMetadata(nodeRef);
// generate "doclib" thumbnail asynchronously
connector.createThumbnails(nodeRef, Collections.singleton("doclib"));
connector.applyVersioningState(nodeRef, versioningState);
String objectId = connector.createObjectId(nodeRef);
connector.getActivityPoster().postFileFolderAdded(nodeRef);
return objectId;
}
use of org.alfresco.opencmis.dictionary.TypeDefinitionWrapper in project alfresco-repository by Alfresco.
the class AlfrescoCmisServiceImpl method createItem.
@Override
public String createItem(String repositoryId, Properties properties, String folderId, List<String> policies, Acl addAces, Acl removeAces, ExtensionsData extension) {
checkRepositoryId(repositoryId);
// get the parent folder node ref
final CMISNodeInfo parentInfo = getOrCreateFolderInfo(folderId, "Folder");
// get name and type
final String name = connector.getNameProperty(properties, null);
final String objectTypeId = connector.getObjectTypeIdProperty(properties);
final TypeDefinitionWrapper type = connector.getTypeForCreate(objectTypeId, BaseTypeId.CMIS_ITEM);
connector.checkChildObjectType(parentInfo, type.getTypeId());
/**
* The above code specifies a folder - so the contents of a folder (as defined by the alfresco model) are
* ASSOC cm:contains to a TYPE sys:base
*/
QName assocQName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, QName.createValidLocalName(name));
Map<QName, Serializable> props = new HashMap<QName, Serializable>(11);
props.put(ContentModel.PROP_NAME, (Serializable) assocQName.getLocalName());
ChildAssociationRef newRef = connector.getNodeService().createNode(parentInfo.getNodeRef(), ContentModel.ASSOC_CONTAINS, assocQName, type.getAlfrescoClass(), props);
NodeRef nodeRef = newRef.getChildRef();
connector.setProperties(nodeRef, type, properties, new String[] { PropertyIds.NAME, PropertyIds.OBJECT_TYPE_ID });
connector.getNodeService().setProperty(nodeRef, ContentModel.PROP_NAME, assocQName.getLocalName());
connector.applyPolicies(nodeRef, type, policies);
connector.applyACL(nodeRef, type, addAces, removeAces);
String objectId = connector.createObjectId(nodeRef);
return objectId;
}
use of org.alfresco.opencmis.dictionary.TypeDefinitionWrapper in project alfresco-repository by Alfresco.
the class CMISConnector method addSecondaryTypes.
public void addSecondaryTypes(NodeRef nodeRef, List<String> secondaryTypes) {
if (secondaryTypes != null && secondaryTypes.size() > 0) {
for (String secondaryType : secondaryTypes) {
TypeDefinitionWrapper type = getType(secondaryType);
if (type == null) {
throw new CmisInvalidArgumentException("Invalid secondaryType: " + secondaryType);
}
nodeService.addAspect(nodeRef, type.getAlfrescoName(), null);
}
}
}
use of org.alfresco.opencmis.dictionary.TypeDefinitionWrapper in project alfresco-repository by Alfresco.
the class CMISConnector method getAspectExtensions.
/**
* Builds aspect extension.
*/
public List<CmisExtensionElement> getAspectExtensions(CMISNodeInfo info, String filter, Set<String> alreadySetProperties) {
List<CmisExtensionElement> extensions = new ArrayList<CmisExtensionElement>();
Set<String> propertyIds = new HashSet<String>(alreadySetProperties);
List<CmisExtensionElement> propertyExtensionList = new ArrayList<CmisExtensionElement>();
Set<String> filterSet = splitFilter(filter);
Set<QName> aspects = info.getNodeAspects();
for (QName aspect : aspects) {
TypeDefinitionWrapper aspectType = getOpenCMISDictionaryService().findNodeType(aspect);
if (aspectType == null) {
continue;
}
AspectDefinition aspectDefinition = dictionaryService.getAspect(aspect);
Map<QName, org.alfresco.service.cmr.dictionary.PropertyDefinition> aspectProperties = aspectDefinition.getProperties();
extensions.add(new CmisExtensionElementImpl(ALFRESCO_EXTENSION_NAMESPACE, APPLIED_ASPECTS, null, aspectType.getTypeId()));
for (PropertyDefinitionWrapper propDef : aspectType.getProperties()) {
boolean addPropertyToExtensionList = getRequestCmisVersion().equals(CmisVersion.CMIS_1_1) && aspectProperties.keySet().contains(propDef.getAlfrescoName());
// MNT-11876 : add property to extension even if it has been returned (CMIS 1.1)
if (propertyIds.contains(propDef.getPropertyId()) && !addPropertyToExtensionList) {
// skip properties that have already been added
continue;
}
if ((filterSet != null) && (!filterSet.contains(propDef.getPropertyDefinition().getQueryName()))) {
// skip properties that are not in the filter
continue;
}
Serializable value = propDef.getPropertyAccessor().getValue(info);
propertyExtensionList.add(createAspectPropertyExtension(propDef.getPropertyDefinition(), value));
// mark property as 'added'
propertyIds.add(propDef.getPropertyId());
}
}
if (!propertyExtensionList.isEmpty()) {
CmisExtensionElementImpl propertiesExtension = new CmisExtensionElementImpl(ALFRESCO_EXTENSION_NAMESPACE, "properties", null, propertyExtensionList);
extensions.addAll(Collections.singletonList(propertiesExtension));
}
return extensions;
}
use of org.alfresco.opencmis.dictionary.TypeDefinitionWrapper in project alfresco-repository by Alfresco.
the class CMISConnector method getContentStream.
/**
* Gets the content from the repository.
*/
public ContentStream getContentStream(CMISNodeInfo info, String streamId, BigInteger offset, BigInteger length) {
// get the type and check if the object can have content
TypeDefinitionWrapper type = info.getType();
checkDocumentTypeForContent(type);
// looks like a document, now get the content
ContentStreamImpl result = new ContentStreamImpl();
result.setFileName(info.getName());
// if streamId is set, fetch other content
NodeRef streamNodeRef = info.getNodeRef();
if ((streamId != null) && (streamId.length() > 0)) {
CMISNodeInfo streamInfo = createNodeInfo(streamId);
if (!streamInfo.isVariant(CMISObjectVariant.CURRENT_VERSION)) {
throw new CmisInvalidArgumentException("Stream id is invalid: " + streamId + ", expected variant " + CMISObjectVariant.CURRENT_VERSION + ", got variant " + streamInfo.getObjectVariant());
}
streamNodeRef = streamInfo.getNodeRef();
type = streamInfo.getType();
checkDocumentTypeForContent(type);
}
// get the stream now
try {
ContentReader contentReader = contentService.getReader(streamNodeRef, ContentModel.PROP_CONTENT);
if (contentReader == null) {
throw new CmisConstraintException("Document has no content!");
}
result.setMimeType(contentReader.getMimetype());
long contentSize = contentReader.getSize();
if ((offset == null) && (length == null)) {
result.setStream(contentReader.getContentInputStream());
result.setLength(BigInteger.valueOf(contentSize));
publishReadEvent(streamNodeRef, info.getName(), result.getMimeType(), contentSize, contentReader.getEncoding(), null);
} else {
long off = (offset == null ? 0 : offset.longValue());
long len = (length == null ? contentSize : length.longValue());
if (off + len > contentSize) {
len = contentReader.getSize() - off;
}
result.setStream(new RangeInputStream(contentReader.getContentInputStream(), off, len));
result.setLength(BigInteger.valueOf(len));
publishReadEvent(streamNodeRef, info.getName(), result.getMimeType(), contentSize, contentReader.getEncoding(), off + " - " + len);
}
} catch (Exception e) {
if (e instanceof CmisBaseException) {
throw (CmisBaseException) e;
} else {
StringBuilder msg = new StringBuilder("Failed to retrieve content: " + e.getMessage());
Throwable cause = e.getCause();
if (cause != null) {
// add the cause to the CMIS exception
msg.append(", ");
msg.append(cause.getMessage());
}
throw new CmisRuntimeException(msg.toString(), e);
}
}
return result;
}
Aggregations