// Validation stuff
TypeValidator.validateRequiredSystemProperties(properties);
TypeDefinition typeDef = getTypeDefinition(repositoryId, properties);
Folder folder = null;
if (null != folderId) {
StoredObject so = objectStore.getObjectById(folderId);
if (null == so)
throw new CmisInvalidArgumentException(" Cannot create document, folderId: " + folderId + " is invalid");
if (so instanceof Folder)
folder = (Folder) so;
else
throw new CmisInvalidArgumentException("Can't creat document, folderId does not refer to a folder: "
+ folderId);
TypeValidator.validateAllowedChildObjectTypes(typeDef, folder.getAllowedChildObjectTypeIds());
}
// check if the given type is a document type
if (!typeDef.getBaseTypeId().equals(BaseTypeId.CMIS_DOCUMENT))
throw new CmisInvalidArgumentException("Cannot create a document, with a non-document type: " + typeDef.getId());
// check name syntax
if (!NameValidator.isValidId(name))
throw new CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
TypeValidator.validateVersionStateForCreate((DocumentTypeDefinition) typeDef, versioningState);
// set properties that are not set but have a default:
Map<String, PropertyData<?>> propMapNew = setDefaultProperties(typeDef, propMap);
if (propMapNew != propMap) {
properties = new PropertiesImpl(propMapNew.values());
propMap = propMapNew;
}
TypeValidator.validateProperties(typeDef, properties, true);
// set user, creation date, etc.
if (user == null)
user = "unknown";
StoredObject so = null;
// check if content stream parameters are set and if not set some defaults
if (null != contentStream && (contentStream.getFileName() == null || contentStream.getFileName().length() == 0 ||
contentStream.getMimeType() == null || contentStream.getMimeType().length() == 0)) {
ContentStreamImpl cs = new ContentStreamImpl();
cs.setStream(contentStream.getStream());
if (contentStream.getFileName() == null || contentStream.getFileName().length() == 0)
cs.setFileName(name);
else
cs.setFileName(contentStream.getFileName());
cs.setLength(contentStream.getBigLength());
if (contentStream.getMimeType() == null || contentStream.getMimeType().length() == 0)
cs.setMimeType("application/octet-stream");
else
cs.setMimeType(contentStream.getMimeType());
cs.setExtensions(contentStream.getExtensions());
contentStream = cs;
}
// Now we are sure to have document type definition:
if (((DocumentTypeDefinition) typeDef).isVersionable()) {
VersionedDocument verDoc = fStoreManager.getObjectStore(repositoryId).createVersionedDocument(name);
verDoc.createSystemBasePropertiesWhenCreated(properties.getProperties(), user);
verDoc.setCustomProperties(properties.getProperties());
DocumentVersion version = verDoc.addVersion(contentStream, versioningState, user);
if (null != folder)
folder.addChildDocument(verDoc); // add document to folder and
// set parent in doc
else
verDoc.persist();
version.createSystemBasePropertiesWhenCreated(propMap, user);
version.setCustomProperties(propMap);
version.persist();
so = version; // return the version and not the version series to
// caller
} else {
Document doc = fStoreManager.getObjectStore(repositoryId).createDocument(name);
doc.setContent(contentStream, false);
// add document to folder
doc.createSystemBasePropertiesWhenCreated(propMap, user);
doc.setCustomProperties(propMap);
if (null != folder)
folder.addChildDocument(doc); // add document to folder and set
// parent in doc
else
doc.persist();
so = doc;
}