How to use Java services and classes in Alfresco when developing your own modules?

cancel
Showing results for 
Search instead for 
Did you mean: 
varlok
Active Member II

How to use Java services and classes in Alfresco when developing your own modules?

Good afternoon. I got acquainted with an example of programming using the built-in classes and services in Alfresco (for example, https://github.com/Redpill-Linpro/alfresco-uploading). About the use of embedded services at the software level in the official documentation did not find.

Question: where can I get acquainted with the full documentation on the development of my modules in Java using the built-in services and classes of Alfresco?

7 Replies
marktielemans
Active Member II

Re: How to use Java services and classes in Alfresco when developing your own modules?

The Alfresco docs actually contain a decent amount of useful documentation about this: Public Java API services | Alfresco Documentation .

jpotts
Professional

Re: How to use Java services and classes in Alfresco when developing your own modules?

What specifically can we help you with? As Mark says, there are a number of resources out there. The docs are good and there are a number of books that cover this topic as well as tutorials and blog posts.

I'm not sure what you're looking for beyond general pointers to resources. Maybe that's all you need?

varlok
Active Member II

Re: How to use Java services and classes in Alfresco when developing your own modules?

Thank you Jeff  and Mark. I got acquainted with this documentation, but I did not find a suitable solution in it. The task in the next - it is necessary to conduct end-to-end numbering of documents that participated in certain business processes. I have thoughts of doing this through service in these processes. I plan to store the numbering data as a property of the document model. There was a question - how to determine the latest number to generate the following?

jpotts
Professional

Re: How to use Java services and classes in Alfresco when developing your own modules?

One thing I've done in the past is create a folder in the Data Dictionary and then place objects in it that keep track of my numbering. You can use properties on the objects to store the number sequence information or you can use JSON to do that and write it as the content of the object.

Then, when you need to know what the next number is you can do a query to find your numbering object, use its property values to determine the next number, update the object, and return.

All of that logic should be in a "NumberingService" class which is a Spring bean you will configure in a Spring context XML file. Any other classes, like web scripts or behaviors, that need to do numbering, will inject the NumberingService as a dependency.

This is a common pattern that you will find throughout those tutorials and many add-ons.

varlok
Active Member II

Re: How to use Java services and classes in Alfresco when developing your own modules?

Thank you, Jeff. Your advice is very useful. I reasoned in the same direction - use a separate folder, and use my written service to generate a number, injection of service is carried out through Spring.

But I do not quite understand how to track the last assigned document number? How to search for the last assigned number?

jpotts
Professional

Re: How to use Java services and classes in Alfresco when developing your own modules?

It depends on how you decide to persist the sequence. One way is to extend the content model to create a new type, let's say it is called Sequence. The type could have a property called "sequenceNumber". I would suggest also a property called "sequenceKey" so that you could maintain multiple sequences if needed. Then you can just do a search to find the instance of Sequence with the key you need and grab the sequenceNumber property from it.

For example, you might do something like...

public getNextNumber(String key) {

        final NodeRef seqNode = getSequenceNode(key);
        long value = (Long) nodeService.getProperty(seqNode, CoreModel.PROP_VALUE);

        final long incrValue = value + 1;
        AuthenticationUtil.runAsSystem(new AuthenticationUtil.RunAsWork<Object>() {
            public Object doWork() throws Exception {
                nodeService.setProperty(seqNode, CoreModel.PROP_VALUE, incrValue);
                return "";
            }
        });

        return value;

}

private NodeRef getSequenceNode(String key) {
        String query = "TYPE:\"sc:autoNumberSequence\" AND sc:key:\"" + key + "\"";
        ResultSet rs = searchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_FTS_ALFRESCO, query);
        NodeRef sequenceNode = null;
        if (rs.length() == 0) {
            sequenceNode = createSequenceNode(key);
        } else if (rs.length() > 1) {
            logger.warn("More than one auto number sequence node found for: " + key);
            sequenceNode = rs.getNodeRef(0);
        } else {
            sequenceNode = rs.getNodeRef(0);
        }

        return sequenceNode;
}

varlok
Active Member II

Re: How to use Java services and classes in Alfresco when developing your own modules?

Thank you so much! This is very useful advice. I myself thought in this direction, but did not take into account the possibility of different sequences. Only the question remains about the code section in the method "getSequenceNode(String key)"

...
if (rs.length() == 0) {
sequenceNode = createSequenceNode(key);
} else if (rs.length() > 1) {
logger.warn("More than one auto number sequence node found for: " + key);
sequenceNode = rs.getNodeRef(0);
} else {
sequenceNode = rs.getNodeRef(0);
}
.....

As far as I understand, all nodes that have the corresponding type and property are searched. If there are more than one such nodes, a message is written to the log, and in any case, the first (zero in the set) node is taken from the result set. 

The question is how to ensure that the element numbered "0" in the result set always has the last registered number?