Uploading a file with a custom type to the Repository means creating a node with a type other than cm:content. See Uploading a File for info on how to upload a file with the out-of-the-box content type cm:content set. There’s actually not much difference to how you upload a file with a custom type. Let’s say we have a content model type acme:document and an aspect acme:securityClassified and these are defined as follows:
<type name="acme:document">
<title>Sample Document Type</title>
<parent>cm:content</parent>
<properties>
<property name="acme:documentId">
<title>Document Identification Number</title>
<type>d:text</type>
</property>
</properties>
<mandatory-aspects>
<aspect>acme:securityClassified</aspect>
</mandatory-aspects>
</type>
<aspect name="acme:securityClassified">
<title>ACME Security Classified</title>
<description>Content has been security classified</description>
<properties>
<property name="acme:securityClassification">
<type>d:text</type>
<index enabled="true">
<atomic>true</atomic>
<stored>false</stored>
<tokenised>false</tokenised>
</index>
<constraints>
<constraint ref="acme:securityClassificationOptions"/>
</constraints>
</property>
</properties>
</aspect>
We got two custom properties acme:documentId and acme:securityClassification. For more information on how to implement and deploy this custom content model see Upload a File with Custom Type.
Now, to upload a file and set this custom type and aspect we use the same code as for upload a file (see Uploading a File) with minor updates to the contentType variable and the createFileMetadata method:
import org.alfresco.core.handler.NodesApi;
import org.alfresco.core.model.Node;
import org.alfresco.core.model.NodeBodyCreate;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class CreateFileCustomTypeCmd {
static final Logger LOGGER = LoggerFactory.getLogger(CreateFileCustomTypeCmd.class);
private String contentType = "acme:document"; // Set custom content type
private Boolean autoRename = true;
private Boolean majorVersion = true;
private Boolean versioningEnabled = true;
private String updateComment = null;
private String updatedName = null;
private List<String> include = null;
private List<String> fields = null;
@Autowired
NodesApi nodesApi;
public void execute() throws IOException {
// Create a text file under the /Company Home/Guest Home folder
Node newTextFile = createTextFile("-root-", "somestuff2.txt", "TextfileTitle2",
"TextfileDesc2", "/Guest Home", "Some text for the file2");
// Upload a file from disk to the /Company Home/Guest Home folder, file resides in app dir
Node newFile = uploadFile("-root-", "somepicture2.png", "PicturefileTitle2",
"PicturefileDesc2", "/Guest Home", "somepicture.png");
}
/**
* Upload a file from disk
*/
private Node uploadFile(String parentFolderId, String fileName, String title, String description,
String relativeFolderPath, String filePath) {
Node fileNode = createFileMetadata(parentFolderId, fileName, title, description, relativeFolderPath);
// Get the file bytes
File someFile = new File(filePath);
byte[] fileData = null;
try {
fileData = FileUtils.readFileToByteArray(someFile);
} catch (IOException e) {
e.printStackTrace();
}
// Add the file node content
Node updatedFileNode = nodesApi.updateNodeContent(fileNode.getId(),
fileData, majorVersion, updateComment, updatedName, include, fields).getBody().getEntry();
LOGGER.info("Created file with content: {}", updatedFileNode.toString());
return updatedFileNode;
}
/**
* Create a text file
*/
private Node createTextFile(String parentFolderId, String fileName, String title, String description,
String relativeFolderPath, String textContent) {
Node fileNode = createFileMetadata(parentFolderId, fileName, title, description, relativeFolderPath);
// Add the file node content
Node updatedFileNode = nodesApi.updateNodeContent(fileNode.getId(),
textContent.getBytes(), majorVersion, updateComment, updatedName, include, fields).getBody().getEntry();
LOGGER.info("Created file with content: {}", updatedFileNode.toString());
return updatedFileNode;
}
/**
* Create metadata for a file node
*
* @param parentFolderId the parent folder node ID where the file should be stored
* @param fileName the name for the new file
* @param title the title property value for the new file
* @param description the description property value for the new file
* @param relativeFolderPath path relative to /Company Home
* @return a Node object with file metadata and the Node ID
*/
private Node createFileMetadata(String parentFolderId, String fileName, String title, String description,
String relativeFolderPath) {
List<String> fileAspects = new ArrayList<String>();
fileAspects.add("cm:titled");
fileAspects.add("acme:securityClassified");
Map<String, String> fileProps = new HashMap<>();
fileProps.put("cm:title", title);
fileProps.put("cm:description", description);
fileProps.put("acme:documentId", "DOC-001"); // custom prop from type
fileProps.put("acme:securityClassification", "Company Confidential"); // custom prop from aspect
NodeBodyCreate nodeBodyCreate = new NodeBodyCreate();
nodeBodyCreate.setName(fileName);
nodeBodyCreate.setNodeType(contentType);
nodeBodyCreate.setAspectNames(fileAspects);
nodeBodyCreate.setProperties(fileProps);
nodeBodyCreate.setRelativePath(relativeFolderPath);
// Create the file node metadata
Node fileNode = nodesApi.createNode(parentFolderId, nodeBodyCreate, autoRename, majorVersion, versioningEnabled,
include, fields).getBody().getEntry();
return fileNode;
}
}
See also Working with Relationships between Folders or Files for more examples of uploading and creating nodes.