String user = context.getUsername();
TypeDefinition typeDef = getTypeDefinition(repositoryId, properties);
validator.createDocument(context, repositoryId, folderId, extension);
ObjectStore objectStore = fStoreManager.getObjectStore(repositoryId);
Map<String, PropertyData<?>> propMap = properties.getProperties();
// get name from properties
PropertyData<?> pd = propMap.get(PropertyIds.NAME);
String name = (String) pd.getFirstValue();
// Validation stuff
TypeValidator.validateRequiredSystemProperties(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()) {
DocumentVersion version = objectStore.createVersionedDocument(name, propMap,
user, folder, addACEs, removeACEs, contentStream, versioningState);
version.persist();
so = version; // return the version and not the version series to caller
} else {
Document doc = objectStore.createDocument(name, propMap, user, folder, addACEs, removeACEs);
doc.setContent(contentStream, false);
doc.persist();
so = doc;
}