Tutorial - Example metadata action

Introduction

In this tutorial we will create an example of a Move2Alf metadata action. A metadata  action is responsible for setting the metadata and related parameters for the files that are processed by Move2Alf. It is implemented as a Java class. We will start by creating a simple Alfresco document model. Then we will set-up an Eclipse project in which we can create the metadata action class. After developing the class we will show how to deploy it to a Move2Alf instance. A set of test files is provided to test the newly created action.

The action we design will load all files from a folder from the filesystem into Alfresco. In Alfresco we will group the files in a subfolder based on the first letter of the filename. We will also use a part of the filename as a document code and save it in a metadata field.

Alfresco Document Model

First we will create a document model for Alfresco. In this model we will define one document type. The document type is called "m2a:m2aTestDocument", it has one text property (type "d:text") called "m2a:DocumentCode". We will use this to store the document code that's present in the filename of the imported files.

The model file is included in this tutorial. It's called "m2a-test-model.xml"

To load the model in Alfresco we will use the dynamic model functionality. This allows us to activate the document model without restarting Alfresco. Follow these steps to enable the model:

1. Add the model file to the "Data Dictionary/Models" folder inside Alfresco.
2. Edit the metadata of the model file and set Model Active to true.

Setting up your IDE

Next start Eclipse and create a new Java project. Edit the build path of the project and  add "move2alf.jar". Now your project is ready and we can create the action class.

Creating the Metadata Action

Create a new class called "Move2AlfTutorialAction" in package tutorial. As superclass  choose "eu.xenit.move2alf.core.action.Action". In this class we need to implement four methods.  Three are for configuration: "getName", "getDescription" and "getCategory". The last one is called "executeImpl" and performs the actual work.

To configure the action let the "getName" and "getDescription" method return an apropriate String. For example:

    @Override
    public String getName() {
        return "Move2Alf Tutorial Action";
    }

    @Override
    public String getDescription() {
        return "Basic action that demonstrates how to customize Move2Alf";
    }

The "getCategory" method should return "ConfigurableObject.CAT_METADATA"; like this:

    @Override
    public String getCategory() {
        return ConfigurableObject.CAT_METADATA;
    }

This indicates that the action should be displayed in the list of metadata actions when you configure a job.

Now we will implement the "executeImpl" method. This method is called once for every file that's processed by Move2Alf. It takes two parameters: "configurableAction" and "parameterMap". The first allows you to configure your action in the GUI. This will not be covered by this example. The "parameterMap" is a map that's used as input and output for the action. The keys you can use are defined as constants in the "Parameters" class. Here is an overview:

Input:

Parameter

Type

Description

PARAM_FILE

File

The file that is currently being processed

Output:

Parameter

Type

Description

PARAM_NAMESPACE

String

The full namespace of the content type, enclosed by {}.

PARAM_CONTENTTYPE

String

The name of the content type, without prefix

PARAM_METADATA

Map<String, String>

A map containing the metadata, the key contains the name of the field without prefix

PARAM_RELATIVE_PATH

String

The subfolder where the file should be placed.

All output parameters are optional. If nothing is provided a cm:content file is created without custom metadata in a folder structure that mirrors the input folder.

More advanced parameters are not covered by this example. (ACLs, reporting, ...)

First we will fetch the current file from the parameter map so we can get the filename:

    File file = (File) parameterMap.get(Parameters.PARAM_FILE);
    String filename = file.getName();

Then we will set the content type to the new type we defined in the content model above:

    parameterMap.put(Parameters.PARAM_NAMESPACE, "{http://www.xenit.eu/model/move2alf/test/1.0}");
    parameterMap.put(Parameters.PARAM_CONTENTTYPE, "m2aTestDocument");

Next we write the document code to the DocumentCode metadata field:

    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("DocumentCode", filename.substring(5, 9));
    parameterMap.put(Parameters.PARAM_METADATA, metadata);

Lastly we will group the files in subfolders based on the first letter of the filename:

    parameterMap.put(Parameters.PARAM_RELATIVE_PATH, filename.substring(0, 1));

The full code for "Move2AlfTutorialAction" class is included with this tutorial.

Deploy

To deploy your action first stop Move2Alf. We have to configure Move2Alf to load actions in the "tutorial" package. To do this edit move2alf/WEB-INF/classes/move2alf.properties.

Add the line:

base.package=tutorial

Next copy your compiled action, the .class file, to move2alf/WEB-INF/classes/. Since the action is member of the "tutorial" package it should be in a subfolder called "tutorial".

Now restart Move2Alf.

Test

Create a job using your new action. Use the "input" folder that's part of this tutorial as input folder. Run the job and verify if the files are loaded into Alfresco. Make sure the correct subfolders are created as expected and you can use the Node Browser to check if the document code is set in the metadata field.